I have programmed Tic Tac Toe in C. How can I improve the code? I have been told that system("cls")
is not secure, but I don't know any other way of clearing the screen.
main.c:
#include <stdio.h>
#include <stdlib.h>
#define BOOL short int
#define TRUE 1
#define FALSE 0
#define NUMBER_OF_ROWS 3
#define NUMBER_OF_COLUMNS 3
void setup_grid(char (*grid)[NUMBER_OF_COLUMNS]);
void display_grid(char (*grid)[NUMBER_OF_COLUMNS]);
void add_piece(char (*grid)[NUMBER_OF_COLUMNS], char, char);
BOOL space_is_empty(char (*grid)[NUMBER_OF_COLUMNS], char);
BOOL three_in_a_row(char (*grid)[NUMBER_OF_COLUMNS], char);
int main()
{
char grid[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS];
setup_grid(grid);
char player = '1';
char player_piece = 'O';
char space;
int space_int;
int piece_count = 0;
BOOL program_is_running = TRUE;
BOOL input_loop;
while(program_is_running)
{
system("cls");
display_grid(grid);
do
{
printf("Player %c: ", player);
space = getchar();
getchar(); // For the \n char.
printf("\n");
space_int = space - '0';
if(space_int < 1 || space_int > 9)
{
printf("Please enter an integer from 1-9\n");
input_loop = TRUE;
}
else if(space_is_empty(grid, space))
{
add_piece(grid, player_piece, space);
piece_count++;
input_loop = FALSE;
}
else
{
printf("That space is already taken.\n");
input_loop = TRUE;
}
}
while(input_loop);
if(three_in_a_row(grid, player_piece))
{
system("cls");
display_grid(grid);
printf("Player %c won!", player);
program_is_running = FALSE;
}
else if(piece_count >= 9)
{
system("cls");
display_grid(grid);
printf("It is a draw.\n");
program_is_running = FALSE;
}
else if(player == '1')
{
player = '2';
player_piece = 'X';
}
else
{
player = '1';
player_piece = 'O';
}
}
getchar();
return 0;
}
void setup_grid(char (*grid)[NUMBER_OF_COLUMNS])
{
int count = 1;
for(int y = 0; y < NUMBER_OF_ROWS; y++)
{
for(int x = 0; x < NUMBER_OF_COLUMNS; x++, count++)
{
grid[y][x] = count + '0';
}
}
}
void display_grid(char (*grid)[NUMBER_OF_COLUMNS])
{
printf("\n");
for(int y = 0; y < NUMBER_OF_ROWS; y++)
{
for(int x = 0; x < NUMBER_OF_COLUMNS; x++)
{
printf(" %c ", grid[y][x]);
}
printf("\n");
}
printf("\n");
}
void add_piece(char (*grid)[NUMBER_OF_COLUMNS], char player, char space)
{
for(int y = 0; y < NUMBER_OF_ROWS; y++)
for(int x = 0; x < NUMBER_OF_COLUMNS; x++)
if(grid[y][x] == space)
grid[y][x] = player;
}
BOOL space_is_empty(char (*grid)[NUMBER_OF_COLUMNS], char space)
{
for(int y = 0; y < NUMBER_OF_ROWS; y++)
for(int x = 0; x < NUMBER_OF_COLUMNS; x++)
if(grid[y][x] == space)
return TRUE;
return FALSE;
}
BOOL three_in_a_row(char (*grid)[NUMBER_OF_COLUMNS], char player)
{
// Horizontal check.
for(int y = 0; y < NUMBER_OF_ROWS; y++)
{
for(int x = 0; x < NUMBER_OF_COLUMNS; x++)
{
if(grid[y][x] == player)
{
if(x == 2)
return TRUE;
}
else
break;
}
}
// Vertical check.
for(int x = 0; x < NUMBER_OF_COLUMNS; x++)
{
for(int y = 0; y < NUMBER_OF_ROWS; y++)
{
if(grid[y][x] == player)
{
if(y == 2)
return TRUE;
}
else
break;
}
}
// Diagonal check.
// Top left to bottom right.
if(grid[0][0] == player && grid[1][1] == player && grid[2][2] == player)
return TRUE;
// Bottom left to top right
if(grid[2][0] == player && grid[1][1] == player && grid[0][2] == player)
return TRUE;
return FALSE;
}