【Python】基础-井字棋游戏
本文最后更新于 341 天前,其中的信息可能已经有所发展或是发生改变。

[success]
又是一个Python的案例,学习留笔记用。
游戏中,board棋盘存储玩家,计算机落子的信息,未落子处为EMPTY,由于人机对战需要实现计算机的智能性,下面为策略:

  • 如果有一步棋可以让计算机机器人在本轮获胜,就走那一步。
  • 如果有一步棋可以让玩家本轮获胜,就选那一步。
  • 否则,计算机机器人应该选择最佳空位置来走,最佳的位置是中间那个,第二好位置就是四个角,剩下的就都算好位置。

[/success]
1.主函数。
[sourcecode language=”python” title=”test.py”]
#主函数
import demo4

def main():
computer, human = demo4.pieces()
turn = demo4.X
board = demo4.new_board()
demo4.display_board(board)
while not demo4.winner(board):
if turn == human:
move = demo4.human_move(board, human)
board[move] = human
else:
move = demo4.computer_move(board, computer, human)
board[move] = computer
demo4.display_board(board)
turn = demo4.next_turn(turn)
#游戏结束输出输赢或和棋的信息
the_winner = demo4.winner(board)
if the_winner == computer:
print("计算机赢!\n")
elif the_winner == human:
print("玩家赢!\n")
elif the_winner == "TIE":
print("平局和棋,游戏结束\n")

#调用main()函数

main()
input("按任意键退出游戏。")
[/sourcecode]
2.子函数。
[sourcecode language=”python” title=”demo4.py”]
#井子棋游戏
X = "X"
O = "O"
EMPTY = " "

#询问是否继续
def ask_yes_no(question):
response = None
while response not in ("y", "n"):
response = input(question).lower()
return response

#输入位置数字
def ask_number(question, low, high):
response = None
while response not in range(low, high):
response = int(input(question))
return response

#询问谁先走先走的人为X
#函数返回计算机方和玩家的代号
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 new_board():
board = []
for square in range(9):
board.append(EMPTY)
return board

#显示棋盘
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 legal_moves(board):
moves = []
for square in range(9):
if board[square] == EMPTY:
moves.append(square)
return moves

#判断输赢
def winner(board):
#所有赢的可能情况
WAYS_TO_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))
for row in WAYS_TO_WIN:
if board[row[0]] == board[row[1]] == board[row[2]] != EMPTY:
winner = board[row[0]]
return winner
#棋盘没有空位置
if EMPTY not in board:
return "TIE"
return False

#人走棋
def human_move(board, human):
legal = legal_moves(board)
move = None
while move not in legal:
move = ask_number("你走那个位置?(0 – 8):", 0, 9)
if move not in legal:
print("\n此位置已经落过子了")
return move

#计算机走棋
def computer_move(board, computer, human):
board = board[:]
BEST_MOVES = (4, 0, 2, 6, 8, 1, 3, 5, 7)
for move in legal_moves(board):
board[move] = computer
if winner(board) == computer:
print("计算机下棋的位置…" ,move)
return move
#取消走棋方案
board[move] = EMPTY
#如果上面情况本轮赢不了就从最佳的下棋位置挑一个
for move in BEST_MOVES:
if move in legal_moves(board):
print("计算机下棋的位置…", move)
return move
#转换角色
def next_turn(turn):
if turn == X:
return O
if turn == O:
return X
[/sourcecode]

运行效果图:

暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇