コード例 #1
0
class StudentAI():

    def __init__(self,row,col,p):
        self.row = row
        self.col = col
        self.p = p
        self.board = Board(row,col,p)
        self.board.initialize_game()
        self.color = ''
        self.opponent = {1:2,2:1}
        self.color = 2

    def get_move(self, move):
        if len(move) != 0:
            self.board.make_move(move, self.opponent[self.color])
        else:
            self.color = 1

        moves = self.board.get_all_possible_moves(self.color)
        tree_depth = 4
        
		# for beta alpha pruning
		alpha = -math.inf
        beta = math.inf
        
		# best moves list
		best_moves = []
        
		# Get best moves
		for row in moves:
コード例 #2
0
 def train_one_episode(self):
     # This is training function for one episode, start a new board and
     # update Q value until win or loss
     # QUESTION IS HOW TO DECIDE OPPONENT MOVE? BY SELF-TRAIN? AND HOW TO SELF-TRAIN?
     new_board = Board()
     new_board.initialize_game()
     turn = ''
コード例 #3
0
ファイル: StudentAI.py プロジェクト: ColePoulos1/CheckersAI
 def __init__(self, col, row, p):
     self.col = col
     self.row = row
     self.p = p
     self.board = Board(col, row, p)
     self.board.initialize_game()
     self.color = 2
コード例 #4
0
 def __init__(self, col, row, k, g):
     self.k = k
     self.col = col
     self.row = row
     self.board = Board(col, row, k, g)
     self.g = True if g == 1 else False
     self.win = 10**k
コード例 #5
0
 def __init__(self, col, row, k, g):
     """Board class used to track the moves."""
     Board.__init__(self, col, row, k, g)
     self.available = set()
     if not g:
         for i in range(row):
             for j in range(col):
                 self.available.add((i, j))
コード例 #6
0
 def __init__(self, col, row, p):
     self.col = col
     self.row = row
     self.p = p
     self.board = Board(col, row, p)
     self.board.initialize_game()
     self.color = ''
     self.opponent = {1: 2, 2: 1}
     self.color = 2
コード例 #7
0
class StudentAI():

    def __init__(self,col,row,p):
        self.col = col
        self.row = row
        self.p = p
        self.board = Board(col,row,p)
        self.board.initialize_game()
        self.color = ''
        self.opponent = {1:2,2:1}
        self.color = 2
        self.count = 0
    def get_move(self,move):
        if len(move) != 0:
            self.board.make_move(move,self.opponent[self.color])
        else:
            self.color = 1
        moves = self.board.get_all_possible_moves(self.color)
        # index = randint(0,len(moves)-1)
        # inner_index =  randint(0,len(moves[index])-1)
        # move = moves[index][inner_index]
        if len(moves) == 1 and len(moves[0]) == 1:
            move = moves[0][0]
        if self.count < 15:
            mct = MonteCarloTree(self.board, self.color, self.opponent, (10, 0, -10))
            move = mct.get_action(10, 0)
            self.board.make_move(move, self.color)
        else:
            mct = MonteCarloTree(self.board, self.color, self.opponent, (10, 0, -10))
            move = mct.get_action(10, 0)
            self.board.make_move(move, self.color)
        return move
コード例 #8
0
ファイル: StudentAI.py プロジェクト: tuantruong5770/CheckerAI
 def __init__(self, col, row, p):
     self.col = col
     self.row = row
     self.p = p
     self.board = Board(col, row, p)
     self.board.initialize_game()
     self.color = ''
     self.opponent = {1: 2, 2: 1}
     self.color = 2
     self.search_lim = 5
     self.current_node = TreeNode(None, self.color)
コード例 #9
0
ファイル: StudentAI.py プロジェクト: ethanlouie/DeepBlueDream
 def __init__(self, col, row, p):
     self.col = col
     self.row = row
     self.p = p
     self.board = Board(col, row, p)
     self.board.initialize_game()
     self.color = 2
     self.mcts = MCTS(TreeNode(self.board, self.color, None, None))
     self.total_time_remaining = 479
     self.time_divisor = row * col * 0.5
     self.timed_move_count = 2
コード例 #10
0
 def __init__(self, col, row, p):
     self.col = col
     self.row = row
     self.p = p
     self.board = Board(col, row, p)
     self.board.initialize_game()
     self.color = ''
     self.opponent = {1: 2, 2: 1}
     self.color = 2
     self.movecount = 0
     self.simulate_times = 100
コード例 #11
0
ファイル: CA.py プロジェクト: GaryZLi/checkers_ai
 def __init__(self, col, row, p):
     self.col = col
     self.row = row
     self.p = p
     self.board = Board(col, row, p)
     self.board.initialize_game()
     self.color = ''
     self.opponent = {1: 2, 2: 1}
     self.color = 2
     self.bestMove = None
     self.blackVal = 0  #-------
     self.whiteVal = 0  #-------
コード例 #12
0
ファイル: StudentAI.py プロジェクト: emmohac/delmd
 def __init__(self, col, row, p):
     self.col = col
     self.row = row
     self.p = p
     self.board = Board(col, row, p)
     self.board.initialize_game()
     self.number_of_move = 0
     self.EARLY_GAME = 10
     self.MID_GAME = 20
     self.END_GAME = 30
     self.run_time_depth = 5
     self.opponent = {1: 2, 2: 1}
     self.color = 2
コード例 #13
0
 def __init__(self, col, row, p):
     self.col = col
     self.row = row
     self.p = p
     self.board = Board(col, row, p)
     self.board.initialize_game()
     self.color = ''
     self.opponent = {1: 2, 2: 1}
     self.color = 2
     self.halftime = time.time() + 240
     self.endtime = time.time() + 360
     self.finaltime = time.time() + 460
     self.root = Node(0, 0)
コード例 #14
0
    def simulate_lr(self, color):
        # simulate one time
        # record all X features to feature_matrix
        # update the y value accordingly

        print("entering simulations")
        newboard = Board(self.col, self.row, self.p)
        newboard.initialize_game()

        feature_list_b = []
        feature_list_w = []

        win = 0
        ### TODO: Fixing Current move in a new board
        curr_turn = self.opponent[color]

        for turn in range(50):
            if newboard.is_win(color) == color:
                win = 1
                break
            elif newboard.is_win(self.opponent[color]) == self.opponent[color]:
                break
            move = self.minimax_move(newboard.get_all_possible_moves(curr_turn))
            newboard.make_move(move, curr_turn)

            b, w = self.get_X(self.board)
            feature_list_b.append(b)
            feature_list_w.append(w)

            self.feature_matrix = np.append(self.feature_matrix, np.array([b, w]), axis=0)
            print(self.feature_matrix)
            curr_turn = self.opponent[curr_turn]

        else:
            win = 0.5

        # matrix = np.array([feature_list_b, feature_list_w])
        # feature_matrix = np.hstack((matrix, np.zeros((matrix.shape[0], 1))))

        # TODO: Fixing y value update
        if win == 1 and color == 1:
            for fb in feature_list_b:
                index = np.where(fb in self.feature_matrix[:, 0:self.feature_size])
                if index == []:
                    self.feature_matrix = np.append(self.feature_matrix, np.array([b, w]), axis=0)
                self.feature_matrix[index, self.feature_size] += 1

        elif win == 0 and color == 1:
            for fw in feature_list_w:
                index = np.where(fw in self.feature_matrix[:, 0:self.feature_size])
                if index == []:
                    self.feature_matrix = np.append(self.feature_matrix, np.array([b, w]), axis=0)
                self.feature_matrix[index, self.feature_size] += 1

        return win
コード例 #15
0
    def __init__(self, col, row, p):
        self.col = col
        self.row = row
        self.p = p
        self.board = Board(col, row, p)
        self.board.initialize_game()
        self.color = ''
        self.opponent = {1: 2, 2: 1}
        self.color = 2

        self.search_depth = 5
        self.debug = True
        self.time_used = 0

        self.transposition_table = dict()
コード例 #16
0
 def __init__(self, col, row, p):
     self.col = col
     self.row = row
     self.p = p
     self.board = Board(col, row, p)
     self.board.initialize_game()
     self.color = ''
     self.opponent = {1: 2, 2: 1}
     self.color = 2
     self.depth = 0
     self.turn = 0
     self.control = asyncio.get_event_loop()
     self.iterative_depth_limit = self.INITIAL_DEPTH_LIMIT
     self.time_left = TimeFlags.UNDER
     self.time_used = 0
     self.upper_depth_limit = float('inf')
コード例 #17
0
ファイル: ManualAI.py プロジェクト: timhakobian/CheckerAI
 def __init__(self, col, row, p):
     """
     Intializes manualAI
     @param row: no of rows in the board
     @param col: no of columns in the board
     @param k: no of rows to be filled with checker pieces at the start
     @return :
     @raise :
     """
     self.col = col
     self.row = row
     self.p = p
     self.board = Board(col, row, p)
     self.board.initialize_game()
     self.color = 2
     self.opponent = {1: 2, 2: 1}  # to switch turns after each turn
コード例 #18
0
    def __init__(self, col, row, p):
        self.col = col
        self.row = row
        self.p = p
        self.board = Board(col, row, p)
        self.board.initialize_game()
        self.color = ''
        self.opponent = {1: 2, 2: 1}
        self.color = 2

        ##ADDED
        self.calc_time = 1999
        self.visited_tree = []
        self.C = 1.6
        self.colors = {1: "B", 2: "W"}
        self.letters = {"B": 1, "W": 2}
コード例 #19
0
    def __init__(self, col, row, p):
        self.col = col
        self.row = row
        self.p = p
        self.board = Board(col, row, p)
        self.board.initialize_game()
        self.color = ''
        self.opponent = {1: 2, 2: 1}
        self.color = 2
        self.movecount = 1

        self.file = f"{self.col}-{self.row}-{self.color}-{randint(0, 500)}-test.txt"
        self.start = time.time()

        self.theta1, self.theta2 = self.get_theta()
        self.cutoff = self.get_cutoff()
コード例 #20
0
class StudentAI():

    def __init__(self,col,row,p):
        self.col = col
        self.row = row
        self.p = p
        self.board = Board(col,row,p)
        self.board.initialize_game()
        self.color = ''
        self.opponent = {1:2,2:1}
        self.color = 2

    def get_move(self, move):
        #print(self.color)
        if len(move) != 0:
            self.board.make_move(move,self.opponent[self.color])
        else:
            self.color = 1
        moves = self.board.get_all_possible_moves(self.color)

        #self.train()
        self.simulate_lr(self.color)

        #index = randint(0,len(moves)-1)
        #inner_index =  randint(0,len(moves[index])-1)
        #move = moves[index][inner_index]

        ql = QLearning()
        move = ql.make_action(self.board, moves)
        self.board.make_move(move, self.color)
        self.movecount += 1
        return move
コード例 #21
0
class StudentAI():
    col = 0
    row = 0
    k = 0
    g = 0

    def __init__(self,col,row,k,g):
        self.g = g
        self.col = col
        self.row = row
        self.k = k
        self.board = Board(col,row,k,g)

    def get_move(self,move):
        self.board.make_move(move,1)
        if self.g == 0:
            return Move(randint(0,self.col-1),randint(0,self.row-1))
        else:
            return Move(randint(0,self.col-1),0)
コード例 #22
0
 def __init__(self, col, row, p):
     self.col = col
     self.row = row
     self.p = p
     self.board = Board(col, row, p)
     self.board.initialize_game()
     self.color = ''
     self.opponent = {1: 2, 2: 1}
     self.color = 2
     self.depth = 0
     self.turn = 0
     self.control = asyncio.get_event_loop()
     self.iterative_depth_limit = self.INITIAL_DEPTH_LIMIT
     self.time_left = TimeFlags.UNDER
     self.time_used = 0
     self.upper_depth_limit = float('inf')
     self.time_limit = 480  # 8 minutes
     self.late_game_flag = False
     self.heuristic_flag = 1  #use ieee1, if 2, use ieee2
     self.flag_just_changed = 0
コード例 #23
0
 def __init__(self, col, row, p):
     self.col = col
     self.row = row
     self.p = p
     self.board = Board(col, row, p)
     self.board.initialize_game()
     self.color = ''
     self.opponent = {1: 2, 2: 1}
     self.color = 2
     #---------------------------------#
     self.area = self.row * self.col
     self.count = 0
     if self.area <= 39:
         self.depth = 8
     elif self.area <= 49:
         self.depth = 5
     elif self.area <= 79:
         self.depth = 4
     else:
         self.depth = 4
コード例 #24
0
ファイル: ManualAI.py プロジェクト: timhakobian/CheckerAI
class ManualAI():
    """
    This class describes the ManualAI.
    """
    def __init__(self, col, row, p):
        """
        Intializes manualAI
        @param row: no of rows in the board
        @param col: no of columns in the board
        @param k: no of rows to be filled with checker pieces at the start
        @return :
        @raise :
        """
        self.col = col
        self.row = row
        self.p = p
        self.board = Board(col, row, p)
        self.board.initialize_game()
        self.color = 2
        self.opponent = {1: 2, 2: 1}  # to switch turns after each turn

    def get_move(self, move):
        """
        get_move function for manualAI called from the gameloop in the main module.
        @param move: A Move object describing the move.
        @return res_move: A Move object describing the move manualAI wants to make. This move is basically console input.
        @raise :
        """
        if move.seq:
            # if move.seq is not an empty list
            self.board.make_move(move, self.opponent[self.color])
        else:
            self.color = 1
        moves = self.board.get_all_possible_moves(self.color)
        #print(moves)
        while True:
            try:
                for i, checker_moves in enumerate(moves):
                    print(i, ':[', end="")
                    for j, move in enumerate(checker_moves):
                        print(j, ":", move, end=", ")
                    print("]")
                index, inner_index = map(
                    lambda x: int(x),
                    input("Select Move {int} {int}: ").split(
                    ))  # input is from console is handled here.
                res_move = moves[index][inner_index]
            except KeyboardInterrupt:
                raise KeyboardInterrupt
            except:
                print('invalid move')
                continue
            else:
                break
        self.board.make_move(res_move, self.color)
        return res_move
コード例 #25
0
ファイル: StudentAI.py プロジェクト: Timmichi/CheckerAI
 def __init__(self, col, row, p):
     self.col = col
     self.row = row
     self.p = p
     self.startnode = Node()
     self.startnode.board = Board(col, row, p)
     self.startnode.board.initialize_game()
     self.color = ''
     self.opponent = {1: 2, 2: 1}
     self.color = 2
     self.maxiter = 100
     self.curriter = 0
コード例 #26
0
    def __init__(self,col,row,p):
        self.col = col
        self.row = row
        self.p = p
        self.board = Board(col,row,p)
        self.board.initialize_game()
        self.color = ''
        self.opponent = {1:2,2:1}
        self.color = 2

        # new params
        '''
            new data clearification:
                feature_matrix = [[X1, X2, ..., X_feature_size, Y],
                                    ...
                                    [X1m, X2m, ..., X_feature_size_m, Y]]
        '''
        self.movecount = 0
        self.feature_size = 5
        self.thetas = np.random.rand(self.feature_size)
        self.feature_matrix = np.empty((0, self.feature_size))
コード例 #27
0
    def __init__(self, col, row, p):
        self.col = col
        self.row = row
        self.p = p
        self.board = Board(col, row, p)
        self.board.initialize_game()
        self.color = ''
        self.opponent = {1: 2, 2: 1}
        self.color = 2

        # ---------- What we added -----------

        self.calc_time = datetime.timedelta(seconds=3)
        self.max_moves = 35
        self.wins = {}
        self.plays = {}
        self.max_depth = 0
        self.C = 1.4
        self.colors = {1: "B", 2: "W"}
        self.letters = {"B": 1, "W": 2}
        self.states = []
コード例 #28
0
ファイル: StudentAI.py プロジェクト: tuantruong5770/CheckerAI
    def simulate(self, player):
        win = 0
        counter = 0
        fake_board = Board(self.col, self.row, self.p)
        self.copy_board(fake_board)
        # print("DIT ME DIEN")
        # fake_board.show_board()
        # totaltime = 0
        while win == 0:
            moves = fake_board.get_all_possible_moves(player)
            if len(moves) == 1:
                index = 0
            elif len(moves) == 0:
                win = self.opponent[player]
                break
            else:
                index = randint(0, len(moves) - 1)
            if len(moves[index]) == 1:
                inner_index = 0
            else:
                inner_index = randint(0, len(moves[index]) - 1)
            move = moves[index][inner_index]
            fake_board.make_move(move, player)
            counter += 1
            # bt = time.time()
            if fake_board.tie_counter >= fake_board.tie_max:
                win = -1
            # totaltime += time.time() - bt
            # print("self.board.is_win():", time.time() - bt)
            player = self.opponent[player]

        # #print("total time is_win:", totaltime)
        # #bt = time.time()
        # for i in range(counter):
        #     self.board.undo()
        # #rint("total time undo:", time.time() - bt)
        # fake_board.show_board()
        return win
コード例 #29
0
 def __init__(self, col, row, p):
     self.col = col
     self.row = row
     self.p = p
     self.board = [Board(col, row, p) for i in range(num_processes)]
     for board in self.board:
         board.initialize_game()
     self.color = ''
     self.opponent = {1: 2, 2: 1}
     self.color = 2
     self.search_lim = 5
     self.current_node = TreeNode(None, self.color)
     self.sim_total = 0
     self.sim_counter = 0
コード例 #30
0
 def __init__(self, col, row, p):
     self.col = col
     self.row = row
     self.p = p
     self.board = Board(col, row, p)
     self.board.initialize_game()
     self.color = ''
     self.opponent = {1: 2, 2: 1}
     self.color = 2
     self.ct = 0
     #self.dif_val = False
     self.size = self.col * self.row
     if self.size < 40:  #6x6
         #print(8)
         self.search_depth = 8
     elif self.size < 50:  #7x7
         #print(7)
         self.search_depth = 5
     elif self.size < 80:  #8x8
         #print(6)
         self.search_depth = 4
     else:
         self.search_depth = 4