Пример #1
0
    def test_initalizer(self):
        """ Test the initialization of the game """

        game = morpion.Morpion(size=2)

        self.assertEqual(game.matrix, [
            [None, None],
            [None, None],
        ])

        game = morpion.Morpion(size=3)

        self.assertEqual(game.matrix, [
            [None, None, None],
            [None, None, None],
            [None, None, None],
        ])
Пример #2
0
    def test_iter_line(self):
        """ test itering on each line of the game """

        game = morpion.Morpion(size=3)

        game.matrix = [
            [0, None, 1],
            [None, 1, 0],
            [None, None, 0],
        ]

        self.assertEqual(game.matrix, [line for line in game.iter_line()])
Пример #3
0
    def test_iter_diagonal(self):
        """ test itering on each diagonal of the game """

        game = morpion.Morpion(size=3)

        game.matrix = [
            [0, None, 0],
            [None, 1, 1],
            [None, None, 0],
        ]

        self.assertEqual([
            [0, 1, 0],
            [None, 1, 0],
        ], [diag for diag in game.iter_diagonal()])
Пример #4
0
    def test_iter_column(self):
        """ test itering on each line of the game """

        game = morpion.Morpion(size=3)

        game.matrix = [
            [0, None, 0],
            [None, 1, 1],
            [None, None, 0],
        ]

        self.assertEqual([
            [0, None, None],
            [None, 1, None],
            [0, 1, 0],
        ], [col for col in game.iter_column()])
Пример #5
0
    def test_check_victory(self):
        """ Check if someone have gain the game """

        game = morpion.Morpion(size=3)
        self.assertEqual(game.winner(), None)

        game.matrix = [
            [0, None, None],
            [None, 1, None],
            [None, None, 0],
        ]
        self.assertEqual(game.winner(), None)

        game.matrix = [
            [0, None, None],
            [1, 1, 1],
            [None, None, 0],
        ]
        self.assertEqual(game.winner(), 1)

        game.matrix = [
            [0, None, None],
            [1, 0, 1],
            [None, None, 0],
        ]
        self.assertEqual(game.winner(), 0)

        game.matrix = [
            [0, 1, 0],
            [0, 1, 1],
            [1, 0, 0],
        ]
        self.assertEqual(game.winner(), None)

        game.matrix = [
            [0, 1, 0],
            [0, 1, 1],
            [1, 1, 0],
        ]
        self.assertEqual(game.winner(), 1)

        game.matrix = [
            [0, 1, 0],
            [0, 0, 1],
            [1, 1, 0],
        ]
        self.assertEqual(game.winner(), 0)
Пример #6
0
    def test_reset(self):
        """ Test reinitialize the game """

        game = morpion.Morpion(size=3)

        game.matrix = [
            [0, None, None],
            [None, 1, None],
            [None, None, 0],
        ]

        game.reset()
        self.assertEqual(game.matrix, [
            [None, None, None],
            [None, None, None],
            [None, None, None],
        ])
Пример #7
0
    def test_play(self):
        """ Test to place an element on the grid """

        game = morpion.Morpion(size=3)

        self.assertTrue(game.play(1, 1, 1))

        self.assertEqual(game.matrix, [
            [None, None, None],
            [None, 1, None],
            [None, None, None],
        ])

        self.assertFalse(game.play(1, 1, 1))
        self.assertFalse(game.play(1, 3, 1))
        self.assertFalse(game.play(3, 1, 1))

        self.assertEqual(game.matrix, [
            [None, None, None],
            [None, 1, None],
            [None, None, None],
        ])
Пример #8
0
    def on_pubmsg(self, msg):
        if msg.key not in self.helper:
            return

        chan_name = msg.chan.name

        if not msg.value.strip():
            self.privmsg(chan_name, self.helper[msg.key])
            return

        svalue = msg.value.split(" ", 1)
        if svalue[0].lower() == "start":
            size = 3
            if len(svalue) == 2:
                try:
                    size = int(svalue[1])
                except ValueError:
                    size = -1

            if size <= 0:
                self.privmsg(chan_name, "Invalid size of the morpion")
                return

            self.morpions[chan_name] = morpion.Morpion(size)
            self.privmsg(chan_name, "New morpion start (size=%s)" % size)
            return

        if chan_name not in self.morpions:
            self.privmsg(
                chan_name,
                "No morpion game started. Use !morpion start [size=3] to start a game"
            )
            return

        if len(svalue) != 2:
            self.privmsg(chan_name, self.helper[msg.hey])
            return

        self.play(chan_name, svalue[0], svalue[1], msg.user.name)