wins 100% with optimal strategy
This commit is contained in:
8
nimm.py
8
nimm.py
@ -13,7 +13,7 @@ def optimal_move():
|
||||
return n
|
||||
return random.randint(1, 3) # Wenn keine perfekte Wahl, dann irgendein Zug
|
||||
|
||||
def ki_move():
|
||||
def ki_move(lastMove):
|
||||
global state
|
||||
antiMoves = lostMoves.get(state)
|
||||
if antiMoves == None:
|
||||
@ -24,6 +24,9 @@ def ki_move():
|
||||
for i, good in enumerate(availableMoves):
|
||||
if good:
|
||||
return i + 1
|
||||
|
||||
# Es gibt keine guten Züge mehr, also muss der letzte Zug schlecht gewesen sein.
|
||||
addLostMove(lastMove.state, lastMove.move)
|
||||
return random.randint(1, 3)
|
||||
|
||||
def makeMove(move): # gibt True zurück, wenn der Spieler verloren hat
|
||||
@ -39,6 +42,7 @@ def addLostMove(state, move):
|
||||
if moves == None:
|
||||
moves = []
|
||||
moves.append(move)
|
||||
lostMoves[state] = moves
|
||||
|
||||
def game(train):
|
||||
global state
|
||||
@ -46,7 +50,7 @@ def game(train):
|
||||
lastMove = None
|
||||
while True:
|
||||
# KI beginnt, sonst kann sie nicht gewinnen
|
||||
move = ki_move()
|
||||
move = ki_move(lastMove)
|
||||
lost = makeMove(move)
|
||||
if lost:
|
||||
if train:
|
||||
|
||||
Reference in New Issue
Block a user