C语言实现三子棋(具体步骤和完整代码)
📜个人简介 |
⭐️个人主页:摸鱼の文酱博客主页🙋♂️
🍑博客领域:java编程基础,mysql
🍅写作风格:干货,干货,还是tmd的干货
🌸精选专栏:【Java】【mysql】 【算法刷题笔记】
🎯博主的码云gitee,平常博主写的程序代码都在里面。
🚀支持博主:点赞👍、收藏⭐、留言💬
🍭作者水平很有限,如果发现错误,一定要及时告知作者哦!感谢感谢!
本篇文章让我们来写一个经典的小游戏–三子棋
文末有完整代码,需要者可以自行前往.
这里写目录标题
- 🌟实现方法:
- 🌟实现步骤
- 🌟结果演示
-
- 🌴1.玩家胜利
- 🌴2.电脑胜利
- 🌴3.和棋
- 🌟完整代码
🌟实现方法:
相信大家都知道这个游戏的规则,那么接下来先看一下用c语言实现时要注意什么逻辑:
1.菜单界面选择开始或者退出游戏。2.创建棋盘并初始化。3.打印棋盘。4.玩家落子(玩家输入行列坐标的方式来落子),'*’表示玩家落子。5.判定胜负关系(输,赢,和棋)。6.电脑落子(随机位置落子) ,'#’表示电脑落子。7.判定胜负关系。如果玩家赢 -> return '*'如果电脑赢 -> return '#'如果平局 -> return 'Q'游戏继续 -> return 'C'8.回到 步骤2 继续执行。
🌟实现步骤
🌴1.菜单界面
void menu(){printf("**************************************\n");printf("***************三子棋*****************\n");printf("*************1.开始游戏***************\n");printf("*************0.退出游戏***************\n");printf("**************************************\n");}
🌴2.打印棋盘
棋盘:使用3行3列的二维数组来表示,元素类型是char。
使用宏定义的原因:
1.推高代码可读性,后续代码中遇到3,方便理解含义。
2.提高扩展性,如果将来要修改棋盘尺寸,代码修改会很方便。
#define ROW 3#define COL 3
🌴3.棋盘初始化
要实现一个3*3大小的棋盘,
可以借助宏定义的方式,使row,col这种有代表性的字符附带数值
方便后面代码的编写
//初始化棋盘void InidBoard(char board[ROW][COL], int row, int col){int i = 0, j = 0;//初始化元素为空格for (i = 0; i < row; i++){for (j = 0; j < col; j++){board[i][j] = ' ';}}}
🌴4.打印棋盘
void DisplayBoard(char board[ROW][COL], int row, int col){int j = 0;for (j = 0; j < row; ++j){int i = 0;for (i = 0; i < col; ++i){printf(" %c ", board[j][i]);if (i < col - 1){printf("|"); //打印垂直分割线}}printf("\n"); //换行//打印水平分割线if (j < row - 1){for (i = 0; i < col; ++i){printf("---");if (i < col - 1){printf("|");}}printf("\n"); //换行}}}
🌴5.玩家落子
/玩家下棋void PlayerMove(char board[ROW][COL], int row, int col){printf("玩家下棋:\n");int x = 0; //行int y = 0; //列while (1){printf("请输入你要落棋子的坐标:");scanf("%d %d", &x, &y); //在此处坐标,实际输入坐标为[1][1] -> 数组中位置为[0][0]if (x >= 1 && x <= row && y >= 1 && y <= col){if (board[x - 1][y - 1] == ' '){board[x - 1][y - 1] = '*';break;}else{printf("该位置已有棋子!,请重新输入\n");}}else{printf("没有这个位置哦!请重新输入\n");}}}
🌴6.电脑落子
//电脑下棋void ComputerMove(char board[ROW][COL], int row, int col){int x = 0; //行int y = 0; //列printf("电脑下棋:\n");while (1){x = rand() % row; //生成随机数0、1、2,让电脑随机下棋y = rand() % col;if (board[x][y] == ' '){board[x][y] = '#';break;}}}
🌴7.判断胜负
//判断输赢char IsWin(char board[ROW][COL], int row, int col){int i = 0, j = 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 (j = 0; j < col; j++){{if (board[0][j] == board[1][j] && board[1][j] == board[2][j] && board[0][j] != ' ') return board[0][j];}}//左对角线if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != ' '){return board[1][1];}//右对角线if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] != ' ') {return board[1][1];}if (is_full(board,row,col)){return 'Q';}else{return 'C';}}
🌴8.判断棋盘是否为满
//判断棋盘是否已满int is_full(char board[ROW][COL], int row, int col){int flag = 0;int i = 0, j = 0;for (i = 0; i < row; i++){for (j = 0; j < col; j++){if (board[i][j] == ' ') //棋盘未满{return flag;}}}flag = 1;return flag;}
🌴9.游戏逻辑描述
void game(){//实现三子棋过程//定义二维数组 表示棋盘char board[ROW][COL]; //初始化棋盘 未开始下棋时 棋盘为空(board数组元素为空格)InidBoard(board, ROW, COL); //打印棋盘DisplayBoard(board, ROW, COL); //下棋过程char ret = 0; //设置该字符变量用来判断输赢while (1) //电脑和玩家交替下棋{//玩家下棋PlayerMove(board, ROW, COL);DisplayBoard(board, ROW, COL);//每个人下完以后,都要判断一下是否已有人获胜 ret = IsWin(board, ROW, COL);if (ret != 'C'){break;}//电脑下棋ComputerMove(board, ROW, COL);//打印棋盘,获取电脑落子位置 DisplayBoard(board, ROW, COL);ret = IsWin(board, ROW, COL);if(ret != 'C'){break;}}if (ret == '*'){printf("玩家赢\n");}else if (ret == '#'){printf("电脑赢\n");}else if (ret == 'Q'){printf("平局\n");}}
🌟结果演示
🌴1.玩家胜利
🌴2.电脑胜利
🌴3.和棋
🌟完整代码
//三子棋#include#include#include#define ROW 3#define COL 3//初始化棋盘void InidBoard(char board[ROW][COL], int row, int col);//打印棋盘void DisplayBoard(char board[ROW][COL], int row, int col);//玩家下棋void PlayerMove(char board[ROW][COL], int row, int col);//电脑下棋void ComputerMove(char board[ROW][COL], int row, int col);//判断输赢 结束游戏char IsWin(char board[ROW][COL], int row, int col);//该函数要返回四种不同的状态//如果玩家赢 -> return '*'//如果电脑赢 -> return '#'//如果平局 -> return 'Q'//游戏继续 -> return 'C'//判断棋盘是否已满int is_full(char board[ROW][COL], int row, int col);void menu(){printf("**************************************\n");printf("***************三子棋*****************\n");printf("*************1.开始游戏***************\n");printf("*************0.退出游戏***************\n");printf("**************************************\n");}void game(){//实现三子棋过程//定义二维数组 表示棋盘char board[ROW][COL]; //初始化棋盘 未开始下棋时 棋盘为空(board数组元素为空格)InidBoard(board, ROW, COL); //打印棋盘DisplayBoard(board, ROW, COL); //下棋过程char ret = 0; //设置该字符变量用来判断输赢while (1) //电脑和玩家交替下棋{//玩家下棋PlayerMove(board, ROW, COL);DisplayBoard(board, ROW, COL);ret = IsWin(board, ROW, COL);if (ret != 'C'){break;}//电脑下棋ComputerMove(board, ROW, COL);DisplayBoard(board, ROW, COL);ret = IsWin(board, ROW, COL);if(ret != 'C'){break;}}if (ret == '*'){printf("玩家赢\n");}else if (ret == '#'){printf("电脑赢\n");}else if (ret == 'Q'){printf("平局\n");}}int main(){int input = 0;srand((unsigned int)time(NULL)); //让电脑随机下棋do{menu();printf("请输入你的选择->");scanf("%d", &input);switch (input){case 1:game();break;case 0:printf("游戏已退出\n");break;default:printf("输入错误 请重新输入\n");break;}} while (input);return 0;} //初始化棋盘void InidBoard(char board[ROW][COL], int row, int col){int i = 0, j = 0;//初始化元素为空格for (i = 0; i < row; i++){for (j = 0; j < col; j++){board[i][j] = ' ';}}}//打印棋盘void DisplayBoard(char board[ROW][COL], int row, int col){int j = 0;for (j = 0; j < row; ++j){int i = 0;for (i = 0; i < col; ++i){printf(" %c ", board[j][i]);if (i < col - 1){printf("|"); //打印垂直分割线}}printf("\n"); //换行//打印水平分割线if (j < row - 1){for (i = 0; i < col; ++i){printf("---");if (i < col - 1){printf("|");}}printf("\n"); //换行}}}//玩家下棋void PlayerMove(char board[ROW][COL], int row, int col){printf("玩家下棋:\n");int x = 0; //行int y = 0; //列while (1){printf("请输入你要落棋子的坐标:");scanf("%d %d", &x, &y); //在此处坐标,实际输入坐标为[1][1] -> 数组中位置为[0][0]if (x >= 1 && x <= row && y >= 1 && y <= col){if (board[x - 1][y - 1] == ' '){board[x - 1][y - 1] = '*';break;}else{printf("坐标被占用,请重新输入\n");}}else{printf("坐标非法,请重新输入\n");}}}//电脑下棋void ComputerMove(char board[ROW][COL], int row, int col){int x = 0; //行int y = 0; //列printf("电脑下棋:\n");while (1){x = rand() % row; //生成随机数0、1、2,让电脑随机下棋y = rand() % col;if (board[x][y] == ' '){board[x][y] = '#';break;}}}//判断输赢char IsWin(char board[ROW][COL], int row, int col){int i = 0, j = 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 (j = 0; j < col; j++){{if (board[0][j] == board[1][j] && board[1][j] == board[2][j] && board[0][j] != ' ') return board[0][j];}}//左对角线if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != ' '){return board[1][1];}//右对角线if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] != ' ') {return board[1][1];}if (is_full(board,row,col)){return 'Q';}else{return 'C';}}//判断棋盘是否已满int is_full(char board[ROW][COL], int row, int col){int flag = 0;int i = 0, j = 0;for (i = 0; i < row; i++){for (j = 0; j < col; j++){if (board[i][j] == ' ') //棋盘未满{return flag;}}}flag = 1;return flag;}