井字棋是一种在3 × 3网格上玩的经典纸笔游戏。玩家轮流放置 X 或 O 标记,试图连续获得三个。大多数井字棋都以平局告终,但如果你的对手不小心,你也有可能智胜他们。
当您运行tictactoe.py时,输出将如下所示:
Welcome to Tic-Tac-Toe!
| | 1 2 3
-+-+-
| | 4 5 6
-+-+-
| | 7 8 9
What is X's move? (1-9)
> 1
X| | 1 2 3
-+-+-
| | 4 5 6
-+-+-
| | 7 8 9
What is O's move? (1-9)
`--snip--`
X|O|X 1 2 3
-+-+-
X|O|O 4 5 6
-+-+-
O|X|X 7 8 9
The game is a tie!
Thanks for playing!为了在这个程序中表示井字棋棋盘,我们使用了一个字典,用键'1'到'9'来表示棋盘上的空格。数字空间的排列方式与手机键盘相同。本词典中的值是代表球员标记的字符串'X'或'O'和代表空白的' '。
"""Tic-Tac-Toe, by Al Sweigart email@protected
The classic board game.
This code is available at https://nostarch.com/big-book-small-python-programming
Tags: short, board game, game, two-player"""
ALL_SPACES = ['1', '2', '3', '4', '5', '6', '7', '8', '9']
X, O, BLANK = 'X', 'O', ' ' # Constants for string values.
def main():
print('Welcome to Tic-Tac-Toe!')
gameBoard = getBlankBoard() # Create a TTT board dictionary.
currentPlayer, nextPlayer = X, O # X goes first, O goes next.
while True: # Main game loop.
# Display the board on the screen:
print(getBoardStr(gameBoard))
# Keep asking the player until they enter a number 1-9:
move = None
while not isValidSpace(gameBoard, move):
print('What is {}\'s move? (1-9)'.format(currentPlayer))
move = input('> ')
updateBoard(gameBoard, move, currentPlayer) # Make the move.
# Check if the game is over:
if isWinner(gameBoard, currentPlayer): # Check for a winner.
print(getBoardStr(gameBoard))
print(currentPlayer + ' has won the game!')
break
elif isBoardFull(gameBoard): # Check for a tie.
print(getBoardStr(gameBoard))
print('The game is a tie!')
break
# Switch turns to the next player:
currentPlayer, nextPlayer = nextPlayer, currentPlayer
print('Thanks for playing!')
def getBlankBoard():
"""Create a new, blank tic-tac-toe board."""
# Map of space numbers: 1|2|3
# -+-+-
# 4|5|6
# -+-+-
# 7|8|9
# Keys are 1 through 9, the values are X, O, or BLANK:
board = {}
for space in ALL_SPACES:
board[space] = BLANK # All spaces start as blank.
return board
def getBoardStr(board):
"""Return a text-representation of the board."""
return '''
{}|{}|{} 1 2 3
-+-+-
{}|{}|{} 4 5 6
-+-+-
{}|{}|{} 7 8 9'''.format(board['1'], board['2'], board['3'],
board['4'], board['5'], board['6'],
board['7'], board['8'], board['9'])
def isValidSpace(board, space):
"""Returns True if the space on the board is a valid space number
and the space is blank."""
return space in ALL_SPACES and board[space] == BLANK
def isWinner(board, player):
"""Return True if player is a winner on this TTTBoard."""
# Shorter variable names used here for readablility:
b, p = board, player
# Check for 3 marks across 3 rows, 3 columns, and 2 diagonals.
return ((b['1'] == b['2'] == b['3'] == p) or # Across top
(b['4'] == b['5'] == b['6'] == p) or # Across middle
(b['7'] == b['8'] == b['9'] == p) or # Across bottom
(b['1'] == b['4'] == b['7'] == p) or # Down left
(b['2'] == b['5'] == b['8'] == p) or # Down middle
(b['3'] == b['6'] == b['9'] == p) or # Down right
(b['3'] == b['5'] == b['7'] == p) or # Diagonal
(b['1'] == b['5'] == b['9'] == p)) # Diagonal
def isBoardFull(board):
"""Return True if every space on the board has been taken."""
for space in ALL_SPACES:
if board[space] == BLANK:
return False # If any space is blank, return False.
return True # No spaces are blank, so return True.
def updateBoard(board, space, mark):
"""Sets the space on the board to mark."""
board[space] = mark
if __name__ == '__main__':
main() # Call main() if this module is run, but not when imported. 试着找出下列问题的答案。尝试对代码进行一些修改,然后重新运行程序,看看这些修改有什么影响。
- 如果把第 7 行的
X, O, BLANK = 'X', 'O', ' '改成X, O, BLANK = 'X', 'X', ' '会怎么样? - 如果把第 95 行的
board[space] = mark改成board[space] = X会怎么样? - 如果把第 50 行的
board[space] = BLANK改成board[space] = X会怎么样?
