Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Design And Implementation

Authors: Edward Sciore

2nd Edition

3030338355, 978-3030338350

More Books

Students also viewed these Databases questions

Question

=+2. How should reference providers be selected?

Answered: 1 week ago

Question

What do Dimensions represent in OLAP Cubes?

Answered: 1 week ago