摘要:如果上述两种情况均不存在,当发现,位置是空时,电脑先下这一步。其他情况均随意落子。
朋友们,我们还记得以前上课经常和同桌玩起井字棋,那么我们就当我们回忆童年,现在也用C语言来实现井字棋,本次代码相对于初阶的井字棋,在电脑下棋代码部分做了优化,使得电脑更加具有威胁,但是由于博主水平有限,代码不够精简优化,或许朋友们有更好的算法,还希望能够与博主分享
井字棋棋盘我们总体可以当成一个二维数组来操作,我们分别需要实现初始化二维数组,打印棋盘,玩家下棋,电脑下棋,判断输赢等代码
我们创建了头文件用于放函数的声明,game.c文件放置函数的实现,test.c文件用于测试。
电脑获胜:
玩家获胜:
平局:
我们在game.c文件中实现函数功能的代码
void InitBoard(char Board[ROW][COL], int row, int col){ for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { Board[i][j] = " "; } }}
void DisplayBoard(char Board[ROW][COL], int row, int col){ for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { printf(" %c ", Board[i][j]); if (j < col - 1) printf("|"); } printf("/n"); if (i < row - 1) { for (int j = 0; j < col; j++) { printf("---"); if (j < col - 1) printf("|"); } } printf("/n"); }}
这里的棋盘我们只打印了井字的形状,如果为了美观,还可以进行封边,这里就留给朋友们自行实现啦。
void PlayMove(char Board[ROW][COL], int row, int col){ while (1) { int x = 0, y = 0; 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("该位置已被占,请重新输入:/n"); } else { printf("输入错误,请重新输入:/n"); } }}
在电脑落子之前,先检测有没有下一步可以直接赢的,若有,则落子在此处,电脑获胜,若没有,再检测玩家下一步是否有机会获胜,若玩家下一步可以直接获胜,那么电脑将抢占这一步。如果上述两种情况均不存在,当发现(1,1)位置是空时,电脑先下这一步。其他情况均随意落子。
void ComputerMove(char Board[ROW][COL], int row, int col){ int x = 0, y = 0; while (1) { if (JudgeComputer(Board, ROW, COL)) { for (int i = 0; i < row; i++) { if (Board[i][0] == Board[i][1] && Board[i][0] == "*" && Board[i][2] == " ") { Board[i][2] = "*"; return; } else if (Board[i][0] == Board[i][2] && Board[i][0] == "*" && Board[i][1] == " ") { Board[i][1] = "*"; return; } else if (Board[i][1] == Board[i][2] && Board[i][1] == "*" && Board[i][0] == " ") { Board[i][0] = "*"; return; } } for (int j = 0; j < col; j++) { if (Board[0][j] == Board[1][j] && Board[0][j] == "*" && Board[2][j] == " ") { Board[2][j] = "*"; return; } else if (Board[0][j] == Board[2][j] && Board[0][j] == "*" && Board[1][j] == " ") { Board[1][j] = "*"; return; } else if (Board[1][j] == Board[2][j] && Board[1][j] == "*" && Board[0][j] == " ") { Board[0][j] = "*"; return; } } if (Board[0][0] == Board[1][1] && Board[0][0] == "*" && Board[2][2] == " ") { Board[2][2] = "*"; return; } else if (Board[0][0] == Board[2][2] && Board[0][0] == "*" && Board[1][1] == " ") { Board[1][1] = "*"; return; } else if (Board[1][1] == Board[2][2] && Board[1][1] == "*" && Board[0][0] == " ") { Board[0][0] = "*"; return; } else if (Board[0][2] == Board[1][1] && Board[0][2] == "*" && Board[2][0] == " ") { Board[2][0] = "*"; return; } else if (Board[0][2] == Board[2][0] && Board[0][2] == "*" && Board[1][1] == " ") { Board[1][1] = "*"; return; } else if (Board[2][0] == Board[1][1] && Board[2][0] == "*" && Board[0][2] == " ") { Board[0][2] = "*"; return; } } else if (JudgePlayer(Board, ROW, COL)) { for (int i = 0; i < row; i++) { if (Board[i][0] == Board[i][1] && Board[i][0] == "#" && Board[i][2] == " ") { Board[i][2] = "*"; return; } else if (Board[i][0] == Board[i][2] && Board[i][0] == "#" && Board[i][1] == " ") { Board[i][1] = "*"; return; } else if (Board[i][1] == Board[i][2] && Board[i][1] == "#" && Board[i][0] == " ") { Board[i][0] = "*"; return; } } for (int j = 0; j < col; j++) { if (Board[0][j] == Board[1][j] && Board[0][j] == "#" && Board[2][j] == " ") { Board[2][j] = "*"; return; } else if (Board[0][j] == Board[2][j] && Board[0][j] == "#" && Board[1][j] == " ") { Board[1][j] = "*"; return; } else if (Board[1][j] == Board[2][j] && Board[1][j] == "#" && Board[0][j] == " ") { Board[0][j] = "*"; return; } } if (Board[0][0] == Board[1][1] && Board[0][0] == "#" && Board[2][2] == " ") { Board[2][2] = "*"; return; } else if (Board[0][0] == Board[2][2] && Board[0][0] == "#" && Board[1][1] == " ") { Board[1][1] = "*"; return; } else if (Board[1][1] == Board[2][2] && Board[1][1] == "#" && Board[0][0] == " ") { Board[0][0] = "*"; return; } else if (Board[0][2] == Board[1][1] && Board[0][2] == "#" && Board[2][0] == " ") { Board[2][0] = "*"; return; } else if (Board[0][2] == Board[2][0] && Board[0][2] == "#" && Board[1][1] == " ") { Board[1][1] = "*"; return; } else if (Board[2][0] == Board[1][1] && Board[2][0] == "#" && Board[0][2] == " ") { Board[0][2] = "*"; return; } } else { if (Board[1][1] == " ") { Board[1][1] = "*"; return; } else { x
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/123066.html
摘要:三子棋三子棋游戏介绍规则游戏设计思路代码分析文件代码分析。玩家下棋实现。三子棋是一种民间传统游戏,又叫九宫棋圈圈叉叉一条龙井字棋等。落子后打印棋盘以显示。假设平局时判定输赢的函数返回后面代码实现时详细讲。 ...
摘要:三子棋目录一问题介绍三子棋,在民间又叫井字棋。因为人们在游玩时常常不画棋盘的边框,正如汉字中的井字,多称为井字棋。 三子棋 目录 一、问题介绍 三子棋,在民间又叫井字棋。因为人们在游玩时常常不画棋盘的边框,正如汉字中的井字,多称为井字棋。 三子棋的游戏规则十分的简单: ...
目前为止学了分支,循环,数组和函数,现在利用这些知识来写一个三子棋小游戏来巩固加深所学哈哈哈 目录 1、测试结果 2、 整体思路 3、代码模块解读 (1)、打印菜单 (2)、初始化棋盘 (3)、打印棋盘 (4)、玩家下棋 (5)、电脑下棋 (6)、判断输赢 4、代码整体实现 game.h game.c test.c 1、测试结果 这就是游戏的整个过程,接下来我将慢慢讲解整个工程是如何实现的...
摘要:文章目录前言正文一,游戏实现基本流程二游戏实现步骤创建工程及分配功能给头文件上内容当当当,主函数出场游戏函数里面打印游戏菜单棋盘初始化打印棋盘玩家下棋电脑下棋判断结果三游戏结果演示恭喜友友获胜啦电脑获胜,很遗憾,再接再厉 ...
摘要:说起井字游戏,真是满满的童年味道,还记得最疯狂的时候是小时候跟同桌拿着一张草稿纸就能玩一节课,回到家跟弟弟也能继续玩,对于没有太多娱乐节目的童年来说,真是一款玩不厌的小游戏。 99% of information we read, we forget anyway. The best way to remember is to DO. 体验地址:http://www.hoohack.m...
阅读 3875·2021-11-16 11:50
阅读 892·2021-11-11 16:55
阅读 3634·2021-10-26 09:51
阅读 818·2021-09-22 15:03
阅读 3352·2019-08-30 15:54
阅读 3243·2019-08-30 15:54
阅读 2449·2019-08-30 14:04
阅读 899·2019-08-30 13:53