|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
做了好长时间终于做出了一个井字棋游戏,
当初是看书上有的就试着做了一下,没想到都是bug,后来对照着书改了一下,终于搞定了!!
- #include <stdio.h>
- void main()
- {
- int x = 0;//玩家输入字符
- char board[3][3] = { { '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[0][0], board[0][1], board[0][2]);
- printf("+---+---+---+\n");
- printf("| %c | %c | %c |\n", board[1][0], board[1][1], board[1][2]);
- printf("+---+---+---+\n");
- printf("| %c | %c | %c |\n", board[2][0], board[2][1], board[2][2]);
- 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[0][0], board[0][1], board[0][2]);
- printf("+---+---+---+\n");
- printf("| %c | %c | %c |\n", board[1][0], board[1][1], board[1][2]);
- printf("+---+---+---+\n");
- printf("| %c | %c | %c |\n", board[2][0], board[2][1], board[2][2]);
- 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[row][column] > '9')
- {
- printf("重新输入!");
- }
- } while (x < 0 || x>9 || board[row][column] > '9');
- board[row][column] = (play == 1) ? '@' : 'X';
- /*查找胜利者*/
- if (board[0][0] == board[0][1] && board[0][2] == board[0][0]
- || board[1][0] == board[1][1] && board[1][2] == board[1][0]
- || board[2][0] == board[2][1] && board[2][2] == board[2][0]
- || board[0][0] == board[1][1] && board[0][0] == board[2][2]
- || board[0][2] == board[1][1] && board[0][2] == board[2][0]
- || board[0][0] == board[1][0] && board[0][0] == board[2][0]
- || board[0][1] == board[1][1] && board[0][1] == board[2][1]
- || board[0][2] == board[1][2] && board[0][2] == board[2][2])
- {
- printf("+---+---+---+\n");
- printf("| %c | %c | %c |\n", board[0][0], board[0][1], board[0][2]);
- printf("+---+---+---+\n");
- printf("| %c | %c | %c |\n", board[1][0], board[1][1], board[1][2]);
- printf("+---+---+---+\n");
- printf("| %c | %c | %c |\n", board[2][0], board[2][1], board[2][2]);
- winner = play;
- printf("玩家%d,你赢了!!\n游戏结束!", winner);
- break;
- }
- }
- getchar();
- }
复制代码
|
|