def test_try_move_on_valid_column(self): board = Board() for i in range(board.width): col = board.try_move(i) # expected position is bottom of board assert col == board.height - 1
def test__build_winning_zones_map(self): board = Board() zones_map = board._build_winning_zones_map() default_seven_by_size_zones_map = [ [[0, 24, 45], [4, 24, 25, 49], [8, 24, 25, 26, 53], [12, 24, 25, 26, 60], [16, 25, 26, 64], [20, 26, 68]], [[0, 1, 27, 46], [4, 5, 27, 28, 45, 50], [8, 9, 27, 28, 29, 49, 54, 60], [12, 13, 27, 28, 29, 53, 59, 64], [16, 17, 28, 29, 63, 68], [20, 21, 29, 67]], [[0, 1, 2, 30, 47], [4, 5, 6, 30, 31, 46, 51, 60], [8, 9, 10, 30, 31, 32, 45, 50, 55, 59, 64], [12, 13, 14, 30, 31, 32, 49, 54, 58, 63, 68], [16, 17, 18, 31, 32, 53, 62, 67], [20, 21, 22, 32, 66]], [[0, 1, 2, 3, 33, 48, 60], [4, 5, 6, 7, 33, 34, 47, 52, 59, 64], [8, 9, 10, 11, 33, 34, 35, 46, 51, 56, 58, 63, 68], [12, 13, 14, 15, 33, 34, 35, 45, 50, 55, 57, 62, 67], [16, 17, 18, 19, 34, 35, 49, 54, 61, 66], [20, 21, 22, 23, 35, 53, 65]], [[1, 2, 3, 36, 59], [5, 6, 7, 36, 37, 48, 58, 63], [9, 10, 11, 36, 37, 38, 47, 52, 57, 62, 67], [13, 14, 15, 36, 37, 38, 46, 51, 56, 61, 66], [17, 18, 19, 37, 38, 50, 55, 65], [21, 22, 23, 38, 54]], [[2, 3, 39, 58], [6, 7, 39, 40, 57, 62], [10, 11, 39, 40, 41, 48, 61, 66], [14, 15, 39, 40, 41, 47, 52, 65], [18, 19, 40, 41, 51, 56], [22, 23, 41, 55]], [[3, 42, 57], [7, 42, 43, 61], [11, 42, 43, 44, 65], [15, 42, 43, 44, 48], [19, 43, 44, 52], [23, 44, 56]] ] assert zones_map == default_seven_by_size_zones_map
def test_valid_move_on_valid_move(self): board = Board() col = 2 # at start, a peice should be placed on the bottom assert board.valid_move(board.height - 1, col) assert not board.valid_move(board.height - 2, col)
def test_terminal_on_finished_game(self): board = Board() # A connect four game is considered finished when all columns # are full. In the Board class, this is indicated by non-zero values # in the first row for i in range(board.width): board.board[0][i] = 1 assert board.terminal()
def test_try_move_on_invalid_column(self): board = Board() column_to_fill = 1 for i in range(board.height): board.board[i][column_to_fill] = 1 # Placement in column should now fail assert board.try_move(column_to_fill) == -1 # Other columns should still be valid assert board.try_move(column_to_fill + 1) >= 0
def test_legal_moves(self): board = Board() # fill rows 0, 3, and 5 for i in range(board.height): board.board[i][0] = board.board[i][3] = board.board[i][5] = 1 legal_columns = board.legal_moves() expected = [1, 2, 4, 6] for i, col in enumerate(legal_columns): assert expected[i] == col
def evaluateBoardState(self, board: Board): """ Your evaluation function should look at the current state and return a score for it. As an example, the random agent provided works as follows: If the opponent has won this game, return -1. If we have won the game, return 1. If neither of the players has won, return a random number. """ """ These are the variables and functions for board objects which may be helpful when creating your Agent. Look into board.py for more information/descriptions of each, or to look for any other definitions which may help you. Board Variables: board.width board.height board.last_move board.num_to_connect board.winning_zones board.score_array board.current_player_score Board Functions: get_cell_value(row, col) try_move(col) valid_move(row, col) valid_moves() terminal(self) legal_moves() next_state(turn) winner() """ evaluation_value = 0 for row, col in board.valid_moves(): next_board = board.next_state(1, col) horizonatal_lines = self.get_horizontal_lines(next_board) vertical_lines = self.get_vertical_lines(next_board) diagonal_down_lines = self.get_diagonal_down_lines(next_board) diagonal_up_lines = self.get_diagonal_up_lines(next_board) lines = horizonatal_lines + vertical_lines + diagonal_down_lines + diagonal_up_lines evaluation_value += self.calc_line_weights(lines) # col = last_move[1] # col_val = 8 # if col == 0 or col == 6: # col_val = 0 # if col == 1 or col == 5: # col_val = 1 # if col == 2 or col == 4: # col_val = 2 return evaluation_value
class TestNode: board = Board() def test_add_child(self): node = Node(self.board) child_state = self.board # TODO really should be a different board state move = 3 node.add_child(child_state, move) assert len(node.children) is 1 assert type(node.children[0]) is Node assert len(node.children_move) is 1 assert node.children_move[0] is 3 def test_update(self): node = Node(self.board) # initial values assert 0.0 == node.reward assert 1 == node.visits node.update(3.3) assert 3.3 == node.reward assert 2 == node.visits node.update(0.1) assert 3.4 == node.reward assert 3 == node.visits
def __init__(self, player_one, player_two, board_height, board_width, fast_play=False, auto_close=False): self.player_one = player_one self.player_two = player_two self.current_player = self.player_one self.player_one.id = self.PLAYER_ONE_ID self.player_two.id = self.PLAYER_TWO_ID self.board = Board(height=board_height, width=board_width) self.fast_play = fast_play self.exit_on_game_end = auto_close self.metrics = {'num_moves': 0, 'all_moves': []}
def test_player_token_locations(): player_id = "hello" player = MockPlayer(player_id) board = Board() board.board[1][1] = player_id board.board[2][1] = player_id board.board[2][2] = "not player id" board.board[2][4] = player_id board.board[5][5] = player_id expected = [(1, 1), (2, 1), (2, 4), (5, 5)] assert expected == list(util.player_token_locations(board, player))
def getMovesinTime(board, maxTime): maxTime = maxTime / 1000.0 result = [] start = getTime() # board = connectfour.board.Board() # board.reset() avaialblePositions = Board.getPossibleBoardPositions(board) sortedPositions = getSortedPositions(avaialblePositions) for i in sortedPositions: p1 = players.negamax.NegamaxPlayer(2) # # print "===============" # # print(i[0]) # # print "===============" # i[0].playerTurn = 1 move = p1.getMove(i[0], 1) # print "Time Taken : " + str((getTime() - start) * 1000) if (getTime() - start < maxTime): result.append((i[0], move)) else: break return result
def reset(self): self.board = Board(height=self.board.height, width=self.board.width) self.current_player = self.player_one
def test_terminal_on_unfinished_game(self): board = Board() assert not board.terminal() board.board[1][1] = 1 assert not board.terminal()
def test__num_of_winning_zones(self): board = Board() expected = 69 assert expected == board._num_of_winning_zones(num_to_connect=4)
from connectfour.agents.agent_student import StudentAgent from connectfour.board import Board import numpy as np agent_student = StudentAgent("Sample Name") agent_student.id = 1 agent_student2 = StudentAgent("Agent 2") agent_student2.id = 2 simple_board = Board() # for j in range(1): # for i in range(2): # simple_board = simple_board.next_state(agent_student.id, i) # simple_board = simple_board.next_state(agent_student.id, 3) # simple_board = simple_board.next_state(agent_student.id, 2) # # simple_board = simple_board.next_state(agent_student.id % 2 + 1, 3) # simple_board = simple_board.next_state(agent_student.id % 2 + 1, 2) # simple_board = simple_board.next_state(agent_student.id, 3) # simple_board = simple_board.next_state(agent_student.id, 3) simple_board.board = [[0, 0, 1, 1, 2, 0, 2], [0, 0, 2, 1, 2, 0, 1], [0, 1, 2, 2, 1, 0, 2], [1, 2, 1, 1, 1, 0, 1], [2, 1, 2, 1, 2, 0, 2], [2, 2, 2, 1, 2, 1, 1]] print(np.matrix(simple_board.board)) print("Player 1:") #print(agent_student.evaluateBoardState(simple_board))