コード例 #1
0
 def test_horizontal_two_wins(self):
     """
     Tests a won horizontal does result in a game win declaration
     """
     test = Game()
     test.board = [[None, None, None, '1010'], [None, None, '1100', None],
                   [None, '1000', None, None], ['1111', None, None, None]]
     self.assertTrue(test.has_won_game('1000'))
コード例 #2
0
 def test_horizontal_two_loses(self):
     """
     Tests a not won horizontal does not result in a game win declaration
     """
     test = Game()
     test.board = [[None, None, None, '1010'], [None, None, '0100', None],
                   [None, '1000', None, None], ['1111', None, None, None]]
     self.assertFalse(test.has_won_game('1010'))
コード例 #3
0
 def test_fourth_column_loses(self):
     """
     Tests a not won fourth column does not result in a game win declaration
     """
     test = Game()
     test.board = [[None, None, None, '0010'], [None, None, None, '1100'],
                   [None, None, None, '1000'], [None, None, None, '1111']]
     self.assertFalse(test.has_won_game('0010'))
コード例 #4
0
 def test_fourth_column_wins(self):
     """
     Tests a won fourth column does result in a game win declaration
     """
     test = Game()
     test.board = [[None, None, None, '1010'], [None, None, None, '1100'],
                   [None, None, None, '1000'], [None, None, None, '1111']]
     self.assertTrue(test.has_won_game('1000'))
コード例 #5
0
 def test_third_column_loses(self):
     """
     Tests a not won third column does not result in a game win declaration
     """
     test = Game()
     test.board = [[None, None, '1010', None], [None, None, '0100', None],
                   [None, None, '1010', None], [None, None, '1111', None]]
     self.assertFalse(test.has_won_game('1111'))
コード例 #6
0
 def test_second_column_wins(self):
     """
     Tests a won second column does result in a game win declaration
     """
     test = Game()
     test.board = [[None, '1010', None, None], [None, '1100', None, None],
                   [None, '1000', None, None], [None, '1111', None, None]]
     self.assertTrue(test.has_won_game('1111'))
コード例 #7
0
 def test_third_row_loses(self):
     """
     Tests a not won third row does not result in a game win declaration
     """
     test = Game()
     test.board = [[None, None, None, None], [None, None, None, None],
                   ['0000', '1000', '0101', '0010'],
                   [None, None, None, None]]
     self.assertFalse(test.has_won_game('0101'))
コード例 #8
0
 def test_third_row_wins(self):
     """
     Tests a won third row does result in a game win declaration
     """
     test = Game()
     test.board = [[None, None, None, None], [None, None, None, None],
                   ['0000', '1000', '0100', '0010'],
                   [None, None, None, None]]
     self.assertTrue(test.has_won_game('0000'))
コード例 #9
0
 def test_most_pieces_loses(self):
     """
     Tests a mostly full board does not result in a game win declaration
     """
     test = Game()
     test.board = [['0001', '1000', None, '0100'],
                   ['1110', '0001', '0010', '0111'],
                   ['1001', None, '1110', '1011'],
                   ['0100', '0110', '0011', '1111']]
     self.assertFalse(test.has_won_game('1111'))
コード例 #10
0
ファイル: play.py プロジェクト: IG-AI/UU-Game
class Play:
    """
    The Play class
    Author(s):      Adam Ross; Gustav From
    Last-edit-date: 14/02/2019
    """
    def __init__(self):
        """
        The Play class constructor
        """
        self.game = Game()  # initiates a Game class instance
        self.players = []  # a list for players to be added to when initialised
        self.current_player = None  # initiates current player during game play
        self.selected_piece = None  # initiates current selected piece

    def play_selection(self, pce=None):
        """
        The selecting of a piece part of a game turn and
        swapping of current player after a piece is selected
        :param pce: the piece being selected for placing on the board
        """
        if isinstance(self.current_player, PlayerHuman):
            self.selected_piece = self.current_player.choose_piece(pce)

            if self.selected_piece:
                self.selected_piece = self.game.pieces.pop(self.selected_piece)
        else:
            self.selected_piece = self.game.pieces.pop(
                self.current_player.choose_piece())
        if self.selected_piece:
            self.current_player = self.change_player()  # swaps player turn
        return self.selected_piece

    def play_placement(self, y=None, x=None):
        """
        The placing of a selected piece on the board part of a game turn
        :param y: the board row position the piece is being placed for human
        :param x: the board column position the piece is being placed for human
        """
        if isinstance(self.current_player, PlayerHuman):
            return self.current_player.place_piece(self.selected_piece, y, x)
        else:
            self.current_player.place_piece(self.selected_piece)  # place piece

    def play_auto(self):
        """
        Where the game turns are played automatically until game won or drawn
        :return: the winning player if game won, or None if game drawn
        """
        while True:
            self.play_selection()  # current player selects a piece, play swaps
            self.play_placement()  # new current player places piece on board

            if self.game.has_won_game(self.selected_piece):  # if game is won
                return self.current_player.name  # returns game winner name
            elif not self.game.has_next_play():  # checks if play turns remain
                return None  # returns no game winner

    def init_players(self, mod, dif=0, p_one="Player One", p_two="Player Two"):
        """
        Initializes the two players for the game dependent on game mode
        :param mod: the game mode being played; human vs human, AI vs AI, ...
        :param dif: the difficulty being played when playing AI
        :param p_one: the name for player one; default: Player One
        :param p_two: the name for player two; default: Player Two
        """
        if mod == 1:  # if human player vs human player
            self.players = [
                PlayerHuman(self.game, p_one),
                PlayerHuman(self.game, p_two)
            ]
        elif mod == 2:  # if human player vs AI player
            if dif == 1:  # if easy difficulty AI
                self.players = [
                    PlayerHuman(self.game, p_one),
                    PlayerEasyAI(self.game, p_two)
                ]
            elif dif == 2:  # if medium difficulty AI
                self.players = [
                    PlayerHuman(self.game, p_one),
                    PlayerMediumAI(self.game, p_two)
                ]
            else:  # if hard difficulty AI
                self.players = [
                    PlayerHuman(self.game, p_one),
                    PlayerHardAI(self.game, p_two)
                ]
        else:  # if AI player vs AI player
            if dif == 1:  # if easy difficulty AI
                self.players = [
                    PlayerEasyAI(self.game, p_one),
                    PlayerEasyAI(self.game, p_two)
                ]

            elif dif == 2:  # if medium difficulty AI
                self.players = [
                    PlayerMediumAI(self.game, p_one),
                    PlayerMediumAI(self.game, p_two)
                ]
            else:  # if hard difficulty AI
                self.players = [
                    PlayerHardAI(self.game, p_one),
                    PlayerHardAI(self.game, p_two)
                ]
        self.current_player = choice(self.players)  # random starting player

    def change_player(self):
        """
        Toggles player selection for when the game play turn changes
        :return: the new selected player for the current game play turn
        """
        return self.players[abs(self.players.index(self.current_player) - 1)]