def test_make_valid_move1(self): game = Game(self.player1, self.player2) gb = game.game_board game.who_plays_first() gb.update_board(game.who_is_moving(), 1) self.assertFalse(gb.is_space_free(1)) self.assertTrue(gb.is_space_free(3))
def create_hosted_game(cls): p_name, ai_diff = None, None if input("Host as AI? (y/n): ").strip().lower() == "y": p_name = "Host" ai_diff = cls._prompt_difficulty("AI") else: p_name = cls._prompt_name("host player", []) print("Waiting for opponent...") csock = cls._establish_client_conn(cls._default_port) if csock is None: print("No opponent connected within {} seconds.".format( cls._wait_client_timeout)) print() return None, None p = None if ai_diff is None: p = NetworkHumanPlayer(p_name, csock) else: p = NetworkAIPlayer(p_name, ai_diff, csock) csock.send(p.name.encode("utf-8")) opp_name = csock.recv(1024).decode("utf-8") print("Established connection with opponent {}.".format(opp_name)) o = NetworkOpponent(opp_name, csock) print() return Game(p, o), csock
def create_p_vs_ai_game(cls): p_name = cls._prompt_name("Player", []) ai_diff = cls._prompt_difficulty("AI") print() p = HumanPlayer(p_name) ai = AIPlayer("Bot", ai_diff) return Game(p, ai)
def create_ai_vs_ai_game(cls): ai1_diff = cls._prompt_difficulty("AI 1") ai2_diff = cls._prompt_difficulty("AI 2") print() ai1 = AIPlayer("Bot 1", ai1_diff) ai2 = AIPlayer("Bot 2", ai2_diff) return Game(ai1, ai2)
def create_p_vs_p_game(cls): p1_name = cls._prompt_name("Player 1", []) p2_name = cls._prompt_name("Player 2", [p1_name]) print() p1 = HumanPlayer(p1_name) p2 = HumanPlayer(p2_name) return Game(p1, p2)
def test_draw(self): player1 = StrategyPlayer('player1', 'X', [1, 3, 6, 8]) player2 = StrategyPlayer('player2', 'O', [2, 4, 5, 7, 9]) game = Game(player1, player2) game.current_player = player2 result = game.enter_game_loop() self.assertEqual(result, None)
def test_winner(self): losing_player = StrategyPlayer('loser', 'X', [1, 4, 7]) winning_player = StrategyPlayer('winner', 'O', [2, 5, 8]) game = Game(losing_player, winning_player) game.current_player = winning_player winner = game.enter_game_loop() self.assertEqual(winner, winning_player)
def test_make_invalid_move1(self): game = Game(self.player1, self.player2) gb = game.game_board game.current_player = self.player1 gb.update_board(game.who_is_moving(), 2) game.toggle_player() gb.update_board(game.who_is_moving(), 2) self.assertFalse(gb.is_space_free(2))
def _play_brackets(self, players): games = [Game(*p) for p in zip(players[::2], players[1::2])] winners = [] for g in games: g.start() while g.winner is None: g.reset() g.start() winners.append(g.winner) self._bp.increment(g.winner.name) self._bp.print_bracket() self._press_enter_to_continue() return winners
def test_who_plays_first(self): c1 = 0 c2 = 0 game = Game(self.player1, self.player2) n_samples = 1000 for i in range(n_samples): first = game.who_plays_first() if first == self.player1: c1 += 1 elif first == self.player2: c2 += 1 p = c1 / n_samples self.assertTrue(0.4 < p and p < 0.6)
def _get_games(self, players): games, pairs = [], [] for i in range(0, len(players), 2): p1, p2 = players[i], players[i + 1] players_data = {p1["name"]: p1, p2["name"]: p2} p1, p2 = self._get_player_instances(p1, p2) pairs.append((p1, p2) if self._is_hosting else (p2, p1)) if not self._is_hosting: p1, p2 = p2, p1 games.append((Game(p1, p2), players_data)) print("For this bracket segment:") print() print("{0: <23}{1: <23}".format("On host computer:", "On client computer:")) for p1, p2 in pairs: print(" - {0: <20} - {1: <20}".format(p1.name, p2.name)) print() return games
def create_joined_game(cls): print("Connecting to host...") lsock, hsock = cls._establish_host_conn(cls._default_port) if hsock is None: print("Couldn't connect to host.") print() return None, None, None opp_name = hsock.recv(1024).decode("utf-8") print("Established connection with opponent {}.".format(opp_name)) p = None if input("Join as AI? (y/n): ").strip().lower() == "y": p_name = "Client" ai_diff = cls._prompt_difficulty("AI") p = NetworkAIPlayer(p_name, ai_diff, hsock) else: p_name = cls._prompt_name("joining player", [opp_name.split("AI ").pop()]) p = NetworkHumanPlayer(p_name, hsock) hsock.send(p.name.encode("utf-8")) o = NetworkOpponent(opp_name, hsock) print() return Game(o, p), lsock, hsock
def test_make_valid_move2(self): game = Game(self.player1, self.player2) gb = game.game_board game.who_plays_first() gb.update_board(game.who_is_moving(), 1) game.toggle_player() gb.update_board(game.who_is_moving(), 2) game.toggle_player() gb.update_board(game.who_is_moving(), 3) game.toggle_player() gb.update_board(game.who_is_moving(), 4) game.toggle_player() gb.update_board(game.who_is_moving(), 5) game.toggle_player() gb.update_board(game.who_is_moving(), 6) game.toggle_player() gb.update_board(game.who_is_moving(), 7) game.toggle_player() gb.update_board(game.who_is_moving(), 8) game.toggle_player() self.assertFalse(gb.is_board_full()) gb.update_board(game.who_is_moving(), 9) game.toggle_player() self.assertTrue(gb.is_board_full())
def test_represent_gameboard_fresh(self): game = Game(self.player1, self.player2) game.turn = 1 self.assertEqual(str(game.game_board), fresh_gameboard_string)
def test_who_is_playing(self): game = Game(self.player1, self.player2) game.current_player = self.player1 self.assertEqual(game.who_is_moving(), self.player1)
def test_represent_gameboard_with_pieces(self): game = Game(self.player1, self.player2) game.game_board.board = ['x', 'o', 'x', 'o', 'x', 'o', 'x', 'o', 'x'] self.assertTrue(str(game.game_board), full_gameboard_string)
def test_toggle_player(self): game = Game(self.player1, self.player2) previous_player = game.current_player game.toggle_player() current_player = game.current_player self.assertFalse(current_player == previous_player)