【发布时间】:2021-07-16 06:01:28
【问题描述】:
问题的简要说明 - 代码的目的是使用 C 制作一个基本的井字游戏。有两个玩家 X 和 O ,他们都可以输入各种数字,如 choice 从 1- 9 为每个单独的机会。
游戏板类似于 3 x 3 矩阵,其中 -
- 第 1 行用于 1 到 3。
- 第 2 行用于 4 到 6。
- 第 3 行是 7 到 9。
除 1-9 之外的任何数字都会引发错误并提示用户重新输入数字。不幸的是,对于有效输入,我遇到了相同的无效输入错误。除了我的循环之外,其他一切似乎都可以正常工作。
这是供参考的代码 -
#include<stdio.h> //Tic Tac Toe
#include<stdlib.h>
#include<conio.h>
char square[10] = {'0','1','2','3','4','5','6','7','8','9'};
int choice, player;
int checkForWin();
void displayBoard();
void mrkBoard(char mark);
int main()
{
int i;
char mark;
player = 1;
do
{
displayBoard();
player = (player % 2) ? 1:2;
printf("Player %d, enter the number: ",player);
scanf("%d",&choice);
mark = (player == 1) ? 'X' : 'O';
mrkBoard(mark);
i = checkForWin();
player++;
}while(i == -1);
return 0;
}
int checkForWin()
{
int returnValue = 0;
if (square[1] == square[2] && square[2] == square[3])
{
returnValue = 1;
}
else if (square[4] == square[5] && square[5] == square[6])
returnValue = 1;
else if (square[7] == square[8] && square[8] == square[9])
returnValue = 1;
else if (square[1] == square[5] && square[5] == square[9])
returnValue = 1;
else if (square[3] == square[5] && square[5] == square[7])
returnValue = 1;
else if (square[1] == square[4] && square[4] == square[7])
returnValue = 1;
else if (square[2] == square[5] && square[5] == square[8])
returnValue = 1;
else if (square[3] == square[6] && square[6] == square[9])
returnValue = 1;
else if(square[1] != '1' && square[2] != '2' && square[3] != '3' && square[4] != '4' &&
square[5] != '5' && square[6] != '6' && square[7] != '7' &&
square[8] != '8' && square[9] != '9')
returnValue = 0;
else
returnValue = -1;
return returnValue;
}
void displayBoard()
{
system("cls");
printf("\n\nTic Tac Toe\n\n");
printf("Player 1 (X) - Player 2 (O)\n\n\n");
printf(" | | \n");
printf(" %c | %c | %c \n", square[1], square[2],square[3]);
printf("_____|_____|_____\n");
printf(" | | \n");
printf(" %c | %c | %c\n", square[4], square[5],square[6]);
printf("_____|_____|_____\n");
printf(" | | \n");
printf(" %c | %c | %c\n", square[7], square[8],square[9]);
printf(" | | \n\n");
}
void mrkBoard(char mark)
{
if (choice == 1 && square[1] == '1')
square[1] = mark;
else if (choice == 2 && square[1] == '2')
square[2] = mark;
else if (choice == 3 && square[1] == '3')
square[3] = mark;
else if (choice == 4 && square[1] == '4')
square[4] = mark;
else if (choice == 5 && square[1] == '5')
square[5] = mark;
else if (choice == 6 && square[1] == '6')
square[6] = mark;
else if (choice == 7 && square[1] == '7')
square[7] = mark;
else if (choice == 8 && square[1] == '8')
square[8] = mark;
else if (choice == 9 && square[1] == '9')
square[9] = mark;
else
{
printf("Invalid ");
player--;
getch();
}
}
【问题讨论】:
-
很高兴能提供帮助——我们都去过那里......
-
两件事:首先,提取minimal reproducible example。这将帮助您专注于问题。其次,使用调试器单步执行代码。这是一项非常宝贵的技能,可以让您观察您的程序的运行情况。
-
不用担心,你可以简单地做
printf("\n\nTic Tac Toe\n\n"然后下一行"Player 1 (X) - Player 2 (O)\n\n\n"然后下一行"Player 1 (X) - Player 2 (O)\n\n\n"...一直到最后一个" | | \n\n", square[1], square[2],square[3], square[4], square[5],square[6], square[7], square[8],square[9]);(你可以缩进你的板输出任何你想要——把它排成一行,这样看起来不错)所有相邻的字符串文字在编译期间都被连接起来。拨打printf()比拨打 11 更有效率。 -
这是一个Full - One printf() 示例,它展示了如何通过将全局数组
squares[]作为参数传递给displayBoard()来消除对它的使用——祝您编码顺利。
标签: c