The game has become much more user friendly than the previous version – with an example of a grid to illustrate the entries, the X's and Bones are now used.
The game can now detect the draw.
The algorithm has been modified to detect the winner, it is overall less buggy, although there are still some combinations towards the end of a game in which it incorrectly determines a winner or a loser – no idea how to solve this problem or determine its cause.
I would appreciate some tips to improve the structure of the game and the code, and to avoid the mentioned bugs.
#understand
#understand
#understand
// Struct with all the game state variables.
struct game_data {
int win; // 0 or 1.
int turns; // Range from 1 to 9 (end of the game).
int tour; // 0 or 1 where 0 is a human player
tank grill[3][3];
};
// Initialization of game state variables
struct game_data game = {
0
1
0
{{}, & # 39;; & # 39;},
(& # 39; & # 39;) & # 39;
{& # 39; & # 39;, & # 39;}
};
void intro (void) {
printf ("Welcome to NOUGHTS AND CROSSES n n");
printf ("The grid on which you will play is 3x3 and your entry will be determined by the coordinates you entered, in the form" row column ". n n");
printf ("For example, an entry of" 1 1 "will put a" Z "on the first line of the first column, as follows: n n");
printf (
"+ --- + --- + --- + n"
"| Z | | | n"
"+ --- + --- + --- + n"
"| | | | n"
"--- + --- + --- | n"
"| | | | n"
"+ --- + --- + --- + n"
" not");
}
void player_one_move (game struct game_data *)
{
int y_val, x_val;
printf ("You are & # 39; Cross & # 39;. Please enter the coordinates in the form & # 39; column line & # 39; for 3x3 grid: n");
scanf ("% d% d", & y_val, & x_val);
if (game-> grid[y_val - 1][x_val - 1] == & # 39;) {
game-> grid[y_val - 1][x_val - 1] = & # 39; X & # 39;
printf (" nYour turn: n n");
}
other {
player_one_move (game);
}
}
void computer_move (struct game game_data *)
{
int x_val = rand ()% 3;
int y_val = rand ()% 3;
if (game-> grid[y_val][x_val] == & # 39;) {
game-> grid[y_val][x_val] = & # 39 ;;
printf (" nUnumber of computer: n n");
}
other {
computer_move (game);
}
}
void update (struct game game_data *)
{
printf (
"+ --- + --- + --- + n"
"|% c |% c |% c | n"
"+ --- + --- + --- + n"
"|% c |% c |% c | n"
"---- + --- + --- | n"
"|% c |% c |% c | n"
"+ --- + --- + --- + n"
" not",
game-> grid[0][0], game-> grid[0][1], game-> grid[0][2],
game-> grid[1][0], game-> grid[1][1], game-> grid[1][2],
game-> grid[2][0], game-> grid[2][1], game-> grid[2][2])
}
void game_event_won (game struct game_data *)
{
int account;
// char current_mark;
// BUGGY
/ * char current_mark;
if (game-> turn == 0) {
current_mark = & # 39 ;;
other{
current_mark = & # 39;
} * /
for (int y_val = 0; y_val <3; y_val ++) {
for (int x_val = 0; x_val < 3; x_val++) {
count = 0;
while (game->Grid[y_val][x_val] == X & # 39;) {
x_val ++;
count ++;
// BUGGY
/ * if (count == 3 && current_mark == & nbsp; X)
game-> victory = 1;
printf ("You won n");
}
if (count == 3 && current_mark == & nbsp; & nbsp;)
game-> victory = 1;
printf ("You have lost n");
} * /
if (count == 3) {
game-> victory = 1;
printf ("You won n");
}
}
}
}
for (int x_val = 0; x_val <3; x_val ++) {
for (int y_val = 0; y_val < 3; y_val++) {
count = 0;
while (game->Grid[y_val][x_val] == X & # 39;) {
y_val ++;
count ++;
if (count == 3) {
game-> victory = 1;
printf ("You won n");
}
}
}
}
for (int y_val = 0; y_val < 3; y_val++) {
count = 0;
while (game->Grid[y_val][y_val] == X & # 39;) {
count ++;
y_val ++;
if (count == 3) {
game-> victory = 1;
printf ("You won n");
}
}
}
for (int y_val = 0; y_val < 3; y_val++) {
count = 0;
while (game->Grid[y_val][2 - y_val] == X & # 39;) {
count ++;
y_val ++;
if (count == 3) {
game-> victory = 1;
printf ("You won n");
}
}
}
}
// Repeat the previous function but for & # 39; O & # 39; s. Less concise but less buggy than the previous implementation.
void game_event_lost (struct game game_data *)
{
int account;
for (int y_val = 0; y_val <3; y_val ++) {
for (int x_val = 0; x_val < 3; x_val++) {
count = 0;
while (game->Grid[y_val][x_val] == & # 39;)
x_val ++;
count ++;
if (count == 3) {
game-> victory = 1;
printf ("You have lost n");
}
}
}
}
for (int x_val = 0; x_val <3; x_val ++) {
for (int y_val = 0; y_val < 3; y_val++) {
count = 0;
while (game->Grid[y_val][x_val] == & # 39;)
y_val ++;
count ++;
if (count == 3) {
game-> victory = 1;
printf ("You have lost n");
}
}
}
}
for (int y_val = 0; y_val < 3; y_val++) {
count = 0;
while (game->Grid[y_val][y_val] == & # 39;)
count ++;
y_val ++;
if (count == 3) {
game-> victory = 1;
printf ("You have lost n");
}
}
}
for (int y_val = 0; y_val < 3; y_val++) {
count = 0;
while (game->Grid[y_val][2 - y_val] == & # 39;)
count ++;
y_val ++;
if (count == 3) {
game-> victory = 1;
printf ("You have lost n");
}
}
}
}
int main (void)
{
srand (unsigned time (0));
intro ();
while (game.win == 0) {
if (game.turn == 0) {
player_one_move (& game);
game.turns ++;
game.turn = 1;
}
other {
game.turn = 0;
computer_move (& game);
game.turns ++;
}
if (game.turns == 9 && game.win == 0) {
game.win = 1;
printf ("You have drawn n");
Pause;
}
update (& game);
game_event_won (& game);
game_event_lost (& game);
}
returns 0;
}