Exemple #1
0
class MainForm(tk.Canvas):
    size = 8
    cell_size = 60
    margin = 7

    def __init__(self, master):
        width = self.size * self.cell_size + 1
        tk.Canvas.__init__(self,
                           master,
                           relief=tk.RAISED,
                           bd=4,
                           bg="green",
                           width=width,
                           height=width)

        self.grid(column=0, row=0)

        self.controller = GameController(self, self.size, BasicAI(), BasicAI())
        self.controller.play()

    def refresh(self, board):
        for i in range(self.size):
            for j in range(self.size):
                x0 = i * self.cell_size + self.margin
                y0 = j * self.cell_size + self.margin
                self.create_rectangle(x0,
                                      y0,
                                      x0 + self.cell_size,
                                      y0 + self.cell_size,
                                      fill="green")
                if not board.isBlank(j, i) and board.isBlack(j, i):
                    self.create_oval(x0 + 2,
                                     y0 + 2,
                                     x0 + self.cell_size - 2,
                                     y0 + self.cell_size - 2,
                                     fill="black")
                elif not board.isBlank(j, i) and not board.isBlack(j, i):
                    self.create_oval(x0 + 2,
                                     y0 + 2,
                                     x0 + self.cell_size - 2,
                                     y0 + self.cell_size - 2,
                                     fill="white")

    def refresh_color(self, board, isBlack):
        self.refresh(board)
        for i in range(self.size):
            for j in range(self.size):
                if board.canPlace(j, i, isBlack):
                    x = i * self.cell_size + self.margin + self.cell_size / 2
                    y = j * self.cell_size + self.margin + self.cell_size / 2
                    self.create_text(x,
                                     y,
                                     text=str(board.getEValue(j, i, isBlack)),
                                     justify="center")
Exemple #2
0
def play_game():
    game = GameController()
    tracker = PlayTracker()
    bot = BotPlayer(MODEL_FILE, PLAYS_CATEGORIES_FILE, RESULT_CATEGORIES_FILE)

    while game.is_playing():
        ui.draw_board(game)

        x, y = -1, -1
        if game.current_player_A():
            x, y = ui.read_user_play(game)
        else:
            x, y = bot.play(game)
            ui.print_bot_play(x, y)

        tracker.track(game, x, y)
        game.play(x, y)

    ui.draw_winner(game)
    tracker.store(game, PLAYS_FILE)
    train_model(PLAYS_FILE, MODEL_FILE, PLAYS_CATEGORIES_FILE,
                RESULT_CATEGORIES_FILE)