Minimax funktioniert nicht
Verfasst: Mittwoch 18. Juli 2018, 15:12
Ich habe einen unschlagbaren Tic Tac Toe bot gemacht, aber mir war der Algorithmus zu schlampig und jetzt habe ich es mit minimax versucht.
Kann mir jemand sage was an dieser Minimax funktion(aiset()) falsch ist?
Ich kenn mich noch nicht so gut damit aus
Kann mir jemand sage was an dieser Minimax funktion(aiset()) falsch ist?
Ich kenn mich noch nicht so gut damit aus
Code: Alles auswählen
from random import randint
from time import sleep
table = ["0", "1", "2",
"3", "4", "5",
"6", "7", "8"]
computer = ""
human = ""
turn = ""
def randlist(array):
return array[randint(0, len(array)-1)]
def isfree(board, pos):
return False if board[pos] == ["X", "O"] else True
def pickl():
global human
global computer
global turn
letter = ""
while not(letter in ["X", "O"]):
letter = input("Please pick X or O. ")
human = letter
computer = "X" if letter == "O" else "O"
turn = randlist([computer, human])
print("{} beginns! ".format(turn))
def setp(pos, p):
if not(table[pos] in ["X", "O"]):
table[pos] = p
else:
return False
def winning(board, player):
try:
for i in [0, 3, 6]:
if [board[i], board[i+1], board[i+2]].index(player) == 3:
return True
for i in [0, 1, 2]:
if [board[i], board[i+3], board[+6]].index(player) == 3:
return True
if [board[0], board[4], board[8]].index(player) == 3 or [board[2], board[4], board[6]].index(player) == 3:
return True
except ValueError:
return False
return False
def aiset(board, player):
global computer
global human
av = []
for i in range(len(board)):
if not(board[i] in ["O", "X"]):
av.append(i)
moves = []
if winning(board, human):
return {"score": -10}
elif winning(board, computer):
return {"score": +10}
elif len(av) == 0:
return {"score": 0}
for i in av:
move = {}
move["index"] = board[i]
board[i] = player
if player == human:
move["score"] = aiset(board, computer)["score"]
else:
move["score"] = aiset(board, human)["score"]
board[i] = move["index"]
moves.append(move)
bestMove = None
if player == computer:
bestScore = -1000000
for i in range(len(moves)):
if moves[i]["score"] > bestScore:
bestScore = moves[i]["score"]
bestMove = i
else:
bestScore = 100000
for i in range(len(moves)):
if moves[i]["score"] < bestScore:
bestScore = moves[i]["score"]
bestMove = i
return moves[bestMove]
def printtable():
global table
for i in [0, 3, 6]:
print("|"+table[i]+"|"+table[i+1]+"|"+table[i+2]+"|")
print("\n")
if __name__ == "__main__":
turnnum = 0
pickl()
game = True
while game:
if turnnum > 8:
game = "cats"
turn = ""
if turn == human:
turn = computer
printtable()
print("Its your turn: ")
position = int(input("Where would you like to set {}? ".format(human)))
while not(isfree(table, position)):
position = int(input("Where would you like to set {}? ".format(human)))
setp(position, human)
if winning(table, human):
game = human
elif turn == computer:
turn = human
print("Its AI's turn... ")
val = aiset(table.copy(), computer)
print(val)
setp(int(val["index"]), computer)
sleep(0.5)
printtable()
if winning(table, computer):
game = computer
turnnum += 1
if game == human:
print("You won! ")
elif game == computer:
print("You loose! ")
elif game == "cats":
print("its a tie! ")