找回密码
 会员注册
查看: 16|回复: 0

三子棋、井字棋-python版本代码-C语言版本代码

[复制链接]

2万

主题

0

回帖

6万

积分

超级版主

积分
69864
发表于 2024-9-10 17:16:26 | 显示全部楼层 |阅读模式
三子棋是一种民间传统游戏,又叫九宫棋、圈圈叉叉棋、一条龙、井字棋等。游戏分为双方对战,双方依次在9宫格棋盘上摆放棋子,率先将自己的三个棋子走成一条线就视为胜利,而对方就算输了,但是三子棋在很多时候会出现和棋的局面。废话不多说,直接上代码,好用请给个三连关注谢谢,我的B站是技术宅学长的个人空间-技术宅学长个人主页-哔哩哔哩视频(bilibili.com)#作者:技术宅学长(CSDN、B站、抖音均为该ID)#电赛交流1群:586475593#电赛交流2群:878095340#电赛交流3群:873779462#电赛交流4群:208635284#电赛交流5群:903429622importrandom#定义棋盘大小和玩家符号ROWS,COLS=3,3PLAYER_X='X'#玩家PLAYER_O='O'#电脑AIEMPTY=''#函数:打印棋盘defprint_board(board):forrowinboard:print("|".join(row))print("-"*9)#函数:检查当前状态是否是结束状态(胜负或平局)defcheck_game_over(board):#检查行和列foriinrange(ROWS):ifboard[i][0]==board[i][1]==board[i][2]!=EMPTY:#检查行returnboard[i][0]ifboard[0][i]==board[1][i]==board[2][i]!=EMPTY:#检查列returnboard[0][i]#检查对角线ifboard[0][0]==board[1][1]==board[2][2]!=EMPTY:#主对角线returnboard[0][0]ifboard[0][2]==board[1][1]==board[2][0]!=EMPTY:#副对角线returnboard[0][2]#检查是否平局ifall(board[i][j]!=EMPTYforiinrange(ROWS)forjinrange(COLS)):return"Draw"#游戏未结束returnNone#函数:实现Minimax算法来找到最优的下棋位置defminimax(board,depth,is_maximizing):result=check_game_over(board)ifresult==PLAYER_X:return-1#X赢了elifresult==PLAYER_O:return1#O赢了elifresult=="Draw":return0#平局ifis_maximizing:best_score=-float('inf')foriinrange(ROWS):forjinrange(COLS):ifboard[i][j]==EMPTY:board[i][j]=PLAYER_Oscore=minimax(board,depth+1,False)board[i][j]=EMPTYbest_score=max(score,best_score)returnbest_scoreelse:best_score=float('inf')foriinrange(ROWS):forjinrange(COLS):ifboard[i][j]==EMPTY:board[i][j]=PLAYER_Xscore=minimax(board,depth+1,True)board[i][j]=EMPTYbest_score=min(score,best_score)returnbest_score#函数:让电脑选择最优的下棋位置defcomputer_move(board):best_move=Nonebest_score=-float('inf')foriinrange(ROWS):forjinrange(COLS):ifboard[i][j]==EMPTY:board[i][j]=PLAYER_Oscore=minimax(board,0,False)board[i][j]=EMPTYifscore>best_score:best_score=scorebest_move=(i,j)board[best_move[0]][best_move[1]]=PLAYER_O#主函数:控制游戏流程defmain():board=[[EMPTYfor_inrange(COLS)]for_inrange(ROWS)]current_player=PLAYER_O#设置先行玩家PLAYER_X为人,PLAYER_O为AIprint("欢迎使用三子棋程序!")print_board(board)whileTrue:ifcurrent_player==PLAYER_X:row=int(input("请输入你要下的行(0,1,2):"))col=int(input("请输入你要下的列(0,1,2):"))ifboard[row][col]==EMPTY:board[row][col]=PLAYER_Xcurrent_player=PLAYER_Oelse:print("移动错误,当前位置有棋,请重下")continueelse:print("AI走棋")computer_move(board)current_player=PLAYER_Xprint_board(board)game_result=check_game_over(board)ifgame_result:ifgame_result=="Draw":print("平局!")else:ifgame_result=='O':print(f"AI获胜!")else:print(f"恭喜你获胜!")breakif__name__=="__main__":main()#include#include#include#defineROWS3#defineCOLS3#definePLAYER_X'X'#definePLAYER_O'O'#defineEMPTY''//打印棋盘voidprint_board(charboard[ROWS][COLS]){for(inti=0;ibest_score?score:best_score;}}}returnbest_score;}else{intbest_score=1000;//初始最大值for(inti=0;ibest_score){best_score=score;best_move_row=i;best_move_col=j;}}}}board[best_move_row][best_move_col]=PLAYER_O;}//主函数:控制游戏流程intmain(){charboard[ROWS][COLS];charcurrent_player=PLAYER_X;//先落子玩家,PLAYER_X为玩家,PLAYER_O为电脑//初始化棋盘for(inti=0;i
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 会员注册

本版积分规则

QQ|手机版|心飞设计-版权所有:微度网络信息技术服务中心 ( 鲁ICP备17032091号-12 )|网站地图

GMT+8, 2025-1-7 06:45 , Processed in 0.419397 second(s), 26 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表