Question: Problem description: The halving game Start with a number N Players take turns either decrementing N ( hint: N - 1 ) or replacing it
Problem description: The halving game
Start with a number N
Players take turns either decrementing N hint: N or replacing it with hint: N
The player that is left with wins
Requirement:
Write python codes to implement minimax policy.
Let the player use human policy and the opponent use minimax policy
Let both player and opponent use minimax policy
Hint:
Sample code has been provided in game.py Simply complete the recurse method in
minimaxPolicygame state
Code:
class HalvingGameobject:
def initself N:
self.N N
# state player number where player agent or opponent
def startStateself:
return self.N
def actionsself state:
return
def succself state, action:
player, number state
if action:
return player, number
elif action:
return player, number
def isEndself state:
player, number state
return number
def utilityself state:
assert self.isEndstate
player, number state
return player floatinf
def playerself state:
player, number state
return player
# policies
def simplePolicygame state:
action
printsimplePolicy: playing formataction
return action
def humanPolicygame state:
while True:
action inputhumanPolicy: please input action:
if action in game.actionsstate:
return action
def minimaxPolicygame state:
# recursively find utility bestAction
def recursestate:
# implement here
value, action recursestate
printminimaxPolicy: action value formataction value
return action
# main loop
policies
#: humanPolicy,
: minimaxPolicy,
: minimaxPolicy
game HalvingGame
state game.startState
while not game.isEndstate:
print 'state formatstate
player game.playerstate
policy policiesplayer
action policygame state
state game.succstate action
printutility formatgameutilitystate
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
