Tic Tac Toe en C
Solved
Abby14
Posted messages
2
Status
Membre
-
Collraoul -
Collraoul -
Hello,
I created a C program for the tic-tac-toe game. But when I run it, it always says that the square has already been filled. Does anyone see what I did wrong?
Thank you very much in advance for your answers
Here is my program:
#include <stdio.h>
#define nblignes 3
#define nbcolonnes 3
char grid[nblignes][nbcolonnes];
/*Initialization of the tic-tac-toe grid to empty: */
void initializeGrid(char grid[nblignes][nbcolonnes])
{
int i, j ;
for (i=0 ; i<nblignes ; i++)
{
for (j=0 ; j<nbcolonnes ; j++)
{
grid[i][j] = ' ' ;
}
}
}
/*Displaying the tic-tac-toe grid: */
void displayGrid(char grid[nblignes][nbcolonnes])
{
int i, j ;
for (i=0 ; i<nblignes ; i++)
{
for (j=0 ; j<(nbcolonnes) ; j++)
{
printf("%c", grid[i][j]) ;
printf("|") ;
}
printf("\n") ;
printf("- - -\n") ;
}
return ;
}
/*Input for the coordinates of the symbol to put on the grid. If the coordinates
are out of the grid or if the square has already been filled, input is
refused, an error message is displayed and the player must play again. */
void placeSymbol(char symbol, char grid[nblignes][nbcolonnes])
{
int row, column ;
printf("Please provide the row and column numbers:") ;
do
{
scanf_s("%i %i", &row, &column) ;
if ((row>0) && (row<=nblignes) && (column>0) && (column<=nbcolonnes))
{
row-- ; /*to be compatible with array indices*/
column-- ;
if (grid[row][column]!=' ')
printf("The square has already been filled. Please choose another one:") ;
else { grid[row][column]=symbol ;
if (symbol=='X') {symbol='O' ; }
else {symbol='X' ; }
}
}
else printf("Row or column index incorrect. Please re-enter:") ;
} while(grid[row][column]!=' ') ;
}
/*Test to see if one of the players has won: */
int winningTest(char grid[nblignes][nbcolonnes], char symbol)
{
int i, j, sum, won ;
sum = 0 ;
won = 0 ;
for (i=0 ; i<nblignes ; i++)
{
for (j=0 ; j<nbcolonnes ; j++)
{
sum+=grid[i][j] ;
}
if((sum==237) || (sum==264))
{ won = 1 ; }
}
for(j=0 ; j<nbcolonnes ; j++)
{
for(i=0 ; i<nblignes ; i++)
{
sum+=grid[i][j] ;
}
if((sum==237) || (sum==264))
{ won = 1 ; }
}
if(grid[0][0] + grid[1][1] + grid[2][2] == 237 || grid[0][0] + grid[1][1] + grid[2][2] == 264)
{ won = 1 ; }
if(grid[0][2] + grid[1][1] + grid[2][0] == 237 || grid[0][2] + grid[1][1] + grid[2][0] == 264)
{ won = 1 ; }
if (won==1)
{ printf("Congratulations to the player with the ") ;
if(symbol=='X')
printf("X") ;
else
printf("O") ;
printf("who has won.\n") ;
}
/* Test if the grid is full:*/
if(won==0){
for(i=0 ; i<nblignes ; i++)
{
for(j=0 ; j<= nbcolonnes ; j++)
{
if(grid[i][j] != ' ')
won=2 ;
printf("Neither player has won") ;
}
}
}
return won ;
}
void main()
{
char grid[nblignes][nbcolonnes] ;
char symbol ;
int won ;
initializeGrid(grid) ;
displayGrid(grid) ;
printf("Please choose your symbol, O or X:") ;
scanf("%c", &symbol) ;
do
{ placeSymbol(symbol, grid) ;
displayGrid(grid);
winningTest(grid, symbol) ;
}
while(won=0) ;
}
I created a C program for the tic-tac-toe game. But when I run it, it always says that the square has already been filled. Does anyone see what I did wrong?
Thank you very much in advance for your answers
Here is my program:
#include <stdio.h>
#define nblignes 3
#define nbcolonnes 3
char grid[nblignes][nbcolonnes];
/*Initialization of the tic-tac-toe grid to empty: */
void initializeGrid(char grid[nblignes][nbcolonnes])
{
int i, j ;
for (i=0 ; i<nblignes ; i++)
{
for (j=0 ; j<nbcolonnes ; j++)
{
grid[i][j] = ' ' ;
}
}
}
/*Displaying the tic-tac-toe grid: */
void displayGrid(char grid[nblignes][nbcolonnes])
{
int i, j ;
for (i=0 ; i<nblignes ; i++)
{
for (j=0 ; j<(nbcolonnes) ; j++)
{
printf("%c", grid[i][j]) ;
printf("|") ;
}
printf("\n") ;
printf("- - -\n") ;
}
return ;
}
/*Input for the coordinates of the symbol to put on the grid. If the coordinates
are out of the grid or if the square has already been filled, input is
refused, an error message is displayed and the player must play again. */
void placeSymbol(char symbol, char grid[nblignes][nbcolonnes])
{
int row, column ;
printf("Please provide the row and column numbers:") ;
do
{
scanf_s("%i %i", &row, &column) ;
if ((row>0) && (row<=nblignes) && (column>0) && (column<=nbcolonnes))
{
row-- ; /*to be compatible with array indices*/
column-- ;
if (grid[row][column]!=' ')
printf("The square has already been filled. Please choose another one:") ;
else { grid[row][column]=symbol ;
if (symbol=='X') {symbol='O' ; }
else {symbol='X' ; }
}
}
else printf("Row or column index incorrect. Please re-enter:") ;
} while(grid[row][column]!=' ') ;
}
/*Test to see if one of the players has won: */
int winningTest(char grid[nblignes][nbcolonnes], char symbol)
{
int i, j, sum, won ;
sum = 0 ;
won = 0 ;
for (i=0 ; i<nblignes ; i++)
{
for (j=0 ; j<nbcolonnes ; j++)
{
sum+=grid[i][j] ;
}
if((sum==237) || (sum==264))
{ won = 1 ; }
}
for(j=0 ; j<nbcolonnes ; j++)
{
for(i=0 ; i<nblignes ; i++)
{
sum+=grid[i][j] ;
}
if((sum==237) || (sum==264))
{ won = 1 ; }
}
if(grid[0][0] + grid[1][1] + grid[2][2] == 237 || grid[0][0] + grid[1][1] + grid[2][2] == 264)
{ won = 1 ; }
if(grid[0][2] + grid[1][1] + grid[2][0] == 237 || grid[0][2] + grid[1][1] + grid[2][0] == 264)
{ won = 1 ; }
if (won==1)
{ printf("Congratulations to the player with the ") ;
if(symbol=='X')
printf("X") ;
else
printf("O") ;
printf("who has won.\n") ;
}
/* Test if the grid is full:*/
if(won==0){
for(i=0 ; i<nblignes ; i++)
{
for(j=0 ; j<= nbcolonnes ; j++)
{
if(grid[i][j] != ' ')
won=2 ;
printf("Neither player has won") ;
}
}
}
return won ;
}
void main()
{
char grid[nblignes][nbcolonnes] ;
char symbol ;
int won ;
initializeGrid(grid) ;
displayGrid(grid) ;
printf("Please choose your symbol, O or X:") ;
scanf("%c", &symbol) ;
do
{ placeSymbol(symbol, grid) ;
displayGrid(grid);
winningTest(grid, symbol) ;
}
while(won=0) ;
}
4 réponses
#include <stdio.h> #define nblignes 3 #define nbcolonnes 3 //char grille[nblignes][nbcolonnes]; /*Initialisation de la grille du morpion a vide : */ void initialisergrille(char grille[nblignes][nbcolonnes]) { int i, j; for (i=0; i<nblignes; i++) for (j=0; j<nbcolonnes; j++) grille[i][j] = ' '; } /*Affichage de la grille du morpion : */ void affichergrille(char grille[nblignes][nbcolonnes]) { int i, j; for (i=0; i<nblignes; i++) { for (j=0; j<nbcolonnes; j++) printf("%c|", grille[i][j]); printf("\n- - -\n"); } return; } /*Saisie des coordonnees du symbole a mettre sur la grille. Si les coordonnees sont en dehors de la grille ou si la case a deja ete remplie, la saisie est refusee, un message d'erreur s'affiche et le joueur doit rejouer. */ void mettresymbole(char symbole, char grille[nblignes][nbcolonnes]) { int ligne, colonne; printf("Veuillez donner les numeros de la ligne et de la colonne :"); do { scanf("%i %i", &ligne, &colonne); if ((ligne>0) && (ligne<=nblignes) && (colonne>0) && (colonne<=nbcolonnes)) { ligne--; /*pour etre compatible avec les indices du tableau*/ colonne--; if (grille[ligne][colonne]!=' ') printf("La case a deja ete remplie. Veuillez en choisir une autre :"); else { grille[ligne][colonne]=symbole; if (symbole=='X') symbole='O'; else symbole='X'; } } else printf("Indice de ligne ou colonne incorrect. Veuillez resaisir :"); } while(grille[ligne][colonne]==' '); } /*Test pour voir si l'un des joueurs a gagne : */ int testgagnant(char grille[nblignes][nbcolonnes], char symbole) { int i, j, somme, gagne=0; for (i=0; i<nblignes; i++, somme=0) { for (j=0; j<nbcolonnes; j++) somme+=grille[i][j]; if((somme==237) || (somme==264)) gagne = 1; } for(j=0 ; j<nbcolonnes ; j++, somme=0) { for(i=0 ; i<nblignes ; i++) somme+=grille[i][j] ; if((somme==237) || (somme==264)) gagne = 1; } if ( (grille[0][0] + grille[1][1] + grille[2][2] == 237) || (grille[0][0] + grille[1][1] + grille[2][2] == 264) || (grille[0][2] + grille[1][1] + grille[2][0] == 237) || (grille[0][2] + grille[1][1] + grille[2][0] == 264) ) gagne = 1; if (gagne) { printf("Felicitations au joueur ayant les %c qui a gagne\n", symbole); return 1; } /* Test si la grille est pleine :*/ for(i=0; i<nblignes; i++) for(j=0; j<nbcolonnes; j++) if(grille[i][j] == ' ') return 0; printf("Aucun des 2 joueurs n'a gagne"); return 2; } int main() { char grille[nblignes][nbcolonnes]; char symbole; initialisergrille(grille); affichergrille(grille); printf("Veuillez choisir votre symbole, O ou X :"); scanf("%c", &symbole); do { mettresymbole(symbole, grille); affichergrille(grille); } while(!testgagnant(grille, symbole)); return 0; }
I think that most of your issues come from your putSymbol() function.
The condition of your do...while() loop is always true if you enter correct coordinates (since in that case, you will have placed a symbol in place of the space, so the condition of your while is true because symbol != ' ').
I don't know about the %i but if it works for you and you want to use it, that's fine, I use %d for integers.
Otherwise, in your main() function, it should be while(gagne==0); otherwise, it's not a test but an assignment (which usually always works, and returns the assigned value, so in its current state it's like a while(0); so there's essentially no do...while();).
And one last thing: a main() function returns an int (not void).
I hope this helps you, I don’t know if this is the only thing that is "messing up" your program ;-).
The condition of your do...while() loop is always true if you enter correct coordinates (since in that case, you will have placed a symbol in place of the space, so the condition of your while is true because symbol != ' ').
I don't know about the %i but if it works for you and you want to use it, that's fine, I use %d for integers.
Otherwise, in your main() function, it should be while(gagne==0); otherwise, it's not a test but an assignment (which usually always works, and returns the assigned value, so in its current state it's like a while(0); so there's essentially no do...while();).
And one last thing: a main() function returns an int (not void).
I hope this helps you, I don’t know if this is the only thing that is "messing up" your program ;-).
Thank you both very much for your responses.
Loupius, with the changes you made, the program works. Thank you a thousand times. There is just one problem left, which is that it doesn't change O or X.
Loupius, with the changes you made, the program works. Thank you a thousand times. There is just one problem left, which is that it doesn't change O or X.
Thank you