一个小游戏
做了好长时间终于做出了一个井字棋游戏,当初是看书上有的就试着做了一下,没想到都是bug,后来对照着书改了一下,终于搞定了!!
#include <stdio.h>
void main()
{
int x = 0;//玩家输入字符
char board = { { '1', '2', '3' }, { '4', '5', '6' }, { '7', '8', '9' } };/*棋盘*/
int i = 0;//控制循环
int play = 1;
int winner = 0;
char player = '\0';
char keep = 0;
int game_stat = 1;
int row = 0;
int column = 0;//row为行,column为列;
printf("***这是一个井字棋小游戏***\n");
printf("***载入中***\n\n");
/*显示棋盘*/
printf("+---+---+---+\n");
printf("| %c | %c | %c |\n", board, board, board);
printf("+---+---+---+\n");
printf("| %c | %c | %c |\n", board, board, board);
printf("+---+---+---+\n");
printf("| %c | %c | %c |\n", board, board, board);
printf("\n按回车键继续……\n");
getchar();
printf("请输入1-9任意数字对应方格中的数字\n"
"玩家1棋子为@,玩家2为X");
/*游戏开始*/
for (i = 0; i < 9;i++)
{
play = i % 2 + 1;//换玩家下棋
printf("+---+---+---+\n");
printf("| %c | %c | %c |\n", board, board, board);
printf("+---+---+---+\n");
printf("| %c | %c | %c |\n", board, board, board);
printf("+---+---+---+\n");
printf("| %c | %c | %c |\n", board, board, board);
printf("输入:\n你的棋子是%c\n玩家%d行动,",(play == 1) ? '@' : 'X' , play);
do
{
scanf("%d", &x);
row = --x / 3;
column = x % 3;
if (x < 0 || x>9 || board > '9')
{
printf("重新输入!");
}
} while (x < 0 || x>9 || board > '9');
board = (play == 1) ? '@' : 'X';
/*查找胜利者*/
if (board == board && board == board
|| board == board && board == board
|| board == board && board == board
|| board == board && board == board
|| board == board && board == board
|| board == board && board == board
|| board == board && board == board
|| board == board && board == board)
{
printf("+---+---+---+\n");
printf("| %c | %c | %c |\n", board, board, board);
printf("+---+---+---+\n");
printf("| %c | %c | %c |\n", board, board, board);
printf("+---+---+---+\n");
printf("| %c | %c | %c |\n", board, board, board);
winner = play;
printf("玩家%d,你赢了!!\n游戏结束!", winner);
break;
}
}
getchar();
}
页:
[1]