Beispiel #1
0
    def displayBoard(self, playing = True):
        self.clicks = []
        self.destroyButtons()

        self.resetDist()

        if playing:
            self.lbl.config( text="Click on a location to prepare a move. If a tank is available and you are surfaced then you can purchase it.", fg = "black")
        if self.state.isOver():
            self.lbl.config(text = "GAMEOVER", fg = "red")
        tanks = [tank for tank in self.state.tanks]
        self.cash = Label(self.master, text = "     Cash: " + str(self.state.cash) + "     ", font = ("Helvetica", 15))
        self.cash.grid(row = 15, column = 15)
        self.time = Label(self.master, text = "    Time: " + str(self.state.timeLeft) + "     ", font = ("Helvetica", 15))
        self.time.grid(row = 16, column = 15)
        self.oxygen = Label(self.master, text = "        Oxygen Left: " + str(self.state.oxygenLeft) + " / " + str(self.state.tankSize), font = ("Helvetica", 15))
        self.oxygen.grid(row = 17, column = 15)
        self.holding = Label(self.master, text = "     Holding: " + str(sum((self.state.holding))) + "     ", font = ("Helvetica", 15))
        self.holding.grid(row = 18, column = 15)

        self.buttons['cash'] = self.cash
        self.buttons['time'] = self.time
        self.buttons['oxygen'] = self.oxygen
        self.buttons['holding'] = self.holding


        if playing:
            def clickTank(action):
                def executeMove():
                    self.clicks.append(action)
                    def do():
                        self.move = action
                        if self.move in self.state.getLegalActions():
                            self.var.set(1)
                    if not self.showing:    
                        if  (self.state.playerLoc == (0,0) or self.state.playerLoc == (0,9)) and self.state.cash >= action[0]:
                            self.lbl.config(text = "Tank | Cost: " + str(action[0]) + " | Size: " + str(action[1]))
                            if hasattr(self, 'next'):
                                        self.next.destroy()
                            self.next = Button(self.master, text="Execute Move", font = ("Helvetica", 15), command = do)
                            self.next.grid(row = 5, column=15)
                            self.buttons['next'] = self.next
                        else:
                            print(self.state.cash, action[1])
                            self.lbl.config(text = "Cannot Buy Tank")
                    if self.prevButton:
                        prevbutton = self.buttons[self.prevButton]
                        if self.prevButton != self.state.playerLoc:
                            if self.prevButton == (0, 0) or self.prevButton == (0, 9):
                                prevbutton.config(background = "Green")
                            elif len(self.prevButton) == 3:
                                prevbutton.config(background = "white")
                            else:
                                prevbutton.config(background = "Light Gray")
                    self.buttons[action].config(background = "orange")
                    self.prevButton = action
                    return do
                return executeMove
        else:
            def clickTank(action):
                pass
        c = 0
        if playing:
            enabled = "normal"
        else:
            enabled = "disabled"
        self.tanks = []
        for tank in tanks:
            if tank in self.state.getLegalActions():
                t = Button(self.master, text = "Tank | Cost: " + str(tank[0]) + " | Size: " + str(tank[1]), font = ("Helvetica", 15), command = clickTank(tank), state = enabled)
                t.grid(row = 10 + c, column = 15)
                self.tanks.append(t)
                self.buttons[tank] = t
                c += 1
            else:
                t = Button(self.master, text = "Tank | Cost: " + str(tank[0]) + " | Size: " + str(tank[1]), font = ("Helvetica", 15), command = clickTank(tank), state = enabled)
                t.config(state = "disabled")
                t.grid(row = 10 + c, column = 15)
                self.tanks.append(t)
                self.buttons[tank] = t
                c += 1
        if self.state.playerLoc == (0,0) or self.state.playerLoc == (0,9) and playing:
            def exit():
                self.move = (None, None, 'exit')
                self.var.set(1) 
            print("EXIT")
            if (self.state.playerLoc == (0, 0) or self.state.playerLoc == (0, 9)):
                self.exit = Button(self.master, text = "EXIT with all your current cash! ", font = ("Helvetica", 15), command = exit, state = enabled)
                self.buttons['exit'] = self.exit
            self.exit.grid(row = 19, column = 15)
        for i in range(0, 20):
            for j in range(0, 10):
                if not (i == 0 and (j < 9 and j > 0)):
                    bg = None
                    fg = "black"
                    color = "white"
                    font = ("Helvetica", 11)
                    if i > 0:
                        color = "navy"
                    if self.state.board[i][j]:
                        color = "Light Gray"
                        text = str(self.state.board[i][j])
                    elif (i, j) == (0,0) or (i, j) == (0,9):
                        text = ""
                        color = "Green"
                    else:
                        text = ""
                    if self.state.playerLoc == (i, j):
                        text = "P"
                        color = "Yellow"
                        fg = "brown4"
                        font = ("Helvetica", 10, "bold")
                    if self.state.playerLoc == (i, j) and self.state.playerLoc[0] != 0: 
                        color = "Yellow"
                    b = Button(self.master)
                    b.grid(row=i, column = j)
                    b.row = i
                    b.column = j
                    self.buttons[(i, j)] = b
                    def click(action):
                        i = action[0]
                        j = action[1]
                        def do():
                            valid = False
                            def executeMove(action):
                                def do():
                                    self.move = action
                                    self.prevButton = None
                                    if self.move in self.state.getLegalActions():
                                        self.var.set(1)
                                return do
                            if not self.showing:
                                if ((i, j) == (0, 0) or (i, j) == (0, 9)) and (i, j) != self.state.playerLoc:
                                    valid = True
                                    text = "Surface Location: " + str((i, j))# + "| distance is " + str(manDist((i, j), self.state.playerLoc))
                                if self.state.board[i][j]:
                                    valid = True
                                    text = "Pick up " + str(self.state.board[i][j])+ " at location " + str((i, j))# + " | distance is " + str(manDist((i, j), self.state.playerLoc))
                                if valid:
                                    self.clicks.append(action)
                                    if self.prevButton:
                                        prevbutton = self.buttons[self.prevButton]
                                        if self.prevButton != self.state.playerLoc:
                                            if self.prevButton == (0, 0) or self.prevButton == (0, 9):
                                                prevbutton.config(background = "Green")
                                            else:
                                                prevbutton.config(background = "Light Gray")
                                    self.buttons[(i, j)].config(background = "orange")
                                    self.prevButton = (i, j)
                                    if not self.locations:
                                        self.distance += manDist((i, j), self.state.playerLoc)
                                    else:
                                        self.distance += manDist((i, j), self.locations[-1])
                                    self.locations += [(i, j)]
                                    self.distanceLabel.config(text = str(self.locations) + ": " + str(self.distance))
                                    self.lbl.config(text = text)
                                    if hasattr(self, 'next'):
                                        self.next.destroy()
                                    self.next = Button(self.master, text="Execute Move", font = ("Helvetica", 15), command = executeMove(action), state = enabled)
                                    self.next.grid(row = 5, column=15)
                                    self.buttons['next'] = self.next

                        return do   
                    b.config(highlightthickness=0, text = text, width="3",height="2",font= font, fg = fg, command = click((i, j, "move")), background = color)