将函数 return 设为布尔值并在 tic/tac/toe 游戏中实现 AI
Make function return boolean and implement AI in tic/tac/toe game
我制作了一个 tic/tac/toe 游戏,但我想创建一个控制 "O" 而用户控制 "X" 的计算机播放器。
对于我代码中的 clicked
函数,我试图 return 一个布尔值,但我不确定我是否做对了。函数 returns True
如果用户点击操作成功。如果用户点击已经在棋盘区域之外的位置,移动无效,函数应该 return False
。我的代码:
import turtle
import time
import random
pieces = ["_", "_", "_", "_", "_", "_", "_", "_", "_"]
turn = "X"
def drawgame(brd):
# draw board
turtle.setup(600, 600)
turtle.bgcolor("silver")
turtle.color("white")
turtle.hideturtle()
turtle.speed('fastest')
turtle.width(10)
turtle.up()
# Horizontal bars
turtle.goto(-300, 100)
turtle.down()
turtle.forward(600)
turtle.up()
turtle.goto(-300, -100)
turtle.down()
turtle.forward(600)
turtle.up()
# Vertical bars
turtle.goto(-100, 300)
turtle.setheading(-90)
turtle.down()
turtle.forward(600)
turtle.up()
turtle.goto(100, 300)
turtle.down()
turtle.forward(600)
turtle.up()
turtle.color("blue")
x, y = -300, 300
for pos in pieces:
if pos == "X":
# Draw X
turtle.up()
turtle.goto(x + 20, y - 20)
turtle.setheading(-45)
turtle.down()
turtle.forward(226)
turtle.up()
turtle.goto(x + 180, y - 20)
turtle.setheading(-135)
turtle.down()
turtle.forward(226)
turtle.up()
elif pos == "O":
#Draw O
turtle.up()
turtle.goto(x + 100, y - 180)
turtle.setheading(0)
turtle.down()
turtle.circle(80)
turtle.up()
x += 200
if x > 100:
x = -300
y -= 200
def clicked(board, x, y):
#sig: list(str), int, int -> NoneType
global turn, pieces
turtle.onscreenclick(None) # disabling handler when inside handler
column = (x + 300) // 200
row = (y - 300) // -200
square = int(row * 3 + column)
print("User clicked ", x, ",", y, " at square ", square)
if pieces[square] == "_":
pieces[square] = turn
if turn == "X":
turn = "O"
else:
turn = "X"
drawgame(pieces)
else:
print("That square is already taken")
turtle.onscreenclick(clicked)
def computer_AI(board):
#sig: list(str) -> NoneType
def gameover(board):
#sig: list(str) -> bool
#checks gameover on board if there is a three in a row pattern or not
def handler(x, y):
#sig: int, int -> NoneType
if clicked(the_board, x, y):
drawgame(the_board)
if not gameover(pieces):
computer_AI(pieces)
drawgame(pieces)
gameover(pieces)
def main():
#Runs the game
turtle.tracer(0,0)
turtle.hideturtle()
turtle.onscreenclick(handler)
drawgame(pieces)
turtle.mainloop()
main()
我正在尝试实现此输出:
感谢任何帮助。
For my clicked
function in my code, I am trying to return a bool but I
am not sure if I am doing it right.
clicked()
函数是一个事件处理器,它不会 return 任何东西 到 任何人。我们必须以此为基础进行设计。我在下面重新编写了您的代码,结合了 hander()
和 clicked()
函数,现在 "plays"。即用户先走,为"X",然后电脑响应为玩家"O":
import turtle
import random
board = ["_", "_", "_", "_", "_", "_", "_", "_", "_"]
def drawgame(board):
# draw board
turtle.setup(600, 600)
turtle.bgcolor("silver")
turtle.color("white")
turtle.hideturtle()
turtle.speed('fastest')
turtle.width(10)
turtle.up()
# Horizontal bars
turtle.goto(-300, 100)
turtle.down()
turtle.forward(600)
turtle.up()
turtle.goto(-300, -100)
turtle.down()
turtle.forward(600)
turtle.up()
# Vertical bars
turtle.setheading(-90)
turtle.goto(-100, 300)
turtle.down()
turtle.forward(600)
turtle.up()
turtle.goto(100, 300)
turtle.down()
turtle.forward(600)
turtle.up()
turtle.color("blue")
x, y = -300, 300
for pos in board:
if pos == "X":
# Draw X
turtle.up()
turtle.goto(x + 20, y - 20)
turtle.setheading(-45)
turtle.down()
turtle.forward(226)
turtle.up()
turtle.goto(x + 180, y - 20)
turtle.setheading(-135)
turtle.down()
turtle.forward(226)
turtle.up()
elif pos == "O":
# Draw O
turtle.up()
turtle.goto(x + 100, y - 180)
turtle.setheading(0)
turtle.down()
turtle.circle(80)
turtle.up()
x += 200
if x > 100:
x = -300
y -= 200
def handler(x, y):
# sig: list(str), int, int -> NoneType
turtle.onscreenclick(None) # disabling handler when inside handler
column = (x + 300) // 200
row = (y - 300) // -200
square = int(row * 3 + column)
if board[square] == "_":
board[square] = "X"
drawgame(board)
if not gameover(board):
computer_AI(board)
drawgame(board)
if not gameover(board):
turtle.onscreenclick(handler) # allow player to take a turn
else:
print("That square is already taken!")
turtle.onscreenclick(handler) # allow player to retake turn
def computer_AI(board): # sig: list(str) -> NoneType
""" stupid robot player, just picks randomly from what's available """
available = [index for index, character in enumerate(board) if character == "_"]
if available:
index = random.choice(available)
board[index] = "O"
def gameover(board):
# sig: list(str) -> bool
# checks game over on board if there is a three in a row pattern or not
pass # to be implemented!
return False
def main():
# Runs the game
turtle.hideturtle()
turtle.onscreenclick(handler)
drawgame(board)
turtle.mainloop()
main()
要做的事情:判断游戏是否结束,谁赢了的逻辑还不存在,所以你需要写那个。目前它只是 returns False
表示游戏还没有结束。 computer_AI()
代码根本没有智能,它只是记录所有空心方块并随机选择一个。你需要改进这一点。
我制作了一个 tic/tac/toe 游戏,但我想创建一个控制 "O" 而用户控制 "X" 的计算机播放器。
对于我代码中的 clicked
函数,我试图 return 一个布尔值,但我不确定我是否做对了。函数 returns True
如果用户点击操作成功。如果用户点击已经在棋盘区域之外的位置,移动无效,函数应该 return False
。我的代码:
import turtle
import time
import random
pieces = ["_", "_", "_", "_", "_", "_", "_", "_", "_"]
turn = "X"
def drawgame(brd):
# draw board
turtle.setup(600, 600)
turtle.bgcolor("silver")
turtle.color("white")
turtle.hideturtle()
turtle.speed('fastest')
turtle.width(10)
turtle.up()
# Horizontal bars
turtle.goto(-300, 100)
turtle.down()
turtle.forward(600)
turtle.up()
turtle.goto(-300, -100)
turtle.down()
turtle.forward(600)
turtle.up()
# Vertical bars
turtle.goto(-100, 300)
turtle.setheading(-90)
turtle.down()
turtle.forward(600)
turtle.up()
turtle.goto(100, 300)
turtle.down()
turtle.forward(600)
turtle.up()
turtle.color("blue")
x, y = -300, 300
for pos in pieces:
if pos == "X":
# Draw X
turtle.up()
turtle.goto(x + 20, y - 20)
turtle.setheading(-45)
turtle.down()
turtle.forward(226)
turtle.up()
turtle.goto(x + 180, y - 20)
turtle.setheading(-135)
turtle.down()
turtle.forward(226)
turtle.up()
elif pos == "O":
#Draw O
turtle.up()
turtle.goto(x + 100, y - 180)
turtle.setheading(0)
turtle.down()
turtle.circle(80)
turtle.up()
x += 200
if x > 100:
x = -300
y -= 200
def clicked(board, x, y):
#sig: list(str), int, int -> NoneType
global turn, pieces
turtle.onscreenclick(None) # disabling handler when inside handler
column = (x + 300) // 200
row = (y - 300) // -200
square = int(row * 3 + column)
print("User clicked ", x, ",", y, " at square ", square)
if pieces[square] == "_":
pieces[square] = turn
if turn == "X":
turn = "O"
else:
turn = "X"
drawgame(pieces)
else:
print("That square is already taken")
turtle.onscreenclick(clicked)
def computer_AI(board):
#sig: list(str) -> NoneType
def gameover(board):
#sig: list(str) -> bool
#checks gameover on board if there is a three in a row pattern or not
def handler(x, y):
#sig: int, int -> NoneType
if clicked(the_board, x, y):
drawgame(the_board)
if not gameover(pieces):
computer_AI(pieces)
drawgame(pieces)
gameover(pieces)
def main():
#Runs the game
turtle.tracer(0,0)
turtle.hideturtle()
turtle.onscreenclick(handler)
drawgame(pieces)
turtle.mainloop()
main()
我正在尝试实现此输出:
感谢任何帮助。
For my
clicked
function in my code, I am trying to return a bool but I am not sure if I am doing it right.
clicked()
函数是一个事件处理器,它不会 return 任何东西 到 任何人。我们必须以此为基础进行设计。我在下面重新编写了您的代码,结合了 hander()
和 clicked()
函数,现在 "plays"。即用户先走,为"X",然后电脑响应为玩家"O":
import turtle
import random
board = ["_", "_", "_", "_", "_", "_", "_", "_", "_"]
def drawgame(board):
# draw board
turtle.setup(600, 600)
turtle.bgcolor("silver")
turtle.color("white")
turtle.hideturtle()
turtle.speed('fastest')
turtle.width(10)
turtle.up()
# Horizontal bars
turtle.goto(-300, 100)
turtle.down()
turtle.forward(600)
turtle.up()
turtle.goto(-300, -100)
turtle.down()
turtle.forward(600)
turtle.up()
# Vertical bars
turtle.setheading(-90)
turtle.goto(-100, 300)
turtle.down()
turtle.forward(600)
turtle.up()
turtle.goto(100, 300)
turtle.down()
turtle.forward(600)
turtle.up()
turtle.color("blue")
x, y = -300, 300
for pos in board:
if pos == "X":
# Draw X
turtle.up()
turtle.goto(x + 20, y - 20)
turtle.setheading(-45)
turtle.down()
turtle.forward(226)
turtle.up()
turtle.goto(x + 180, y - 20)
turtle.setheading(-135)
turtle.down()
turtle.forward(226)
turtle.up()
elif pos == "O":
# Draw O
turtle.up()
turtle.goto(x + 100, y - 180)
turtle.setheading(0)
turtle.down()
turtle.circle(80)
turtle.up()
x += 200
if x > 100:
x = -300
y -= 200
def handler(x, y):
# sig: list(str), int, int -> NoneType
turtle.onscreenclick(None) # disabling handler when inside handler
column = (x + 300) // 200
row = (y - 300) // -200
square = int(row * 3 + column)
if board[square] == "_":
board[square] = "X"
drawgame(board)
if not gameover(board):
computer_AI(board)
drawgame(board)
if not gameover(board):
turtle.onscreenclick(handler) # allow player to take a turn
else:
print("That square is already taken!")
turtle.onscreenclick(handler) # allow player to retake turn
def computer_AI(board): # sig: list(str) -> NoneType
""" stupid robot player, just picks randomly from what's available """
available = [index for index, character in enumerate(board) if character == "_"]
if available:
index = random.choice(available)
board[index] = "O"
def gameover(board):
# sig: list(str) -> bool
# checks game over on board if there is a three in a row pattern or not
pass # to be implemented!
return False
def main():
# Runs the game
turtle.hideturtle()
turtle.onscreenclick(handler)
drawgame(board)
turtle.mainloop()
main()
要做的事情:判断游戏是否结束,谁赢了的逻辑还不存在,所以你需要写那个。目前它只是 returns False
表示游戏还没有结束。 computer_AI()
代码根本没有智能,它只是记录所有空心方块并随机选择一个。你需要改进这一点。