十个步骤简单带你用Python实现一个井字游戏

第四步

======================================================================

编写一个函数,接收棋盘并检查是否有人赢了

def win_check(board,mark):

return ((board[7] == mark and board[8] == mark and board[9] == mark) or # across the top

(board[4] == mark and board[5] == mark and board[6] == mark) or # across the middle

(board[1] == mark and board[2] == mark and board[3] == mark) or # across the bottom

(board[7] == mark and board[4] == mark and board[1] == mark) or # down the middle

(board[8] == mark and board[5] == mark and board[2] == mark) or # down the middle

(board[9] == mark and board[6] == mark and board[3] == mark) or # down the right side

(board[7] == mark and board[5] == mark and board[3] == mark) or # diagonal

(board[9] == mark and board[5] == mark and board[1] == mark)) # diagonal

测试4:针对我们的 test_board 运行 win_check 函数——它应该返回 True

win_check(test_board,‘X’)

第五步

======================================================================

编写一个函数,使用 random 模块随机决定哪个玩家先走。您可能想要查找random.randint()返回哪个玩家先去的字符串。

import random

def choose_first():

if random.randint(0, 1) == 0:

return ‘Player 2’

else:

return ‘Player 1’

第六步

======================================================================

编写一个函数,该函数返回一个布尔值,指示板上的空间是否可用。

def space_check(board, position):

return board[position] == ’ ’

第七步

======================================================================

编写一个函数来检查地图是否已满并返回一个布尔值。满则为真,否则为假。

def full_board_check(board):

for i in range(1,10):

if space_check(board, i):

return False

return True

第八步

======================================================================

编写一个函数来询问玩家的下一个位置(作为数字 1-9),然后使用步骤 6 中的函数检查它是否是空闲位置。如果是,则返回该位置以备后用。

def player_choice(board):

position = 0

while position not in [1, 2, 3, 4, 5, 6, 7, 8, 9] or not space_check(board, position):

position = int(input('Choose your next position: (1-9) '))

return position

第九步

======================================================================

编写一个函数,询问玩家是否想再玩一次,如果他们想再玩一次,则返回一个布尔值 True。

def replay():

return input('Do you want to play again? Enter Yes or No: ').lower().startswith(‘y’)

第十步

======================================================================

困难的部分来了!使用 while 循环和为运行游戏所做的函数。

print(‘欢迎来到井字游戏!’)

while True:

重置地图

theBoard = [’ '] * 10

player1_marker, player2_marker = player_input()

turn = choose_first()

print(turn + ’ will go first.')

play_game = input(‘Are you ready to play? Enter Yes or No.’)

if play_game.lower()[0] == ‘y’:

game_on = True

else:

game_on = False

while game_on:

if turn == ‘Player 1’:

display_board(theBoard)

position = player_choice(theBoard)

place_marker(theBoard, player1_marker, position)

if win_check(theBoard, player1_marker):

display_board(theBoard)

print(‘恭喜!你赢了比赛!’)

game_on = False

else:

if full_board_check(theBoard):

display_board(theBoard)

print(‘平局’)

break

else:

turn = ‘Player 2’

else:

display_board(theBoard)

position = player_choice(theBoard)

place_marker(theBoard, player2_marker, position)

if win_check(theBoard, player2_marker):

display_board(theBoard)

print(‘玩家2赢了!’)

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化学习资料的朋友,可以戳这里无偿获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

  • 15
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值