Exemplo n.º 1
0
    def play(self, w1, w2, w1_2, w2_2):
        board = boards.Board()  # オセロ盤インスタンスを生成

        computer1 = nn.NN()  # コンピューター1インスタンスを生成
        computer2 = nn.NN()  # コンピューター2インスタンスを生成

        total_moves = 0  # 総手数を初期化

        while board.is_playable() and not (
                board.is_pass("BLACK") and board.is_pass("WHITE")
        ):  # ゲーム終了(全てのマスが埋まる or 両プレイヤーがパス)が訪れるまでループ
            # -------------------------------------------コンピューター1の手番の処理------------------------------------------- #
            if not board.is_pass("BLACK"):
                total_moves += 1
                y, x = computer1.nn_think("BLACK", w1, w2,
                                          board)  # コンピューター1の打つマスを決定し、受け取り
                board.reverse_othello("BLACK", y, x)

            # -----------------------------------------コンピューター2の手番の処理----------------------------------------- #
            if not board.is_pass("WHITE"):
                total_moves += 1
                y, x = computer2.nn_think("WHITE", w1_2, w2_2,
                                          board)  # コンピューター2の打つマスを決定し、受け取り
                board.reverse_othello("WHITE", y, x)

        self.show_result(board)  # 最終結果表示
        blacks, whites = board.count_stones()

        # 先手が勝利した場合スコア1を獲得 (今回、先手のコンピューター1について学習を行う)
        if blacks >= whites:
            score = 1
        else:
            score = 0

        return score
Exemplo n.º 2
0
def search(search_num, user_start, user_end):
    oldmoveslist = oldmoves.oldmoves()
    pqueue = puzzleQueue.PuzzleQueue()
    board = boards.Board(user_start)
    end_board = boards.Board(user_end)
    if board_funcs.__eq__(board.get_board(), end_board.get_board()):
        return board.get_branch_num(), board.get_depth()
    else:
        oldboard = copy.copy(board.get_board())
        oldmoveslist.add_to_list(oldboard)
        while not board_funcs.__eq__(board.get_board(), end_board.get_board()):
            new_branches = board_funcs.branch(board)
            for i in new_branches:
                if oldmoveslist.check_list(i.get_board()):
                    if search_num == 1:
                        # Breadth First Search
                        pqueue.queueit(i, i.get_branch_num())
                    elif search_num == 2:
                        h = board_funcs.misplaced_tiles(
                            i.get_board(), end_board.get_board())
                        g = i.get_depth()
                        f = h + g
                        pqueue.queueit(i, f)
                    elif search_num == 3:
                        h = board_funcs.manhattan(i.get_board(),
                                                  end_board.get_board())
                        g = i.get_depth()
                        f = h + g
                        pqueue.queueit(i, f)
                    elif search_num == 4:
                        h = board_funcs.gashnig(i.get_board(),
                                                end_board.get_board())
                        g = i.get_depth()
                        f = h + g
                        pqueue.queueit(i, f)
                    else:
                        exit()
            if oldmoveslist.check_list(board.get_board()):
                oldmoveslist.add_to_list(board.get_board())
            board = pqueue.dequeue()
        board_funcs.clear_branch_num()
        return board.get_branch_num(), board.get_depth()
Exemplo n.º 3
0
    def play(self):
        board = boards.Board() # オセロ盤インスタンスを生成

        B_or_W = ""
        while B_or_W != "BLACK" and B_or_W != "WHITE": # 正しい入力がなされるまでループ
            B_or_W = input("先手を選択する場合BLACK, 後手を選ぶ場合WHITEと入力して下さい。") # ユーザーの先手後手を選択
        
        computer = random_com.Computer() # コンピューターインスタンスを生成

        total_moves = 0 # 総手数を初期化

        while board.is_playable() and not(board.is_pass("BLACK") and board.is_pass("WHITE")): # ゲーム終了(全てのマスが埋まる or 両プレイヤーがパス)が訪れるまでループ
            if B_or_W == "BLACK": # ユーザーが先手の場合
                # -------------------------------------------ユーザーの手番の処理------------------------------------------- #
                if board.is_pass("BLACK"): # パスするしかない場合
                    print("あなたはパスしました。")
                    
                else:
                    total_moves += 1 
                    print(str(total_moves) + "手目です。") 
                    blacks, whites = board.count_stones()
                    print("あなた:{},コンピューター:{}".format(blacks, whites))
                    board.print()

                    y, x = 100, 100 # ユーザーの打つマスを初期化
                    while not board.is_OK("BLACK", y, x): # ユーザーがルール上OKの場所に打つまでループ
                        y, x = map(int,input("あなたの手を入力して下さい。(上から何行目?,左から何列目?)").split()) # ユーザーの打つマスを受け取り
                        y -= 1
                        x -= 1

                    board.reverse_othello("BLACK", y, x) # オセロをひっくり返し、盤面を更新

                # -----------------------------------------コンピューターの手番の処理----------------------------------------- #
                if board.is_pass("WHITE"): 
                    print("コンピューターはパスしました。")
                    continue
                
                else:
                    total_moves += 1 
                    print(str(total_moves) + "手目です。")
                    blacks, whites = board.count_stones()
                    print("あなた:{},コンピューター:{}".format(blacks, whites))
                    board.print()
                    print("コンピューターは考え中です...")

                    y, x = computer.random_think(board) # コンピューターの打つマスを決定し、受け取り

                    board.reverse_othello("WHITE", y, x) 

            else: # ユーザーが後手の場合
                # -----------------------------------------コンピューターの手番の処理----------------------------------------- #
                if board.is_pass("BLACK"): 
                    print("コンピューターはパスしました。")
                    continue
                
                else:
                    total_moves += 1
                    print(str(total_moves) + "手目です。") 
                    blacks, whites = board.count_stones()
                    print("コンピューター:{},あなた:{}".format(blacks,whites))
                    board.print()
                    print("コンピューターは考え中です...")

                    y, x = computer.random_think(board)

                    board.reverse_othello("BLACK", y, x)
    
                # -------------------------------------------ユーザーの手番の処理------------------------------------------- #
                if board.is_pass("WHITE"): 
                    print("あなたはパスしました。")

                else:
                    total_moves += 1 
                    print(str(total_moves) + "手目です。") 
                    blacks, whites = board.count_stones()
                    print("コンピューター:{},あなた:{}".format(blacks,whites))
                    board.print()

                    y, x = 100,100 
                    while not board.is_OK("WHITE", y, x): 
                        y, x = map(int,input("あなたの手を入力して下さい。(上から何行目?,左から何列目?)").split()) 
                        y -= 1
                        x -= 1
            
                    board.reverse_othello("WHITE", y, x)

        
        self.show_result(B_or_W, board) # 最終結果表示
Exemplo n.º 4
0
				elif is_draw:
					return -1

				else:
					self.current_player = 0

			else:
				print "ERROR in move"

## Run iterated trials
num_trials = 1000
eps = 0.1
alpha = 0.01

## Setup game
board = boards.Board()
init_state = board.get_state()
value_dict = {}
player1 = players.GreedyRLPlayer('o', value_dict, eps, alpha)
player2 = players.GreedyRLPlayer('x', value_dict, eps, alpha)
game = Game(board, player1, player2)

wins = []
for i in range(num_trials):
	board.set_state(init_state)
	winner = game.play()

	if winner == 0:
		player1.win_update(board)
		player2.loss_update(board)