def test_heuristic_is_diagonal_getting_double_counted_2(): game = Game() board = Board() game.set_heuristic_score = check_potential_win board.board = [['?', '?', '?', '?', '?', '?', '1'], ['?', '?', '?', '?', '?', '1', '?'], ['?', '?', '?', '?', '1', '?', '?'], ['?', '?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?', '?']] assert game.heuristic_score(board) == 1
def test_heuristic_3_complex(): game = Game() board = Board() game.set_heuristic_score = check_potential_win board.board = [['?', '?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?', '?'], ['?', '?', '1', '?', '1', '?', '?'], ['?', '?', '?', '?', '?', '?', '?'], ['?', '?', '1', '?', '1', '1', '?'], ['?', '1', '1', '?', '1', '?', '?']] assert game.heuristic_score(board) == 5
def test_heuristic_2_w(): game = Game() board = Board() game.set_heuristic_score = check_potential_win_two_weighted board.board = [['?', '?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '2', '?', '?'], ['?', '?', '2', '?', '2', '2', '?'], ['?', '2', '2', '?', '2', '2', '2']] assert game.heuristic_score(board) == -29
def new_game(self, event): self.state = 'waiting' self.root.unbind('<Return>') self.canvas.delete(self.tint) self.canvas.delete(self.text1) self.canvas.delete(self.text2) self.canvas.delete(self.line) self.clear_board() self.b = Board() self.c = Board() self.g = Game() self.update()
def test_minmax_winning_board_scoring(): new_game = Game() new_game.current_color = "1" new_game.board.last_placed_column =3 new_game.board.last_placed_row = 2 new_game.board.last_placed_player = "1" new_game.board.board = [ ['?', '?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?', '?'], ['?', '?', '?', '1', '?', '?', '?'], ['?', '?', '?', '1', '2', '?', '?'], ['?', '?', '?', '1', '2', '?', '?'], ['?', '?', '?', '1', '2', '?', '?']] assert new_game.min_max(new_game.board, "1", 4, -math.inf, math.inf) == (new_game.max, None)
def test_minmax_maximizer_tie(): new_game = Game() new_game.board.last_placed_column = 4 new_game.board.last_placed_row = 0 new_game.board.last_placed_player = "2" new_game.current_color = "1" new_game.board.board = [ ['1', '2', '1', '2', '1', '2', '?'], ['1', '2', '1', '1', '1', '2', '1'], ['2', '1', '2', '2', '2', '1', '2'], ['1', '2', '1', '1', '1', '2', '1'], ['2', '1', '2', '2', '2', '1', '2'], ['2', '1', '2', '1', '2', '1', '2']] assert new_game.min_max(new_game.board,"1", 4, -math.inf, math.inf) == (0,6)
def test_minmax_tied_board_scoring(): new_game = Game() new_game.current_color = "1" new_game.board.last_placed_column = 6 new_game.board.last_placed_row = 0 new_game.board.last_placed_player = "1" new_game.board.board = [ ['1', '2', '1', '2', '1', '2', '1'], ['1', '2', '1', '1', '1', '2', '1'], ['2', '1', '2', '2', '2', '1', '2'], ['1', '2', '1', '1', '1', '2', '1'], ['2', '1', '2', '2', '2', '1', '2'], ['2', '1', '2', '1', '2', '1', '2']] assert new_game.min_max(new_game.board, "1", 4, -math.inf, math.inf) == (0, None)
def test_minmax_maximizer_win(): new_game = Game() new_game.current_color = "1" new_game.board.last_placed_column = 5 new_game.board.last_placed_row = 0 new_game.board.last_placed_player = "2" new_game.board.board = [ ['1', '1', '1', '2', '2', '2', '?'], ['2', '2', '2', '1', '1', '1', '?'], ['1', '1', '1', '2', '2', '2', '1'], ['2', '2', '2', '1', '1', '1', '2'], ['1', '1', '1', '2', '2', '2', '1'], ['2', '2', '2', '1', '1', '1', '2']] _score, column = new_game.min_max(new_game.board,"1", 4, -math.inf, math.inf) assert column == 6
def test_minmax_just_returning_first_column_checked(): new_game = Game() new_game.board.last_placed_column = 3 new_game.board.last_placed_row = 0 new_game.board.last_placed_player = "2" new_game.current_color = "1" new_game.board.board = [ ['?', '?', '1', '2', '1', '2', '1'], ['1', '?', '1', '1', '1', '2', '1'], ['2', '1', '2', '2', '2', '1', '2'], ['1', '2', '1', '1', '1', '2', '1'], ['2', '1', '2', '2', '2', '1', '2'], ['2', '1', '2', '1', '2', '1', '2']] _score, column = new_game.min_max(new_game.board,"1", 8, -math.inf, math.inf) assert column == 1
def test_minmax_diminishing_future_returns(): new_game = Game() new_game.board.last_placed_column = 2 new_game.board.last_placed_row = 1 new_game.board.last_placed_player = "1" new_game.current_color = "2" new_game.set_heuristic_score = check_potential_win new_game.board.board = [ ['?', '?', '?', '?', '?', '?', '?'], ['2', '?', '1', '?', '?', '?', '?'], ['1', '?', '1', '?', '?', '2', '?'], ['2', '?', '2', '1', '?', '1', '?'], ['2', '?', '1', '2', '?', '1', '?'], ['2', '?', '2', '1', '1', '1', '2']] _score, column = new_game.min_max(new_game.board,"2", 5, -math.inf, math.inf) assert column == 4
def play(games, win, ai): player1_wins = 0 player2_wins = 0 ties = 0 last_winner = -1 num_games = 1 while player1_wins < win and player2_wins < win and num_games <= games: g = Game(7, 6, 4) turns_played = 0 if last_winner == -1: turn = random.randint(1, 2) else: turn = last_winner # winner gets to go first while g.get_winner() is None and turns_played < g.rows * g.cols - 1: if turn == YELLOW: next_move = minimax_decision(g, YELLOW, ai) g.insert(next_move, turn) else: g.print_board() next_move = input( '{}\'s turn: '.format('Red' if turn == RED else 'Yellow')) g.insert(int(next_move), turn) # time.sleep(0.1) turns_played += 1 # g.print_board() turn = YELLOW if turn == RED else RED num_games += 1 winner = g.get_winner() if winner == 1: player1_wins += 1 elif winner == 2: player2_wins += 1 else: ties += 1 return [player1_wins, player2_wins, ties]
def main(): net = Net() net.load_state_dict(torch.load(PATH)) g = Game() red = True moves = 0 while not g.won(): board = g.to_tensor(red) prediction = request_move(net, board, chance=1 if red else 0.0) max_value = torch.max(prediction) move = np.zeros((1, 7)) for i, element in enumerate(prediction): if element == max_value: move[:, i] = 1 prediction[i] = 0. column = move.tolist()[0].index(1.) for iter in range(7): try: g.insert(column, RED if red else YELLOW) moves += 1 break except Exception as e: column = (column + 1) % 7 else: return False, moves if g.won(): g.print_board() return red, moves # switch turn if red: red = False else: red = True
def __init__(self): self.size = dim self.root = Tk(className='Connect Four') self.state = 'waiting' self.cpu = True self.g = Game() self.b = Board() self.chip = None # Draw Upper Control Panel self.control = Frame(self.root, width=WIDTH, height=CONTROL_HEIGHT, bg=BACKGROUND_COLOR) self.control.pack(fill=tk.BOTH, expand=tk.YES) self.slide = Canvas(self.control, width=WIDTH, height=CHIP_PADDING * 2 + CHIP_DIAMETER, bg=BACKGROUND_COLOR) self.slide.pack() # Draw Board self.canvas = Canvas(self.root, width=WIDTH, height=HEIGHT, bg=BOARD_COLOR, bd=0, highlightthickness=0) self.canvas.bind("<Button-1>", self.drop_chip) self.clear_board() self.canvas.pack() self.update() # Constantly checks pointer position to move chip slider self.root.mainloop()
def play_wo_human(agent1, agent2, first_player): ''' Default: agent 1 plays RED agent 2 plays YELLOW ''' turn_dict = {1: RED, 2: YELLOW} agent_turn_dict = {RED: agent1.name, YELLOW: agent2.name} times = {agent1.name: 0, agent2.name: 0} count_moves = 0 winner = None g = Game() # #randomize turn who starts first # turn_ind = random.randint(1,2) # turn = turn_dict[turn_ind] turn = turn_dict[first_player] print "Starting with turn = ", agent_turn_dict[turn] while True: g.printBoard() if turn == RED: print agent1.name, "'s turn" start = time.time() move = agent1.play_move() end = time.time() times[agent1.name] += (end - start) if move is None: print "DRAW GAME" break w = g.insert(move, turn) if w: winner = agent_turn_dict[w] print "####################WINNER: ", winner, " TOTAL MOVES: ", count_moves break agent2.play_opponent_move(move) else: print agent2.name, "'s turn" start = time.time() move = agent2.play_move() end = time.time() times[agent2.name] += (end - start) if move is None: print "DRAW GAME" break w = g.insert(move, turn) if w: winner = agent_turn_dict[w] print "#################WINNER: ", winner, " TOTAL MOVES: ", count_moves break agent1.play_opponent_move(move) count_moves += 1 turn = YELLOW if turn == RED else RED return winner, count_moves, times
def play(games, win, ai): player1_wins = 0 player2_wins = 0 ties = 0 last_winner = -1 num_games = 1 while player1_wins < win and player2_wins < win and num_games <= games: g = Game(7, 6, 4) turns_played = 0 if last_winner == -1: turn = random.randint(1,2) else: turn = last_winner # winner gets to go first while g.get_winner() is None and turns_played < g.rows * g.cols - 1: if turn == YELLOW: next_move = minimax_decision(g,YELLOW,ai) g.insert(next_move, turn) else: g.print_board() next_move = input('{}\'s turn: '.format('Red' if turn == RED else 'Yellow')) g.insert(int(next_move), turn) # time.sleep(0.1) turns_played += 1 # g.print_board() turn = YELLOW if turn == RED else RED num_games += 1 winner = g.get_winner() if winner == 1: player1_wins += 1 elif winner == 2: player2_wins += 1 else: ties += 1 return [player1_wins, player2_wins, ties]
def play(games, win, player1, player2): player1_wins = 0 player2_wins = 0 ties = 0 last_winner = -1 num_games = 1 while player1_wins < win and player2_wins < win and num_games < games: g = Game(7, 6, 4) turns_played = 0 if last_winner == -1: turn = random.randint(1, 2) else: turn = last_winner # winner gets to go first while g.get_winner() is None and turns_played < g.rows * g.cols - 1: if turn == YELLOW: next_move = minimax_decision(g, YELLOW, player2) g.insert(next_move, turn) else: next_move = minimax_decision(g, RED, player1) g.insert(next_move, turn) # time.sleep(0.1) turns_played += 1 # g.print_board() turn = YELLOW if turn == RED else RED num_games += 1 winner = g.get_winner() last_winner = winner if winner == 1: player1_wins += 1 elif winner == 2: player2_wins += 1 else: ties += 1 return [player1_wins, player2_wins, ties]
def play(games, win, player1, player2): player1_wins = 0 player2_wins = 0 ties = 0 last_winner = -1 num_games = 1 while player1_wins < win and player2_wins < win and num_games < games: g = Game(7, 6, 4) turns_played = 0 if last_winner == -1: turn = random.randint(1,2) else: turn = last_winner # winner gets to go first while g.get_winner() is None and turns_played < g.rows * g.cols - 1: if turn == YELLOW: next_move = minimax_decision(g,YELLOW,player2) g.insert(next_move, turn) else: next_move = minimax_decision(g,RED,player1) g.insert(next_move, turn) # time.sleep(0.1) turns_played += 1 # g.print_board() turn = YELLOW if turn == RED else RED num_games += 1 winner = g.get_winner() last_winner = winner if winner == 1: player1_wins += 1 elif winner == 2: player2_wins += 1 else: ties += 1 return [player1_wins, player2_wins, ties]
def play_w_human(): g = Game() # agent = MinimaxAgent() #MCTSAgent() agent = QLearningAgent('q_values') turn = RED while True: g.printBoard() if turn == RED: row = input( '{}\'s turn: '.format('Red' if turn == RED else 'Yellow')) w = g.insert(int(row), turn) agent.play_opponent_move(int(row)) else: move = agent.play_move() w = g.insert(move, turn) if w: print "WINNER: ", w break turn = YELLOW if turn == RED else RED
# -*- coding: utf-8 -*- """ Created on Wed Apr 20 13:53:01 2016 @author: Bang """ from itertools import groupby, chain from connect_four import Game from connect_four import diagonals_pos as diag_pos from connect_four import diagonals_neg as diag_neg from copy import deepcopy import random search_depth = 20 game = Game() rows = game.rows cols = game.cols required_to_win = 4 NONE = 0 RED = 1 YELLOW = 2 def insert(board, column, color): """Insert the color in the given column.""" c = board[column] if c[0] != NONE: raise Exception('Column is full')
def test_get_opposite_symbol(): game = Game() assert game.get_opposite_symbol("1") == "2" assert game.get_opposite_symbol("2") == "1"
from connect_four import Game Game(mode='cc').play()
class Window: def __init__(self): self.size = dim self.root = Tk(className='Connect Four') self.state = 'waiting' self.cpu = True self.g = Game() self.b = Board() self.chip = None # Draw Upper Control Panel self.control = Frame(self.root, width=WIDTH, height=CONTROL_HEIGHT, bg=BACKGROUND_COLOR) self.control.pack(fill=tk.BOTH, expand=tk.YES) self.slide = Canvas(self.control, width=WIDTH, height=CHIP_PADDING * 2 + CHIP_DIAMETER, bg=BACKGROUND_COLOR) self.slide.pack() # Draw Board self.canvas = Canvas(self.root, width=WIDTH, height=HEIGHT, bg=BOARD_COLOR, bd=0, highlightthickness=0) self.canvas.bind("<Button-1>", self.drop_chip) self.clear_board() self.canvas.pack() self.update() # Constantly checks pointer position to move chip slider self.root.mainloop() # Clears all chips from the visual board display def clear_board(self): for row in range(self.size[0]): for col in range(self.size[1]): self.canvas.create_oval( PADDING_X + (CHIP_DIAMETER + CHIP_PADDING) * col, PADDING_Y + (CHIP_DIAMETER + CHIP_PADDING) * row, PADDING_X + (CHIP_DIAMETER + CHIP_PADDING) * col + CHIP_DIAMETER, PADDING_Y + (CHIP_DIAMETER + CHIP_PADDING) * row + CHIP_DIAMETER, fill=BACKGROUND_COLOR) def update(self): while self.state == 'waiting': self.root.update() # Chip Slider self.x = self.root.winfo_pointerx() - self.root.winfo_rootx() if self.chip != None: self.slide.delete(self.chip) if self.g.turn % 2 != 0: color = COLOR_P1 else: color = COLOR_P2 self.chip = self.slide.create_oval(self.x - CHIP_DIAMETER / 2, CHIP_PADDING, CHIP_DIAMETER / 2 + self.x, CHIP_PADDING + CHIP_DIAMETER, fill=color) # Resets everything for a new game def new_game(self, event): self.state = 'waiting' self.root.unbind('<Return>') self.canvas.delete(self.tint) self.canvas.delete(self.text1) self.canvas.delete(self.text2) self.canvas.delete(self.line) self.clear_board() self.b = Board() self.c = Board() self.g = Game() self.update() # Adds a chip to the correct column of the matrix def drop_chip(self, event): if not self.b.gameover: col_width = (WIDTH - PADDING_X * 2) / dim[1] col = int((self.x - PADDING_X) // col_width) if self.b.chips[0][col] == 0: self.state == 'dropping' chip = self.g.create_chip() chip.drop_chip(self.b, col) self.draw_chip(chip, chip.pos[0], col) # Draws a chip on the visual board def draw_chip(self, chip, row, col): l = PADDING_X + (CHIP_DIAMETER + CHIP_PADDING) * col u = PADDING_Y + (CHIP_DIAMETER + CHIP_PADDING) * row r = PADDING_X + (CHIP_DIAMETER + CHIP_PADDING) * col + CHIP_DIAMETER d = PADDING_Y + (CHIP_DIAMETER + CHIP_PADDING) * row + CHIP_DIAMETER # for row in range(row): # self.canvas.create_oval(l,u,r,d,fill=chip.color) # time.sleep(0.2) # self.canvas.create_oval(l,u,r,d,fill=BACKGROUND_COLOR) self.canvas.create_oval(l, u, r, d, fill=chip.color) self.turn() # Checks if game is over and starts the next move def turn(self): check = self.b.check() if self.b.gameover == True: # New Game self.state = 'gameover' self.gameover(check) else: self.g.turn += 1 if self.g.turn == 43: self.state = 'gameover' self.gameover() if self.cpu: if self.g.turn % 2 == 0: self.cpu_drop_chip() # Adds a chip to the column of the matrix determined by ai def cpu_drop_chip(self): if not self.b.gameover: col = ai.minimax(self.g, self.b, 4, True)[0] self.state == 'dropping' chip = self.g.create_chip() chip.drop_chip(self.b, col) self.draw_chip(chip, chip.pos[0], col) # Draws a line through a 4-in-a-row def draw_line(self, direction, pos): row1 = pos[0] col1 = pos[1] row2 = pos[2] col2 = pos[3] x1 = PADDING_X + (CHIP_DIAMETER + CHIP_PADDING) * col1 y1 = PADDING_Y + (CHIP_DIAMETER + CHIP_PADDING) * row1 x2 = PADDING_X + (CHIP_DIAMETER + CHIP_PADDING) * col2 + CHIP_DIAMETER y2 = PADDING_Y + (CHIP_DIAMETER + CHIP_PADDING) * row2 + CHIP_DIAMETER if direction == 'h': x1 -= CHIP_PADDING / 2 y1 += CHIP_DIAMETER / 2 x2 += CHIP_PADDING / 2 y2 -= CHIP_DIAMETER / 2 elif direction == 'v': x1 += CHIP_DIAMETER / 2 y1 += CHIP_DIAMETER + CHIP_PADDING / 2 x2 -= CHIP_DIAMETER / 2 y2 -= CHIP_DIAMETER + CHIP_PADDING / 2 elif direction == 'p': y1 += CHIP_DIAMETER y2 -= CHIP_DIAMETER self.line = self.canvas.create_line(x1, y1, x2, y2, fill=LINE_COLOR) def gameover(self, check): self.draw_line(check[0], check[1]) self.tint = self.canvas.create_rectangle(0, 0, WIDTH, HEIGHT, fill="white", stipple='gray50') self.text1 = self.canvas.create_text(WIDTH / 2, HEIGHT / 2 - 30, text='Game Over', font=('Helvetica', 50, 'bold'), fill='black') self.text2 = self.canvas.create_text(WIDTH / 2, HEIGHT / 2 + 30, text='Press ENTER to play again', font=('Helvetica', 25, 'bold'), fill='black') self.root.bind('<Return>', self.new_game)
def test_minmax_alpha_beta_breaking_min_max_block(): alpha_beta = Game() min_max = Game() alpha_beta.current_color = "2" min_max.current_color = "2" alpha_beta.do_alpha_beta_pruning = True min_max.do_alpha_beta_pruning = False alpha_beta.set_heuristic_score = check_potential_win min_max.set_heuristic_score = check_potential_win alpha_beta.board.board = [ ['?', '?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?', '2'], ['1', '?', '1', '1', '?', '2', '2']] min_max.board.board = [ ['?', '?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?', '2'], ['1', '?', '1', '1', '?', '2', '2']] _score_1, column_1 = alpha_beta.min_max(alpha_beta.board,"2", 5, -math.inf, math.inf) _score_2, column_2 = min_max.min_max(min_max.board,"2", 5, -math.inf, math.inf) assert column_1 == column_2 assert column_1 == 1 assert column_2 == 1
def test_get_all_moves(): new_game = Game() new_game.board.board = [ ['?', '?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?', '?'], ['?', '?', '?', '1', '?', '?', '?']] all_moves_board_objects = new_game.board.get_all_moves("2") list_of_just_boards_tuple = [] for board_move_tuple in all_moves_board_objects: board = board_move_tuple[0] column = board_move_tuple[1] actual_board = board.board list_of_just_boards_tuple.append((actual_board,column)) assert list_of_just_boards_tuple == [([ ['?', '?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?', '?'], ['2', '?', '?', '1', '?', '?', '?']],0), ([ ['?', '?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?', '?'], ['?', '2', '?', '1', '?', '?', '?']],1), ([ ['?', '?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?', '?'], ['?', '?', '2', '1', '?', '?', '?']],2), ([ ['?', '?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?', '?'], ['?', '?', '?', '2', '?', '?', '?'], ['?', '?', '?', '1', '?', '?', '?']],3), ([ ['?', '?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?', '?'], ['?', '?', '?', '1', '2', '?', '?']],4), ([ ['?', '?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?', '?'], ['?', '?', '?', '1', '?', '2', '?']],5), ([ ['?', '?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?', '?'], ['?', '?', '?', '1', '?', '?', '2']],6)]