英文:
my tic-tac-toe doesn't return the winner on column 2 and 3
问题
我有一个井字游戏的C程序。我的问题是,所有获胜条件都正常,除非在第2或第3列中出现3个'X'或'O',程序才不会返回获胜者,程序将继续运行,直到没有空格剩下或其他玩家(或计算机)获胜。但是,如果第一列中有3个'X'或'O',游戏将停止并返回获胜者。我曾尝试修复它,但我无法弄清楚我的代码有什么问题。
以下是我的获胜条件
#include <stdio.h>
#include <time.h>
char board[3][3];
const char PLAYER = 'X';
const char PLAYER2 = 'O';
const char BOT = 'O';
void resetBoard(){
int i,j;
for(i=0;i<3;i++){
for(j=0;j<3;j++){
board[i][j]=' ';
}
}
}
void printWinner(char winner){
if (winner==PLAYER){
printf("\nPlayer 1 wins!");
}
else if(winner==BOT){
printf("\nYou lose!");
}
else printf("\nTie!");
}
void printWinnerHuman(char winner){
if (winner==PLAYER){
printf("\nPlayer 1 wins!");
}
else if(winner==PLAYER2){
printf("\nPlayer 2 wins!");
}
else printf("\nTie!");
}
void playerMove(){
int x,y;
do{
printf("\nYou are X!");
printf("\nEnter move (Ex: 0 2): ");
scanf("%d %d",&x,&y);
if (board[x][y]!=' '){
printf("Space already taken\n");
printBoard();
}
else {
board[x][y]=PLAYER;
break;
}
}
while(board[x][y]!=' ');
}
void player2Move(){
int x,y;
do{
printf("You are O!");
printf("Enter your move (Ex: 1 2): ");
scanf("%d %d",&x,&y);
if(board[x][y]!=' '){
printf("\nSpace already taken!");
printBoard();
}
else{
board[x][y]=PLAYER2;
break;
}
}
while(board[x][y]!=' ');
}
void botMove(){
//create random moves for bot
srand(time(0));
int x,y;
if(checkFreeSpaces()>0){
do{
x=rand()%3;
y=rand()%3;
}
while(board[x][y]!=' ');
board[x][y]=BOT;
}
else printWinner(' ');
}
char checkWinner(){
int i;
//check rows winner
for(i=0;i<3;i++){
if (board[i][0]==board[i][1]&&board[i][0]==board[i][2]){
return board[i][0];
}
}
//check columns winner
for(i=0;i<3;i++){
if (board[0][i]==board[1][i]&&board[0][i]==board[2][i]){
return board[0][i];
}
//check diagnols winner
if(board[0][0]==board[1][1]&&board[0][i]==board[2][2]){
return board[0][0];
}
//check second diagnols winner
if(board[0][2]==board[1][1]&&board[0][2]==board[2][0]){
return board[0][2];
}
return ' '; //no winners
}
//This is a test line to test github saving
int checkFreeSpaces(){
int i,j;
int freeSpaces=9;
for(i=0;i<3;++i){
for(j=0;j<3;++j){
if (board[i][j]!=' '){
freeSpaces--;
}
}
}
return freeSpaces;
}
void printBoard(){
printf(" 0 1 2");
printf("\n |---|---|---|");
printf("\n0| %c | %c | %c |",board[0][0],board[0][1],board[0][2]);
printf("\n |---|---|---|");
printf("\n1| %c | %c | %c |",board[1][0],board[1][1],board[1][2]);
printf("\n |---|---|---|");
printf("\n2| %c | %c | %c |",board[2][0],board[2][1],board[2][2]);
printf("\n |---|---|---|");
}
void humanPlay(){
resetBoard(); //reset default declaration of computer
char winner =' ';
while(winner == ' ' && checkFreeSpaces()!=0){ //while there is no winner and there are still spaces
printBoard();
playerMove();
winner=checkWinner(); //after every move of player, program checks if there is a winner and if there are spaces left.
if(winner!=' ' || checkFreeSpaces()==0){
break;
}
printBoard();
player2Move();
winner=checkWinner();
if(winner!=' '||checkFreeSpaces()==0){ //if there is no spaces left and no winner, break loop.
break;
}
}
printBoard();
printWinner(winner);
}
void botPlay(){
resetBoard(); //reset default declarartion of computer
char winner =' ';
while(winner == ' ' && checkFreeSpaces()!=0){ //while there is no winner and there are still spaces
printBoard();
playerMove();
winner=checkWinner(); //after every move of player, program checks if there is a winner and if there are spaces left.
if(winner!=' ' || checkFreeSpaces()==0){
break;
}
botMove();
winner=checkWinner();
if(winner!=' ' || checkFreeSpaces()==0){
break;
}
}
printBoard();
printWinner(winner);
}
void menuMain(){
int choice;
printf("*-------MAIN MENU-------*");
printf("\n1. Play with other players");
printf("\n2. Play with BOT");
printf("\n3. Replay");
printf("\n4. Player's information");
printf("\n5. Guide");
printf("\n6. Exit");
printf("\nPress number to choose: "); scanf("%d",&choice);
switch(choice){
case 1:
humanPlay();
break;
case 2:
botPlay();
break;
case 3:
break;
case 4:
break;
case 5:
printf("\n1.The game is played on a grid that's
<details>
<summary>英文:</summary>
I have a tic-tac-toe C program. My problem is that all winning conditions is fine, except whenever there is a 3 'X' or 'O' in column 2 or 3, the program doesn't return the winner, the program will keep running until there are no spaces left or the other player(or the computer) wins. But, if there are 3 'X' or 'O' on the first column, the game will stop and return the winner. I had tried to fix it but i couldn't figure out what is wrong with my code.
Here are my winning conditions
#include <stdio.h>
#include <time.h>
char board[3][3];
const char PLAYER ='X';
const char PLAYER2 = 'O';
const char BOT ='O';
void resetBoard(){
int i,j;
for(i=0;i<3;i++){
for(j=0;j<3;j++){
board[i][j]=' ';
}
}
}
void printWinner(char winner){
if (winner==PLAYER){
printf("\nPlayer 1 wins!");
}
else if(winner==BOT){
printf("\nYou lose!");
}
else printf("\nTie!");
}
void printWinnerHuman(char winner){
if (winner==PLAYER){
printf("\nPlayer 1 wins!");
}
else if(winner==PLAYER2){
printf("\nPlayer 2 wins!");
}
else printf("\nTie!");
}
void playerMove(){
int x,y;
do{
printf("\nYou are X!");
printf("\nEnter move (Ex: 0 2): ");
scanf("%d %d",&x,&y);
if (board[x][y]!=' '){
printf("Space already taken\n");
printBoard();
}
else {
board[x][y]=PLAYER;
break;
}
}
while(board[x][y]!=' ');
}
void player2Move(){
int x,y;
do{
printf("You are O!");
printf("Enter your move (Ex: 1 2): ");
scanf("%d %d",&x,&y);
if(board[x][y]!=' '){
printf("\nSpace already taken!");
printBoard();
}
else{
board[x][y]=PLAYER2;
break;
}
}
while(board[x][y]!=' ');
}
void botMove(){
//create random moves for bot
srand(time(0));
int x,y;
if(checkFreeSpaces()>0){
do{
x=rand()%3;
y=rand()%3;
}
while(board[x][y]!=' ');
board[x][y]=BOT;
}
else printWinner(' ');
}
char checkWinner(){
int i;
//check rows winner
for(i=0;i<3;i++){
if (board[i][0]==board[i][1]&&board[i][0]==board[i][2]){
return board[i][0];
}
}
//check columns winner
for(i=0;i<3;i++){
if (board[0][i]==board[1][i]&&board[0][i]==board[2][i]){
return board[0][i];
}
//check diagnols winner
if(board[0][0]==board[1][1]&&board[0][i]==board[2][2]){
return board[0][0];
}
//check second diagnols winner
if(board[0][2]==board[1][1]&&board[0][2]==board[2][0]){
return board[0][2];
}
return ' '; //no winners
}
}
//This is a test line to test github saving
int checkFreeSpaces(){
int i,j;
int freeSpaces=9;
for(i=0;i<3;++i){
for(j=0;j<3;++j){
if (board[i][j]!=' '){
freeSpaces--;
}
}
}
return freeSpaces;
}
void printBoard(){
printf(" 0 1 2");
printf("\n |---|---|---|");
printf("\n0| %c | %c | %c |",board[0][0],board[0][1],board[0][2]);
printf("\n |---|---|---|");
printf("\n1| %c | %c | %c |",board[1][0],board[1][1],board[1][2]);
printf("\n |---|---|---|");
printf("\n2| %c | %c | %c |",board[2][0],board[2][1],board[2][2]);
printf("\n |---|---|---|");
}
void humanPlay(){
resetBoard(); //reset default declaration of computer
char winner =' ';
while(winner == ' ' &&checkFreeSpaces()!=0){ //while there is no winner and there are still spaces
printBoard();
playerMove();
winner=checkWinner(); //after every move of player, program checks if there is a winner and if there are spaces left.
if(winner!=' ' || checkFreeSpaces()==0){
break;
}
printBoard();
player2Move();
winner=checkWinner();
if(winner!=' '||checkFreeSpaces()==0){ //if there is no spaces left and no winner, break loop.
break;
}
}
printBoard();
printWinner(winner);
}
void botPlay(){
resetBoard(); //reset default declarartion of computer
char winner =' ';
while(winner == ' ' &&checkFreeSpaces()!=0){ //while there is no winner and there are still spaces
printBoard();
playerMove();
winner=checkWinner(); //after every move of player, program checks if there is a winner and if there are spaces left.
if(winner!=' ' || checkFreeSpaces()==0){
break;
}
botMove();
winner=checkWinner();
if(winner!=' ' || checkFreeSpaces()==0){
break;
}
}
printBoard();
printWinner(winner);
}
void menuMain(){
int choice;
printf("-------MAIN MENU-------");
printf("\n1. Play with other players");
printf("\n2. Play with BOT");
printf("\n3. Replay");
printf("\n4. Player's information'");
printf("\n5. Guide");
printf("\n6. Exit");
printf("\nPress number to choose: "); scanf("%d",&choice);
switch(choice){
case 1:
humanPlay();
break;
case 2:
botPlay();
break;
case 3:
break;
case 4:
break;
case 5:
printf("\n1.The game is played on a grid that's 3 squares by 3 squares");
printf("\n2.You are X, your friend (or the computer in this case) is O. Players take turns putting their marks in empty squares.");
printf("\n3.The first player to get 3 of her marks in a row (up, down, across, or diagonally) is the winner.");
printf("\n3.The first player to get 3 of her marks in a row (up, down, across, or diagonally) is the winner.");
printf("\n4.When all 9 squares are full, the game is over.");
break;
case 6:
printf("Thank you for playing");
break;
}
}
int main(){
menuMain();
return 0;
}
Thanks!!
I tried changing from ++i to i++
</details>
# 答案1
**得分**: 2
在你的`checkWinners`函数中,你在for循环内部有一个return语句:
```c
char checkWinner(){
int i;
//检查行的获胜者
for(i=0;i<3;i++){
if (board[i][0]==board[i][1]&&board[i][0]==board[i][2]){
return board[i][0];
} // 缺少了这个大括号,用于检查列的获胜者
}
//检查列的获胜者
for(i=0;i<3;i++){
if (board[0][i]==board[1][i]&&board[0][i]==board[2][i]){
return board[0][i];
} // 错误
}
//检查对角线的获胜者
if(board[0][0]==board[1][1]&&board[0][i]==board[2][2]){
return board[0][0];
}
//检查第二个对角线的获胜者
if(board[0][2]==board[1][1]&&board[0][2]==board[2][0]){
return board[0][2];
}
return ' '; //没有获胜者
} // 看到这里,这是一个提示,表示有些地方有问题
乍一看,你可能没有注意到for
语句在if
条件之后仍然继续运行。
你可能想表达的是:
char checkWinner(){
int i;
//检查行的获胜者
for(i=0;i<3;i++){
if (board[i][0]==board[i][1]&&board[i][0]==board[i][2]){
return board[i][0];
}
}
//检查列的获胜者
for(i=0;i<3;i++){
if (board[0][i]==board[1][i]&&board[0][i]==board[2][i]){
return board[0][i];
} // 添加这个
}
//检查对角线的获胜者
if(board[0][0]==board[1][1]&&board[0][i]==board[2][2]){
return board[0][0];
}
//检查第二个对角线的获胜者
if(board[0][2]==board[1][1]&&board[0][2]==board[2][0]){
return board[0][2];
}
return ' '; //没有获胜者
}
英文:
In your checkWinners, function, you have a return statement within your for-loop:
char checkWinner(){
int i;
//check rows winner
for(i=0;i<3;i++){
if (board[i][0]==board[i][1]&&board[i][0]==board[i][2]){
return board[i][0];
} // MISSING THIS CURLY BRACE FOR THE COLUMN WINNERS CHECK
}
//check columns winner
for(i=0;i<3;i++){
if (board[0][i]==board[1][i]&&board[0][i]==board[2][i]){
return board[0][i];
} // OOPS
//check diagnols winner
if(board[0][0]==board[1][1]&&board[0][i]==board[2][2]){
return board[0][0];
}
//check second diagnols winner
if(board[0][2]==board[1][1]&&board[0][2]==board[2][0]){
return board[0][2];
}
return ' '; //no winners
} // SEE THIS, THIS IS A HINT SOMETHING IS WRONG
}
On first glance, you won't noticed that the for
statement still continues after the if
condition.
You probably meant this:
char checkWinner(){
int i;
//check rows winner
for(i=0;i<3;i++){
if (board[i][0]==board[i][1]&&board[i][0]==board[i][2]){
return board[i][0];
}
}
//check columns winner
for(i=0;i<3;i++){
if (board[0][i]==board[1][i]&&board[0][i]==board[2][i]){
return board[0][i];
} // ADD THIS
}
//check diagnols winner
if(board[0][0]==board[1][1]&&board[0][i]==board[2][2]){
return board[0][0];
}
//check second diagnols winner
if(board[0][2]==board[1][1]&&board[0][2]==board[2][0]){
return board[0][2];
}
return ' '; //no winners
}
</details>
# 答案2
**得分**: 0
以下是翻译好的部分:
"Then `checkWinner` would see 3 of the same character (`' '`) in column 0 and return that character, which `humanPlay` and `botPlay` correctly ignore."
<details>
<summary>英文:</summary>
For the sake of demonstration, assume the board is like this:
XO
XO
X
Then `checkWinner` would see 3 of the same character (`' '`) in column 0 and return that character, which `humanPlay` and `botPlay` correctly ignore.
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论