• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

C语言代码实现简易三子棋游戏

c语言 搞代码 4年前 (2022-01-06) 39次浏览 已收录 0个评论

这篇文章主要为大家详细介绍了C语言代码实现简易三子棋游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了C语言代码实现简易三子棋游戏的具体代码,供大家参考,具体内容如下

1. 三子棋游戏规则

是黑白棋的一种。三子棋是一种民间传统游戏,又叫九宫棋、圈圈叉叉、一条龙、井字棋等。将正方形对角线连起来,相对两边依次摆上三个双方棋子,只要将自己的三个棋子走成一条线,对方就算输了。但是,有很多时候会出现和棋的情况。

2.设计思路

游戏流程:

1.创建棋盘,并且初始化,将所有位置设置为空格。
2.打印棋盘
3.玩家通过输入坐标(row,col)进行落子
4.判定胜负
5.电脑随机落子
6.判断胜负

具体实现:

1.用一个3*3的二维数组来表示棋盘,数组的每个元素是char类型
2.”x”表示玩家1,”o”表示玩家2,空格即为空白。
3.rand&srand控制电脑随机落子

3.代码详解

(1).3为魔幻数字,为了避免混乱则使用宏定义

 #define MAX_ROW 3 #define MAX_COL  3 char chessBoard[3]3]; char chessBoard[MAX_ROW][MAX_COL];

不建议定义为全局变量

(2).初始化函数 将棋盘的每一个位置都初始化为空格

 void init(char chess[MAX_ROW][MAX_COL]) { for (int row = 0; row <MAX_ROW; row++) { for (int col = 0; col <MAX_COL; col++) { chess[row][col] = ' '; } } }

(3).打印棋盘

 void print(char chess[MAX_ROW][MAX_COL]) { printf("+---+---+---+\n"); for (int row = 0; row <MAX_ROW; row++) { printf("|"); for (int col = 0; col <MAX_COL; col++) { printf(" %c |", chess[row][col]); } printf("\n+---+---+---+\n"); } }

通过不断调整符号来确定棋盘

(4).玩家落子

要判断输入数值是否合法,不可以下标越界,如果不合法要重新输入。
以及输入位置是否有子,如果有子要提醒用户重新输入。

 void playerMove(char chessBoard[MAX_ROW][MAX_COL]) { printf("玩家落子....\n"); while (1) { printf("请输入坐标(row col): "); int row = 0; int col = 0; scanf("%d %d", &row, &col); if (row = MAX_ROW || col = MAX_COL) { printf("输入坐标非法! 请重新输入!\n"); continue; } if (chessBoard[row][col] != ' ') { printf("已经有子, 请重新输入!\n"); continue; } chessBoard[row][col] = 'x'; break; } }

(5)判断胜负

如果返回 x, 表示 玩家获胜;返回 o, 表示 电脑获胜;返回 ‘ ‘, 表示胜负未分
;返回 q, 表示和棋。

 char isGameOver(char chessBoard[MAX_ROW][MAX_COL]) { // 所有的行, 所有的列, 以及对角线 for (int row = 0; row <MAX_ROW; row++) { if (chessBoard[row][0] != ' ' && chessBoard[row][0] == chessBoard[row][1] && chessBoard[row][0] == chessBoard[row][2]) { return chessBoard[row][0]; } } for (int col = 0; col <MAX_COL; col++) { if (chessBoard[0][col] != ' ' && chessBoard[0][col] == chessBoard[1][col] && chessBoard[0][col] == chessBoard[2][col]) { return chessBoard[0][col]; } } if (chessBoard[0][0] != ' ' && chessBoard[0][0] == chessBoard[1][1] && chessBoard[0][0] == chessBoard[2][2]) { return chessBoard[0][0]; } if (chessBoard[0][2] != ' ' && chessBoard[0][2] == chessBoard[1][1] && chessBoard[0][2] == chessBoard[2][0]) { return chessBoard[0][2]; } // 判定是否和棋 //棋盘是否满了 if (isFull(chessBoard)) { return 'q'; } // 胜负未分 return ' '; }

(6)电脑落子

需要判断落子位置之前是否有子

 void computerMove(char chessBoard[MAX_ROW][MAX_COL]) { while (1) { int row = rand() % MAX_ROW; int col = rand() % MAX_COL; if (chessBoard[row][col] != ' ') { continue; } chessBoard[row][col] = 'o'; break; } }

(7)判断棋盘函数

1满 0不满

 int isFull(char chessBoard[MAX_ROW][MAX_COL]) { // 遍历棋盘, 判断是否有空格 for (int row = 0; row <MAX_ROW; row++) { for (int col = 0; col <MAX_COL; col++) { if (chessBoard[row][col] == ' ') { return 0; } } } return 1; }

4.完整代码

 include  #include  #define MAX_ROW 3 #define MAX_COL 3 void init(char chess[MAX_ROW][MAX_COL]) { for (int row = 0; row <MAX_ROW; row++) { for (int col = 0; col <MAX_COL; col++) { chess[row][col] = ' '; } } } void print(char chess[MAX_ROW][MAX_COL]) { printf("+---+---+---+\n"); for (int row = 0; row <MAX_ROW; row++) { printf("|"); for (int col = 0; col <MAX_COL; col++) { printf(" %c |", chess[row][col]); } printf("\n+---+---+---+\n"); } } void playerMove(char chessBoard[MAX_ROW][MAX_COL]) { printf("玩家落子....\n"); while (1)<a style="color:transparent">来源gao*daima.com搞@代#码网</a> { printf("请输入坐标(row col): "); int row = 0; int col = 0; scanf("%d %d", &row, &col); if (row = MAX_ROW || col = MAX_COL) { printf("您输入的坐标非法! 请重新输入!\n"); continue; } if (chessBoard[row][col] != ' ') { printf("您输入的位置已经有子了, 请重新输入!\n"); continue; } chessBoard[row][col] = 'x'; break; } } void computerMove(char chessBoard[MAX_ROW][MAX_COL]) { while (1) { int row = rand() % MAX_ROW; int col = rand() % MAX_COL; if (chessBoard[row][col] != ' ') { continue; } chessBoard[row][col] = 'o'; break; } } // 返回 1 满, 0不满 int isFull(char chessBoard[MAX_ROW][MAX_COL]) { for (int row = 0; row <MAX_ROW; row++) { for (int col = 0; col <MAX_COL; col++) { if (chessBoard[row][col] == ' ') { return 0; } } } return 1; } char isGameOver(char chessBoard[MAX_ROW][MAX_COL]) { for (int row = 0; row <MAX_ROW; row++) { if (chessBoard[row][0] != ' ' && chessBoard[row][0] == chessBoard[row][1] && chessBoard[row][0] == chessBoard[row][2]) { return chessBoard[row][0]; } } for (int col = 0; col <MAX_COL; col++) { if (chessBoard[0][col] != ' ' && chessBoard[0][col] == chessBoard[1][col] && chessBoard[0][col] == chessBoard[2][col]) { return chessBoard[0][col]; } } if (chessBoard[0][0] != ' ' && chessBoard[0][0] == chessBoard[1][1] && chessBoard[0][0] == chessBoard[2][2]) { return chessBoard[0][0]; } if (chessBoard[0][2] != ' ' && chessBoard[0][2] == chessBoard[1][1] && chessBoard[0][2] == chessBoard[2][0]) { return chessBoard[0][2]; } if (isFull(chessBoard)) { return 'q'; } return ' '; } int main() { char chessBoard[MAX_ROW][MAX_COL]; init(chessBoard); char winner = ' '; while (1) { print(chessBoard); playerMove(chessBoard); winner = isGameOver(chessBoard); if (winner != ' ') { break; } computerMove(chessBoard); winner = isGameOver(chessBoard); if (winner != ' ') { break; } } if (winner == 'x') { printf("赢!\n"); } else if (winner == 'o') { printf("蠢!\n"); } else { printf("五五开!\n"); } system("pause"); return 0; }

以上就是C语言代码实现简易三子棋游戏的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:C语言代码实现简易三子棋游戏

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址