我不知道给这篇文章起什么样的标题。我是 python 的新手,我的第一堂课已经 4 周了。我用人工智能写了一个井字游戏程序,但人工智能很奇怪。它在我的家用电脑上的行为与在我的学校电脑上的行为不同。在学校里,程序一旦变成计算机就崩溃了。它甚至不画框。家里的电脑就是不动。顺便说一句,我的家用电脑比我的学校电脑好得多。它还在学校运行 Windows 10 TP 与 Windows 7。我给我的老师看,他不能告诉我这个问题。这是我的代码。抱歉,这都是一个文件,而且是我拥有的所有代码,因为我不知道问题出在哪里。还有我 m 使用 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