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)
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)
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)
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)