我在使用 C 语言创建的井字游戏时遇到了一些问题。
我有一个名为 int cats 的变量(猫游戏是平局游戏),用于帮助计算和显示平局游戏的文本。
我几乎整个星期都在尝试弄清楚如何让比赛打成平局。即使你们能指出我正确的方向,那也太好了!
-弗伦丁
(PS开头的空if语句是针对玩家与电脑游戏的。如果我能弄清楚如何让游戏打成平局,我可以编写那部分没问题!再次感谢大家!)
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void printTheBoard(char boardPieces[3][3]);
int playerOne(char boardPieces[3][3], int cats);
int playerTwo(char boardPieces[3][3], int cats);
void checkBoard(char boardPieces[3][3], int cats);
int main(void)
{
char boardPieces[3][3] = {'\0'};
int cats = 0; //A tie game in Tic-Tac-Toe is called a Cats Game.
int choice;
printf("Would you like to play against the computer or another player?\n");
printf("Press 1 to play against another player.\n");
printf("Press 2 to play against the computer.\n");
scanf("%d", &choice);
if(choice == 1)
{
for(int i = 0; i < 5; ++i)
{
printTheBoard(boardPieces);
playerOne(boardPieces, cats);
checkBoard(boardPieces, cats);
printTheBoard(boardPieces);
playerTwo(boardPieces, cats);
checkBoard(boardPieces, cats);
}
}
if(choice == 2)
{
}
return 0;
}
void printTheBoard(char boardPieces[3][3])
{
printf("\n %c | %c | %c\n", boardPieces[0][0], boardPieces[0][1], boardPieces[0][2]);
printf("---|---|---\n");
printf(" %c | %c | %c\n", boardPieces[1][0], boardPieces[1][1], boardPieces[1][2]);
printf("---|---|---\n");
printf(" %c | %c | %c\n\n", boardPieces[2][0], boardPieces[2][1], boardPieces[2][2]);
}
int playerOne(char boardPieces[3][3], int cats)
{
int choice;
printf("It is your turn. You are player O. Please enter in the space you would like to take. The first space is 1, continuing left to right, top to bottom\n");
scanf("%d", &choice);
if(choice == 1)
{
boardPieces[0][0] = 'O';
++cats;
return cats;
}
else if(choice == 2)
{
boardPieces[0][1] = 'O';
++cats;
return cats;
}
else if(choice == 3)
{
boardPieces[0][2] = 'O';
++cats;
return cats;
}
else if(choice == 4)
{
boardPieces[1][0] = 'O';
++cats;
return cats;
}
else if(choice == 5)
{
boardPieces[1][1] = 'O';
++cats;
return cats;
}
else if(choice == 6)
{
boardPieces[1][2] = 'O';
++cats;
return cats;
}
else if(choice == 7)
{
boardPieces[2][0] = 'O';
++cats;
return cats;
}
else if(choice == 8)
{
boardPieces[2][1] = 'O';
++cats;
return cats;
}
else if(choice == 9)
{
boardPieces[2][2] = 'O';
++cats;
return cats;
}
}
int playerTwo(char boardPieces[3][3], int cats)
{
int choice;
printf("It is your turn. You are player X. Please enter in the space you would like to take. The first space is 1, continuing left to right, top to bottom\n");
scanf("%d", &choice);
if(choice == 1)
{
boardPieces[0][0] = 'X';
++cats;
return cats;
}
else if(choice == 2)
{
boardPieces[0][1] = 'X';
++cats;
return cats;
}
else if(choice == 3)
{
boardPieces[0][2] = 'X';
++cats;
return cats;
}
else if(choice == 4)
{
boardPieces[1][0] = 'X';
++cats;
return cats;
}
else if(choice == 5)
{
boardPieces[1][1] = 'X';
++cats;
return cats;
}
else if(choice == 6)
{
boardPieces[1][2] = 'X';
++cats;
return cats;
}
else if(choice == 7)
{
boardPieces[2][0] = 'X';
++cats;
return cats;
}
else if(choice == 8)
{
boardPieces[2][1] = 'X';
++cats;
return cats;
}
else if(choice == 9)
{
boardPieces[2][2] = 'X';
++cats;
return cats;
}
}
void checkBoard(char boardPieces[3][3], int cats)
{
if(boardPieces[0][0] == 'O')
{
if(boardPieces[0][1] == 'O')
{
if(boardPieces[0][2] == 'O')
{
printf("Player O has won the game!\n");
}
}
else if(boardPieces[1][1] == 'O')
{
if(boardPieces[2][2] == 'O')
{
printf("Player O has won the game!\n");
}
}
else if(boardPieces[1][0] == 'O')
{
if(boardPieces[2][0] == 'O')
{
printf("Player O has won the game!\n");
}
}
}
else if(boardPieces[0][2] == 'O')
{
if(boardPieces[1][1] == 'O')
{
if(boardPieces[2][0] == 'O')
{
printf("Player O has won the game!\n");
}
}
else if(boardPieces[1][2] == 'O')
{
if(boardPieces[2][2] == 'O')
{
printf("Player O has won the game!\n");
}
}
}
else if(boardPieces[1][1] == 'O')
{
if(boardPieces[0][1] == 'O')
{
if(boardPieces[2][1] == 'O')
{
printf("Player O has won the game!\n");
}
}
else if(boardPieces[1][0] == 'O')
{
if(boardPieces[1][2] == 'O')
{
printf("Player O has won the game!\n");
}
}
}
else if(boardPieces[2][0] == 'O')
{
if(boardPieces[2][1] == 'O')
{
if(boardPieces[2][2] == 'O')
{
printf("Player O has won the game!\n");
}
}
}
if(boardPieces[0][0] == 'X')
{
if(boardPieces[0][1] == 'X')
{
if(boardPieces[0][2] == 'X')
{
printf("Player X has won the game!\n");
}
}
else if(boardPieces[1][1] == 'X')
{
if(boardPieces[2][2] == 'X')
{
printf("Player X has won the game!\n");
}
}
else if(boardPieces[1][0] == 'X')
{
if(boardPieces[2][0] == 'X')
{
printf("Player X has won the game!\n");
}
}
}
else if(boardPieces[0][2] == 'X')
{
if(boardPieces[1][1] == 'X')
{
if(boardPieces[2][0] == 'X')
{
printf("Player X has won the game!\n");
}
}
else if(boardPieces[1][2] == 'X')
{
if(boardPieces[2][2] == 'X')
{
printf("Player X has won the game!\n");
}
}
}
else if(boardPieces[1][1] == 'X')
{
if(boardPieces[0][1] == 'X')
{
if(boardPieces[2][1] == 'X')
{
printf("Player X has won the game!\n");
}
}
else if(boardPieces[1][0] == 'X')
{
if(boardPieces[1][2] == 'X')
{
printf("Player X has won the game!\n");
}
}
}
else if(boardPieces[2][0] == 'X')
{
if(boardPieces[2][1] == 'X')
{
if(boardPieces[2][2] == 'X')
{
printf("Player O has won the game!\n");
}
}
}
else if(cats == 9)
{
printf("There was a tie game!\n");
}
}