C语言如何开发实现井字棋

C语言如何开发实现井字棋

这篇文章主要为大家展示了“C语言如何开发实现井字棋”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“C语言如何开发实现井字棋”这篇文章吧。

    总体思路

    井字棋棋盘我们总体可以当成一个二维数组来操作,我们分别需要实现初始化二维数组,打印棋盘,玩家下棋,电脑下棋,判断输赢等代码

    项目的创建

    我们创建了头文件用于放函数的声明,game.c文件放置函数的实现,test.c文件用于测试。

    测试结果

    电脑获胜:

    玩家获胜:

    平局:

    各函数代码的实现

    我们在game.c文件中实现函数功能的代码

    初始化二维数组

    voidInitBoard(charBoard[ROW][COL],introw,intcol){for(inti=0;i<row;i++){for(intj=0;j<col;j++){Board[i][j]='';}}}

    打印棋盘

    voidDisplayBoard(charBoard[ROW][COL],introw,intcol){for(inti=0;i<row;i++){for(intj=0;j<col;j++){printf("%c",Board[i][j]);if(j<col-1)printf("|");}printf("\n");if(i<row-1){for(intj=0;j<col;j++){printf("---");if(j<col-1)printf("|");}}printf("\n");}}

    这里的棋盘我们只打印了井字的形状,如果为了美观,还可以进行封边,这里就留给朋友们自行实现啦。

    玩家下棋

    voidPlayMove(charBoard[ROW][COL],introw,intcol){while(1){intx=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;}elseprintf("该位置已被占,请重新输入:\n");}else{printf("输入错误,请重新输入:\n");}}}

    电脑下棋

    在电脑落子之前,先检测有没有下一步可以直接赢的,若有,则落子在此处,电脑获胜,若没有,再检测玩家下一步是否有机会获胜,若玩家下一步可以直接获胜,那么电脑将抢占这一步。

    如果上述两种情况均不存在,当发现(1,1)位置是空时,电脑先下这一步。

    其他情况均随意落子。

    voidComputerMove(charBoard[ROW][COL],introw,intcol){intx=0,y=0;while(1){if(JudgeComputer(Board,ROW,COL)){for(inti=0;i<row;i++){if(Board[i][0]==Board[i][1]&&Board[i][0]=='*'&&Board[i][2]==''){Board[i][2]='*';return;}elseif(Board[i][0]==Board[i][2]&&Board[i][0]=='*'&&Board[i][1]==''){Board[i][1]='*';return;}elseif(Board[i][1]==Board[i][2]&&Board[i][1]=='*'&&Board[i][0]==''){Board[i][0]='*';return;}}for(intj=0;j<col;j++){if(Board[0][j]==Board[1][j]&&Board[0][j]=='*'&&Board[2][j]==''){Board[2][j]='*';return;}elseif(Board[0][j]==Board[2][j]&&Board[0][j]=='*'&&Board[1][j]==''){Board[1][j]='*';return;}elseif(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;}elseif(Board[0][0]==Board[2][2]&&Board[0][0]=='*'&&Board[1][1]==''){Board[1][1]='*';return;}elseif(Board[1][1]==Board[2][2]&&Board[1][1]=='*'&&Board[0][0]==''){Board[0][0]='*';return;}elseif(Board[0][2]==Board[1][1]&&Board[0][2]=='*'&&Board[2][0]==''){Board[2][0]='*';return;}elseif(Board[0][2]==Board[2][0]&&Board[0][2]=='*'&&Board[1][1]==''){Board[1][1]='*';return;}elseif(Board[2][0]==Board[1][1]&&Board[2][0]=='*'&&Board[0][2]==''){Board[0][2]='*';return;}}elseif(JudgePlayer(Board,ROW,COL)){for(inti=0;i<row;i++){if(Board[i][0]==Board[i][1]&&Board[i][0]=='#'&&Board[i][2]==''){Board[i][2]='*';return;}elseif(Board[i][0]==Board[i][2]&&Board[i][0]=='#'&&Board[i][1]==''){Board[i][1]='*';return;}elseif(Board[i][1]==Board[i][2]&&Board[i][1]=='#'&&Board[i][0]==''){Board[i][0]='*';return;}}for(intj=0;j<col;j++){if(Board[0][j]==Board[1][j]&&Board[0][j]=='#'&&Board[2][j]==''){Board[2][j]='*';return;}elseif(Board[0][j]==Board[2][j]&&Board[0][j]=='#'&&Board[1][j]==''){Board[1][j]='*';return;}elseif(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;}elseif(Board[0][0]==Board[2][2]&&Board[0][0]=='#'&&Board[1][1]==''){Board[1][1]='*';return;}elseif(Board[1][1]==Board[2][2]&&Board[1][1]=='#'&&Board[0][0]==''){Board[0][0]='*';return;}elseif(Board[0][2]==Board[1][1]&&Board[0][2]=='#'&&Board[2][0]==''){Board[2][0]='*';return;}elseif(Board[0][2]==Board[2][0]&&Board[0][2]=='#'&&Board[1][1]==''){Board[1][1]='*';return;}elseif(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=rand()%row;y=rand()%col;if(Board[x][y]==''){Board[x][y]='*';break;}}}}}

    判断电脑是否有位置可以获胜

    staticintJudgeComputer(charBoard[ROW][COL],introw,intcol){for(inti=0;i<row;i++){if(Board[i][0]==Board[i][1]&&Board[i][0]=='*'&&Board[i][2]=='')return1;elseif(Board[i][0]==Board[i][2]&&Board[i][0]=='*'&&Board[i][1]=='')return1;elseif(Board[i][1]==Board[i][2]&&Board[i][1]=='*'&&Board[i][0]=='')return1;}for(intj=0;j<col;j++){if(Board[0][j]==Board[1][j]&&Board[0][j]=='*'&&Board[2][j]=='')return1;elseif(Board[0][j]==Board[2][j]&&Board[0][j]=='*'&&Board[1][j]=='')return1;elseif(Board[1][j]==Board[2][j]&&Board[1][j]=='*'&&Board[0][j]=='')return1;}if(Board[0][0]==Board[1][1]&&Board[0][0]=='*'&&Board[2][2]=='')return1;elseif(Board[0][0]==Board[2][2]&&Board[0][0]=='*'&&Board[1][1]=='')return1;elseif(Board[1][1]==Board[2][2]&&Board[1][1]=='*'&&Board[0][0]=='')return1;elseif(Board[0][2]==Board[1][1]&&Board[0][2]=='*'&&Board[2][0]=='')return1;elseif(Board[0][2]==Board[2][0]&&Board[0][2]=='*'&&Board[1][1]=='')return1;elseif(Board[2][0]==Board[1][1]&&Board[2][0]=='*'&&Board[0][2]=='')return1;elsereturn0;}

    判断玩家是否有位置获胜

    staticintJudgePlayer(charBoard[ROW][COL],introw,intcol){for(inti=0;i<row;i++){if(Board[i][0]==Board[i][1]&&Board[i][0]=='#'&&Board[i][2]=='')return1;elseif(Board[i][0]==Board[i][2]&&Board[i][0]=='#'&&Board[i][1]=='')return1;elseif(Board[i][1]==Board[i][2]&&Board[i][1]=='#'&&Board[i][0]=='')return1;}for(intj=0;j<col;j++){if(Board[0][j]==Board[1][j]&&Board[0][j]=='#'&&Board[2][j]=='')return1;elseif(Board[0][j]==Board[2][j]&&Board[0][j]=='#'&&Board[1][j]=='')return1;elseif(Board[1][j]==Board[2][j]&&Board[1][j]=='#'&&Board[0][j]=='')return1;}if(Board[0][0]==Board[1][1]&&Board[0][0]=='#'&&Board[2][2]=='')return1;elseif(Board[0][0]==Board[2][2]&&Board[0][0]=='#'&&Board[1][1]=='')return1;elseif(Board[1][1]==Board[2][2]&&Board[1][1]=='#'&&Board[0][0]=='')return1;elseif(Board[0][2]==Board[1][1]&&Board[0][2]=='#'&&Board[2][0]=='')return1;elseif(Board[0][2]==Board[2][0]&&Board[0][2]=='#'&&Board[1][1]=='')return1;elseif(Board[2][0]==Board[1][1]&&Board[2][0]=='#'&&Board[0][2]=='')return1;elsereturn0;}

    判断输赢

    charIsWin(charBoard[ROW][COL],introw,intcol)for(inti=0;i<row;i++){if(Board[i][0]==Board[i][1]&&Board[i][2]==Board[i][1]&&Board[i][1]!=''){returnBoard[i][0];}}for(intj=0;j<col;j++){if(Board[0][j]==Board[1][j]&&Board[1][j]==Board[2][j]&&Board[j][1]!=''){returnBoard[0][j];}}if(Board[0][0]==Board[1][1]&&Board[2][2]==Board[1][1]&&Board[2][2]!='')returnBoard[0][0];if(Board[0][2]==Board[1][1]&&Board[2][0]==Board[1][1]&&Board[1][1]!='')returnBoard[1][1];if(IsFull(Board,ROW,COL)){return'o';}return'C';}

    判断和棋

    staticintIsFull(charBoard[ROW][COL],introw,intcol){for(inti=0;i<row;i++){for(intj=0;j<col;j++){if(Board[i][j]=='')return0;}}return1;}

    声明代码

    我们在game.h头文件中进行函数的声明。

    voidmeun();#defineROW3#defineCOL3//游戏主函数voidgame();//初始化二维数组voidInitBoard(charBoard[ROW][COL],introw,intcol);//打印棋盘voidDisplayBoard(charBoard[ROW][COL],introw,intcol);//玩家下棋voidPlayMove(charBoard[ROW][COL],introw,intcol);//电脑下棋voidComputerMove(charBoard[ROW][COL],introw,intcol);//判断赢否charIsWin(charBoard[ROW][COL],introw,intcol);//判断棋盘满了吗//intIsFull(charBoard[ROW][COL],introw,intcol);//判断电脑下一部是否可以直接赢//intJudgeComputer(charBoard[ROW][COL],introw,intcol);//判断玩家下一步是否会赢//intJudgePlayer(charBoard[ROW][COL],introw,intcol);

    测试代码

    我们在test.c文件中测试该游戏。

    #include"game.h"voidgame(){charret=0;charBoard[ROW][COL]={0};//初始化数组InitBoard(Board,ROW,COL);//打印棋盘printf("棋盘如下:\n");DisplayBoard(Board,ROW,COL);while(1){//玩家下棋printf("玩家落子:\n");PlayMove(Board,ROW,COL);//每下一步打印一次棋盘DisplayBoard(Board,ROW,COL);//每下一步判断一次胜负ret=IsWin(Board,ROW,COL);if(ret!='C'){break;}//电脑下棋printf("电脑落子:\n");ComputerMove(Board,ROW,COL);//每下一步打印一次棋盘DisplayBoard(Board,ROW,COL);ret=IsWin(Board,ROW,COL);if(ret!='C'){break;}}if(ret=='#')printf("玩家获胜\n");elseif(ret=='*')printf("电脑获胜\n");elseif(ret=='o')printf("卧龙凤雏\n");}voidtest(){intinput=0;srand((size_t)time(NULL));do{meun();printf("请选择:>");scanf("%d",&input);switch(input){case1:game();break;case0:printf("游戏结束\n");break;default:printf("输入错误,请重新输入:\n");break;}}while(input);}intmain(){test();return0;}

    以上是“C语言如何开发实现井字棋”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注恰卡编程网行业资讯频道!

    发布于 2022-03-03 21:22:37
    收藏
    分享
    海报
    0 条评论
    34
    上一篇:Python PyQt5如何实现文件拷贝器 下一篇:C语言中vector底层实现机制的示例分析
    目录

      0 条评论

      本站已关闭游客评论,请登录或者注册后再评论吧~

      忘记密码?

      图形验证码