class GameGenetics:
    def __init__(self, player_1, player_2):
        self.board = Board()
        self.player_1 = player_1
        self.player_2 = player_2
        self.turn_player_1 = False

    def play_game(self):
        self.board.created_board()
        while (not self.is_winner()):
            if (self.board.is_full()):
                #print("Empate")
                return cf.TIE
            self.turn_player_1 = not self.turn_player_1
            if (self.turn_player_1):
                col = self.player_1.next_action(deepcopy(self.board))
                self.board.set_value_cell(col, 1)
            else:
                col = self.player_2.next_action(deepcopy(self.board))
                self.board.set_value_cell(col, 2)
        return self.who_is_winner()

    def who_is_winner(self):
        if (self.turn_player_1):
            return cf.WINNER_1
        else:
            return cf.WINNER_2

    def is_winner(self):
        last_mov = self.board.get_last_mov()
        if (self.turn_player_1):
            return self.board.winner(last_mov[0], last_mov[1], 1)
        else:
            return self.board.winner(last_mov[0], last_mov[1], 2)
Beispiel #2
0
def test_get_strategy_0():
    """ Check that get_strategy is working correctly
    """
    b = [[1, 0, 1, 2, 1, 1, 0],
     [2, 0, 0, 2, 1, 2, 0],
     [1, 0, 0, 1, 2, 2, 0],
     [2, 0, 0, 0, 1, 0, 0],
     [2, 0, 0, 0, 1, 0, 0],
     [1, 0, 0, 0, 2, 0, 0]]
    board = Board()
    board.board = b
    player_1 = Agent(1, 2, True)  # 1 symbol of the agent, 2 symbol of the opponent
    player_1.set_strategies([0,0,0,0])
    var_action = player_1.get_strategy(board)
    array_result = [[0, 1, 1], [1, 2, 2], [2, 2, 2], 
                    [3, 2, 2], [4, 1, 1], [5, 3, 3], [6, 2, 2]]
    result = True
    for x in var_action: 
        position = array_result[x.position][0]
        amount = array_result[x.position][1]
        strategies_number = array_result[x.position][2]
        print(x.position,x.amount, x.strategies_number)
        if not(x.position == position and x.amount == amount 
                and strategies_number == x.strategies_number):
            result = False
    assert(result)
Beispiel #3
0
def test_winner_false():
    """Given a cell, check if there is not win from it
    """

    array = [[1, 2, 1, 1, 2, 2, 2], [1, 2, 1, 1, 1, 1,
                                     2], [0, 2, 2, 1, 2, 2, 2],
             [0, 1, 0, 1, 2, 0, 2], [0, 0, 0, 2, 0, 0, 0],
             [0, 0, 0, 0, 0, 0, 0]]
    board = Board()
    board.board = array
    test_pos = [[1, 2], [1, 5], [1, 8], [4, 5], [3, 6], [3, 3], [-4, 3],
                [2, 11]]
    sym_p1 = 1
    sym_p2 = 2
    sym_p3 = 3

    # compare with symbols valid
    assert (board.winner(test_pos[0][0], test_pos[0][1], sym_p2) == False)
    assert (board.winner(test_pos[1][0], test_pos[1][1], sym_p2) == False)
    assert (board.winner(test_pos[3][0], test_pos[3][1], sym_p1) == False)
    assert (board.winner(test_pos[4][0], test_pos[4][1], sym_p1) == False)
    assert (board.winner(test_pos[5][0], test_pos[5][1], sym_p2) == False)
    # compare with symbol not valid
    assert (board.winner(test_pos[3][0], test_pos[3][1], sym_p3) == False)
    assert (board.winner(test_pos[4][0], test_pos[4][1], sym_p3) == False)
    assert (board.winner(test_pos[5][0], test_pos[5][1], sym_p3) == False)
    # compare with cell (row, col) not valid
    assert (board.winner(test_pos[2][0], test_pos[2][1], sym_p1) == False)
    assert (board.winner(test_pos[6][0], test_pos[6][1], sym_p1) == False)
    assert (board.winner(test_pos[7][0], test_pos[7][1], sym_p1) == False)
Beispiel #4
0
def test_win_vrt_true():
    """Given a cell, check if there is win from it in vertical line
    """

    array = [[1, 2, 1, 1, 2, 2, 2], [1, 2, 1, 1, 2, 1,
                                     2], [1, 2, 2, 1, 2, 2, 2],
             [1, 1, 0, 1, 2, 0, 2], [0, 0, 0, 1, 2, 0, 0],
             [0, 0, 0, 0, 2, 0, 0]]
    board = Board()
    board.board = array
    test_pos_1 = [[3, 0], [4, 3], [3, 3], [5, 4], [4, 4], [3, 4], [3, 6]]
    size_line = 3
    sym_p1 = 1
    sym_p2 = 2

    assert (board.win_vrt(test_pos_1[0][0], test_pos_1[0][1], sym_p1) >=
            size_line)
    assert (board.win_vrt(test_pos_1[1][0], test_pos_1[1][1], sym_p1) >=
            size_line)
    assert (board.win_vrt(test_pos_1[2][0], test_pos_1[2][1], sym_p1) >=
            size_line)
    assert (board.win_vrt(test_pos_1[3][0], test_pos_1[3][1], sym_p2) >=
            size_line)
    assert (board.win_vrt(test_pos_1[4][0], test_pos_1[4][1], sym_p2) >=
            size_line)
    assert (board.win_vrt(test_pos_1[5][0], test_pos_1[5][1], sym_p2) >=
            size_line)
    assert (board.win_vrt(test_pos_1[6][0], test_pos_1[6][1], sym_p2) >=
            size_line)
Beispiel #5
0
def test_get_cols():
    """ Check that the number of cols is exactly 7 
    """

    # default value of cols is 7
    board = Board()
    cols = 7

    assert (board.get_cols() == cols)
Beispiel #6
0
def test_get_rows():
    """ Check that the number of rows is exactly 6 
    """

    # default value of rows is 6
    board = Board()
    rows = 6

    assert (board.get_rows() == rows)
Beispiel #7
0
def test_is_full_true():
    """ Check that the board is really full 
    """

    array = [[1, 1, 2, 2, 2, 1, 1], [2, 1, 1, 1, 2, 1,
                                     2], [1, 1, 2, 1, 1, 1, 2],
             [2, 2, 1, 2, 2, 2, 1], [1, 1, 2, 2, 1, 1, 2],
             [2, 1, 2, 1, 1, 2, 2]]
    board = Board()
    board.board = array

    assert (board.is_full() == True)
Beispiel #8
0
def test_is_cell_valid_true():
    """ Check that the cell (i,j) of board is valid 
    """

    # default value of dimensions is (6x7)
    board = Board()
    pos = [[1, 2], [3, 4], [0, 1], [0, 5]]

    assert (board.is_cell_valid(pos[0][0], pos[0][1]) == True)
    assert (board.is_cell_valid(pos[1][0], pos[1][1]) == True)
    assert (board.is_cell_valid(pos[2][0], pos[2][1]) == True)
    assert (board.is_cell_valid(pos[3][0], pos[3][1]) == True)
Beispiel #9
0
def test_is_full_false():
    """ Check that the board is not really full 
    """

    array = [[1, 1, 2, 0, 0, 1, 1], [2, 1, 1, 1, 2, 1,
                                     2], [1, 1, 2, 1, 1, 1, 2],
             [2, 2, 1, 2, 2, 2, 1], [1, 1, 2, 2, 1, 1, 2],
             [2, 1, 2, 1, 1, 2, 2]]
    board = Board()
    board.board = array

    assert (board.is_full() == False)
Beispiel #10
0
def test_is_cell_valid_false():
    """ Check that the cell (i,j) of board is not valid 
    """

    # default value of dimensions is (6x7)
    board = Board()
    pos = [[0, -2], [9, 11], [0, 7], [1, 8]]

    assert (board.is_cell_valid(pos[0][0], pos[0][1]) == False)
    assert (board.is_cell_valid(pos[1][0], pos[1][1]) == False)
    assert (board.is_cell_valid(pos[2][0], pos[2][1]) == False)
    assert (board.is_cell_valid(pos[3][0], pos[3][1]) == False)
Beispiel #11
0
def test_validate_action_False():
    """ Check that validate_action is working correctly
    """
    array = [[1, 1, 1, 2, 1, 0, 0], [2, 1, 1, 1, 2, 0,
                                     0], [0, 0, 1, 2, 2, 0, 0],
             [0, 0, 0, 2, 1, 0, 0], [0, 0, 0, 1, 0, 0, 0],
             [0, 0, 0, 2, 0, 0, 0]]
    board = Board()
    board.set_board(array)
    player_1 = Player()
    result = player_1.validate_action(board, 9)
    assert (not result)
Beispiel #12
0
def test_created_board():
    """ Check that the board is created and all the cells
        contains the null_cell of the board 
    """

    array = [[0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0,
                                     0], [0, 0, 0, 0, 0, 0, 0],
             [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0],
             [0, 0, 0, 0, 0, 0, 0]]
    board = Board()
    board.created_board()

    # compare the array with the new board created
    assert (array == board.board)
Beispiel #13
0
def test_is_full_column_true():
    """ Check that the column of board is really full 
    """

    array = [[0, 1, 2, 0, 1, 1, 0], [0, 1, 1, 0, 2, 1,
                                     0], [0, 1, 2, 1, 1, 1, 0],
             [2, 2, 1, 2, 2, 2, 0], [1, 1, 2, 2, 1, 1, 0],
             [2, 1, 2, 1, 1, 2, 0]]
    board = Board()
    board.board = array

    assert (board.is_fill_column(1) == True)
    assert (board.is_fill_column(2) == True)
    assert (board.is_fill_column(4) == True)
    assert (board.is_fill_column(5) == True)
Beispiel #14
0
def test_get_column_not_exist():
    """ Check there is zero match 
    """
    array = [[0, 1, 1, 0, 1, 1, 0], [0, 1, 1, 0, 1, 1,
                                     0], [0, 1, 1, 0, 1, 1, 0],
             [0, 1, 1, 0, 1, 1, 0], [1, 1, 1, 0, 1, 1, 0],
             [1, 1, 1, 1, 1, 1, 0]]
    board = Board()
    board.board = array
    col_7 = []
    col_8 = []
    col_20 = []

    assert (board.get_column(7) == col_7)
    assert (board.get_column(8) == col_8)
    assert (board.get_column(20) == col_20)
Beispiel #15
0
def test_next_action_block():
    """ Check that next_action is working correctly
    """
    array = [[1, 1, 1, 2, 1, 0, 0],
            [2, 1, 1, 1, 2, 0, 0],
            [0, 0, 1, 2, 2, 0, 0],
            [0, 0, 0, 2, 1, 0, 0],
            [0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0]]
    board = Board()
    board.set_board(array)
    player_1 = Agent(1, 2, True)  # 1 symbol of the agent, 2 symbol of the opponent
    player_2 = Agent(2, 1, True)  # 2 symbol of the agent, 1 symbol of the opponent
    col_result = player_2.next_action(board)
    col = 2
    assert(col == col_result)
Beispiel #16
0
def test_last_mov():
    """Check that the board save correctly the last position
       of last movement 
    """

    array = [[1, 1, 1, 1, 1, 1, 0], [1, 1, 1, 0, 1, 1,
                                     0], [0, 1, 1, 0, 1, 1, 0],
             [0, 1, 0, 0, 1, 1, 0], [0, 1, 0, 0, 1, 1, 0],
             [0, 1, 0, 0, 1, 1, 0]]
    board = Board()
    board.board = array
    # insert value in col 2
    board.set_value_cell(2, 1)
    # position where 1 was insert
    last_position = (3, 2)

    assert (board.last_mov == last_position)
Beispiel #17
0
def test_get_column_exist():
    """ Check there is zero match 
    """

    array = [[0, 1, 1, 0, 1, 1, 0], [0, 1, 1, 0, 1, 1,
                                     0], [0, 1, 1, 0, 1, 1, 0],
             [0, 1, 1, 0, 1, 1, 0], [1, 1, 1, 0, 1, 1, 0],
             [1, 1, 1, 1, 1, 1, 0]]
    board = Board()
    board.board = array
    col_0 = [0, 0, 0, 0, 1, 1]
    col_1 = [1, 1, 1, 1, 1, 1]
    col_2 = [1, 1, 1, 1, 1, 1]
    col_3 = [0, 0, 0, 0, 0, 1]
    col_4 = [1, 1, 1, 1, 1, 1]
    col_5 = [1, 1, 1, 1, 1, 1]
    col_6 = [0, 0, 0, 0, 0, 0]

    assert (board.get_column(0) == col_0)
    assert (board.get_column(1) == col_1)
    assert (board.get_column(2) == col_2)
    assert (board.get_column(3) == col_3)
    assert (board.get_column(4) == col_4)
    assert (board.get_column(5) == col_5)
    assert (board.get_column(6) == col_6)
Beispiel #18
0
def test_get_empty_element_available_cell():
    """ Check that all the columns are available to insert
    """

    array = [[1, 1, 1, 1, 1, 1, 0], [1, 1, 1, 0, 1, 1,
                                     0], [0, 1, 1, 0, 1, 0, 0],
             [0, 1, 0, 0, 1, 0, 0], [0, 1, 0, 0, 0, 0, 0],
             [0, 0, 0, 0, 0, 0, 0]]
    board = Board()
    board.board = array
    col_0 = 2
    col_1 = 5
    col_2 = 3
    col_3 = 1
    col_4 = 4
    col_5 = 2
    col_6 = 0

    # compare with all columns of the board
    assert (board.get_empty_element(0) == col_0)
    assert (board.get_empty_element(1) == col_1)
    assert (board.get_empty_element(2) == col_2)
    assert (board.get_empty_element(3) == col_3)
    assert (board.get_empty_element(4) == col_4)
    assert (board.get_empty_element(5) == col_5)
    assert (board.get_empty_element(6) == col_6)
Beispiel #19
0
def test_win_false():
    """ Check that win function is working correctly
    """
    array = [[1, 1, 1, 2, 1, 0, 0],
            [2, 1, 2, 1, 2, 0, 0],
            [0, 0, 1, 2, 2, 0, 0],
            [0, 0, 0, 2, 1, 0, 0],
            [0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0]]
    board = Board()
    board.set_board(array)
    player_1 = Agent(1, 2, True)  # 1 symbol of the agent, 2 symbol of the opponent
    player_2 = Agent(2, 1, True)  # 2 symbol of the agent, 1 symbol of the opponent
    col_to_win_result = player_1.win(board, 1)
    col_to_win = -1
    col_to_win_result_2 = player_2.win(board, 2)
    col_to_win_2 = -1
    assert(col_to_win == col_to_win_result)
    assert(col_to_win_2 == col_to_win_result_2)
Beispiel #20
0
def test_get_cells_symbol_zero():
    """ Check there is zero match 
    """

    array = [[0, 1, 1, 0, 1, 1, 0], [0, 1, 1, 0, 1, 1,
                                     0], [0, 1, 1, 0, 1, 1, 0],
             [0, 1, 1, 0, 1, 1, 0], [1, 1, 1, 0, 1, 1, 0],
             [1, 1, 1, 1, 1, 1, 0]]
    board = Board()
    board.board = array
    symb_a = 3
    symb_b = 2
    # positions of the symbol 3 and 2
    pos_a = []
    pos_b = []

    # compare all cells of each symbol with the respective array
    assert (board.get_cells_symbol(symb_a) == pos_a)
    assert (board.get_cells_symbol(symb_b) == pos_b)
Beispiel #21
0
def test_block_true():
    """ Check that block function is working correctly
    """
    array = [[1, 1, 1, 2, 1, 0, 0],
             [2, 1, 1, 1, 2, 0, 0],
             [0, 0, 1, 2, 2, 0, 0],
             [0, 0, 0, 2, 2, 0, 0],
             [0, 0, 0, 0, 0, 0, 0],
             [0, 0, 0, 0, 0, 0, 0]]
    board = Board()
    board.set_board(array)
    player_1 = Agent(1, 2, True)  # 1 symbol of the agent, 2 symbol of the opponent
    player_2 = Agent(2, 1, True)  # 2 symbol of the agent, 1 symbol of the opponent
    col_to_block_result = player_1.block(board, 2)
    col_to_block = 4
    col_to_block_result_2 = player_2.block(board, 1)
    col_to_block_2 = 2
    assert(col_to_block == col_to_block_result)
    assert(col_to_block_2 == col_to_block_result_2)
Beispiel #22
0
def test_get_cells_symbol_one_or_more():
    """ Check there is at least one or more match 
    """

    array = [[0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0,
                                     0], [0, 1, 0, 0, 0, 0, 0],
             [2, 2, 1, 0, 2, 0, 0], [1, 1, 2, 2, 1, 1, 0],
             [2, 1, 2, 1, 1, 2, 2]]
    board = Board()
    board.board = array
    symb_a = 1
    symb_b = 2
    # positions of the symbol 1 and 2
    pos_a = [(2, 1), (3, 2), (4, 0), (4, 1), (4, 4), (4, 5), (5, 1), (5, 3),
             (5, 4)]
    pos_b = [(3, 0), (3, 1), (3, 4), (4, 2), (4, 3), (5, 0), (5, 2), (5, 5),
             (5, 6)]

    # compare all cells of each symbol with the respective array
    assert (board.get_cells_symbol(symb_a) == pos_a)
    assert (board.get_cells_symbol(symb_b) == pos_b)
Beispiel #23
0
def test_set_value_cell_wrong_col():
    """Check that the board no change with a wrong column 
    """

    array = [[1, 1, 1, 1, 1, 1, 0], [1, 1, 1, 0, 1, 1,
                                     0], [0, 1, 1, 0, 1, 1, 0],
             [0, 1, 0, 0, 1, 1, 0], [0, 1, 0, 0, 1, 1, 0],
             [0, 1, 0, 0, 1, 1, 0]]
    board = Board()
    board.board = array
    # insert value in col 10
    board.set_value_cell(10, 1)
    # insert value in col -1
    board.set_value_cell(-1, 1)

    # verify that the cols are not update
    assert (board.board == array)
Beispiel #24
0
def test_set_value_cell_correct_col():
    """ Check that a symbol is set correctly in column 
    """

    array = [[1, 1, 1, 1, 1, 1, 0], [1, 1, 1, 0, 1, 1,
                                     0], [0, 1, 1, 0, 1, 1, 0],
             [0, 1, 0, 0, 1, 1, 0], [0, 1, 0, 0, 1, 1, 0],
             [0, 1, 0, 0, 1, 1, 0]]
    board = Board()
    board.board = array
    # insert value in col 0
    board.set_value_cell(0, 1)
    # insert value in col 2
    board.set_value_cell(2, 1)

    # verify that the cols are update
    assert (board.board == array)
Beispiel #25
0
def test_is_col_valid_true():
    """ Check that the col of board is valid 
    """

    # default value of dimensions is (6x7)
    board = Board()
    pos = [0, 1, 2, 3, 4, 5, 6]

    assert (board.is_col_valid(pos[0]) == True)
    assert (board.is_col_valid(pos[1]) == True)
    assert (board.is_col_valid(pos[2]) == True)
    assert (board.is_col_valid(pos[3]) == True)
    assert (board.is_col_valid(pos[4]) == True)
    assert (board.is_col_valid(pos[5]) == True)
    assert (board.is_col_valid(pos[6]) == True)
Beispiel #26
0
def test_is_col_valid_false():
    """ Check that the col of board is not valid 
    """

    # default value of dimensions is (6x7)
    board = Board()
    pos = [-1, 7, 8, 9, -2, -4, 10]

    assert (board.is_col_valid(pos[0]) == False)
    assert (board.is_col_valid(pos[1]) == False)
    assert (board.is_col_valid(pos[2]) == False)
    assert (board.is_col_valid(pos[3]) == False)
    assert (board.is_col_valid(pos[4]) == False)
    assert (board.is_col_valid(pos[5]) == False)
    assert (board.is_col_valid(pos[6]) == False)
Beispiel #27
0
def test_clear_cell_not_exist():
    """ Check that the board not change with value out the range
    """

    array = [[1, 1, 1, 2, 1, 0, 0], [2, 1, 1, 1, 2, 0,
                                     0], [0, 0, 1, 1, 2, 0, 0],
             [0, 0, 2, 2, 1, 0, 0], [0, 0, 0, 0, 0, 0, 0],
             [0, 0, 0, 0, 0, 0, 0]]
    board = Board()
    board.set_board(array)
    position_test = [9, 9]  # out the range of board

    # try to clear cell
    board.clear_cell(position_test[0], position_test[0])
    assert (array == board.board)
Beispiel #28
0
def test_clear_cell_exist():
    """ Check that cell is clear correctly
    """

    array = [[1, 1, 1, 2, 1, 0, 0], [2, 1, 1, 1, 2, 0,
                                     0], [0, 0, 1, 1, 2, 0, 0],
             [0, 0, 2, 2, 1, 0, 0], [0, 0, 0, 0, 0, 0, 0],
             [0, 0, 0, 0, 0, 0, 0]]
    board = Board()
    board.set_board(array)
    position_test = [0, 0]  # at the begin of array

    # update position in array to compare
    array[position_test[0]][position_test[1]] = board.null_cell
    board.clear_cell(position_test[0], position_test[0])
    assert (array == board.board)
Beispiel #29
0
def test_is_full_column_false():
    """ Check that the column of board is not really full 
    """

    array = [[0, 1, 2, 0, 1, 1, 0], [0, 1, 1, 0, 2, 1,
                                     0], [0, 1, 2, 1, 1, 1, 0],
             [2, 2, 1, 2, 2, 2, 0], [1, 1, 2, 2, 1, 1, 0],
             [2, 1, 2, 1, 1, 2, 0]]
    board = Board()
    board.board = array

    # compare cases with valid column
    assert (board.is_fill_column(0) == False)
    assert (board.is_fill_column(3) == False)
    assert (board.is_fill_column(6) == False)

    # compare cases with column out the range
    assert (board.is_fill_column(9) == False)
Beispiel #30
0
def test_winner_true():
    """Given a cell, check if there is win from it
    """

    array = [[1, 2, 1, 1, 2, 2, 2], [1, 2, 1, 1, 1, 1,
                                     2], [0, 2, 2, 1, 2, 2, 2],
             [0, 1, 0, 1, 2, 0, 2], [0, 0, 0, 2, 0, 0, 0],
             [0, 0, 0, 0, 0, 0, 0]]
    board = Board()
    board.board = array
    test_pos = [[1, 2], [1, 5], [1, 6], [4, 3], [3, 6], [3, 3]]
    sym_p1 = 1
    sym_p2 = 2

    assert (board.winner(test_pos[0][0], test_pos[0][1], sym_p1) == True)
    assert (board.winner(test_pos[1][0], test_pos[1][1], sym_p1) == True)
    assert (board.winner(test_pos[2][0], test_pos[2][1], sym_p2) == True)
    assert (board.winner(test_pos[3][0], test_pos[3][1], sym_p2) == True)
    assert (board.winner(test_pos[4][0], test_pos[4][1], sym_p2) == True)
    assert (board.winner(test_pos[5][0], test_pos[5][1], sym_p1) == True)