Question: need help with python. part 3 class Nim: def _ _ init _ _ ( self , piles, stones, limit ) : self.piles = piles

need help with python. part 3
class Nim:
def __init__(self, piles, stones, limit):
self.piles = piles
self.stones = stones
self.limit = limit
self.winner = None
self.turns =0
self.cur_player =1
self.board =[stones]* piles
def display(self):
print(f"Current Turn: {self.turns}
Current Player: {self.cur_player}
Piles: {self.board}")
def copy(self):
new_node = Nim(self.piles, self.stones, self.limit)
new_node.winner = self.winner
new_node.turns = self.turns
new_node.cur_player = self.cur_player
new_node.board = self.board.copy()
return new_node
def check_for_win(self):
if all(pile ==0 for pile in self.board):
self.winner = self.cur_player
def get_actions(self):
actions =[]
for pile_index, stones_in_pile in enumerate(self.board):
max_stones = min(stones_in_pile, self.limit)
for stones_to_take in range(1, max_stones +1):
actions.append((pile_index, stones_to_take))
return actions
def take_action(self, a):
new_node = self.copy()
pile_index, stones_removed = a
new_node.board[pile_index]-= stones_removed
new_node.turns +=1
new_node.cur_player =2 if self.cur_player ==1 else 1
new_node.check_for_win()
return new_node
def get_state(self):
s =0
for i in range(self.piles):
s += self.board[i]*(self.stones +1)** i
return s
def heuristic(self, agent, mode=None):
return 0
Part 3: Human vs AI Agents
In Part 3, you will actually play games against the RandomPlayer and MininmaxPlayer agents.
________________________________________
3.A - Human vs Random
Create an instance of Nim with 3 piles, 9 stones per pile, and with a limit of 5 stones per action. Then create an instance of RandomPlayer and an instance of HumanPlayer. When creating the HumanPlayer, set clear=False.
Then use play_game() to play a game against the RandomPlayer agent. Set display_flags='aws' when calling play_game(). You can select which player plays first. When selecting actions, you can enter your actions as a tuple of the form (pile_index, number_of_stones). You can also ommit the parentheses and the action will still be accepted.
You are welcome to play a few games against this agent, but your final submission should demonstrate a game in which you defeat the RandomPlayer agent. This should not require you to learn any deep strategy about playing Nim, and should not be that difficult to do. If you are having difficulties defeating the random player, then I would suggest playing defensively, removing only a few stones at a time and waiting until you have an opportunity to force the random player to take the last stone.
________________________________________
________________________________________
3.B - Human vs Minimax
Create an instance of Nim with 3 piles, 9 stones per pile, and with a limit of 5 stones per action. Then create an instance of MinimaxPlayer with depth=5 and an instance of HumanPlayer. When creating the HumanPlayer, set clear=False.
Then use play_game() to play a game against the MinimaxPlayer agent. Set display_flags='aws' when calling play_game(). You can select which player plays first.
You do not need to win this game (but you are encouraged to try).

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!