我希望这里的人能帮我做这些,我正在尝试用minimax做一个tic-tac-toe游戏。你知道吗
Traceback (most recent call last):
File "C:\Users\Abdul\Desktop\A.I\finishingAttempt1.2.py", line 92, in <module>
max_move=Minimax(state)
File "C:\Users\Abdul\Desktop\A.I\finishingAttempt1.2.py", line 69, in Minimax
minimum=act[0]
TypeError: 'NoneType' object is not subscriptable
对于回溯,我试图保存该变量中返回的值。第二个,我试图保存变量中的第一个元素。这是我的密码:
def player(state):#deciding turn
x_count=0
o_count=0
for i in state[board]:
for j in state[board]:
if state[board][i,j]=='x':
x_count+=1
elif state[board][i,j]=='o':
o_count+=1
if x_count>o_count:
player='o'
elif o_count>x_count:
player='x'
return player
def successors(state):#returns possible successors for given baord
#removed and replaced actions(s)function
b=[]
if not isWin(state['player'],state['board']):
for i in range(9):
if state['board'][i]=='-':
nb=[n for n in state['board']]
nb[i]=state['player']
b.append("".join(nb))
return b
return terminal(state)
def result(state,action):
#called from Minimax()
state['board']=action
return state
def terminal(state):#checks for winning scenarios, returns isWIn(s,board), where s is the player
for i in board:
if board[0]==board[1]and board[1]==board[2]:
return isWin(player,board)
if board[3]==board[4]and board[4]==board[5]:
return isWin(player,board)
if board[6]==board[7]and board[7]==board[8]:
return isWin(player,board)
if board[2]==board[4]and board[4]==board[6]:
return isWin(player,board)
if board[0]==board[4]and board[4]==board[8]:
return isWin(player,board)
return False
def utility(state):
#the VALUE for minmax(1,-1,0), haven't worked it in to update 'utility' value in state
if terminal(state):
if state['player']=='x':
return 1
elif state['palyer']=='o':
return -1
return 0
def Minimax(state):#calls successors(state)for each player and puts list b in its dunction and then uses result(state,action)
if state['terminal']:
return state['utility']
elif state['player']=='o':
#successors(state)#action for max
act=successors(state)
maximum=act[0]
for a in act:
tmp=Minimax(result(state,act))
if tmp>maximum:
maximum=tmp
return maximum
elif state['player']=='x':
act=successors(state)
minimum=[]
minimum[0]=act[0]
for a in act:
tmp=minimum(result(state,act))
if tmp<minimum:#changed comparison for Min
minimum=tmp
return minimum
def isWin(s,board):#called for terminal states as boolean
if s=='x':
return True
if s=='o':
return False
'''--------------------------------now for running main------------------------'''
board = ['-']*9
min_move=int(input("choose a spot: "))
board[min_move]='x'
print (board)
print ('please wait, while I think..')
state={'player':'x','utility':0,'terminal':False,'board':board}
#while (state['utility']!=1 or state['utility']!=0):
while (state['terminal']==False):
max_move=Minimax(state)
state['player']= player(state)
if terminal!=True:
print('you lose, game over')
else:
if (board[max_move]!='x' and board[max_move]!='o'):
board[max_move]='o'
state['player']=player(state)
if (board[min_move]!='x' and board[min_move]!='o'):
board[min_move]='x'
print (board)
目前没有回答
相关问题 更多 >
编程相关推荐