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