我不知道给这篇文章提供什么样的头衔。我是蟒蛇新手,我进入头等舱已有4周了。我用AI写了一个 Tic Tac Toe 程序,但AI很奇怪。它在我的家用电脑上的表现与我在学校的电脑不同。在学校,一旦计算机转向移动,程序就会崩溃。它甚至没有画出盒子。在家里,电脑不动。顺便说一下,我的家用电脑明显优于我的学校电脑。此外,它在学校运行Windows 10 TP与Windows 7。我向老师展示了他无法告诉我的问题。这是我的代码。对不起,这是一个文件,这是我的所有代码,因为我不知道问题出在哪里。我也在使用PyProcessing,它基本上是一个python IDE,为图形提供了一些简单的方法。 setup()函数在开始时自动运行,draw()也是如此,但我不确定何时或是什么原因导致draw()函数运行。
board = [[None, None, None],
[None, None, None],
[None, None, None]]
turn = 0
player_letter = ""
computer_letter = ""
game_ended = False
def setup():
global game_ended
global player_letter
global turn
global board
global computer_letter
size(300,300)
drawBoard(board)
drawLetters(board)
turn = 0
if turn == 0:
player_letter = "X"
computer_letter = "O"
else:
player_letter = "O"
computer_letter = "X"
def check_win(board, letter):
if board[0][0] == letter and board[0][1] == letter and board[0][2] == letter:
return True
elif board[1][0] == letter and board[1][1] == letter and board[1][2] == letter:
return True
elif board[2][0] == letter and board[2][1] == letter and board[2][2] == letter:
return True
elif board[0][0] == letter and board[1][0] == letter and board[2][0] == letter:
return True
elif board[0][1] == letter and board[1][1] == letter and board[2][1] == letter:
return True
elif board[0][2] == letter and board[1][2] == letter and board[2][2] == letter:
return True
elif board[0][0] == letter and board[1][1] == letter and board[2][2] == letter:
return True
elif board[2][0] == letter and board[1][1] == letter and board[0][2] == letter:
return True
else:
return False
def run_player_turn(player_letter):
global board
if mousePressed and board[mouseY//100][mouseX//100] == None:
board[mouseY//100][mouseX//100] = player_letter
return True
else:
return False
def getPotentialWin(computer_letter, player_letter):
global board
check1 = [board[0][0], board[0][1], board[0][2]]
check2 = [board[1][0], board[1][1], board[1][2]]
check3 = [board[2][0], board[2][1], board[2][2]]
check4 = [board[0][0], board[1][0], board[2][0]]
check5 = [board[0][1], board[1][1], board[2][1]]
check6 = [board[0][2], board[1][2], board[2][2]]
check7 = [board[0][0], board[1][1], board[2][2]]
check8 = [board[2][0], board[1][1], board[0][2]]
checks = [check1, check2, check3, check4, check5, check6, check7, check8]
cletters = 0
pletters = 0
letters = 0
for check in checks:
for letter in check:
if letter != None:
letters += 1
if letter == computer_letter:
cletters += 1
else:
pletters += 1
if cletters == 2 and letters == 2:
for letter in check:
if letter == None:
return letter
return None
def getPotentialLoss(computer_letter, player_letter):
global board
check1 = [board[0][0], board[0][1], board[0][2]]
check2 = [board[1][0], board[1][1], board[1][2]]
check3 = [board[2][0], board[2][1], board[2][2]]
check4 = [board[0][0], board[1][0], board[2][0]]
check5 = [board[0][1], board[1][1], board[2][1]]
check6 = [board[0][2], board[1][2], board[2][2]]
check7 = [board[0][0], board[1][1], board[2][2]]
check8 = [board[2][0], board[1][1], board[0][2]]
checks = [check1, check2, check3, check4, check5, check6, check7, check8]
cletters = 0
pletters = 0
letters = 0
for check in checks:
for letter in check:
if letter != None:
letters += 1
if letter == player_letter:
pletters += 1
else:
cletters += 1
if pletters == 2 and letters == 2:
for letter in check:
if letter == None:
return letter
return None
def draw():
global board
global turn
global player_letter
global computer_letter
if (not check_win(board, player_letter)) and (not check_win(board, computer_letter)):
if turn == 0:
if run_player_turn(player_letter):
turn = 1
else:
if run_computer_turn(computer_letter):
turn = 0
drawLetters(board)
def get_starting_player():
random_num = int(random(2))
return random_num
def drawLetters(board):
for row in range(len(board)):
for col in range(len(board[0])):
if board[row][col] != None:
textSize(32)
fill(0,0,0)
text(board[row][col], col*100+25, row*100+75)
def drawBoard(board):
for y in range(len(board)):
for x in range(len(board[0])):
rect(x * 100, y * 100, 100, 100)
def run_computer_turn(computer_letter):
global board
global turn
global cHasGone
global player_letter
potentialLoss = getPotentialLoss(computer_letter, player_letter)
potentialWin = getPotentialWin(computer_letter, player_letter)
if potentialLoss != None:
potentialLoss = computer_letter
return True
elif potentialWin != None:
potentialWin = computer_letter
return True
else:
found = False
while not found:
x = int(random(0, 3))
y = int(random(0, 3))
if board[x][y] == None:
board[x][y] == computer_letter
found = True
return True
return True
def checkAvailibleWin(row, col):
print("Hi")
def getAdjacents(row, col):
adjs = [[None, None, None, None, None],
[None, None, None, None, None],
[None, None, None, None, None],
[None, None, None, None, None],
[None, None, None, None, None]]
row += 1
col += 1
adjs[row - 1][col - 1] = True;
adjs[row - 1][col] = True;
adjs[row - 1][col + 1] = True;
adjs[row][col - 1] = True;
adjs[row][col] = True;
adjs[row][col + 1] = True;
adjs[row + 1][col - 1] = True;
adjs[row + 1][col] = True;
adjs[row + 1][col + 1] = True;
trueAdjs = [[None, None, None],
[None, None, None],
[None, None, None]]
for y in adjs:
for x in adjs:
if((x > 0 and x < 3) and (y > 0 and y < 1)):
trueAdjs[y-1][x-1] = True
return trueAdjs