1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > python写井字棋_python 游戏(井字棋)

python写井字棋_python 游戏(井字棋)

时间:2021-10-16 06:27:26

相关推荐

python写井字棋_python 游戏(井字棋)

1. 游戏思路和流程图

实现功能,现实生活中的井字棋玩法

游戏流程图SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。

2. 使用模块和游戏提示

import random

def game_info():

print('欢迎来到井字棋游戏')

print('输入数字1~9进行下棋')

3. 棋盘显示

def display_board(checkerboard):

'''棋盘显示'''

print(' '.join(checkerboard[6:9]))

print(' '.join(checkerboard[3:6]))

print(' '.join(checkerboard[:3]))

4. 用户下棋输入限制

def chess_limited(number,checkerboard):

'''用户落棋限制

:param number 用户输入

:param checkerboard 棋盘列表

:return 返回验证过的用户输入'''

while True:

if not number.isdigit():

print('请输入整数数字')

elif number not in '1 2 3 4 5 6 7 8 9'.split():

print('请输入1~9之间的数字')

elif checkerboard[int(number)-1]!='_':

print('棋盘上已经有棋子了')

else:

break

number = input()

return int(number)

5. 双选验证

def Double_choice(a,b,hint):

'''双选择验证函数

:param a 第一个选项值

:param b 第二个选项值

:param hint 选项信息

:return 返回输入值'''

choice=''

while choice.lower() !=a and choice.lower() !=b:

print(hint)

choice=input()

return choice

6. 获胜验证

def referee(choice,checkerboard):

'''判断谁胜利了还是平局'''

if checkerboard[0] == choice and checkerboard[1] == choice and checkerboard[2] == choice:

return True #123

if checkerboard[3] == choice and checkerboard[4] == choice and checkerboard[5] == choice:

return True #456

if checkerboard[6] == choice and checkerboard[7] == choice and checkerboard[8] == choice:

return True #789

if checkerboard[0] == choice and checkerboard[3] == choice and checkerboard[6] == choice:

return True #147

if checkerboard[1] == choice and checkerboard[4] == choice and checkerboard[7] == choice:

return True #258

if checkerboard[2] == choice and checkerboard[5] == choice and checkerboard[8] == choice:

return True #369

if checkerboard[2] == choice and checkerboard[4] == choice and checkerboard[6] == choice:

return True #357

if checkerboard[0] == choice and checkerboard[4] == choice and checkerboard[8] == choice:

return True #159

return False

6. 用户落子处理

def user_play(user_choice,checkerboard):

'''用户选择落子点

:param user_choice 用户所选棋子

:param checkerboard 棋盘列表

:param 返回用户落子完后的棋盘列表'''

print('请你选择落子点')

number=chess_limited(input(),checkerboard)

checkerboard[number-1]=user_choice

return checkerboard

7. 电脑落子处理

def compute_play(compute_choice,checkerboard):

'''电脑选择落子点(电脑ai)

:param compute_choice 电脑所选的棋子

:param checkerboard 棋盘

:return 电脑下过的棋盘'''

copy_win=compute_win(compute_choice,checkerboard)

if copy_win: #电脑获胜

checkerboard=copy_win

return checkerboard

user_win=prevent_user_win(compute_choice, checkerboard)

if user_win: #阻止用户获胜

checkerboard = user_win

return checkerboard

angle=choice_random(compute_choice,checkerboard,[0,2,6,8])

if angle: #角落点

checkerboard =angle

return checkerboard

center=choice_random(compute_choice,checkerboard,[4])

if center: #中心落点

checkerboard = center

return checkerboard

side = choice_random(compute_choice, checkerboard, [1, 3, 5, 7])

if side: #边落点

checkerboard =side

return checkerboard

return checkerboard

8. 电脑获胜

def compute_win(compute_choice,checkerboard):

'''获胜的方法(模拟算法)

:return 获胜的棋盘, False 没有可落子的地方'''

compute_choice=compute_choice.lower()

for i in range(9):

copy_board = checkerboard.copy()

if copy_board[i]=='_':

copy_board[i]=compute_choice

if referee(compute_choice,copy_board):

checkerboard[i] = compute_choice

return checkerboard

return False

9. 阻止玩家获胜

def prevent_user_win(compute_choice,checkerboard):

'''阻止玩家获胜(模拟玩家获胜下子)

:return 阻止用户获胜的棋盘 False 没有可以阻止的落子'''

user_choice = 'O' if compute_choice.lower() == 'x' else 'X'

user_choice=user_choice.lower()

for i in range(9):

copy_board = checkerboard.copy()

if copy_board[i] == '_':

copy_board[i] = user_choice

if referee(user_choice, copy_board):

#成功阻止玩家获胜....

checkerboard[i]=compute_choice

return checkerboard

return False

10. 电脑随机落点

def choice_random(compute_choice,checkerboard,choice_list):

'''随机下棋点'''

for i in choice_list:

if checkerboard[i]=='_':

checkerboard[i]=compute_choice

return checkerboard

return False

11. 游戏主程序

def game_start(user_choice,sequence_flag):

'''游戏核心处理

:param user_choice 用户所选棋子

:param sequence_flag 决定先后手'''

checkerboard=['_' for i in range(9)] #棋盘列表

compute_choice = 'O' if user_choice.lower()=='x' else 'X'

if sequence_flag: #显示棋盘

print('玩家先走')

else:

checkerboard=compute_play(compute_choice,checkerboard) #电脑先走棋盘

print('电脑先走')

while True:

display_board(checkerboard)

checkerboard = user_play(user_choice, checkerboard)

if referee(user_choice,checkerboard):

print('用户赢了...')

display_board(checkerboard)

break

checkerboard = compute_play(compute_choice, checkerboard)

if referee(compute_choice,checkerboard):

print('电脑赢了...')

display_board(checkerboard)

break

if '_' not in checkerboard:

print('平局...')

display_board(checkerboard)

break

12. 游戏外壳

def game_shell():

'''外壳程序'''

game_info() # 游戏开始提示

user_choice = Double_choice('x', 'o', hint='请选择你的棋子 X or O')

sequence_flag = random.randint(0, 1)

game_start(user_choice,sequence_flag)

while True:

message='你想在玩一次吗(Y or N)'

again_flag=Double_choice('y','n',message)

if again_flag=='n':

break

if sequence_flag:

sequence_flag=0

else:

sequence_flag=1

game_start(user_choice,sequence_flag)

13. 运行游戏

game_shell()

扫码关注我们

微信号:SRE实战

拒绝背锅 运筹帷幄

×

选择打赏方式:

微信

QQ钱包

支付宝

打赏

打赏

打赏

多少都是心意!谢谢大家!!!

×

选择分享方式:

微信扫一扫,分享朋友圈

Or

手机扫一扫,精彩随身带

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。