前言

最近正在学习Python,所以我会出一个关于Python实例的系列文章。

文章里面有什么不对的地方还请大家多多指教

井字游戏总共分为一下十个步骤

第一步

编写一个可以打印出地图的函数。将棋盘设置为一个列表,其中每个索引 1-9 对应数字键盘上的一个数字,因此就会得到一个 3 x 3 的棋盘。

from IPython.display import clear_output
def display_board(board):
clear_output()  s only works in jupyter!
print('   |   |')
print(' ' + board[1] + ' | ' + board[2] + ' | ' + board[3])
print('   |   |')
print('-----------')
print('   |   |')
print(' ' + board[4] + ' | ' + board[5] + ' | ' + board[6])
print('   |   |')
print('-----------')
print('   |   |')
print(' ' + board[7] + ' | ' + board[8] + ' | ' + board[9])
print('   |   |')

测试:在地图列表上运行函数,并根据需要进行调整

test_board = ['*','X','O','X','O','X','O','X','O','X'] 
display_board(test_board)

第二步

编写一个可以接收玩家输入并将其标记分配为“X”或“O”的函数。
考虑使用while循环不断询问,直到得到正确答案。

def player_input():
marker = ''
while not (marker == 'X' or marker == 'O'):
marker = input('Player 1: Do you want to be X or O? ').upper()
if marker == 'X':
return ('X', 'O')
else:
return ('O', 'X')

测试2:运行该函数以确保它返回所需的输出。

player_input()

第三步

编写一个函数,接收板列表对象、标记(“X”或“O”)和所需位置(数字 1-9)并将其分配给板。

def place_marker(board, marker, position): 
board[position] = marker

测试3:使用测试参数运行位置标记功能并显示修改后的地图

place_marker(test_board,'$',8) 
display_board(test_board)

第四步

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

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赢了!')
game_on = False
else:
if full_board_check(theBoard):
display_board(theBoard)
print('平局')
break
else:
turn = 'Player 1'
if not replay():
break

今天的内容到这里也就结束了,喜欢的话记得三连哦