Answered step by step
Verified Expert Solution
Question
1 Approved Answer
I need help setting up this table class using my domino175.py code which I have below. Your help is greatly appreciated. from queues import CircularQueuefrom
I need help setting up this table class using my domino175.py code which I have below. Your help is greatly appreciated.
from queues import CircularQueuefrom stack import Stackimport randomclass Domino: def __init__(self, dotsA, dotsB): assert isinstance(dotsA, int) and isinstance(dotsB, int), 'Error: Type error, must input int' assert (0 6) and (0 6), 'Error: Dots must be between 0 and 6' self.top = dotsA self.bottom = dotsB self.faceDown = False def setTop(self, dots): new_set = {self.top, self.bottom} if dots in new_set: if self.bottom > self.top: self.bottom = self.top self.top = dots else: raise AssertionError('Value not in domino') def turnOver(self): self.faceDown = True def getTop(self): return self.top def getBottom(self): return self.bottom def isFaceDown(self): return self.faceDown def __str__(self): string = '[{}|{}]' if not self.faceDown: string = string.format(self.bottom, self.top) else: string = string.format('?', '?') return stringclass DominoDeck: def __init__(self): self.__deck = CircularQueue(28) def populate(self, useFile): if useFile: found = False while not found: fileName = input('Name of file that should be used to populate the grid of dominoes:') try: lines = open(fileName, 'r') except FileNotFoundError: print('Cannot read from {}'.format(fileName)) else: found = True for line in lines: line = line.strip().split('/') try: self.__deck.enqueue(Domino(int(line[0]), int(line[1]))) except ValueError: print('Cannot populate deck: invalid data in {}'.format(fileName)) lines.close() if not self.__deck.isFull(): self.__deck.clear() raise Exception('Cannot populate deck: invalid data in {}'.format(fileName)) lines.close() elif not useFile: deck = set() while len(deck) != self.__deck.capacity(): deck.add((random.randint(0, 6), random.randint(0, 6))) for item in deck: self.__deck.enqueue(Domino(item[0], item[1])) else: raise AssertionError('Cannot populate deck: invalid argument provided') def deal(self): if self.__deck.isEmpty(): raise Exception('Cannot deal domino from empty deck') else: item = self.__deck.dequeue() item.turnOver() return item def isEmpty(self): return len(self.__deck.size()) == 0 def size(self): return self.__deck.size() def __str__(self): string = str(self.__deck) string = 'front-> ' + string[1:-1] return stringclass DominoStack(Stack): def peek(self): if not self.items: raise Exception('Error: cannot peek into an empty stack') else: return self.items[len(self.items) - 1].getTop() def push(self, domino): if not isinstance(domino, Domino): raise AssertionError('Can only push Dominoes onto the DominoStack') else: if self.isEmpty(): self.items.append(domino) elif self.peek() == domino.getBottom() or self.peek() == domino.getTop(): domino.setTop(domino.bottom) domino.bottom = self.peek() self.items.append(domino) else: raise Exception('Cannot play {} on stack'.format(str(domino))) def __str__(self): stackAsString = '' for item in self.items: stackAsString += str(item) + '-' stackAsString = stackAsString[:-1] return stackAsString
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started