Beispiel #1
0
class Tests(object):

    def __init__(self, width, height, checkersToWin):
        self.width = width
        self.height = height
        self.checkersToWin = 3

        self.board = Board(width, height)
        self.boardView = BoardView(6)

    def runTests(self):
        print("Running tests...")
        self._checkVerticals()
        self._checkHorizontals()
        self._checkPositiveDiagonals()
        self._checkNegativeDiagonals()
        self._checkForCatsGame()
        self.board.reset()

    def _checkHorizontals(self):
        
        print("Checking horizontal win states")
        for r in range(0, self.height):
            self.board.reset()
            for c in range(0, self.width):
                self.board.checkers[c][r] = Checker(True)
            self.boardView.showBoard(self.board)

            self._checkWinState()

    def _checkVerticals(self):

        print("Checking vertical win states")

        for c in range(0, self.width):
            self.board.reset()
            for r in range(0, self.height):
                self.board.checkers[c][r] = Checker(True)
            self._checkWinState()
            self.boardView.showBoard(self.board)

    def _checkPositiveDiagonals(self):

        print("Checking positive diaganol win states")

        for c in range(0, self.width):
            for r in range(0, self.height):

                self.board.reset()
                if (c + self.checkersToWin -1  < self.width):
                    if (r + self.checkersToWin - 1 < self.height):
                        for n in range(0, self.checkersToWin):
                            self.board.setChecker(c+n, r+n, True)
                        self._checkWinState()
                        self.boardView.showBoard(self.board)

    def _checkNegativeDiagonals(self):

        print("Checking negative diaganol win states")

        for c in range(0, self.width):
            for r in range(0, self.height):

                self.board.reset()
                if (c + self.checkersToWin  <= self.width):
                    if (1 + r - self.checkersToWin >= 0):

                        for n in range(0, self.checkersToWin):
                            self.board.setChecker(c+n, r -n, True)
                        self._checkWinState()
                        self.boardView.showBoard(self.board)
    
    def _checkForCatsGame(self):

        print("Checking if cats game is detected")
        pieces = [(0,0, True), (1,0, True), (2, 0, False), 
                  (0, 1, False), (1,1, False), (2, 1, True),
                  (0,2, True), (1, 2 ,False), (2, 2, True)]

        for p in range(0, len(pieces)):
            column, row, color = pieces[p]
            self.board.setChecker(column, row, color)


        cats = self.board.isCatsGame(self.checkersToWin)

        if (cats) :
            print("* Pass")
        else :
            print("Fail")
  
    def _checkWinState(self):
        hasWin = self.board.hasWinner(3)
        if (hasWin > -1):
            print("* Pass")
        else :
            print("Fail")
class TicTacToe(object):
    def __init__(self):
        rows = 3
        columns = 3
        cellSize = 6
        self.checkersToWin = 3
        self.humanIsBlack = False
        self.humanTurn = True
        self.board = Board(rows, columns)
        self.boardView = BoardView(cellSize)

        print ("Welcome to Tic Tac Toe :)")

        t = Tests(rows, columns, self.checkersToWin)
        t.runTests()

        print ("*** Game Start ***")
        self._gameLoop()

    # responsible for running the game
    # gives turn to the approproate player
    # checks state after turn is completed
    def _gameLoop(self):

        if self.humanTurn:
            self._humanTurn()
        else:
            self._computerTurn()

        self.boardView.showBoard(self.board)

        # check for winning state
        winner = self.board.hasWinner(self.checkersToWin)
        if winner != -1:
            self._handleGameOver(winner)
            return

        if self.board.isCatsGame(self.checkersToWin):
            print ("Cat's game: ")
            print (cat())
            return

        self.humanTurn = not self.humanTurn
        self._gameLoop()

    # handles game over
    def _handleGameOver(self, winner):
        # there is a winner
        self.boardView.showBoard(self.board)
        if winner == 1:
            print ("You won! ")
        else:
            print ("You lost :( ... Better luck next time!")

    # human turn logic

    def _humanTurn(self):
        print ("It's your turn. You are O's!")
        self.boardView.showBoard(self.board)
        self._requestHumanMove()

    def _requestHumanMove(self):
        column = self._getInputInRange("Enter column for you move: ", self.board.height)
        row = self._getInputInRange("Enter row for you move: ", self.board.height)

        if self.board.isEmptyAtColumnRow(column, row):
            self.board.setChecker(column, row, self.humanIsBlack)
        else:
            print ("Looks like the square at column: " + str(column) + ", row: " + str(row) + " is occupied!")
            print ("Please pick a different location!")
            self._requestHumanMove()

    def _getInputInRange(self, string, range):
        while True:
            try:
                row = int(raw_input(string))
                if row >= range:
                    print (str(row) + " is not less than " + str(range))
                    return self._getInputInRange(string, range)

                return row
            except ValueError:

                print "Whoops that doesn't look like a valid number.  Try again..."

    # computer turn logic

    def _computerTurn(self):
        print ("It's the computer's turn!")
        column, row = getAIMoveTuple(self.board, self.checkersToWin, not self.humanIsBlack)
        self.board.setChecker(column, row, not self.humanIsBlack)
        print ("The computer played at column: " + str(column) + " and at row: " + str(row))