我们直接给出目前test.c文件中的所有代码
#define _CRT_SECURE_NO_WARNINGS 1 #include"game.h" void meau() { printf("********************************\n"); printf("******** 1.play *******\n"); printf("******** 0.exit *******\n"); printf("********************************\n"); } void game() { char board[ROW][COL]; //初始化棋盘 init_board(board, ROW, COL); //打印棋盘 print_board(board, ROW, COL); //玩家下棋 char ret = ' '; //ret用来接受一个返回值,用来判断输赢 while (1) { player_move(board, ROW, COL); print_board(board, ROW, COL); ret = is_win(board, ROW, COL); //判断游戏是否继续 if (ret != "C") { break;//如果不继续,打破循环 } computer_move(board, ROW, COL); print_board(board, ROW, COL); //判断游戏是否继续 ret = is_win(board, ROW, COL); if (ret != "C") { break;//如果不继续,打破循环 } } if (ret == '*') { printf("玩家赢\n"); } else if (ret == '#') { printf("电脑赢\n"); } else if (ret == 'Q') { printf("平局\n"); } } void test() { srand((unsigned int)time(NULL)); int input = 0; do { meau(); printf("请输入>:\n"); scanf("%d", &input); switch (input) { case 1: printf("进入三子棋游戏\n"); game(); break; case 0: printf("退出游戏\n"); break; default: printf("选择错误,请重新选择\n"); break; } } while (input); } int main() { test(); return 0; }
(2)具体函数的实现
我们先来实现一下谁赢了的场景,如下图所示,一共有四种判断方式,这种方式其实是比较写死了。不适合进行推广到N子棋。这个后续是可以进行优化的。但是我们今天先不做优化,我们今天的目标就只是三子棋。不过我们后续会专门出一篇文章,来将三子棋的各大功能进行全方位的升级。今天我们只讨论基础版本的三子棋。
判断玩输赢,那么还需要平局和游戏继续两种状态,平局其实不难理解,就是整个棋盘都满了,还没有决出胜负。所以我们是需要将判断平局放在决出胜负之后的。判断棋盘是否满,我们可以封装一共函数。is_full,这个函数判断是否满。如果满返回1,否则返回0。
具体代码实现如下,这个代码没必要写在头文件声明,因为这个代码其实只需要被判断输赢函数使用即可
然后is_win剩余的逻辑为
(3)代码测试
玩家赢
电脑赢
和棋
这块当时好不容易下成了和棋,结果网卡了没传上........不过好歹还有没上传成功时候的正在处理的截图,还是可以使用的。
(4)本模块代码
//判断棋盘是否满了 int is_full(char board[ROW][COL],int row,int col) { int i = 0; int j = 0; for (i = 0; i < row; i++) { for (j = 0; j < col; j++) { if(board[i][j] == ' ') return 0; } } return 1; } //判断输赢 char is_win(char board[ROW][COL], int row, int col) { int i = 0; //判断三行 for (i = 0; i < row; i++) { if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][0] != ' ') { return board[i][0]; } } //判断三列 for (i = 0; i < col; i++) { if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][i] != ' ') { return board[0][i]; } } //判断主对角线 if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[0][0] != ' ') { return board[0][0]; } //判断副对角线 if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[0][2] != ' ') { return board[0][2]; } if (is_full(board,row,col) == 1) { return 'Q'; } return 'C'; }
四、最终代码展示(分文件)
我们代码是三个文件的
1.test.c文件
#define _CRT_SECURE_NO_WARNINGS 1 #include"game.h" void meau() { printf("********************************\n"); printf("******** 1.play *******\n"); printf("******** 0.exit *******\n"); printf("********************************\n"); } void game() { char board[ROW][COL]; //初始化棋盘 init_board(board, ROW, COL); //打印棋盘 print_board(board, ROW, COL); //玩家下棋 char ret = ' '; //ret用来接受一个返回值,用来判断输赢 while (1) { player_move(board, ROW, COL); print_board(board, ROW, COL); ret = is_win(board, ROW, COL); //判断游戏是否继续 if (ret != 'C') { break;//如果不继续,打破循环 } computer_move(board, ROW, COL); print_board(board, ROW, COL); //判断游戏是否继续 ret = is_win(board, ROW, COL); if (ret != 'C') { break;//如果不继续,打破循环 } } if (ret == '*') { printf("玩家赢\n"); } else if (ret == '#') { printf("电脑赢\n"); } else if (ret == 'Q') { printf("平局\n"); } } void test() { srand((unsigned int)time(NULL)); int input = 0; do { meau(); printf("请输入>:\n"); scanf("%d", &input); switch (input) { case 1: printf("进入三子棋游戏\n"); game(); break; case 0: printf("退出游戏\n"); break; default: printf("选择错误,请重新选择\n"); break; } } while (input); } int main() { test(); return 0; }
2.game.h文件
#pragma once #include<stdio.h> #include<stdlib.h> #include<time.h> #define ROW 3 #define COL 3 //头文件声明函数 void init_board(char board[ROW][COL], int row, int col); //打印棋盘 void print_board(char board[ROW][COL], int row, int col); //玩家下棋 void player_move(char board[ROW][COL], int row, int col); //电脑下棋 void computer_move(char board[ROW][COL], int row, int col); //判断输赢 char is_win(char board[ROW][COL], int row, int col);
3.game.c文件
#define _CRT_SECURE_NO_WARNINGS 1 #include"game.h" void meau() { printf("********************************\n"); printf("******** 1.play *******\n"); printf("******** 0.exit *******\n"); printf("********************************\n"); } void game() { char board[ROW][COL]; //初始化棋盘 init_board(board, ROW, COL); //打印棋盘 print_board(board, ROW, COL); //玩家下棋 char ret = ' '; //ret用来接受一个返回值,用来判断输赢 while (1) { player_move(board, ROW, COL); print_board(board, ROW, COL); ret = is_win(board, ROW, COL); //判断游戏是否继续 if (ret != 'C') { break;//如果不继续,打破循环 } computer_move(board, ROW, COL); print_board(board, ROW, COL); //判断游戏是否继续 ret = is_win(board, ROW, COL); if (ret != 'C') { break;//如果不继续,打破循环 } } if (ret == '*') { printf("玩家赢\n"); } else if (ret == '#') { printf("电脑赢\n"); } else if (ret == 'Q') { printf("平局\n"); } } void test() { srand((unsigned int)time(NULL)); int input = 0; do { meau(); printf("请输入>:\n"); scanf("%d", &input); switch (input) { case 1: printf("进入三子棋游戏\n"); game(); break; case 0: printf("退出游戏\n"); break; default: printf("选择错误,请重新选择\n"); break; } } while (input); } int main() { test(); return 0; }
五、代码展示(三个文件合成为一个文件)
#define _CRT_SECURE_NO_WARNINGS 1 #include<stdio.h> #include<stdlib.h> #include<time.h> #define ROW 3 #define COL 3 //头文件声明函数 void init_board(char board[ROW][COL], int row, int col); //打印棋盘 void print_board(char board[ROW][COL], int row, int col); //玩家下棋 void player_move(char board[ROW][COL], int row, int col); //电脑下棋 void computer_move(char board[ROW][COL], int row, int col); //判断输赢 char is_win(char board[ROW][COL], int row, int col); void meau() { printf("********************************\n"); printf("******** 1.play *******\n"); printf("******** 0.exit *******\n"); printf("********************************\n"); } void game() { char board[ROW][COL]; //初始化棋盘 init_board(board, ROW, COL); //打印棋盘 print_board(board, ROW, COL); //玩家下棋 char ret = ' '; //ret用来接受一个返回值,用来判断输赢 while (1) { player_move(board, ROW, COL); print_board(board, ROW, COL); ret = is_win(board, ROW, COL); //判断游戏是否继续 if (ret != 'C') { break;//如果不继续,打破循环 } computer_move(board, ROW, COL); print_board(board, ROW, COL); //判断游戏是否继续 ret = is_win(board, ROW, COL); if (ret != 'C') { break;//如果不继续,打破循环 } } if (ret == '*') { printf("玩家赢\n"); } else if (ret == '#') { printf("电脑赢\n"); } else if (ret == 'Q') { printf("平局\n"); } } void test() { srand((unsigned int)time(NULL)); int input = 0; do { meau(); printf("请输入>:\n"); scanf("%d", &input); switch (input) { case 1: printf("进入三子棋游戏\n"); game(); break; case 0: printf("退出游戏\n"); break; default: printf("选择错误,请重新选择\n"); break; } } while (input); } int main() { test(); return 0; } //初始化棋盘 void init_board(char board[ROW][COL], int row, int col) { int i = 0; for (i = 0; i < row; i++) { int j = 0; for (j = 0; j < col; j++) { board[i][j] = ' '; } } } //打印棋盘 void print_board(char board[ROW][COL], int row, int col) { int i = 0; for (i = 0; i < row; i++) { int j = 0; for (j = 0; j < col; j++) { printf(" %c ", board[i][j]); if (j < col - 1) { printf("|"); } } printf("\n"); if (i < row - 1) { for (j = 0; j < col; j++) { printf("---"); if (j < col - 1) { printf("|"); } } printf("\n"); } } } //玩家下棋 void player_move(char board[ROW][COL], int row, int col) { int x = 0; int y = 0; printf("玩家下棋\n"); while (1) { printf("请输入坐标>:\n"); scanf("%d %d", &x, &y); if (x >= 1 && x <= row && y >= 1 && y <= col) { if (board[x - 1][y - 1] == ' ') { board[x - 1][y - 1] = '*'; break; } else { printf("该位置已经有棋子了,请重新下棋"); } } else { printf("坐标非法,请重新输入\n"); } } } //电脑下棋 void computer_move(char board[ROW][COL], int row, int col) { int x = 0; int y = 0; printf("电脑下棋\n"); while (1) { x = rand() % row + 1; y = rand() % col + 1; if (x >= 1 && x <= row && y >= 1 && y <= col) { if (board[x - 1][y - 1] == ' ') { board[x - 1][y - 1] = '#'; break; } } } } //判断棋盘是否满了 int is_full(char board[ROW][COL], int row, int col) { int i = 0; int j = 0; for (i = 0; i < row; i++) { for (j = 0; j < col; j++) { if (board[i][j] == ' ') return 0; } } return 1; } //判断输赢 char is_win(char board[ROW][COL], int row, int col) { int i = 0; //判断三行 for (i = 0; i < row; i++) { if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][0] != ' ') { return board[i][0]; } } //判断三列 for (i = 0; i < col; i++) { if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][i] != ' ') { return board[0][i]; } } //判断主对角线 if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[0][0] != ' ') { return board[0][0]; } //判断副对角线 if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[0][2] != ' ') { return board[0][2]; } if (is_full(board, row, col) == 1) { return 'Q'; } return 'C'; }
总结
本节主要讲解了三子棋的详细实现,为大家熟练的打通了各种思考逻辑,为什么是这样子,常见的错误。同时最终也给出了三子棋的最终代码,分文件和合并为一个文件都给出了具体的代码。如果对你有帮助,不要忘记点赞加收藏哦!!!