I made a simple connect four terminal game in C and was wondering if I could get feedback on it. I mainly want to know where pointers could have optimised my code, and if there are any major redundancies. ( I know that the is_occupied
function is one, but I added that for readability) Here is the code:
/* simple connect 4 game */
/* current version: 1.0 */
// inclusions
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <windows.h>
#include <time.h>
// definitions
#define LEN_X 7 // width of a normal board ( I think )
#define LEN_Y 6 // length of a normal board ( I think )
// global variables
char board_red[LEN_Y][LEN_X]; // the board where the game will be played on
bool particle_exists_red[LEN_Y][LEN_X]; // a boolean board that holds the slots where red has a piece
bool particle_exists_yellow[LEN_Y][LEN_X]; // a boolean board that holds the slots where yellow has a piece
bool particle_exists[LEN_Y][LEN_X]; // a boolean board that holds the slots where a piece exists in general
bool temp_particle_exists[LEN_Y][LEN_X]; // a boolean board that holds the value of each slot, for the temp print function
bool temp_particle_exists_red[LEN_Y][LEN_X];
bool temp_particle_exists_yellow[LEN_Y][LEN_X];
int red_wins = 0;
int yellow_wins = 0;
bool win = false;
// protoypes
void particle_move_red(int x); // function in charge of moving the red pieces(particles)
void particle_move_yellow(int x); // function in charge of moving the yellow pieces(particles)
void print_grid(void); // function that prints the finished grid
bool is_occupied(int x, int y); // functino used to see if a slot is occupied
void print_temp_grid(void); // function that prints the temporary grid
bool win_check(void); // finction that checks if winconditons are met
void yellow_plays(void); // lets the robot make a move
void game_loop(void); // main game loop
void clear_grid(void); // function that clears the grid for subsequent games
// main
int main(void)
{
srand(time(NULL));
char keep_playing = 'y';
do
{
printf("Do you want to play? (y/n): ");
scanf(" %c", &keep_playing);
if(keep_playing == 'y')
{
game_loop();
}
else
{
printf("You scored: %d points\nComputer scored: %d points\n", red_wins, yellow_wins);
}
} while (keep_playing == 'y');
return 0;
}
// function that moves the red particle
void particle_move_red(int x)
{
int move_y = LEN_Y;
int i;
for(i = LEN_Y; i > 1; i--)
{
if(!is_occupied(x,move_y-1))
{
move_y--;
temp_particle_exists_red[x][move_y] = true;
print_temp_grid();
temp_particle_exists_red[x][move_y] = false;
Sleep(60);
system("cls");
}
}
particle_exists[x][move_y] = true;
particle_exists_red[x][move_y] = true;
win_check();
}
// function that moves the yellow particle
void particle_move_yellow(int x)
{
int move_y = LEN_Y;
int i;
for(i = LEN_Y; i > 1; i--)
{
if(!is_occupied(x,move_y-1))
{
move_y--;
temp_particle_exists_yellow[x][move_y] = true;
print_temp_grid();
temp_particle_exists_yellow[x][move_y] = false;
Sleep(60);
system("cls");
}
}
particle_exists[x][move_y] = true;
particle_exists_yellow[x][move_y] = true;
win_check();
}
// function that checks if a slot is occupied
bool is_occupied(int x, int y)
{
return particle_exists[x][y] == true;
}
// function that prints the falling pieces
void print_temp_grid()
{
int i, j;
printf("\n");
printf(" |1|2|3|4|5|6|7|\n");
for(i = LEN_Y; i > 0; i--)
{
printf(" |");
for(j = 1; j <= LEN_X; j++)
{
if(temp_particle_exists_red[j][i] == true || particle_exists_red[j][i] == true)
printf("O|");
else if(temp_particle_exists_yellow[j][i] == true || particle_exists_yellow[j][i] == true)
printf("X|");
else
printf(" |");
}
printf("\n");
}
}
// function that checks for a win
bool win_check(void)
{
int i, j;
for(i = LEN_Y; i > 0; i--)
{
for(j = 1; j <= LEN_X; j++)
{
// check for red win y axis
if (particle_exists_red[j][i] == true)
if (particle_exists_red[j][i+1] == true)
if (particle_exists_red[j][i+2] == true)
if (particle_exists_red[j][i+3] == true)
{
printf("Red wins!\n");
red_wins++;
win = true;
return true;
}
// check for red win x axis
if (particle_exists_red[j][i] == true)
if (particle_exists_red[j+1][i] == true)
if (particle_exists_red[j+2][i] == true)
if (particle_exists_red[j+3][i] == true)
{
printf("Red wins!\n");
red_wins++;
win = true;
return true;
}
// check for red win oblique from top y
if (particle_exists_red[j][i] == true)
if (particle_exists_red[j-1][i-1] == true)
if (particle_exists_red[j-2][i-2] == true)
if (particle_exists_red[j-3][-3] == true)
{
printf("Red wins!\n");
red_wins++;
win = true;
return true;
}
// check for red win oblique from bottom y
if (particle_exists_red[j][i] == true)
if (particle_exists_red[j+1][i+1] == true)
if (particle_exists_red[j+2][i+2] == true)
if (particle_exists_red[j+3][+3] == true)
{
printf("Red wins!\n");
red_wins++;
win = true;
return true;
}
// check for yellow win y axis
if (particle_exists_yellow[j][i] == true)
if (particle_exists_yellow[j][i+1] == true)
if (particle_exists_yellow[j][i+2] == true)
if (particle_exists_yellow[j][i+3] == true)
{
printf("yellow wins!\n");
yellow_wins++;
win = true;
return true;
}
// check for yellow win x axis
if (particle_exists_yellow[j][i] == true)
if (particle_exists_yellow[j+1][i] == true)
if (particle_exists_yellow[j+2][i] == true)
if (particle_exists_yellow[j+3][i] == true)
{
printf("yellow wins!\n");
yellow_wins++;
win = true;
return true;
}
// check for yellow win oblique from top y
if (particle_exists_yellow[j][i] == true)
if (particle_exists_yellow[j-1][i-1] == true)
if (particle_exists_yellow[j-2][i-2] == true)
if (particle_exists_yellow[j-3][-3] == true)
{
printf("yellow wins!\n");
yellow_wins++;
win = true;
return true;
}
// check for yellow win oblique from bottom y
if (particle_exists_yellow[j][i] == true)
if (particle_exists_yellow[j+1][i+1] == true)
if (particle_exists_yellow[j+2][i+2] == true)
if (particle_exists_yellow[j+3][+3] == true)
{
printf("yellow wins!\n");
yellow_wins++;
win = true;
return true;
}
}
}
}
void yellow_plays(void)
{
int randslot = rand() % 6 +1;
particle_move_yellow(randslot);
}
// main loop that plays until a win is reached
void game_loop(void)
{
int slot;
win = false;
clear_grid();
while(!win)
{
print_temp_grid();
printf("Choose a slot to insert piece into(1 to 7): ");
scanf("%d", &slot);
if (slot > 7 || slot < 1)
printf("value is outside domain\n");
else
{
system("cls");
particle_move_red(slot);
if (!win)
yellow_plays();
}
}
}
void clear_grid(void)
{
int i, j;
for(i = LEN_Y; i > 0; i--)
{
for(j = 1; j <= LEN_X; j++)
{
particle_exists[j][i] = false;
particle_exists_red[j][i] = false;
particle_exists_yellow[j][i] = false;
}
}
}
-Wall
\$\endgroup\$