Example #1
0
class App(QWidget):
    """
    This class is intended to contain other classes and functions that are
    relevant to running the application on a high level. Primarily, it
    initializes the game, and contains that functions that respond to user
    interaction with the UI during gameplay, without actually altering the UI.
    """
    def __init__(self):
        """Creates the game object as well as the UI, and displays it."""
        super().__init__()

        # Create the game model object
        self.tictactoe = TicTacToe()
        self.turn = self.tictactoe.player1  # player1 goes first

        # Load the UI
        self.win = view.Magic(self)
        self.win.show()

    def switch_turns(self):
        """Switches the active player."""
        # TODO: This can be a one-liner, too lazy to do it now. Somehow it's
        # easier to write a comment about something that would take 10 seconds
        # to do than actually doing it
        if self.turn == self.tictactoe.player1:
            self.turn = self.tictactoe.player2
        else:
            self.turn = self.tictactoe.player1

    def play_round(self, tile):
        """
        This function plays a single round of the game. It is called when a
        tile is pressed by the user, and essentially just calls tests to
        determine the current status of the game, and takes action accordingly.

        :param tile: A Tile object, which most important has a player attribute
        """
        QApplication.setOverrideCursor(Qt.WaitCursor)  # waiting cursor
        status = self.tictactoe.play(tile, self.turn)
        QApplication.restoreOverrideCursor()  # cursor is back to normal

        # If the game has ended, display a message with the result
        if status != 0:
            if status == 1:  # player has won the game
                QMessageBox.information(self, self.tr("Victory!"),
                                        self.tr(self.turn.symbol + " won :)"),
                                        QMessageBox.Ok)
            elif status == 2:  # there is a tie
                QMessageBox.warning(self, self.tr("Tie!"),
                                    self.tr("You tied"), QMessageBox.Ok)
            else:  # something impossible happened
                QMessageBox.critical(self, self.tr("Error"),
                                     self.tr("You should never see this"),
                                     QMessageBox.Ok)
            # Restart the game
            self.tictactoe.reset()
            self.turn == self.tictactoe.player1
        else:
            self.switch_turns()  # switch turns
Example #2
0
    def __init__(self):
        """Creates the game object as well as the UI, and displays it."""
        super().__init__()

        # Create the game model object
        self.tictactoe = TicTacToe()
        self.turn = self.tictactoe.player1  # player1 goes first

        # Load the UI
        self.win = view.Magic(self)
        self.win.show()
Example #3
0
    def test_init(self):
        ttt = TicTacToe()
        self.assertEqual(len(ttt.board), 3)
        self.assertEqual(len(ttt.board[0]), 3)
        self.assertEqual(ttt.board[0][0], None)
        self.assertEqual(ttt.board[2][2], None)

        ttt = TicTacToe(markers=('*', '#'))
        ttt.make_move(0, 2)
        ttt.make_move(1, 6)
        self.assertEqual(ttt.render_row(0), ' 1 | * | 3 \n')
        self.assertEqual(ttt.render_row(1), ' 4 | 5 | # \n')
Example #4
0
    def test_render_board(self):
        ttt = TicTacToe()
        self.assertEqual(
            ttt.render_board(), ' 1 | 2 | 3 \n' + '-----------\n' +
            ' 4 | 5 | 6 \n' + '-----------\n' + ' 7 | 8 | 9 \n')

        ttt = TicTacToe(n=4)
        self.assertEqual(
            ttt.render_board(), ' 1  | 2  | 3  | 4  \n' +
            '-------------------\n' + ' 5  | 6  | 7  | 8  \n' +
            '-------------------\n' + ' 9  | 10 | 11 | 12 \n' +
            '-------------------\n' + ' 13 | 14 | 15 | 16 \n')
Example #5
0
    def test_is_stalemate(self):
        ttt = TicTacToe()
        ttt.make_move(0, 2)
        ttt.make_move(1, 6)
        ttt.make_move(0, 8)
        ttt.make_move(1, 5)
        ttt.make_move(0, 4)
        self.assertFalse(ttt.is_stalemate())
        ttt.make_move(1, 7)
        ttt.make_move(0, 3)
        ttt.make_move(1, 1)
        ttt.make_move(0, 9)
        self.assertTrue(ttt.is_stalemate())

        # is_stalemate() should return True as long as no valid moves are possible, even if there is a winner
        ttt = TicTacToe()
        ttt.make_move(0, 1)
        ttt.make_move(1, 6)
        ttt.make_move(0, 2)
        ttt.make_move(1, 7)
        ttt.make_move(0, 3)
        ttt.make_move(1, 8)
        ttt.make_move(0, 4)
        ttt.make_move(1, 9)
        ttt.make_move(0, 5)
        self.assertTrue(ttt.is_stalemate())
Example #6
0
    def test_is_valid_move(self):
        ttt = TicTacToe()
        self.assertTrue(ttt.is_valid_move(1))
        self.assertTrue(ttt.is_valid_move(2))
        self.assertTrue(ttt.is_valid_move(3))
        self.assertTrue(ttt.is_valid_move(4))
        self.assertTrue(ttt.is_valid_move(9))

        ttt = TicTacToe()
        ttt.make_move(0, 2)
        ttt.make_move(1, 6)
        ttt.make_move(0, 8)
        ttt.make_move(1, 5)
        ttt.make_move(0, 4)
        self.assertTrue(ttt.is_valid_move(1))
        self.assertFalse(ttt.is_valid_move(2))
        self.assertTrue(ttt.is_valid_move(3))
        self.assertFalse(ttt.is_valid_move(4))
        self.assertTrue(ttt.is_valid_move(9))
        ttt.make_move(1, 7)
        ttt.make_move(0, 3)
        ttt.make_move(1, 1)
        ttt.make_move(0, 9)
        self.assertFalse(ttt.is_valid_move(1))
        self.assertFalse(ttt.is_valid_move(2))
        self.assertFalse(ttt.is_valid_move(3))
        self.assertFalse(ttt.is_valid_move(4))
        self.assertFalse(ttt.is_valid_move(9))

        ttt = TicTacToe(n=1)
        self.assertTrue(ttt.is_valid_move(1))
        ttt.make_move(0, 1)
        self.assertFalse(ttt.is_valid_move(1))
Example #7
0
 def test_make_move(self):
     ttt = TicTacToe()
     ttt.make_move(0, 2)
     ttt.make_move(1, 6)
     self.assertEqual(ttt.render_row(0), ' 1 | X | 3 \n')
     self.assertEqual(ttt.render_row(1), ' 4 | 5 | O \n')
Example #8
0
    def test_render_row(self):
        ttt = TicTacToe()
        self.assertEqual(ttt.render_row(0), ' 1 | 2 | 3 \n')
        self.assertEqual(ttt.render_row(1), ' 4 | 5 | 6 \n')
        self.assertEqual(ttt.render_row(2), ' 7 | 8 | 9 \n')

        ttt = TicTacToe(n=5)
        self.assertEqual(ttt.render_row(0), ' 1  | 2  | 3  | 4  | 5  \n')
        self.assertEqual(ttt.render_row(1), ' 6  | 7  | 8  | 9  | 10 \n')
        self.assertEqual(ttt.render_row(2), ' 11 | 12 | 13 | 14 | 15 \n')
        self.assertEqual(ttt.render_row(4), ' 21 | 22 | 23 | 24 | 25 \n')

        ttt = TicTacToe(n=10)
        self.assertEqual(
            ttt.render_row(0),
            ' 1   | 2   | 3   | 4   | 5   | 6   | 7   | 8   | 9   | 10  \n')
        self.assertEqual(
            ttt.render_row(5),
            ' 51  | 52  | 53  | 54  | 55  | 56  | 57  | 58  | 59  | 60  \n')
Example #9
0
    def test_get_winner(self):
        # Test no winner
        ttt = TicTacToe()
        ttt.make_move(0, 2)
        ttt.make_move(1, 6)
        ttt.make_move(0, 8)
        ttt.make_move(1, 5)
        ttt.make_move(0, 4)
        ttt.make_move(1, 7)
        ttt.make_move(0, 3)
        ttt.make_move(1, 1)
        ttt.make_move(0, 9)
        self.assertEqual(ttt.get_winner(), None)

        # Test winning column
        ttt = TicTacToe()
        ttt.make_move(0, 3)
        ttt.make_move(1, 5)
        ttt.make_move(0, 6)
        ttt.make_move(1, 1)
        ttt.make_move(0, 9)
        self.assertEqual(ttt.get_winner(), 0)

        # Test winning row
        ttt = TicTacToe()
        ttt.make_move(0, 2)
        ttt.make_move(1, 6)
        ttt.make_move(0, 8)
        ttt.make_move(1, 5)
        ttt.make_move(0, 7)
        ttt.make_move(1, 4)
        self.assertEqual(ttt.get_winner(), 1)

        # Test winning diagonal
        ttt = TicTacToe()
        ttt.make_move(0, 3)
        ttt.make_move(1, 8)
        ttt.make_move(0, 5)
        ttt.make_move(1, 9)
        ttt.make_move(0, 7)
        self.assertEqual(ttt.get_winner(), 0)

        ttt = TicTacToe(n=1)
        self.assertEqual(ttt.get_winner(), None)
        ttt.make_move(0, 1)
        self.assertEqual(ttt.get_winner(), 0)
Example #10
0
def create_game():
    return TicTacToe()
Example #11
0
def create_player():
    return TicTacToe.Player('x')
Example #12
0
 def setUp(self):
     self.ticTacToe = TicTacToe()
Example #13
0
class TicTacToeTest(unittest.TestCase):
    def setUp(self):
        self.ticTacToe = TicTacToe()

    def testInitializeGame(self):
        pass

    def test_FirstMove_picksHighestChanceOfVictory(self):
        currentBoard = ""
        self.ticTacToe.moves = {
            "START": {
                "1": {
                    "failures": 500,
                    "victories": 1000,
                    "draws": 2000
                },
                "2": {
                    "failures": 1000,
                    "victories": 300,
                    "draws": 2000
                },
                "3": {
                    "failures": 500,
                    "victories": 1000,
                    "draws": 2000
                },
                "4": {
                    "failures": 500,
                    "victories": 1000,
                    "draws": 2000
                },
                "5": {
                    "failures": 500,
                    "victories": 1000,
                    "draws": 2000
                },
                "6": {
                    "failures": 500,
                    "victories": 1000,
                    "draws": 2000
                },
                "7": {
                    "failures": 500,
                    "victories": 1000,
                    "draws": 2000
                },
                "8": {
                    "failures": 500,
                    "victories": 1000,
                    "draws": 2000
                },
                "9": {
                    "failures": 200,
                    "victories": 8000,
                    "draws": 2000
                }
            }
        }
        nextMove = self.ticTacToe.nextMove(currentBoard)
        self.assertEquals("9", nextMove,
                          "first player puts an X on bottom right box")

    def test_FirstMove_picksHighestChanceOfVictoryOrLowerChanceOfFailure(self):
        currentBoard = ""
        self.ticTacToe.moves = {
            "START": {
                "1": {
                    "failures": 500,
                    "victories": 1000,
                    "draws": 2000
                },
                "2": {
                    "failures": 1000,
                    "victories": 300,
                    "draws": 2000
                },
                "3": {
                    "failures": 100,
                    "victories": 1000,
                    "draws": 2000
                },
                "4": {
                    "failures": 500,
                    "victories": 1000,
                    "draws": 2000
                },
                "5": {
                    "failures": 500,
                    "victories": 1000,
                    "draws": 2000
                },
                "6": {
                    "failures": 500,
                    "victories": 1000,
                    "draws": 2000
                },
                "7": {
                    "failures": 500,
                    "victories": 1000,
                    "draws": 2000
                },
                "8": {
                    "failures": 500,
                    "victories": 1000,
                    "draws": 2000
                },
                "9": {
                    "failures": 200,
                    "victories": 1000,
                    "draws": 2000
                }
            }
        }
        nextMove = self.ticTacToe.nextMove(currentBoard)
        self.assertEquals("3", nextMove,
                          "first player puts an X on bottom right box")

    def test_SecondMove_picksHighestChanceOfVictoryOrLowerChanceOfFailure(
            self):
        currentBoard = "3"
        self.ticTacToe.moves = {
            "3": {
                "31": {
                    "failures": 500,
                    "victories": 1000,
                    "draws": 2000
                },
                "32": {
                    "failures": 1000,
                    "victories": 300,
                    "draws": 2000
                },
                "34": {
                    "failures": 100,
                    "victories": 1000,
                    "draws": 2000
                },
                "35": {
                    "failures": 500,
                    "victories": 1000,
                    "draws": 2000
                },
                "36": {
                    "failures": 500,
                    "victories": 1000,
                    "draws": 2000
                },
                "37": {
                    "failures": 500,
                    "victories": 1000,
                    "draws": 2000
                },
                "38": {
                    "failures": 500,
                    "victories": 1000,
                    "draws": 2000
                },
                "39": {
                    "failures": 200,
                    "victories": 1000,
                    "draws": 2000
                }
            }
        }

        nextMove = self.ticTacToe.nextMove(currentBoard)
        self.assertEquals("34", nextMove,
                          "second player puts and X on top right box")
Example #14
0
def tictactoe():
    return TicTacToe()