best_weizhi = (4, 0, 2, 6, 8, 1, 3, 5, 7)
win = ((0, 1, 2), (3, 4, 5), (6, 7, 8), (0, 3, 6), (1, 4, 7), (2, 5, 8), (0, 4, 8), (2, 4, 6))
X = "X"
O = "O"
EMPTY = " "
def new_board():
board = []
for square in range(9):
board.append(EMPTY)
return board
def ask_yes_no(question):
response = None
while response not in ("y", "n"):
response = input(question).lower()
return response
def pieces():
go_first = ask_yes_no("玩家你是否先走 (y/n): ")
if go_first == "y":
print("\n玩家你先走.")
human = X
computer = O
else:
print("\n电脑先走.")
computer = X
human = O
return computer, human
def display_board(board):
board2 = board[:]
for i in range(len(board)):
if board[i] == EMPTY:
board2[i] = i
print("\t", board2[0], "|", board2[1], "|", board2[2])
print("\t", "---------")
print("\t", board2[3], "|", board2[4], "|", board2[5])
print("\t", "---------")
print("\t", board2[6], "|", board2[7], "|", board2[8], "\n")
def ask_number(question, low, high):
response = None
while response not in range(low, high):
response = int(input(question))
return response
def legal_moves(board):
moves = []
for i in range(9):
if board[i] == EMPTY:
moves.append(i)
return moves
def winner(board):
for row in win:
if board[row[0]] == board[row[1]] == board[row[2]] != EMPTY:
winner = board[row[0]]
return winner
if EMPTY not in board:
return "True"
return False
def human_move(board, human):
legal = legal_moves(board)
move = None
while move not in legal:
move = ask_number("你走那个位置? (0 - 9):", 0, 9)
if move not in legal:
print("\n此位置已经落过子了")
return move
def computer_move(board, computer, human):
board = board[:]
for move in legal_moves(board):
board[move] = computer
if winner(board) == computer:
print("电脑下棋位置是", move)
return move
board[move] = EMPTY
for move in legal_moves(board):
board[move] = human
if winner(board) == human:
print("电脑下棋位置是", move)
return move
board[move] = EMPTY
for move in best_weizhi:
if move in legal_moves(board):
print("电脑下棋位置是", move)
return move
def next_turn(turn):
if turn == X:
return O
else:
return X
def main():
computer, human = pieces()
turn = X
board = new_board()
display_board(board)
while not winner(board):
if turn == human:
move = human_move(board, human)
board[move] = human
else:
move = computer_move(board, computer, human)
board[move] = computer
display_board(board)
turn = next_turn(turn)
the_winner = winner(board)
if the_winner == computer:
print("电脑赢!\n")
elif the_winner == human:
print("玩家赢!\n")
elif the_winner == "True":
print("平局,和棋,游戏结束\n")
main()
input("按任意键退出游戏.")
best_weizhi = (4, 0, 2, 6, 8, 1, 3, 5, 7)
win = ((0, 1, 2), (3, 4, 5), (6, 7, 8), (0, 3, 6), (1, 4, 7), (2, 5, 8), (0, 4, 8), (2, 4, 6))
X = "X"
O = "O"
EMPTY = " "
def new_board():
board = []
for square in range(9):
board.append(EMPTY)
return board
def ask_yes_no(question):
response = None
while response not in ("y", "n"):
response = input(question).lower()
return response
def pieces():
go_first = ask_yes_no("玩家你是否先走 (y/n): ")
if go_first == "y":
print("\n玩家你先走.")
human = X
computer = O
else:
print("\n电脑先走.")
computer = X
human = O
return computer, human
def display_board(board):
board2 = board[:]
for i in range(len(board)):
if board[i] == EMPTY:
board2[i] = i
print("\t", board2[0], "|", board2[1], "|", board2[2])
print("\t", "---------")
print("\t", board2[3], "|", board2[4], "|", board2[5])
print("\t", "---------")
print("\t", board2[6], "|", board2[7], "|", board2[8], "\n")
def ask_number(question, low, high):
response = None
while response not in range(low, high):
response = int(input(question))
return response
def legal_moves(board):
moves = []
for i in range(9):
if board[i] == EMPTY:
moves.append(i)
return moves
def winner(board):
for row in win:
if board[row[0]] == board[row[1]] == board[row[2]] != EMPTY:
winner = board[row[0]]
return winner
if EMPTY not in board:
return "True"
return False
def human_move(board, human):
legal = legal_moves(board)
move = None
while move not in legal:
move = ask_number("你走那个位置? (0 - 9):", 0, 9)
if move not in legal:
print("\n此位置已经落过子了")
return move
def computer_move(board, computer, human):
board = board[:]
for move in legal_moves(board):
board[move] = computer
if winner(board) == computer:
print("电脑下棋位置是", move)
return move
board[move] = EMPTY
for move in legal_moves(board):
board[move] = human
if winner(board) == human:
print("电脑下棋位置是", move)
return move
board[move] = EMPTY
for move in best_weizhi:
if move in legal_moves(board):
print("电脑下棋位置是", move)
return move
def next_turn(turn):
if turn == X:
return O
else:
return X
def main():
computer, human = pieces()
turn = X
board = new_board()
display_board(board)
while not winner(board):
if turn == human:
move = human_move(board, human)
board[move] = human
else:
move = computer_move(board, computer, human)
board[move] = computer
display_board(board)
turn = next_turn(turn)
the_winner = winner(board)
if the_winner == computer:
print("电脑赢!\n")
elif the_winner == human:
print("玩家赢!\n")
elif the_winner == "True":
print("平局,和棋,游戏结束\n")
main()
input("按任意键退出游戏.")
需求分析
能实现人机交替落子,当行,列或对角有连续三个相同一方棋时,则判定一方胜利,若无此情形为和局。
系统设计
游戏中,board棋盘存储玩家、计算机的落子信息,未落子处未EMPTY。由于人机对战,需要实现计算机智能性,下面是为这个计算机机器人设计的简单策略: 如果有一步棋可以让计算机机器人在本轮获胜,那就选那一步走。 否则,如果有一步棋可以让玩家在本轮获胜,那就选那一步走。 否则,计算机机器人应该选择最佳位置来走。 最佳位置就是中间,其次是四个角 定义第一个元组best_weizhi存储最佳方格位置: 按优劣顺序排序的下棋位置 best_weizhi= (4, 0, 2, 6, 8, 1, 3, 5, 7) 井字棋盘输赢判断规则只有8种方式。每种获胜方式都被写成一个元组,利用嵌套元组表达: win = ((0, 1, 2), (3, 4, 5), (6, 7, 8), (0, 3, 6),(1, 4, 7), (2, 5, 8), (0, 4, 8), (2, 4, 6))
|