Example #1
0
File: gui.py Project: KeivanR/chess
 def display_pieces(self, table, to=1, save=True):
     self.bkg = Image.open(constants.chessboard_path[constants.os_name])
     for i in range(8):
         for j in range(8):
             if table[j, i] != 0:
                 load = Image.open(pieces.images[6 + table[j, i]])
                 load = load.rotate(90 * (to + 1))
                 load = load.resize((40, 40))
                 self.bkg.paste(load, (46 * (7 - j) + 32, 46 * i + 32),
                                load)
     self.bkg = self.bkg.rotate(90 * (to + 1))
     w = 0
     b = 0
     piece = -5
     for i in range(len(self.taken)):
         if self.taken[i] < 0:
             if piece == self.taken[i]:
                 b += 0.2
             else:
                 b += 0.45
             mouse = tabletomouse(-1 + b, 3.5 - 4.4 * to, 1)
         else:
             if piece == self.taken[i]:
                 w += 0.2
             else:
                 w += 0.45
             mouse = tabletomouse(-1 + w, 3.5 + 4.4 * to, 1)
         piece = self.taken[i]
         load = Image.open(pieces.images[6 + piece])
         load = load.resize((30, 30))
         self.bkg.paste(load, (mouse[0], mouse[1]), load)
     if pieces.exposed_king(self.cb.table,
                            self.last,
                            self.still,
                            no_move=True):
         color = 2 * (self.cb.table[pieces.xy(self.last[1])[0],
                                    pieces.xy(self.last[1])[1]] > 0) - 1
         [x, y] = np.where(self.cb.table == -6 * color)
         mouse = tabletomouse(int(x), int(y), self.chess_up)
         load = Image.open(constants.reds_path[constants.os_name])
         load = load.resize((35, 35))
         load.putalpha(128)
         self.bkg.paste(load, (mouse[0], mouse[1]), load)
     if save:
         self.hist.append(self.bkg)
         self.data_hist.append(self.cb.table)
         self.hist_time = len(self.hist) - 1
     render = ImageTk.PhotoImage(self.bkg)
     img = Label(self, image=render)
     img.image = render
     img.grid(row=0, column=0)
Example #2
0
File: gui.py Project: KeivanR/chess
 def allowed_moves(self, allrules, movexy):
     for r in allrules:
         if r.split()[0] == movexy:
             xy2 = pieces.xy(r.split()[1])
             mouse2 = tabletomouse(xy2[0], xy2[1], self.chess_up)
             load = Image.open(constants.yellows_path[constants.os_name])
             load = load.resize((35, 35))
             load.putalpha(128)
             self.bkg.paste(load, (mouse2[0], mouse2[1]), load)
Example #3
0
File: gui.py Project: KeivanR/chess
 def show_last(self):
     xy1 = pieces.xy(self.last[0])
     xy2 = pieces.xy(self.last[1])
     mouse1 = tabletomouse(xy1[0], xy1[1], self.chess_up)
     mouse2 = tabletomouse(xy2[0], xy2[1], self.chess_up)
     p1 = -5
     p2 = 33
     load = Image.open(constants.reds_path[constants.os_name])
     load = load.resize((42, 5))
     self.bkg.paste(load, (mouse1[0] + p1, mouse1[1] + p2), load)
     self.bkg.paste(load, (mouse2[0] + p1, mouse2[1] + p2), load)
     load = load.resize((42, 5))
     self.bkg.paste(load, (mouse1[0] + p1, mouse1[1] + p1), load)
     self.bkg.paste(load, (mouse2[0] + p1, mouse2[1] + p1), load)
     load = load.resize((5, 42))
     self.bkg.paste(load, (mouse1[0] + p2, mouse1[1] + p1), load)
     self.bkg.paste(load, (mouse2[0] + p2, mouse2[1] + p1), load)
     load = load.resize((5, 42))
     self.bkg.paste(load, (mouse1[0] + p1, mouse1[1] + p1), load)
     self.bkg.paste(load, (mouse2[0] + p1, mouse2[1] + p1), load)
Example #4
0
File: ai.py Project: KeivanR/chess
    def move(self, table, last, still, data_hist):
        if self.level == -1:
            allrules = pieces.allrules_ek(table, last, still)
            return allrules[0]
        elif self.level == 0:
            allrules = pieces.allrules_ek(table, last, still)
            return random.choice(allrules)
        else:
            if last is None:
                color = 1
            else:
                color = -table[pieces.xy(last[1])[0]][pieces.xy(
                    last[1])[1]] / np.abs(table[pieces.xy(
                        last[1])[0]][pieces.xy(last[1])[1]])
            if self.tree:
                res = rec_sum_tree(table,
                                   self.node,
                                   last,
                                   still,
                                   color,
                                   self.level - 1,
                                   noha=0,
                                   noha_lim=self.noha_lim)
                self.update_tree(res[0])
            else:
                res = rec_sum(table,
                              last,
                              still,
                              data_hist,
                              color,
                              self.level - 1,
                              noha=0,
                              noha_lim=self.noha_lim,
                              first_layer=True)

            print('AI(', color, ') assessment: ', res[1])
            return res[0]
Example #5
0
File: gui.py Project: KeivanR/chess
    def callback(self, event):
        if self.startGame and self.option != 'Two computers' and not self.checkmate:
            [x, y] = (mousetotable(event.x, event.y, self.chess_up))
            if not pieces.oncb(x, y):
                self.a = self.b = None
                return 0
            movexy = pieces.mv(x, y)
            allrules = pieces.allrules_ek(self.cb.table, self.last, self.still)
            if self.a is None:
                self.a = movexy
                self.allowed_moves(allrules, movexy)
            else:
                if np.abs(self.cb.table[pieces.xy(self.a)[0]][pieces.xy(
                        self.a)[1]]) == 1 and y == 3.5 + 3.5 * self.chess_up:
                    prom = input('Promotion:N,B,R,Q?')
                    self.b = movexy + prom
                else:
                    self.b = movexy
                if self.a + ' ' + self.b not in allrules:
                    self.a = movexy
                    self.b = None
                    self.display_pieces(self.cb.table,
                                        to=self.chess_up,
                                        save=False)
                    self.allowed_moves(allrules, movexy)
                else:
                    s = np.sum(self.cb.table)
                    self.cb.table = pieces.move(self.cb.table, self.a, self.b,
                                                self.still)
                    if ai.repet(self.cb.table, self.data_hist):
                        self.checkmate = 1
                    self.add_taken(s - np.sum(self.cb.table))
                    self.last = [self.a, self.b]
                    self.display_pieces(self.cb.table, to=self.chess_up)
                    self.update()
                    allrules = pieces.allrules_ek(self.cb.table, self.last,
                                                  self.still)
                    if len(allrules) == 0 or self.checkmate:
                        if pieces.exposed_king(self.cb.table,
                                               self.last,
                                               self.still,
                                               no_move=True):
                            self.winfo_toplevel().title("Checkmate!")
                        else:
                            self.winfo_toplevel().title("Stalemate!")
                        self.checkmate = 1

                    if self.option == 'Two players':
                        time.sleep(1)
                        self.chess_up = -self.chess_up
                        self.display_pieces(self.cb.table, to=self.chess_up)
                    elif not self.checkmate:
                        self.comp.update_tree(self.last[0] + ' ' +
                                              self.last[1])
                        start = time.time()
                        cmove = self.comp.move(self.cb.table, self.last,
                                               self.still,
                                               self.data_hist).split()
                        print(
                            int(1000 * float(time.time() - start)) / 1000, 's')
                        s = np.sum(self.cb.table)
                        self.cb.table = pieces.move(self.cb.table, cmove[0],
                                                    cmove[1], self.still)
                        if ai.repet(self.cb.table, self.data_hist):
                            self.checkmate = 1
                        self.add_taken(s - np.sum(self.cb.table))
                        self.last = cmove
                        self.display_pieces(self.cb.table, to=self.chess_up)
                        allrules = pieces.allrules_ek(self.cb.table, self.last,
                                                      self.still)
                        if len(allrules) == 0:
                            if pieces.exposed_king(self.cb.table,
                                                   self.last,
                                                   self.still,
                                                   no_move=True):
                                self.winfo_toplevel().title("Checkmate!")
                            else:
                                self.winfo_toplevel().title("Stalemate!")
                            self.checkmate = 1
            if self.last is not None:
                self.show_last()
            render = ImageTk.PhotoImage(self.bkg)
            img = Label(self, image=render)
            img.image = render
            img.grid(row=0, column=0)
            self.update()