Esempio n. 1
0
 def __init__(self):
     self.root = Tk()
     self.game = TicTacToeGame()
     sys.setrecursionlimit(10000)
     self.minimax = SimpleMinimax(self.game)
     self.started = False
     self.initializeGui()
Esempio n. 2
0
    def __init__(self, width=500, height=500):

        self.gamm = TicTacToeGame()
        pygame.init()
        pygame.font.init()
        self.SURF = pygame.display.set_mode((width, height))
        pygame.display.set_caption("Brick Builder")
        self.myfont = pygame.font.SysFont('Arial', 50)
        self.score_font = pygame.font.SysFont('Arial', 30)
        self.dot_font = pygame.font.SysFont('Arial', 15)
        self.state = self.gamm.getInitialState()
        sys.setrecursionlimit(100000)
        self.score = [0, 0]
        self.minimax = SimpleMinimax(self.gamm)
        self.SURF.fill((255, 255, 255))
        self.disp_board()
        self.cost = 4
        pygame.display.update()
Esempio n. 3
0
class dotGui(object):
    def __init__(self, width=500, height=500):

        self.gamm = TicTacToeGame()
        pygame.init()
        pygame.font.init()
        self.SURF = pygame.display.set_mode((width, height))
        pygame.display.set_caption("Brick Builder")
        self.myfont = pygame.font.SysFont('Arial', 50)
        self.score_font = pygame.font.SysFont('Arial', 30)
        self.dot_font = pygame.font.SysFont('Arial', 15)
        self.state = self.gamm.getInitialState()
        sys.setrecursionlimit(100000)
        self.score = [0, 0]
        self.minimax = SimpleMinimax(self.gamm)
        self.SURF.fill((255, 255, 255))
        self.disp_board()
        self.cost = 4
        pygame.display.update()

    def disp_board(self):
        score = [0, 0]
        RED = (255, 0, 0)
        BLUE = (0, 0, 255)
        self.Black = (0, 0, 0)

        for i, point in enumerate(self.gamm.board):

            gfxdraw.filled_circle(self.SURF, point.x, point.y, 5, self.Black)
            gfxdraw.aacircle(self.SURF, point.x, point.y, 5, self.Black)
            dot_num = self.dot_font.render(str(i), True, self.Black)
            self.SURF.blit(dot_num, (point.x + 10, point.y - 20))

        if len(self.gamm.moves_done) > 0:
            for move in self.gamm.moves_done:
                p1 = self.gamm.board[self.gamm.id_to_index(move.partners[0])]
                p2 = self.gamm.board[self.gamm.id_to_index(move.partners[1])]
                thickness = 3
                if move.O == "X":
                    pygame.draw.line(self.SURF, BLUE, (p1.x, p1.y),
                                     (p2.x, p2.y), thickness)
                elif move.O == "O":
                    pygame.draw.line(self.SURF, RED, (p1.x, p1.y),
                                     (p2.x, p2.y), thickness)

            pygame.display.update()

    def Algorithum(self):

        if len(self.gamm.moves_done) > 4:
            #print("len:{},cost:{}".format(len(self.gamm.moves_done),self.cost))
            self.cost += 1

        self.minimax.minimax_decision(self.state, self.cost)

        self.gamm.board[self.state._action[0]].partners.append(
            self.state._action[1])
        self.gamm.board[self.state._action[1]].partners.append(
            self.state._action[0])

        self.gamm.moves_done.append(
            self.gamm.move_Done((self.state._action[0], self.state._action[1]),
                                'O'))
        if self.complete():
            self.score[1] += 1
            self.disp_board()
            self.comp()
            self.Algorithum()

        self.state._move = 0
        self.state._utility = 0

    def complete(self):

        winposfound = True
        for i, pos in enumerate(self.gamm.boxes):
            winposfound = True
            if pos[4] == 0:
                for indpos in pos[:-1]:
                    if self.gamm.board[self.gamm.id_to_index(
                            indpos[0]
                    )].id not in self.gamm.board[self.gamm.id_to_index(
                            indpos[1]
                    )].partners and self.gamm.board[self.gamm.id_to_index(
                            indpos[1])].id not in self.gamm.board[
                                self.gamm.id_to_index(indpos[0])].partners:
                        winposfound = False

                if winposfound:
                    self.gamm.boxes[i][4] = 1
                    return winposfound
                    break

        return winposfound

    def comp(self):
        if len(self.gamm.getActions(self.state)) == 0:
            if self.score[0] > self.score[1]:
                print("You won! Score: {} to {}".format(
                    self.score[0], self.score[1]))
                input("Press enter to end game:")
                pygame.quit()
                sys.exit()
            elif call.score[1] > call.score[0]:
                print("Computer won :( Score: {} to {}".format(
                    self.score[0], self.score[1]))
                input("Press enter to end game:")
                pygame.quit()
                sys.exit()
            else:
                print("Tie game. Score: {} to {}".format(
                    self.score[0], self.score[1]))
                input("Press enter to end game:")
                pygame.quit()
                sys.exit()
        else:
            print("User Score: {} ,AI Score {}".format(self.score[0],
                                                       self.score[1]))

    def user_move(self):
        try:
            p1, p2 = map(int,
                         input("What move do you want to make?").split(","))
        except ValueError:
            print("Invalid move.")
            self.user_move()
        else:
            if self.gamm.is_connection(p1, p2):
                print("Sorry this move is already taken")
                self.user_move()
            elif not self.gamm.is_valid(p1, p2):
                print("Invalid move.")
                self.user_move()
            else:

                self.gamm.moves_done.append(self.gamm.move_Done((p1, p2), 'X'))
                self.gamm.board[p1].partners.append(p2)
                self.gamm.board[p2].partners.append(p1)
                print('your move is Correct !now you have an other move!!')
                if self.complete():
                    self.score[0] += 1
                    self.disp_board()
                    self.comp()
                    self.user_move()

                self.state._move = 1
Esempio n. 4
0
class GuiHandler(object):
    '''
    classdocs
    '''


    def __init__(self):
        self.root = Tk()
        self.game = TicTacToeGame()
        #sys.setrecursionlimit(10000)
        self.minimax = SimpleMinimax(self.game)

        self.started = True
        self.initializeGui()
        
    def initializeGui(self):
        self.board = TictactoeGUI(self.root)
        self.state = self.game.getInitialState()
        self.board.drawState(self.state)
        self.board.grid(row=0,column = 0,rowspan=5)
        self.btn = Button(self.root, text="Play game")
        self.btn.grid(row=0,column = 1)
    
        self.btn.bind('<Button-1>',self.nextHandler)
        
        self.rb = IntVar()
        Label(self.root, text="Select move ordering: ", justify = LEFT,padx = 20).grid(row=1,column = 1)
        Radiobutton(self.root,text="Move first",padx = 20,variable=self.rb,value=1, justify = LEFT).grid(row=2,column = 1)
        Radiobutton(self.root,text="Move Second",padx = 20,variable=self.rb,value=2, justify = LEFT).grid(row=3,column = 1)
        Radiobutton(self.root,text="Autoplay",padx = 20,variable=self.rb,value=3, justify = LEFT).grid(row=4,column = 1)
        
        self.board.canvas.bind("<Button-1>", self.movePlayer)
        
        
    
        self.lb = Label(self.root,text="Algorithms", font=("Helvetica", 16))
        self.lb.grid(row=6, column=0,sticky="w")
    
        self.root.mainloop()
        
    def movePlayer(self,event):
        if self.started:
            row = int(event.y / 100)
            col = int(event.x / 100)
            self.state._board[row][col] = "O"
            self.board.drawState(self.state)
            self.state._move = 0
            self.minimax.minimax_decision(self.state)
            
#             self.state = st            
            if self.state == None:
                self.lb['text'] = 'Game drawn'
                return                 
            elif self.state._move == -1:
                if self.state._utility == -1:
                    self.lb['text'] = 'Player Won'
                elif  self.state._utility == 1:
                    self.lb['text'] = 'Computer Won'
            else:
                self.state._board[self.state._a][self.state._action[1]] = 'X'
                self.state._move = 1
            self.board.drawState(self.state)
            self.state._utility = 0
        
    def nextHandler(self,_):
        if self.rb.get() != 0:
            self.started = True
        if self.rb.get() == 3:
            threading.Thread(name='c1', target=self.runAlgos, ).start()
        
    def runAlgos(self):
        while self.state._move != -1:
            fun = None
            lmb = None
            retValue = 0
            if self.state._move == 0:
                fun = self.minimax.minvalue
                lmb = lambda a,b: a >= b
                retValue = -1000000000000
            else:
                fun = self.minimax.maxvalue
                lmb = lambda a,b: a <= b
                retValue = 1000000000000
            self.minimax.minimax_decision(self.state)
#             print(action, valu, st)
#             self.state = st
            self.state._utility = 0
            self.board.drawState(self.state)
            self.lb['text'] = str(self.state._utility)
            time.sleep(.1)
Esempio n. 5
0
    def getResult(self, state, action):
        return state._children[action]

    def terminalTest(self, state):
        return state.isLeaf()

    def utility(self, state, player):
        return state._value

    def getAgentCount(self):
        return 2

    def printState(self, state):
        toPrintNodes = []
        toPrintNodes.append(state)
        while len(toPrintNodes) > 0:
            node = toPrintNodes[0]
            del toPrintNodes[0]
            print("Name = %s, value = %d" % (node._name, node._utility))
            toPrintNodes += node._children


if __name__ == "__main__":

    game = MinimaxTreeGame()
    minimax = SimpleMinimax(game)
    initialState = game.getInitialState()
    minimax.minimax_decision(initialState)
    game.printState(initialState)