def test_board_is_empty_when_game_is_initialized(): game = c4.Connect4() output = [] c4.print = lambda s : output.append(s) game.show_board() mock_board = "|0||0||0||0||0||0||0|\n|0||0||0||0||0||0||0|\n|0||0||0||0||0||0||0|\n|0||0||0||0||0||0||0|\n|0||0||0||0||0||0||0|\n|0||0||0||0||0||0||0|\n" assert output.count(mock_board) == 1
def main(): myConnect4 = connect4.Connect4() while (myConnect4.haveGameEnded() == False): lastMoveIsLegal = False while (lastMoveIsLegal == False): inputNum = eval( input(myConnect4.whosTurnIsIt() + " player, please enter input: ")) myConnect4.makeMove(inputNum) lastMoveIsLegal = myConnect4.wasLastMoveLegal()
def build(self): # memory representation of the board connect4_game = connect4.Connect4() # adding screens to the screen manager screen_manager = ScreenManager() mode_screen = ModeScreen(connect4_game, name="mode") game_screen = GameScreen(connect4_game, name="game") screen_manager.add_widget(mode_screen) screen_manager.add_widget(game_screen) return screen_manager
def minPlayer(game, depth, alpha, beta, player): column = 0 best_score = float('inf') options = viablePermuation(game) for option in options: game_copy = connect4.Connect4(game) game_copy.playTurn(option, False) score = minimax(game_copy, depth-1, alpha, beta, True)[0] if score < best_score: best_score = score column = option beta = min(beta, score) if alpha >= beta: break return (best_score, column)
def test_column_full_handled_again(): output = [] c4_input_values = ["Human", "Human"] human_input_values = [1, 2, 1, 2, 1, 2, 1] def mock_input(s): output.append(s) return f'{c4_input_values.pop(0)}' c4.input = mock_input c4.print = lambda s: output.append(s) game = c4.Connect4() game.test(human_input_values) game.start() print(output[2]) total = 0 total += output.count( "|0||0||0||0||0||0||0|\n|0||0||0||0||0||0||0|\n|0||0||0||0||0||0||0|\n|0||0||0||0||0||0||0|\n|0||0||0||0||0||0||0|\n|0||0||0||0||0||0||0|\n" ) total += output.count( "|0||0||0||0||0||0||0|\n|0||0||0||0||0||0||0|\n|0||0||0||0||0||0||0|\n|0||0||0||0||0||0||0|\n|0||0||0||0||0||0||0|\n|1||0||0||0||0||0||0|\n" ) total += output.count( "|0||0||0||0||0||0||0|\n|0||0||0||0||0||0||0|\n|0||0||0||0||0||0||0|\n|0||0||0||0||0||0||0|\n|0||0||0||0||0||0||0|\n|1||2||0||0||0||0||0|\n" ) total += output.count( "|0||0||0||0||0||0||0|\n|0||0||0||0||0||0||0|\n|0||0||0||0||0||0||0|\n|0||0||0||0||0||0||0|\n|1||0||0||0||0||0||0|\n|1||2||0||0||0||0||0|\n" ) total += output.count( "|0||0||0||0||0||0||0|\n|0||0||0||0||0||0||0|\n|0||0||0||0||0||0||0|\n|0||0||0||0||0||0||0|\n|1||2||0||0||0||0||0|\n|1||2||0||0||0||0||0|\n" ) total += output.count( "|0||0||0||0||0||0||0|\n|0||0||0||0||0||0||0|\n|0||0||0||0||0||0||0|\n|1||0||0||0||0||0||0|\n|1||2||0||0||0||0||0|\n|1||2||0||0||0||0||0|\n" ) total += output.count( "|0||0||0||0||0||0||0|\n|0||0||0||0||0||0||0|\n|0||0||0||0||0||0||0|\n|1||2||0||0||0||0||0|\n|1||2||0||0||0||0||0|\n|1||2||0||0||0||0||0|\n" ) total += output.count( "|0||0||0||0||0||0||0|\n|0||0||0||0||0||0||0|\n|1||0||0||0||0||0||0|\n|1||2||0||0||0||0||0|\n|1||2||0||0||0||0||0|\n|1||2||0||0||0||0||0|\n" ) total += output.count( "|0||0||0||0||0||0||0|\n|0||0||0||0||0||0||0|\n|1||2||0||0||0||0||0|\n|1||2||0||0||0||0||0|\n|1||2||0||0||0||0||0|\n|1||2||0||0||0||0||0|\n" ) total += output.count( "|0||0||0||0||0||0||0|\n|1||0||0||0||0||0||0|\n|1||2||0||0||0||0||0|\n|1||2||0||0||0||0||0|\n|1||2||0||0||0||0||0|\n|1||2||0||0||0||0||0|\n" ) assert total == 8
def test_horizontal_player1_win_in_the_middle(): output = [] c4_input_values = ["Human", "Human"] human_input_values = [6, 1, 5, 1, 3, 1, 4] def mock_input(s): output.append(s) return f'{c4_input_values.pop(0)}' c4.input = mock_input c4.print = lambda s: output.append(s) game = c4.Connect4() game.test(human_input_values) game.start() assert output.count("Player 1 wins") == 1
def test_simplest_player1_win(): output = [] c4_input_values = ["Human", "Human"] human_input_values = [6, 7, 6, 7, 6, 7, 6] def mock_input(s): output.append(s) return f'{c4_input_values.pop(0)}' c4.input = mock_input c4.print = lambda s : output.append(s) game = c4.Connect4() game.test(human_input_values) game.start() assert output.count("Player 1 wins") == 1
def run_program(): args = parser.parse_args() if args.debug: global DEBUG DEBUG = True if args.print: global PRINT PRINT = True if args.player1 == "input": p1 = inputPlayer.InputPlayer('O', DEBUG) elif args.player1 == "minimax": p1 = miniMaxPlayer.MiniMaxPlayer('O', args.depth, args.time1, DEBUG) elif args.player1 == "mcts": p1 = mcts.MCTSPlayer('O', args.time1, DEBUG) else: print("tu 1") parser.print_help() sys.exit() if args.player2 == "input": p2 = inputPlayer.InputPlayer('X', DEBUG) elif args.player2 == "minimax": p2 = miniMaxPlayer.MiniMaxPlayer('X', args.depth, args.time2, DEBUG) elif args.player2 == "mcts": p2 = mcts.MCTSPlayer('X', args.time2, DEBUG) else: print("tu 2", args.player2) parser.print_help() sys.exit() c = connect4.Connect4() p1w = 0 p2w = 0 for i in range(args.games): game = c.new_game(p1, p2, 7, 6) symbol = game.play(PRINT) if symbol == p1.symbol: p1w += 1 elif symbol == p2.symbol: p2w += 1 print("Won Player", symbol) print("P1:", p1.symbol, p1w) print("P2:", p2.symbol, p2w)
def test_diaganol_win_for_player1_from_bottom_right_corner(): output = [] c4_input_values = ["Human", "Human"] human_input_values = [7, 6, 6, 5, 5, 4, 5, 4, 4, 2, 4] def mock_input(s): output.append(s) return f'{c4_input_values.pop(0)}' c4.input = mock_input c4.print = lambda s: output.append(s) game = c4.Connect4() game.test(human_input_values) game.start() assert output.count("Player 1 wins") == 1
def test_illegal_move_prompts_for_legal_move(): output = [] c4_input_values = ["Human", "Human"] human_input_values = [1, 8, 2, 1, 2, 1, 2, 1] def mock_input(s): output.append(s) return f'{c4_input_values.pop(0)}' c4.input = mock_input c4.print = lambda s: output.append(s) game = c4.Connect4() game.test(human_input_values) game.start() assert output.count("Please make a legal move: ") == 1
def test_diaganol_win_for_player1_from_2nd_collumn_row_2_last_move_in_1_from_top( ): output = [] c4_input_values = ["Human", "Human"] human_input_values = [2, 1, 2, 3, 4, 3, 3, 5, 5, 5, 4, 5, 5, 4, 4] def mock_input(s): output.append(s) return c4_input_values.pop(0) c4.input = mock_input c4.print = lambda s: output.append(s) game = c4.Connect4() game.test(human_input_values) game.start() assert output.count("Player 1 wins") == 1
def test_diaganol_win_for_player1_from_bottom_left_corner_to_middle_last_move_bottom_left( ): output = [] c4_input_values = ["Human", "Human"] human_input_values = [3, 2, 2, 3, 3, 4, 4, 4, 4, 5, 1] def mock_input(s): output.append(s) return c4_input_values.pop(0) c4.input = mock_input c4.print = lambda s: output.append(s) game = c4.Connect4() game.test(human_input_values) game.start() assert output.count("Player 1 wins") == 1
def test_players_draw_the_game(): output = [] c4_input_values = ["Human", "Human"] human_input_values = [ 1, 2, 1, 2, 1, 2, 2, 1, 2, 1, 2, 1, 3, 4, 3, 4, 3, 4, 4, 3, 4, 3, 4, 3, 5, 6, 5, 6, 5, 6, 6, 5, 6, 5, 6, 5, 7, 7, 7, 7, 7, 7 ] def mock_input(s): output.append(s) return f'{c4_input_values.pop(0)}' c4.input = mock_input c4.print = lambda s: output.append(s) game = c4.Connect4() game.test(human_input_values) game.start() assert output.count("It's a Draw!") == 1
def test_players_are_set_to_ai_vs_ai(): output = [] c4_input_values = ["AI", "AI"] human_input_values = [1, 1, 1, 1] def mock_input(s): output.append(s) return f'{c4_input_values.pop(0)}' c4.input = mock_input c4.print = lambda s: output.append(s) game = c4.Connect4() game.test(human_input_values) game.start() total = 0 total += output.count("Player 1 is a Human or AI?") total += output.count("Player 2 is a Human or AI?") assert total == 2
def __init__(self): super().__init__() self.title = "Connect 4" self.top = 200 self.left = 100 self.width = 265 self.height = 275 self.paintCircle = False self.line = QLine() self.circles_filled = {} for i in range(7): for j in range(6): self.circles_filled[(i, j)] = False self.circles_pos = {} self.space = 35 start_x = 15 start_y = 20 x = range(start_x, start_x + self.space * 7, self.space) y = range(start_y, start_y + self.space * 6, self.space)[::-1] for i in range(7): for j in range(6): self.circles_pos[(i, j)] = (x[i], y[j]) self.c4 = connect4.Connect4() c = ["-"] * 7 self.board = [c] * 6 self.init_window()
# Y = [] global model while True: X = [] Y = [] results = training_games(agents, 10) for result in results: x, y = to_training_data(game, result, agents[0].name()) X.extend(x) Y.extend(y) train_model(model, np.array(X), np.array(Y)) model.save('model_final_updated.h5') print('model saved!') game = connect4.Connect4() model = None model = create_model() model.save('model_final_updated.h5') agent1_param = { 'name': 'mc_AZ', 'advanced': True, 'simulations': 150, 'explore': 5, 'model': model } agent2_param = {'name': 'mc_standard', 'simulations': 250, 'explore': 5} agents = [ mcts_agent.MCTSAgent(agent1_param), mcts_agent.MCTSAgent(agent2_param) ]
def cpu2(game): game_tree = GameTree.GameTree(depth, game.board, game.turn) # return the move the cpu player will make return minimax_search.run_minimax(game_tree.root, -sys.maxint - 1, sys.maxint, Heuristics.streaks_234)[1] if __name__ == "__main__": draws = 0 red_wins = 0 red_losses = 0 num_games_to_play = 20 connect4_game = connect4.Connect4() first_turns = True while (num_games_to_play > 0): print "cpu1 thinking" if (first_turns): # introduce random variation cpu1_move_num = Heuristics.rando() else: cpu1_move_num = cpu1(connect4_game) status = connect4_game.updateBoard(cpu1_move_num) if (status == "red won"): red_wins += 1 num_games_to_play -= 1 first_turns = True
import connect4 api = connect4.Connect4() def start_session(board): api.print_board(board) session = True winner = False p1_p2 = True column_tally = {} while session: cmd = input('Select a column (1-7): ') if api.handle_input(board, cmd, p1_p2, column_tally): api.print_board(board) winner = api.check_winner(board, cmd, p1_p2, column_tally) if winner: award = 'Player Two' if p1_p2: award = 'Player One' print(award + ' Wins!') session = False p1_p2 = api.switch_players(p1_p2) print('Game Over') print('Play Again?') api.play_again() def start_game(): board = api.create_board()
#Requests a match with an existing game #('REQUEST', int(game_id), int(player_id:1 or 2), str(player_name)) if isinstance(command[1], int): waitingGames.pop(command[1]) playerB = player(command[2], client, command[3]) print "sending: (True,)" msg.send(client, (True, )) gameLists.pop(command[1]).send(playerB) #Requests a new game to be made #('REQUEST', str(game_Name), int(player_id:1 or 2), str(player_name)) else: key = gameID gameID += 1 playerA = player(command[2], client, command[3]) newGame = connect4.Connect4() gameLists[key] = newGame if playerA.id == 1: value = (key, playerA.name, None, command[1]) if playerA.id == 2: value = (key, None, playerA.name, command[1]) waitingGames[key] = value print "sending: (True,)" msg.send(client, (True, )) # gameLists[key].settimeout(None) gameLists[key].start() gameLists[key].send(playerA) else: msgbody = """After connect returns, you have 5 seconds to send one of these message tuples: ('REQUEST', str(game_name), int(player_id:1 or 2), str(player_name))
def __init__(self): self.game = connect4.Connect4() self.player1 = agents.MinimaxPlayer() self.player2 = agents.MinimaxPlayer() self.active_player = self.game.player