Example #1
0
    def __init__(self, master):
        self.frame = Frame(master)
        self.frame.pack()
        self.height = 400
        self.width = 400
        self.grid_column = 8
        self.grid_row = 8
        self.canvas = Canvas(self.frame, height=self.height, width=self.width)
        self.cellwidth = int(self.canvas["width"]) / self.grid_column
        self.cellheight = int(self.canvas["height"]) / self.grid_row
        self.draw_grid()
        self.canvas.pack()

        with open("board.json", "r") as boar_data:
            board = json.load(boar_data)
            self.model = board["board"]
        self.player = 1
        self.pos = (0, 0)
        self.draw_chips()
        self.game = Othello(copy.deepcopy(self.model))
        self.machine = Label(self.frame, text="CPU 2")
        self.machine.pack(side=LEFT)
        self.player1 = Label(self.frame, text="Player1 2")
        self.player1.pack(side=RIGHT)

        def handler(event, self=self):
            return self.__on_click(event)

        self.canvas.bind('<Button-1>', handler)

        self.hi_there = Button(self.frame,
                               text="Jugar",
                               command=self.start_game)
        self.hi_there.pack(side=LEFT)
Example #2
0
    def learn(self, num_episode, p_type, op_type, ld_val):
        for i in xrange(num_episode):
            #print(i)
            game = Othello()
            black_player = Player(self, game, True, p_type)
            white_player = Player(self, game, False, op_type)

            game.game_board.updateValidMoves()
            print("{}: {} vs. {}, lambda - {}:").format(
                self.iteration, p_type, op_type, ld_val)
            print("Valid Moves: {}").format(game.validMovesStringify())
            for k, v in black_player.getNNInputs().items():
                print("{}: {}").format(game.validMoveStringify(k),
                                       self.getValue(np.matrix(v)))
            print("")

            while True:
                #print turn
                if game.game_board.black_turn:
                    #print("Black's Turn")
                    pstateVector = black_player.getBoardVector()
                    black_player.makeMove()
                    cstateVector = black_player.getBoardVector()

                else:
                    #print("White's Turn")
                    pstateVector = white_player.getBoardVector()
                    white_player.makeMove()
                    cstateVector = white_player.getBoardVector()

                if game.isGameOver():
                    break
                else:
                    self.train(pstateVector, 0, cstateVector, False)

            if (game.black_score > game.white_score):
                #game is over, update matrix and reset elegibility matrix
                self.bwin += 1
                self.train(pstateVector, 1, cstateVector, True)
                self.reset()
                # print("black wins\n")

            elif (game.black_score < game.white_score):
                #game is over, update matrix and reset elegibility matrix
                self.wwin += 1
                self.train(pstateVector, 0, cstateVector, True)
                self.reset()
                # print("white wins\n")

            elif (game.black_score == game.white_score):
                #game is over, update matrix and reset elegibility matrix
                self.train(pstateVector, 0.5, cstateVector, True)
                self.reset()
                # print("tie\n")

            self.iteration += 1
        print("{}: {} vs. {}, lambda - {}:").format(self.iteration, p_type,
                                                    op_type, self.ld)
        print("black wins: {}").format(self.bwin)
        print("white wins: {}\n").format(self.wwin)
Example #3
0
def Play(i, players):
    oth = Othello(players)
    oth.StartGame(startByFirst=bool(i % 2), showBoard=True)
    result = oth.board.GetCount()
    if (result[0] > result[1]): return 1
    elif (result[0] < result[1]): return -1
    else: return 0
Example #4
0
 def  start_Game(self):
     telo = Othello()
     print('*** Start game *** ')
     print("Jugadas : ",telo.actions(self.model))
     alpa = alphabeta_search(self.model,telo)
     print("Maquina pos nueva: ",alpa)
     self.model = telo.result(self.model,[alpa[0],alpa[1]])
     self.drawChips()
Example #5
0
def play1(nn_black, nn_white, player_black):
    game = Othello()
    if player_black:
        agent = Player(nn_white, game, not player_black, "alphabeta")
    else:
        agent = Player(nn_black, game, not player_black, "alphabeta")
    while True:
        #if no valid moves, switch turns and check for winner
        if game.isGameOver():
            break

        #print score
        print("Black - {}\tWhite - {}").format(game.black_score, game.white_score)
        #print board
        print(game.game_board)


        #agent's turn
        if game.game_board.black_turn and not player_black:
            print("Black's Turn")
            agent.makeMove()
        elif not game.game_board.black_turn and player_black:
            print("White's Turn")
            agent.makeMove()

        #player's turn
        else:
            if player_black:
                print("Black's Turn")
            else:
                print("White's Turn")
            #Print valid moves
            print("Valid Moves: {}").format(game.validMovesStringify())

            #Get move input
            move = raw_input("Choose move (q to quit): ")

            #validate input
            is_valid_move = game.validateMoveInput(move)

            if is_valid_move:
                if move == "q" :
                    break
                else:
                    move = game.moveToCoords(move)
                    game.setTile(move[0], move[1])

        print("\n==========================================================\n")

    #Game Over
    print("Black - {}\tWhite - {}").format(game.black_score, game.white_score)
    print(game.game_board)

    #Check score
    if(game.black_score > game.white_score):
        print("Black Wins!")
    elif(game.black_score < game.white_score):
        print("White Wins!")
    elif(game.black_score == game.white_score):
        print("It's a tie!")
Example #6
0
def playGui(nn):
    root = tk.Tk()
    game = Othello()
    black_player = Player(nn, game, True, "human_gui")
    white_player = Player(nn, game, False, "nn")
    gui_board = GameBoard(root, game, black_player, white_player)
    gui_board.play()
Example #7
0
def play2():
    game = Othello()
    while True:
        game.game_board.updateValidMoves()

        #if no valid moves, switch turns and check for winner
        if game.game_board.valid_moves == {}:
            if game.game_board.black_turn:
                print("Black cannot make any valid moves")
            else:
                print("White's cannot make any valid moves")
            game.game_board.switchTurns()
            #check for winner
            game.game_board.updateValidMoves()
            if game.game_board.valid_moves == {}:
                break

        #print score
        print("Black - {}\tWhite - {}").format(game.black_score, game.white_score)
        #print board
        print(game.game_board)


        #print turn
        if game.game_board.black_turn:
            print("Black's Turn")
        else:
            print("White's Turn")

        #Print valid moves
        print("Valid Moves: {}").format(game.validMovesStringify())

        #Get move input
        move = raw_input("Choose move (q to quit): ")

        #validate input
        is_valid_move = game.validateMoveInput(move)

        if is_valid_move:
            if move == "q" :
                break
            else:
                move = game.moveToCoords(move)
                game.setTile(move[0], move[1])

        print("\n==========================================================\n")

    #Game Over
    print("Black - {}\tWhite - {}").format(game.black_score, game.white_score)
    print(game.game_board)

    #Check score
    if(game.black_score > game.white_score):
        print("Black Wins!")
    elif(game.black_score < game.white_score):
        print("White Wins!")
    elif(game.black_score == game.white_score):
        print("It's a tie!")
Example #8
0
 def __onClick(self, event):
         telo = Othello()
         i=int(event.y/self.cellheight)
         j=int(event.x/self.cellwidth)
         self.pos =(i,j)
         
         self.player = telo.to_move(self.model)
         lisMovValidos = telo.actions(self.model)
         lisNew = [i,j]
         movValido = False
         if self.model[i][j]==0 and lisNew in lisMovValidos:
             self.model = telo.result(self.model,[i,j])
             movValido = True
         if movValido:
             if(self.player==1):
                 self.player=2
             else:
                 self.player=1
             self.drawChips()
         """alpa = alphabeta_search(self.model,telo)
Example #9
0
def play0(nn_black, nn_white):
    global bWin, wWin, ties
    game = Othello()
    black_player = Player(nn_black, game, True)
    white_player = Player(nn_white, game, False, "random")
    while True:
        #if no valid moves, switch turns and check for winner
        if game.isGameOver():
            break

        #print score
        print("Black - {}\tWhite - {}").format(game.black_score, game.white_score)
        #print board
        print(game.game_board)


        #print turn
        if game.game_board.black_turn:
            print("Black's Turn")
            black_player.makeMove()
        else:
            print("White's Turn")
            white_player.makeMove()

        print("\n==========================================================\n")

    #Game Over
    print("Black - {}\tWhite - {}").format(game.black_score, game.white_score)
    print(game.game_board)

    #Check score
    if(game.black_score > game.white_score):
        bWin +=1
        print("Black Wins!")
    elif(game.black_score < game.white_score):
        wWin +=1
        print("White Wins!")
    elif(game.black_score == game.white_score):
        ties+=1
        print("It's a tie!")
Example #10
0
        nn.addModule(hiddenLayer3)
        nn.addModule(hiddenLayer4)
        nn.addModule(hiddenLayer5)
        nn.addConnection(FullConnection(inLayer, hiddenLayer1))
        nn.addConnection(FullConnection(inLayer, hiddenLayer2))
        nn.addConnection(FullConnection(hiddenLayer1, hiddenLayer3))
        nn.addConnection(FullConnection(hiddenLayer2, hiddenLayer3))
        nn.addConnection(FullConnection(hiddenLayer2, hiddenLayer5))
        nn.addConnection(FullConnection(hiddenLayer3, hiddenLayer4))
        nn.addConnection(FullConnection(hiddenLayer3, hiddenLayer5))
        nn.addConnection(FullConnection(hiddenLayer3, outLayer))
        nn.addConnection(FullConnection(hiddenLayer4, outLayer))				
        nn.addConnection(FullConnection(hiddenLayer5, outLayer))
        nn.sortModules()

    othello = Othello()
    smartPlayer = SmartPlayer(nn,othello.boardSize)
    tacticalPlayer = TacticalPlayer()
    tacticalPlayer.newGame(othello,random.choice([othello.WHITE_PLAYER,othello.BLACK_PLAYER]));
    smartPlayer.newGame(othello,tacticalPlayer.enemy);

    playGame(othello,tacticalPlayer,smartPlayer)
    if othello.getWinner() == tacticalPlayer.color:
        outcome = -1
        print "Tactical Player wins over Network Player 4"
        count += 1
        semicount += 1
	
    elif othello.getWinner() == smartPlayer.color:
        outcome = 1
        print "Network Player 4 wins over Tactical Player"		
Example #11
0
def game_begin():
    print('New game starts!')

    try:
        size = int(input('Input the size of the board (Integers Only!!!!):'))
        block = int(input('Input the number of blocks on the board (Integers Only!!!!):'))
    except:
        print('You did not input a number, now game starts with 8*8 board and 3 blocks')
        size = 8
        block = 3

    print('-----------------------------------------------------')
    print('Choose whether to play first')
    first = input('input Y or N: ')

    if first == 'Y' : play = 1
    elif first == 'N' : play = -1
    else:
        print('Your input is wrong, now you need to play first')
        play = 1

    print('-----------------------------------------------------')
    print(' Hints : 2 is wall, 1 is you and -1 is AI')
    oth = Othello(size, block)
    oth.print_board()

    while True:
        actions = oth.action_valid(play)
        isfinish = oth.finish(actions)
        if isfinish < 0:
            if play == 1:
                print('-----------------------------------------------------')
                print('Now you have several valid actions to choose')
                print(actions)
                indexIn = input('Input the index you want to choose(Integers Only and must be less than {}):'.format(len(actions)))

                index = int(indexIn) -1
                if index >= size or index < 0:
                    print('Your input is inValid. You determined to choose the first one.')
                    index = 0
                action = actions[index]
                oth.player_action(action)
                oth.change(action, play)
                print('Ok, now the board has changed:')
                oth.print_board()
            else:
                print('-----------------------------------------------------')
                print('Now it is the AI turn')
                print('Valid actions:', actions)
                oth.computer_action(actions)
                oth.print_board()
            play *= -1
        else:
            print('-----------------------------------------------------')
            print('Okay, game over = 3 = ')
            if isfinish == 1:
                print('Great !!! You Win!!!!')
            elif isfinish == 2:
                print('Oops ..Sorry, good Luck next time!')
            else:
                print('Not bad, you are the same as AI!')
            break
Example #12
0
        hiddenLayer2 = LinearLayer(80)
        hiddenLayer3 = SigmoidLayer(33)
        outLayer = SoftmaxLayer(64)
        nn.addInputModule(inLayer)
        nn.addOutputModule(outLayer)
        nn.addModule(hiddenLayer1)
        nn.addModule(hiddenLayer2)
        nn.addModule(hiddenLayer3)
        nn.addConnection(FullConnection(inLayer, hiddenLayer1))
        nn.addConnection(FullConnection(inLayer, hiddenLayer2))
        nn.addConnection(FullConnection(hiddenLayer2, hiddenLayer3))
        nn.addConnection(FullConnection(hiddenLayer1, outLayer))
        nn.addConnection(FullConnection(hiddenLayer3, outLayer))
        nn.sortModules()

    othello = Othello()
    smartPlayer = SmartPlayer(nn, othello.boardSize)
    randomPlayer = TacticalPlayer()
    randomPlayer.newGame(othello, random.choice([othello.WHITE_PLAYER, othello.BLACK_PLAYER]))
    smartPlayer.newGame(othello, randomPlayer.enemy)

    playGame(othello, randomPlayer, smartPlayer)
    if othello.getWinner() == randomPlayer.color:
        outcome = -1
        print "Tactical Player wins over Network Player 1"
        count += 1
        semicount += 1
    elif othello.getWinner() == smartPlayer.color:
        outcome = 1
        print "Network Player 1 wins over Tactical Player"
        wincnt += 1
Example #13
0
        if mode == 'NextOne':
            player = NextOne(Stone("○"),name,education_bord)
        elif mode == 'CountStone':
            player = CountStone(Stone("○"),name,education_bord)
        elif mode == 'Random':
            player = Random(Stone("○"),name,education_bord)
        elif mode == 'Q_learning':
            player = Q_learning(Stone("○"),name,education_bord)
        elif mode == 'Naive':
            player = Naive(Stone("○"),name,education_bord)
        elif mode == 'MLP':
            player = MLP_p(Stone("○"),name,education_bord)
    return player

player1 = get_player('MLP','P1','black')
player2 = get_player('Random','P2','white')
player1.battleMode()
game = Othello(nplay=600,show_result=True,show_board=False)
game.play(player1,player2)
# player2 = get_player('Q_learning','P2','white','no')
# game = Othello(nplay=120000,show_result=True,show_board=False)
# game.play(player1,player2)

# player1.q.save('p1.pickle')
# player2.q.save('p2.pickle')
# player1.battleMode()
# print()

# player2 = get_player('Random','P2','white')
# game = Othello(nplay=600,show_result=True,show_board=False)
# game.play(player1,player2)
from SmartPlayer import SmartPlayer
from RandomPlayer import RandomPlayer
from GreedyPlayer import GreedyPlayer
from Othello import Othello
from Player import playGame
from HumanPlayer import HumanPlayer
from pybrain.tools.customxml.networkreader import NetworkReader
from TacticalPlayer import TacticalPlayer
import random

#nn =  NetworkReader.readFrom("othelloNetwork.xml")
#opponentPlayer = SmartPlayer(nn,8)  #change this to change the opponent to be testing against
opponentPlayer = TacticalPlayer()
humanPlayer = HumanPlayer()

othello = Othello()

othello.resetGame()
humanPlayer.newGame(othello,random.choice([othello.WHITE_PLAYER,othello.BLACK_PLAYER]))
opponentPlayer.newGame(othello,humanPlayer.enemy)
playGame(othello,humanPlayer,opponentPlayer)

if othello.getWinner() == humanPlayer.color:
    print "You won!"
elif othello.getWinner() == opponentPlayer.color:
    print "You lost!"
else:
    print "Tie game! :("
Example #15
0
class App:
    def __init__(self, master):
        self.frame = Frame(master)
        self.frame.pack()
        self.height = 400
        self.width = 400
        self.grid_column = 8
        self.grid_row = 8
        self.canvas = Canvas(self.frame, height=self.height, width=self.width)
        self.cellwidth = int(self.canvas["width"]) / self.grid_column
        self.cellheight = int(self.canvas["height"]) / self.grid_row
        self.draw_grid()
        self.canvas.pack()

        with open("board.json", "r") as boar_data:
            board = json.load(boar_data)
            self.model = board["board"]
        self.player = 1
        self.pos = (0, 0)
        self.draw_chips()
        self.game = Othello(copy.deepcopy(self.model))
        self.machine = Label(self.frame, text="CPU 2")
        self.machine.pack(side=LEFT)
        self.player1 = Label(self.frame, text="Player1 2")
        self.player1.pack(side=RIGHT)

        def handler(event, self=self):
            return self.__on_click(event)

        self.canvas.bind('<Button-1>', handler)

        self.hi_there = Button(self.frame,
                               text="Jugar",
                               command=self.start_game)
        self.hi_there.pack(side=LEFT)

    def draw_grid(self):
        for i in range(self.grid_row):
            for j in range(self.grid_column):
                x1 = i * self.cellwidth
                y1 = j * self.cellheight
                x2 = x1 + self.cellwidth
                y2 = y1 + self.cellheight
                self.canvas.create_rectangle(x1, y1, x2, y2, fill="green")

    def draw_chip(self):
        x = self.pos[1] * self.cellwidth
        y = self.pos[0] * self.cellheight
        if self.player == 1:
            self.canvas.create_oval(x,
                                    y,
                                    x + self.cellwidth,
                                    y + self.cellheight,
                                    fill='black')
            self.player = 2
        else:
            self.canvas.create_oval(x,
                                    y,
                                    x + self.cellwidth,
                                    y + self.cellheight,
                                    fill='white')
            self.player = 1

    def draw_chips(self):
        white = 0
        black = 0
        for i in range(len(self.model)):
            row = self.model[i]
            for j in range(len(row)):
                val = self.model[i][j]
                x = j * self.cellwidth
                y = i * self.cellheight
                if val == 1:
                    white += 1
                    self.canvas.create_oval(x,
                                            y,
                                            x + self.cellwidth,
                                            y + self.cellheight,
                                            fill='black')
                elif val == 2:
                    black += 1
                    self.canvas.create_oval(x,
                                            y,
                                            x + self.cellwidth,
                                            y + self.cellheight,
                                            fill='white')

    def set_points(self, white, black):
        txt1 = StringVar()
        txt1.set("CPU: " + str(black))
        self.machine.config(textvariable=txt1)
        txt2 = StringVar()
        txt2.set("Player: " + str(white))
        self.player.config(textvariable=txt2)

    def __on_click(self, event):

        i = int(event.y / self.cellheight)
        j = int(event.x / self.cellwidth)
        self.pos = (i, j)
        if self.model[i][j] == 0:
            self.model[i][j] = self.player
            self.player = 1
            new_state = self.game.game_user(self.pos)
            if len(new_state) is not 0:
                self.model = new_state
                self.game.result_operation = []
                self.player = 1
                self.game.set_move(self.player)
                self.draw_chips()
                self.start_game()

    def start_game(self):
        self.game.set_move(self.player)
        self.model = self.game.play_game()
        self.game.result_operation = []
        self.player = 2
        self.game.set_move(self.player)
        self.draw_chips()
from SmartPlayer import SmartPlayer
from RandomPlayer import RandomPlayer
from GreedyPlayer import GreedyPlayer
from Othello import Othello
from Player import playGame
from HumanPlayer import HumanPlayer
from pybrain.tools.customxml.networkreader import NetworkReader
from TacticalPlayer import TacticalPlayer
import random
import time

nn =  NetworkReader.readFrom("othelloNetwork.xml")
player1 = SmartPlayer(nn,8)  #change this to change the opponent to be testing against
player2 = RandomPlayer()

othello = Othello()

othello.resetGame()
player1.newGame(othello,random.choice([othello.WHITE_PLAYER,othello.BLACK_PLAYER]))
player2.newGame(othello,player1.enemy)

while not othello.isGameOver():
    if (player1.color == othello.WHITE_PLAYER):
        print "Neural Network is white"
    else:
        print "Neural Network is black"
    othello.printBoard()
    time.sleep(1)
    if (othello.whoGoesNext() == player1.color):
        move = player1.getMove()
        othello.makeMove(move[0],move[1],player1.color)
Example #17
0
def playVerbose(nn_black, nn_white):
    continue_play = False
    game = Othello()
    black_player = Player(nn_black, game, True)
    white_player = Player(nn_white, game, False, "pos_values")
    #white_player = Player(None, game, False, "greedy")
    while True:
        if game.isGameOver():
            break

        #print score
        print("Black - {}\tWhite - {}").format(game.black_score, game.white_score)
        #print board
        print(game.game_board)

        #if coninuing play
        if continue_play:
            if game.game_board.black_turn:
                black_player.makeMove()
            else:
                white_player.makeMove()

        #if not coninuing play
        else:
            #print turn
            if game.game_board.black_turn:
                print("Black's Turn")
            else:
                print("White's Turn")

            rand = None
            #print valid moves
            if game.game_board.black_turn:
                print("Black's Turn")
                print("Valid Moves: {}").format(game.validMovesStringify())

                for k, v in black_player.getNNInputs().items():
                    print("{}: {}").format(game.validMoveStringify(k), black_player.nn.getValue(np.matrix(v)))

            else:
                print("White's Turn")
                # print("Valid Moves: {}").format(game.validMovesStringify())
                # rand = random.randrange(0, len(game.game_board.valid_moves.keys()))
                # print("Random index: {}").format(rand)
                print("Valid Moves: {}").format(game.validMovesStringify())
                for k, v in black_player.getNNInputs().items():
                    print("{}: {}").format(game.validMoveStringify(k), black_player.nn.getValue(np.matrix(v)))

            command = raw_input("n to next (default), c to play game, q to quit: ")
            if command == "n" or command == "":
                if game.game_board.black_turn:
                    black_player.makeMove()
                else:
                    white_player.makeMove(rand)
            elif command == "c":
                continue_play = True
                if game.game_board.black_turn:
                    black_player.makeMove()
                else:
                    white_player.makeMove(rand)
            elif command == "q":
                break
            else:
                print("not a valid command, try again")

            print("\n==========================================================\n")

    #Game Over
    print("Black - {}\tWhite - {}").format(game.black_score, game.white_score)
    print(game.game_board)

    #Check score
    if(game.black_score > game.white_score):
        print("Black Wins!")
    elif(game.black_score < game.white_score):
        print("White Wins!")
    elif(game.black_score == game.white_score):
        print("It's a tie!")
Example #18
0
    def minimax_a_b_p(self,
                      boardObj,
                      maximizingPlayer,
                      alpha=-math.inf,
                      beta=math.inf,
                      depth=0):
        """ recursion, finds the best move based on the heuristic of the board """
        if depth == 0 or boardObj.Game_Finish():
            return boardObj  # .Heuristic(self.player,self.enemy)

        if maximizingPlayer:
            maxEval = Othello()
            maxEval.setHeuristic(-math.inf)

            test = self.posibleMoves(boardObj, self.player)
            if test == []:
                return boardObj

            for child in test:

                evalBoard = self.minimax_a_b_p(child,
                                               False,
                                               alpha=alpha,
                                               beta=beta,
                                               depth=depth - 1)

                if maxEval.Heuristic(self.player,
                                     self.enemy) < evalBoard.Heuristic(
                                         self.player, self.enemy):
                    maxEval = evalBoard

                alpha = max(alpha, evalBoard.Heuristic(self.player,
                                                       self.enemy))

                if beta <= alpha:
                    break

                return maxEval

        else:
            minEval = Othello()
            minEval.setHeuristic(math.inf)
            test = self.posibleMoves(boardObj, self.enemy)

            if test == []:
                return boardObj

            for child in test:
                evalBoard = self.minimax_a_b_p(child,
                                               True,
                                               alpha=alpha,
                                               beta=beta,
                                               depth=depth - 1)

                if minEval.Heuristic(self.player,
                                     self.enemy) > evalBoard.Heuristic(
                                         self.player, self.enemy):
                    minEval = evalBoard
                beta = min(beta, evalBoard.Heuristic(self.player, self.enemy))
                if beta <= alpha:
                    break
                return minEval
Example #19
0
def get_play_data(agent_1, agent_2):
    othello = Othello()

    first_states = deque()
    first_rewards = deque()
    first_actions = deque()

    second_states = deque()
    second_rewards = deque()
    second_actions = deque()

    board, changeable_Pos, Position_Row, Position_Col, Change_Position, done = othello.make(
    )

    while not done:

        if othello.color == 1:
            state = getState(board, changeable_Pos, Position_Row, Position_Col,
                             Change_Position)
            first_states.appendleft(state)
            reward_1, reward_2 = getReward(board)
            first_rewards.appendleft(reward_1)

            setrow, setcol = agent_1.take_action(board, changeable_Pos,
                                                 Position_Row, Position_Col,
                                                 Change_Position)
            board, changeable_Pos, Position_Row, Position_Col, Change_Position, done = othello.step(
                setrow, setcol)

            first_actions.appendleft(8 * setrow + setcol)

        else:
            state = getState(board, changeable_Pos, Position_Row, Position_Col,
                             Change_Position)
            second_states.appendleft(state)
            reward_1, reward_2 = getReward(board)
            second_rewards.appendleft(reward_2)

            setrow, setcol = agent_2.take_action(board, changeable_Pos,
                                                 Position_Row, Position_Col,
                                                 Change_Position)
            board, changeable_Pos, Position_Row, Position_Col, Change_Position, done = othello.step(
                setrow, setcol)

            second_actions.appendleft(8 * setrow + setcol)

    state = getState(board, changeable_Pos, Position_Row, Position_Col,
                     Change_Position)
    reward_1, reward_2 = getReward(board)

    first_states.appendleft(state)
    second_states.appendleft(state)
    first_states = torch.FloatTensor(first_states)
    second_states = torch.FloatTensor(second_states)

    first_actions = torch.FloatTensor(first_actions)
    second_actions = torch.FloatTensor(second_actions)

    first_rewards.appendleft(reward_1)
    second_rewards.appendleft(reward_2)
    first_rewards = torch.FloatTensor(list(first_rewards)[:-1])
    second_rewards = torch.FloatTensor(list(second_rewards)[:-1])

    discount_rate = 1
    if reward_1 > reward_2:
        first_values = torch.FloatTensor(
            [discount_rate**i for i in range(len(first_rewards))])
        second_values = torch.FloatTensor(
            [-discount_rate**i for i in range(len(second_rewards))])
    elif reward_1 < reward_2:
        first_values = torch.FloatTensor(
            [-discount_rate**i for i in range(len(first_rewards))])
        second_values = torch.FloatTensor(
            [discount_rate**i for i in range(len(second_rewards))])
    else:
        first_values = torch.FloatTensor(
            [0 for i in range(len(first_rewards))])
        second_values = torch.FloatTensor(
            [0 for i in range(len(second_rewards))])

    data_first = {
        'states': first_states,
        'rewards': first_rewards,
        'actions': first_actions,
        'values': first_values,
    }

    data_secound = {
        'states': second_states,
        'rewards': second_rewards,
        'actions': second_actions,
        'values': second_values,
    }

    return data_first, data_secound
    def __init__(self, input_data):
        '''
		Initializes the GUI board and the Othello model
		'''
        # store input data
        num_rows = input_data.get_num_rows()
        num_cols = input_data.get_num_cols()
        self._next_player = input_data.get_first_player()
        top_left_color = input_data.get_top_left_color()
        self._who_wins = input_data.get_who_wins()

        # set up the GUI: Canvas and labels
        self._root_window = tkinter.Tk()
        self._root_window.title("Othello - FULL")

        self._board = Board(num_rows, num_cols)
        self._board_width, self._board_height = self.board_size(
            num_rows, num_cols)

        self._canvas = tkinter.Canvas(master=self._root_window,
                                      width=self._board_width,
                                      height=self._board_height,
                                      background=_BACKGROUND_COLOR)

        self._canvas.grid(row=0,
                          column=0,
                          padx=2,
                          pady=2,
                          sticky=tkinter.N + tkinter.S + tkinter.E + tkinter.W)

        self._next_turn = tkinter.StringVar()
        self._turn_label = tkinter.Label(master=self._root_window,
                                         textvariable=self._next_turn,
                                         font=_DEFAULT_FONT)

        self._turn_label.grid(row=1,
                              column=0,
                              padx=2,
                              pady=2,
                              sticky=tkinter.W)

        self._disc_counts = tkinter.StringVar()
        self._count_label = tkinter.Label(master=self._root_window,
                                          textvariable=self._disc_counts,
                                          font=_DEFAULT_FONT)

        self._count_label.grid(row=2,
                               column=0,
                               padx=2,
                               pady=2,
                               sticky=tkinter.W)

        # set up window resizing
        self._root_window.columnconfigure(0, weight=1)
        self._root_window.rowconfigure(0, weight=1)

        # set up callback handlers for button release and configuration
        self._canvas.bind('<ButtonRelease-1>', self._on_button_down)
        self._canvas.bind('<Configure>', self._on_canvas_resize)

        # set up Othello model
        self._game_over = False
        self._othello = Othello(num_rows, num_cols, self._next_player)
        self._othello.setBoard(top_left_color)
        self._process_next_move()
class OthelloBoardApp:
    def __init__(self, input_data):
        '''
		Initializes the GUI board and the Othello model
		'''
        # store input data
        num_rows = input_data.get_num_rows()
        num_cols = input_data.get_num_cols()
        self._next_player = input_data.get_first_player()
        top_left_color = input_data.get_top_left_color()
        self._who_wins = input_data.get_who_wins()

        # set up the GUI: Canvas and labels
        self._root_window = tkinter.Tk()
        self._root_window.title("Othello - FULL")

        self._board = Board(num_rows, num_cols)
        self._board_width, self._board_height = self.board_size(
            num_rows, num_cols)

        self._canvas = tkinter.Canvas(master=self._root_window,
                                      width=self._board_width,
                                      height=self._board_height,
                                      background=_BACKGROUND_COLOR)

        self._canvas.grid(row=0,
                          column=0,
                          padx=2,
                          pady=2,
                          sticky=tkinter.N + tkinter.S + tkinter.E + tkinter.W)

        self._next_turn = tkinter.StringVar()
        self._turn_label = tkinter.Label(master=self._root_window,
                                         textvariable=self._next_turn,
                                         font=_DEFAULT_FONT)

        self._turn_label.grid(row=1,
                              column=0,
                              padx=2,
                              pady=2,
                              sticky=tkinter.W)

        self._disc_counts = tkinter.StringVar()
        self._count_label = tkinter.Label(master=self._root_window,
                                          textvariable=self._disc_counts,
                                          font=_DEFAULT_FONT)

        self._count_label.grid(row=2,
                               column=0,
                               padx=2,
                               pady=2,
                               sticky=tkinter.W)

        # set up window resizing
        self._root_window.columnconfigure(0, weight=1)
        self._root_window.rowconfigure(0, weight=1)

        # set up callback handlers for button release and configuration
        self._canvas.bind('<ButtonRelease-1>', self._on_button_down)
        self._canvas.bind('<Configure>', self._on_canvas_resize)

        # set up Othello model
        self._game_over = False
        self._othello = Othello(num_rows, num_cols, self._next_player)
        self._othello.setBoard(top_left_color)
        self._process_next_move()

    def run(self) -> None:
        '''
		Runs the Othello application taking user inputs
		'''
        self._root_window.mainloop()

    def board_size(self, num_rows, num_cols) -> (int, int):
        ''' 
		Returns the size of the board in pixel units
		Draws bigger cells when number of columns is less than 4 
		so that the complete title of the window is displayed.
		'''
        if (num_cols == 4):
            return (num_cols * 70, num_rows * 70)
        else:
            return (num_cols * 50, num_rows * 50)

    def _on_button_down(self, event: tkinter.Event):
        '''
		Event handler for left mouse button release event
		'''
        if self._game_over == False:
            canvas = event.widget
            x = canvas.canvasx(event.x)
            y = canvas.canvasy(event.y)
            #print ((event.x, event.y), (x,y))

            player_input = self._board.cell_location(
                point.from_pixel(x, y, self._board_width, self._board_height))
            try:
                if self._othello._validateInput(player_input,
                                                self._next_player):
                    self._othello.updateBoard(player_input, self._next_player)
                    self._process_next_move()
            except InvalidMoveError:
                pass  # nothing to do if invalid move

    def _process_next_move(self):
        '''
		Processes the next move by calling the Othello model.
		Called after initial board set up and after every uesr selection
		'''
        #self.printBoard()
        self._draw_board(self._othello.getBoard())
        if self._othello.continuePlaying():  # game still continuing
            self._next_player = self._othello.getNextPlayer()
            self._next_turn.set('Next Turn: ' +
                                self.display_color(self._next_player))
        else:  # game over. determine and print winner
            self._game_over = True
            self._next_turn.set("WINNER: " + self._winner())

        # set the tile count for both ongoing or end game
        num_tiles = self._othello.determineNumberTiles()
        self._disc_counts.set('Black: ' + str(num_tiles[_BLACK]) +
                              '  White: ' + str(num_tiles[_WHITE]))

    def _winner(self) -> str:
        ''' 
		Determines the winner by comparing the tile counts
		against the winner criteria provided as input to the game.
		'''
        num_tiles = self._othello.determineNumberTiles()
        winner = "None"
        if (self._who_wins == ">"):  # higher count wins
            if (num_tiles[_BLACK] < num_tiles[_WHITE]):
                winner = "White"
            elif (num_tiles[_BLACK] > num_tiles[_WHITE]):
                winner = "Black"
        elif (self._who_wins == "<"):  # lower count wins
            if (num_tiles[_BLACK] > num_tiles[_WHITE]):
                winner = "White"
            elif (num_tiles[_BLACK] < num_tiles[_WHITE]):
                winner = "Black"

        return winner

    def _on_canvas_resize(self, event: tkinter.Event):
        '''
		Handler for canvas resize. 
		Redraws the entire board with current dimensions
		'''
        self._canvas.delete(tkinter.ALL)
        self._board_width = self._canvas.winfo_width()
        self._board_height = self._canvas.winfo_height()
        self._draw_board(self._othello.getBoard())

    def _draw_board(self, board_info) -> None:
        '''
		Draw the Othello game board
		First draws the vertical and horizontal grid lines on the canvas
		Then draws the currently available black and white discs from the Othello model
		'''
        board_size = self._board.board_size()
        next_x = 0
        for vert_line in range(board_size[1]):
            # print((next_x,0,next_x,self._board_height))
            self._canvas.create_line(next_x,
                                     0,
                                     next_x,
                                     self._board_height,
                                     fill='red',
                                     width='2.0')
            next_x = next_x + (self._board_width / board_size[1])

        next_y = 0
        for hor_line in range(board_size[0]):
            # print((0, next_y, self._board_width, next_y,))
            self._canvas.create_line(0,
                                     next_y,
                                     self._board_width,
                                     next_y,
                                     fill='red',
                                     width='2.0')
            next_y = next_y + (self._board_height / board_size[0])

        for row in range(board_size[0]):
            for col in range(board_size[1]):
                cell = self._board.cell_from_loc(row, col)
                self._draw_disc(cell, self.display_color(board_info[row][col]))

    def display_color(self, color_code: str) -> str:
        '''
		Translates the color code understood by Othello model to readable string for GUI display
		'''
        if (color_code == 'W'):
            return 'White'
        elif (color_code == 'B'):
            return 'Black'
        else:
            return None

    def _draw_disc(self, cell: Cell, color: str) -> None:
        '''
		Draws the disc of the provided color in the given Cell object instance
		'''
        if (color != None):
            disc_coords = cell.disc_coords()
            top_left = disc_coords[0].pixel(self._board_width,
                                            self._board_height)
            bottom_right = disc_coords[1].pixel(self._board_width,
                                                self._board_height)
            self._canvas.create_oval(top_left[0],
                                     top_left[1],
                                     bottom_right[0],
                                     bottom_right[1],
                                     fill=color,
                                     outline='black')

    def printBoard(self):
        #prints the board on console - for debug purposes only
        boardInfo = self._othello.getBoard()
        for row in boardInfo:
            rowStr = ""
            for col in row:
                rowStr += col + " "
            print(rowStr)
Example #22
0
            ai.append((AI(name=i)))
    prev_percentage = 0
    for i in range(len(winner_of_generation)):
        winner_of_generation[i].set_name(i + n_ai)
        ai.append(winner_of_generation[i])
    score = np.zeros(len(ai), dtype='int')
    print('Complete: ', end='', flush=True)
    for i in range(len(ai)):
        percentage = int(i / len(ai) * 100)
        [
            print('#', end='', flush=True)
            for x in range(percentage - prev_percentage)
        ]
        prev_percentage = percentage
        for j in range((i + 1), len(ai)):
            game = Othello()
            winner = game.play_self(ai[i], ai[j])
            score[winner.get_name()] += 1
    print('| 100%')
    winners = score.argsort()[-n_winners:][::-1]
    ai_winners = []
    for winner in winners:
        ai_winners.append(ai[winner])
    print('Ai-{} won {} out of {} ({:.2f}%) games.\n'.format(
        winners[0], score[winners[0]],
        len(ai) - 1, score[winners[0]] / (len(ai) - 1) * 100))

    # Adds the best AI of this generation to the list of contenders
    if ai_winners[0] not in winner_of_generation and (score[winners[0]] /
                                                      (len(ai) - 1)) > 0.625:
        winner_of_generation.append(ai_winners[0])
Example #23
0
 def setup_othello(self):
     othello = Othello()
     return othello, othello.board
Example #24
0
window = Window(size=[800, 800], set=False)
window.open = True
window.text_color = BLACK
window.background_color = BLACK
window.text_size = 30
#window.size=[900,20]

results = []
i = 0
number = 10000
display = False

while i < number and window.open:
    i += 1
    game = Othello(window, display)
    game()
    winner_side = (game.state + 1) % 2
    results.append(winner_side)
    message = "Results for " + str(
        i) + " tests:\n" + "White victories: " + str(
            results.count(0)) + "\n" + "Black victories: " + str(
                results.count(1)) + "\n"
    window.print(str(message), size=[50, 20])
    window.flip()
    print(message)

message = "Results for " + str(i) + " tests:\n" + "White victories: " + str(
    results.count(0)) + "\n" + "Black victories: " + str(
        results.count(1)) + "\n"
print(message)
Example #25
0
    current_generation_individual_group = []
    for i in range(POPULATION_LIST):
        current_generation_individual_group.append(
            population(np.random.normal(0.0, 1.0, (GENE_COUNT))))

    for generation in range(GENERATIONS):
        print(count)
        # オセロ盤の評価
        for i in current_generation_individual_group:
            for j in current_generation_individual_group:
                if i == j:
                    continue
                else:
                    player1 = get_player('MLP', 'P1', 'black', i.individual)
                    player2 = get_player('MLP', 'P2', 'white', j.individual)
                    FirstMove, SecondMove = Othello().play(player1, player2)
                    i.score += FirstMove
                    j.score += SecondMove
        # エリート選択とルーレット選択
        elite, other = select_elite(current_generation_individual_group,
                                    RANKING)
        roulette = roulette_def(other, ROULETTE)
        next_list = elite + roulette

        print("\n-----第 " + str(generation + 1) + " 世代の結果-----")
        print("   First: " + str(elite[0].score) + "  " +
              str(elite[0].individual))
        print("  Second: " + str(elite[1].score) + "  " +
              str(elite[1].individual))
        print("   Third: " + str(other[0].score) + "  " +
              str(other[0].individual))
Example #26
0
from Othello import Othello
from Othello import AI
import numpy as np
import matplotlib.pyplot as plt

generation = int(input('Which generation do you wish to play against:'))
w1 = np.load('weights\weight-Gen{:03d}-1.npy'.format(generation))
w2 = np.load('weights\weight-Gen{:03d}-2.npy'.format(generation))
ai = AI(weight1=w1, weight2=w2)
player = int(
    input(
        'Which player do you wish to be (black: -1, white: 1) (or choose another ai):'
    ))
game = Othello()
print(np.average(np.abs(w1)))
print(np.average(np.abs(w2)))
if player > 1:
    w1 = np.load('weights\weight-Gen{:03d}-1.npy'.format(player))
    w2 = np.load('weights\weight-Gen{:03d}-2.npy'.format(player))
    ai2 = AI(weight1=w1, weight2=w2)
    ai.set_name('Gen' + str(generation))
    ai2.set_name('Gen' + str(player))
    game.play_self(ai, ai2, display_board=True)
else:
    game.play(ai, player)

# n, bins, patches = plt.hist(w1, 50, normed=1, facecolor='green', alpha=0.75)
# plt.show()
Example #27
0
        hiddenLayer3 = SigmoidLayer(50)
        hiddenLayer4 = SigmoidLayer(50)

        outLayer = SoftmaxLayer(64)
        nn.addInputModule(inLayer)
        nn.addOutputModule(outLayer)
        nn.addModule(hiddenLayer1)
		
        nn.addConnection(FullConnection(inLayer, hiddenLayer1, inSliceTo=16))
        nn.addConnection(FullConnection(inLayer, hiddenLayer1, inSliceFrom=16, inSliceTo=32))
        nn.addConnection(FullConnection(inLayer, hiddenLayer1, inSliceFrom=32, insliceTo=48))
        nn.addConnection(FullConnection(inLayer, hiddenLayer1, inSliceFrom=48))
        nn.addConnection(FullConnection(hiddenLayer1, outLayer))
        nn.sortModules()

    othello = Othello()
    smartPlayer = SmartPlayer(nn,othello.boardSize)
    greedyPlayer = TacticalPlayer()
    greedyPlayer.newGame(othello,random.choice([othello.WHITE_PLAYER,othello.BLACK_PLAYER]));
    smartPlayer.newGame(othello,greedyPlayer.enemy);

    playGame(othello,greedyPlayer,smartPlayer)
    if othello.getWinner() == greedyPlayer.color:
        outcome = -1
        print "Tactical Player wins over Network Player 5"
        count += 1
        semicount += 1
	
    elif othello.getWinner() == smartPlayer.color:
        outcome = 1
        print "Network Player 5 wins over Tactical Player"		
Example #28
0
from warnings import filterwarnings
filterwarnings('ignore')

#from GameVision import OthelloGame
from GameVision import OthelloGame
from Othello import Othello
sys.path.insert(0,
                os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
from RL.rule_agent import randomAgent, greedyAgent
from RL.DQN_agent import agent as dqn_agent
from RL.A2C_agent import agent as A2C_agent

if __name__ == "__main__":
    game = OthelloGame()
    othello = Othello()
    game.main()

    random_agent = randomAgent()
    greedy_agent = greedyAgent()

    agent_1 = A2C_agent(input_dim=3, lam=0.8, gamma=0.99, lr=1e-4)
    param = torch.load('a2c_1_param2.pt')
    agent_1.model.load_state_dict(param)

    agent_2 = A2C_agent(input_dim=3, lam=0.8, gamma=0.99, lr=1e-4)
    param = torch.load('a2c_2_param2.pt')
    agent_2.model.load_state_dict(param)

    while (1):
        for event in pygame.event.get():
Example #29
0
File: Game.py Project: Jie211/mcts
def PlayGame():
    verbose1 = False
    # verbose1 = True

    verbose2 = False
    # verbose2 = True

    Wtotalwin = 0
    Btotalwin = 0

    testtime = 1
    nowtime = 0

    while( nowtime < testtime  ):
    
        #8x8のオセロのゲームを生成する
        game = Othello(8)

        #まだ打てる手があるならゲームを続ける
        while(game.GetCanMove() != []):
            if verbose1 : 
                print repr(game)
                print 'Can->'+str(game.GetCanMove())
            
            #使えるAI
            # MCTS(rootgame=game, times=100, verbose=False)
            # MC(rootgame=game, times=100, verbose=False)
            # ScoreMax(gmae)
            # ProbabilitySelect(game)
            # Less_chance(game)
            # RandAI(game)

            if game.lastMoved == 'B':
                #  white 白のAI
                m = MC(game, 100, False) 
                if verbose1 : print "W",
            else:
                # black 黒のAI
                m = RandAI(game)
                if verbose1 : print "B",
            game.DoMove(m)
            if verbose1 : print "Do-> "+str(m)+"\n"

        #勝敗の表示
        if game.GetScore(game.lastMoved) == 1.0:
            print str(game.lastMoved) + " win"
            if game.lastMoved == 'W':
                Wtotalwin+=1
            else:
                Btotalwin+=1
        elif game.GetScore(game.lastMoved) == 0.0:
            print str(game.GetOpponent(game.lastMoved)) + " win"
            if game.lastMoved == 'W':
                Btotalwin+=1
            else:
                Wtotalwin+=1
        else:
            print "draw"
        
        nowtime+=1
        print "--------------------"
        print "White : "+str(Wtotalwin)
        print "Black : "+str(Btotalwin)