def computerRoll(self, diceQueue, myBoard): Interface.printMsg("The Computer is rolling...") time.sleep(1) if diceQueue: diceResults = diceQueue[0][0] + diceQueue[0][1] diceQueue.pop(0) else: if self.willRollSingleDie(myBoard): diceResults = self.rollSingleDie() else: diceResults = self.rollDice() message = "The Computer rolled " + repr(diceResults) Interface.printMsg(message) return diceResults
def humanRoll(self, diceQueue, myBoard): Interface.printMsg("You are rolling...") time.sleep(1) if diceQueue: diceResults = diceQueue[0][0] + diceQueue[0][1] diceQueue.pop(0) else: if self.canRollSingleDie(myBoard) and Interface.validateBoolean( "Would you like to roll one die? (y/n): "): diceResults = self.rollSingleDie() else: diceResults = self.rollDice() message = "You rolled " + repr(diceResults) Interface.printMsg(message) return diceResults
def applyHandicap(self, handicapSquare): if self.returnList[0] and self.humanIsFirstPlayer: Interface.printMsg("The Computer gets a handicap.") self.board.computerBoard[handicapSquare-1] = "*" if self.returnList[0] and not self.humanIsFirstPlayer: Interface.printMsg("You get a handicap.") self.board.humanBoard[handicapSquare-1] = "*" if not self.returnList[0] and not self.humanIsFirstPlayer: Interface.printMsg("You get a handicap.") self.board.humanBoard[handicapSquare-1] = "*" if not self.returnList[0] and self.humanIsFirstPlayer: Interface.printMsg("The Computer gets a handicap.") self.board.computerBoard[handicapSquare-1] = "*"
def playTurn(self, board, diceQueue, turnCount, returnList): board.displayBoard() diceResults = self.humanRoll(diceQueue, board.humanBoard) coverMenu = self.generateCoverMenu(board.humanBoard, diceResults) uncoverMenu = self.generateUncoverMenu(board.computerBoard, diceResults) if returnList[2] != 0: self.removeHandicap(returnList, uncoverMenu) if not self.canMove(coverMenu, uncoverMenu): Interface.printMsg("There are no more moves.") return False self.displayMenu(coverMenu, uncoverMenu) choice = Interface.validateRange("Please choose an option: ", 1, (len(coverMenu)+len(uncoverMenu)+1)) choice -= 1 while(choice == (len(coverMenu)+len(uncoverMenu))): self.logicString = "" self.getHelp(board.humanBoard, board.computerBoard, coverMenu, uncoverMenu, turnCount) self.displayMenu(coverMenu, uncoverMenu) choice = Interface.validateRange("Please choose an option: ", 1, (len(coverMenu)+len(uncoverMenu)+1)) choice -= 1 if choice < len(coverMenu): willCover = True else: willCover = False self.executeChoice(board.humanBoard, board.computerBoard, coverMenu, uncoverMenu, choice, willCover) if self.checkForWin(board.humanBoard, board.computerBoard, turnCount): return True else: return self.playTurn(board, diceQueue, turnCount, returnList)
def playTurn(self, board, diceQueue, turnCount, returnList): board.displayBoard() diceResults = self.computerRoll(diceQueue, board.computerBoard) coverMenu = self.generateCoverMenu(board.computerBoard, diceResults) uncoverMenu = self.generateUncoverMenu(board.humanBoard, diceResults) if returnList[2] != 0: self.removeHandicap(returnList, uncoverMenu) # Returns false if human is next if not self.canMove(coverMenu, uncoverMenu): Interface.printMsg("There are no more moves.") return False willCover = self.willCover(board.computerBoard, board.humanBoard, coverMenu, uncoverMenu, turnCount) choice = self.chooseBestOption(board.computerBoard, board.humanBoard, coverMenu, uncoverMenu, willCover) self.executeChoice(board.computerBoard, board.humanBoard, coverMenu, uncoverMenu, choice, willCover) mergedMenu = coverMenu + uncoverMenu self.logicString = (self.getChoiceString(mergedMenu[choice]) + self.logicString) prefix = "The Computer chose to " if willCover: prefix += "cover " else: prefix += "uncover " self.printLogic(prefix) self.logicString = "" # Returns true if the computer has won the round if self.checkForWin(board.computerBoard, board.humanBoard, turnCount): return True else: return self.playTurn(board, diceQueue, turnCount, returnList)
def saveSequence(self): willSave = Interface.validateBoolean("Would you like to save and quit? (y/n): ") if not willSave: return saveFile = open("savedData.txt", 'w') Interface.printMsg("Saving data...") time.sleep(1) saveFile.write("Computer:\n") saveFile.write("\tSquares: " + self.getBoardString(self.board.computerBoard) + "\n") saveFile.write("\tScore: " + repr(self.computer.score) + "\n") saveFile.write("\n") saveFile.write("Human:\n") saveFile.write("\tSquares: " + self.getBoardString(self.board.humanBoard) + "\n") saveFile.write("\tScore: " + repr(self.human.score) + "\n") saveFile.write("\n") firstString = "First Turn: " if self.humanIsFirstPlayer: firstString += "Human\n" else: firstString += "Computer\n" nextString = "Next Turn: " if self.humanIsNext: nextString += "Human\n" else: nextString += "Computer\n" saveFile.write(firstString) saveFile.write(nextString) saveFile.write("\n") saveFile.write("Dice: ") Interface.printMsg("Game saved.") self.terminate()
def winSequence(self): if self.humanWon: Interface.printMsg("You won the round!") if len(self.board.humanBoard) == self.board.humanBoard.count("*"): score = self.computeScoreCover(self.board.computerBoard) self.human.score += score Interface.printMsg("You have been awarded " + repr(score) + " points!") else: score = self.computeScoreUncover(self.board.humanBoard) self.human.score += score Interface.printMsg("You have been awarded " + repr(score) + " points!") else: Interface.printMsg("The Computer won the round!") if len(self.board.computerBoard) == self.board.computerBoard.count("*"): score = self.computeScoreCover(self.board.humanBoard) self.computer.score += score Interface.printMsg("The Computer has been awarded " + repr(score) + " points!") else: score = self.computeScoreUncover(self.board.computerBoard) self.computer.score += score Interface.printMsg("The Computer has been awarded " + repr(score) + " points!") self.winningScore = score self.returnList[0] = self.humanWon self.returnList[1] = self.winningScore
def chooseFirstPlayer(self): if self.diceQueue: Interface.printMsg("You are rolling...") humanRoll = self.diceQueue[0][0] + self.diceQueue[0][1] self.diceQueue.pop(0) Interface.printMsg("You rolled " + repr(humanRoll)) Interface.printMsg("The Computer is rolling...") computerRoll = self.diceQueue[0][0] + self.diceQueue[0][1] Interface.printMsg("The Computer rolled " + repr(computerRoll)) self.diceQueue.pop(0) else: Interface.printMsg("You are rolling...") humanRoll = self.human.rollDice() Interface.printMsg("You rolled " + repr(humanRoll)) Interface.printMsg("The Computer is rolling...") computerRoll = self.computer.rollDice() Interface.printMsg("The Computer rolled " + repr(computerRoll)) if humanRoll > computerRoll: Interface.printMsg("You go first!") self.humanIsFirstPlayer = 1 self.humanIsNext = 1 elif humanRoll < computerRoll: Interface.printMsg("The Computer goes first!") self.humanIsFirstPlayer = 0 self.humanIsNext = 0 else: Interface.printMsg("It was a tie!") self.chooseFirstPlayer()
def printLogic(self, prefix): fullString = prefix + self.logicString Interface.printMsg(fullString)