我需要在我的井字棋程序中创建一个随机下O的AI
我在网上查了一些关于人工智能的问题,发现每个AI都是针对特定程序的,而且没有一个能帮到我。
我想让电脑在我下完X之后,随机画O。我听说“最小最大算法”可能有帮助,但我不太明白这个算法。毕竟我还是编程初学者。
我该如何让电脑在我下完第一步(用户是X)后自动画O呢?
这是我目前的井字棋程序。文档字符串和注释会告诉你更多关于这个程序的信息。
import turtle
#I need to fix the positions of the X and O's so that they draw more perfectly into the squares of the board.
class TicTacToe:
def __init__(self):
self.u__init_board = []
def draw_board():
'''draws a tic-tac-toe board over the 9 turtle squares'''
t=turtle.Turtle()
t.ht() # makes the turtle invisable
t.up()
t.goto(-40,-40)
t.down()
t.forward(240)
t.left(90)
t.forward(240)
t.left(90)
t.forward(240)
t.left(90)
t.forward(80)
t.left(90)
t.forward(240)
t.right(90)
t.forward(80)
t.right(90)
t.forward(240)
t.left(90)
t.goto(-40,-40)
t.left(180)
t.forward(160)
t.up()
t.goto(40,-40)
t.down()
t.forward(240)
t.right(90)
t.forward(80)
t.right(90)
t.forward(240)
def setup_board():
'''Creates 3 rows of 3 turtles using range(0, 240, 80); turtle.Turtle(); up(); shape('square'); shapesize(4, 4, 4);
color('white'); goto(x, y). Each turtle is registered to respond to click events using onclick(mark).
Calls draw_grid() once the 9 turtles are on the board.'''
for y in range(0,240,80):
for x in range (0,240,80):
t=turtle.Turtle()
t.up()
t.shape('square')
t.shapesize(4,4,4,)
t.color('white')
t.goto(x,y)
t.onclick(TicTacToe.mark)
TicTacToe.draw_board()
def mark(x, y):
'''Function is invoked whenever a turtle registered to respond to click event is clicked. Creates a turtle and draws
either a circle or an x centered on the x, y coordinates of the click.
Be sure to set circle to False once the circle is drawn and to True once the x is drawn. '''
ct = turtle.Turtle()
ct.ht() #hides the turtle (makes the turtle invisable)
ct.up()
global circle
if circle:
turtle.penup()
#turtle.speed(0)
turtle.goto(x,y)
turtle.down()
turtle.circle(30)
turtle.left(45)
circle = False
else:
turtle.up()
#turtle.speed(0)
turtle.goto(x,y)
turtle.down()
turtle.left(45)
turtle.forward(40)
turtle.left(180)
turtle.forward(80)
turtle.left(180)
turtle.forward(40)
turtle.left(90)
turtle.forward(40)
turtle.left(180)
turtle.forward(80)
circle = True
def main():
wn = turtle.Screen()
wn.title('OG Tic Tac Toe')
wn.bgcolor("red")
global circle
circle = False
TicTacToe.setup_board()
return 'Done'
if __name__ == '__main__':
TicTacToe.main()
turtle.TK.mainloop()
1 个回答
2
一些通用建议:
把逻辑和界面分开,这样你就会发现有一个由9个方块组成的网格,有些是空的,有些是X,有些是O。你可以保持一个空方块的列表,然后随机选择一个,画上你的O,并从列表中把它移除。就是这么简单!
代码大纲:
#### GUI Stuff ####
def DrawBoard():
""" Draw The Blank Board and Connect The Mouse Hander """
def DrawSymbol(TileNo, XorO):
""" Draw the required X or O symbol in tile number 0..9 """
def OnMouseClick():
""" Convert the mouse click event to a tile number in the range 0..8 and call Square Picked as a user."""
def ShowWin(WinningTiles):
""" Highlight the winning list of tiles """
### Game Logic ###
def SquarePicked(Square, UserOrComputer):
"""
When a given square is picked check if it has already been taken, if it has then return false otherwise call update square and return true
"""
def UpdateSquare(Square, UserOrComputer):
"""
Update the list of squares to show it taken, update the board, then call check for win
if it returns false and this was a user go call computer go.
"""
def CheckForWin():
"""
Run through the list of squares to check for a win and if it has happened call ShowWin.
"""
def ComputerGo():
"""
Pick a (random)) square from those not taken and call square picked.
"""