Example #1
0
    def grow_tree(self):

        if self.state == 'original':  # if original (current) board

            # add a child and nourish him.
            child = self.add_child()
            child.dic_human = dict(self.dic_human)
            child.dic_ai = dict(self.dic_ai)
            child.state = 'human'
            child.last_piece = 'none'
            child.grow_tree()

        elif self.state == 'human':  # if humans go

            # get all valid moves
            self.get_valid_moves(self.dic_human, self.dic_ai,
                                 self.get_ancestor(1).dic_ai)

            # if i have children
            if self.children != []:

                for c in self.children:

                    c.grow_tree()  # grow each of them

            else:

                possible_moves.append(self)  # end of line, so add it here

        elif self.state == 'ai':  # if ai's turn

            # get all valid moves
            self.get_valid_moves(self.dic_ai, self.dic_human,
                                 self.get_ancestor(1).dic_human)
            #print(self.children)

            # loop thru kids
            for c in self.children:

                # finalize turn
                c.dic_human, mb = grid.check_knight(c.dic_human)
                c.dic_ai, mb = grid.check_knight(c.dic_ai)
                c.dic_human, c.dic_ai, b = grid.finalize_move(
                    c.dic_human, c.dic_ai, self.last_piece, c.last_piece)

            # get the cream of the crop moves
            best = best_moves(self.children)
            #print(best)

            for b in best:  #loop thru best moves

                c = self.children[b]

                won = grid.get_winner(
                    c.dic_human,
                    c.dic_ai)  # check win state in future!? Radical dude!

                if won == 'none':  # no winner in sight...

                    if self.level < difficulty:  # if level not reached

                        c.grow_tree()  # keep on growin
                        #print('next_level')

                    else:

                        possible_moves.append(c)  # were done
                        #print('added!', len(possible_moves))

                elif won == 'ai':  # a future where robots rule the world!

                    override.append(c)  # GET THIS MOVE!!

                elif won == 'human':  # i...i had a nightmare

                    return False  # quit this whole branch

                else:  # if draw. Maybe thats all i can hope for

                    possible_moves.append(c)  # maybe it's the best move...

            # if I have no children
            if self.children == []:

                possible_moves.append(self)  # add myself to possible moves
Example #2
0
    def grid_button_clicked(self, event):

        x = (int((event.y - 2) / 64))
        y = (int((event.x - 2) / 64))

        if x > 4:
            x = 4

        if y > 4:
            y = 4

        if x < 0:
            x = 0

        if y < 0:
            y = 0

        if self.cur_piece == '':  # if you haven't picked a piece yet

            for e in self.human_state.dic:  # loop thru human dict

                if self.human_state.board[x][
                        y] == e:  # if this clicked piece is one of yours!!!!

                    self.output_text(ai.speak.get_speech(1))

                    if sound.get() == 1:
                        audio.play_music("pickup.wav")

                    self.cur_piece = self.human_state.board[x][
                        y]  # set it current piece (so we can go to phase 2)
                    self.last_x = x  # save our position
                    self.last_y = y  # ^ ye

                    #self.output_text('You selected ' + self.cur_piece + ' at (' + str(x) +  ',' +str(y) + ')') # wut dis do? lol output

        elif self.cur_piece == self.human_state.board[x][
                y]:  # if you click on the piece you already selected, deselect it!

            if sound.get() == 1:
                audio.play_music("putdown.wav")

            #self.output_text('You deselected ' + self.cur_piece)

            self.cur_piece = ''  # i aint select no darn piece

        else:  # else

            if self.human_state.validate_location(
                    self.last_x, self.last_y, x, y, self.cur_piece,
                    ai.state.board):  # if valid move

                # get the ai move first (so it doesn't cheat ;) )
                dic_ai, piece_ai = ai.get_move(self.human_state.dic)

                # if ai cant find any moves, it must be stalemate (I trust my ai)
                if dic_ai == False:

                    if messagebox.askyesno(
                            'Play again?',
                            ai.speak.get_speech(7) +
                            '\n\nStalemate! \n Would you like to play again?'):
                        self.new_game()
                    else:  # you lose
                        sys.exit()

                # jus sum outbuts
                #self.output_text('You moved ' + self.cur_piece + ' to (' + str(x) +  ',' +str(y) + ')')

                # move human piece
                self.human_state.dic = self.human_state.move_piece(
                    x, y, self.cur_piece, self.human_state.dic)

                # now actually make the moves (let the carnage begin \(>-<)/ )
                self.human_state.dic, dic_ai, msg = grid.finalize_move(
                    self.human_state.dic, dic_ai, self.cur_piece, piece_ai)
                #print(self.human_state.dic, dic_ai)

                if msg != '':
                    self.output_text(msg)

                if sound.get() == 1:
                    audio.play_music("alert.wav")

                # check if the peasants are worthy of knighthood
                dic_ai, mb = grid.check_knight(dic_ai)

                # make the boards used for display from dicts
                self.human_state.board = grid.recreate_grid(
                    self.human_state.dic)
                ai.state.board = grid.recreate_grid(ai.state.dic)

                # if you have 2 knights, put random
                if mb != 'none':

                    x = random.randint(0, grid.GRID_HEIGHT - 1)
                    y = random.randint(0, grid.GRID_WIDTH - 1)

                    while self.human_state.board[x][
                            y] != grid.b and ai.state.board[x][y] != grid.b:
                        x = random.randint(0, grid.GRID_HEIGHT - 1)
                        y = random.randint(0, grid.GRID_WIDTH - 1)

                    dic_ai[mb] = [x, y]
                    self.output_text('AI relocated ' + mb + ' to (' + str(x) +
                                     ',' + str(y) + ')')

                # k
                self.human_state.dic, mb = grid.check_knight(
                    self.human_state.dic)

                # if you have 2 knights, ask to relocate
                if mb != 'none':
                    a = Popup_Knight(self.root, mb, self.human_state.dic, self)

                # send the ai dictionary back (it got updated)
                ai.dic_ai = dic_ai

                # make the boards used for display from dicts
                self.human_state.board = grid.recreate_grid(
                    self.human_state.dic)
                ai.state.board = grid.recreate_grid(ai.dic_ai)

                #self.update_grid()

                # if WE HAVE A WINNER!
                won = grid.get_winner(self.human_state.dic, ai.dic_ai)

                if won == 'ai':  # if my great ai won (and it will ;) )

                    if messagebox.askyesno(
                            'Play again?',
                            ai.speak.get_speech(5) +
                            '\n\nYou Lose! :( \n Would you like to play again?'
                    ):
                        self.new_game()
                    else:  # you lose
                        sys.exit()

                elif won == 'human':  # if you fluked out somehow :p

                    if messagebox.askyesno(
                            'Play again?',
                            ai.speak.get_speech(6) +
                            '\n\nYou win! :) \n Would you like to play again?'
                    ):
                        self.new_game()
                    else:  # you lose
                        sys.exit()

                elif won == 'draw':  # if it is stalemate, Nova!

                    if messagebox.askyesno(
                            'Play again?',
                            ai.speak.get_speech(7) +
                            '\n\nStalemate!!! \n Would you like to play again?'
                    ):
                        self.new_game()
                    else:  # you lose
                        sys.exit()

                #print(grid.recreate_grid(self.human_state.dic))
                #print(grid.recreate_grid(ai.dic_ai))

                self.cur_piece = ''  # you already moved so why u need dat piece homie?

            else:

                self.output_text('You earned a penalty point')  # oh no!!!
                self.output_text(ai.speak.get_speech(1))
                self.human_state.penalty += 1  # stacks on stacks
                self.bac_canvas.itemconfig('penalty', image=self.spr_penalty)
                #audio.play_beep("SystemHand") #replace

                if self.human_state.penalty == 2:  # red card + 6 fouls :/

                    if messagebox.askyesno(
                            'Play again?',
                            ai.speak.get_speech(5) +
                            '\n\nYou Lose! :( \n Would you like to play again?'
                    ):
                        self.new_game()
                    else:  # you lose
                        sys.exit()  # and your kicked out! <(;-;)>