Question: COMPLETE THE FOLLOWING PROGRAM: # Playing the Game For this assignment, you will implement some new methods on your `GameTree` class so that it can

COMPLETE THE FOLLOWING PROGRAM:

# Playing the Game

For this assignment, you will implement some new methods on your `GameTree` class so that it can determine who will win a game. We say that **victory is ensured** in a game if there is a way to play so that no matter what the other player does, you will still win. This is the same as saying that you can make a move so that no matter what the other player does you can make a move so that no matter what the other player does you can make a move so that... until you win.

There are two main methods to implement:

- `currentwins` - returns `True` if and only if victory is ensured for the current player.

- `currentloses` - returns `True` if and only if every move the current player can make leads to a game state where victory is ensured for the other player.

This will be a slightly odd kind of traversal of the GameTree because it will alternate between the two players. The logic for `currentwins` and `currentloses` will be different (and opposite). In both cases, you will want to first check for a draw and then check if the game is over (base cases!).

nim.py:

class Nim:

def __init__(self, rows = [2,3,4]): self.rows = rows

def isover(self): sum = 0 for i in self.rows: sum += i if sum > 1: return False return True

def moves(self): self.L = [] for x in range (len(self.rows)): if self.rows[x] > 0: for i in range(self.rows[x]): self.rows[x] -= i + 1 row_1 = list(self.rows) self.L.append(Nim(list(row_1))) self.rows[x] += i + 1 return self.L

def draw(self): return False

# This way, we will be able to have ordered trees. def __lt__(self, other): self.rows < other.rows

# We add a hash function so that we can have a set of Nim GameStates. def __hash__(self): return hash(tuple(self.rows))

def __eq__(self, other): return self.rows == other.rows

gametree.py:

class GameTree: def __init__(self, gamestate): self.child= [] self.gamestate = gamestate gamestate.moves() self.child = [GameTree(e) for e in gamestate.moves()]

def currentwins(self): pass

def currentloses(self): pass

def currentdraws(self): return not self.currentwins() and not self.currentloses()

def __eq__(self, other): return (self.game, self.moves) == (other.game, other.moves)

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!