Exemplo n.º 1
0
    def button_click(self):

        # get row/col from entries (can only handle integers)
        try:
            row = int(self.row_entry.get()) - 1
            col = int(self.col_entry.get()) - 1
        except ValueError:
            messagebox.showinfo('Not a number',
                                'Please enter numbers from 1 to 5')

        # recreate dem 2 boards...
        board = grid.recreate_grid(self.dic)
        o_board = grid.recreate_grid(ai.dic_ai)

        # if on grid...
        if 0 <= col < grid.GRID_WIDTH and 0 <= row < grid.GRID_HEIGHT:

            if board[row][col] == grid.b and o_board[row][
                    col] == grid.b:  # if nobody on dat spot homebody

                self.dic[self.piece] = [row, col]  # set the chance
                self.gui.human_board.board = grid.recreate_grid(
                    self.dic)  # and add to official board

                #method = getattr(ApocalypseGUI, 'update_board')
                #method(self.dic)

                # update original...
                self.gui.dic_human = self.dic

                # ye
                #self.gui.update_grid()

                self.top.destroy()  # :( ill miss him

            else:

                messagebox.showinfo('!', 'Invalid coordinates')

        else:

            messagebox.showinfo('!', 'Not on board')
Exemplo n.º 2
0
def get_move(dic_human):

    global possible_moves
    global override

    # setup a tree and expand it
    top = node()
    top.state = 'original'
    top.dic_ai = state.dic
    top.dic_human = dic_human

    top.grow_tree()
    #print(possible_moves)

    if override != []:  # if there is a move that will lead to a checkmate

        best = best_moves(override)

        if len(best) > 1:

            num = random.randint(0, len(best) - 1)  # pick randomly

        else:

            num = 0

        choose = best[num]

        chosen_node = override[choose].get_ancestor(3)  #get the first move

    elif possible_moves != []:  # get best moves

        best = best_moves(possible_moves)

        num = random.randint(0, len(best) - 1)  # pick randomly

        choose = best[num]

        chosen_node = possible_moves[choose].get_ancestor(
            3)  # get the first move

    else:  # if no moves, game is won/lost or stalemate

        return False, False

    # if game is won, will throw error, because it can't expand tree
    # these prevent the loss of fun. All about customer service!
    try:
        state.dic[chosen_node.last_piece] = [
            chosen_node.new_row, chosen_node.new_col
        ]
    except ValueError:
        return False, False
    except AttributeError:
        return False, False

    #print(dic_ai[chosen_node.last_piece])

    # update ai board
    state.board = grid.recreate_grid(chosen_node.dic_ai)
    #print(override)

    # reset moves
    possible_moves = []
    override = []

    return state.dic, chosen_node.last_piece
Exemplo n.º 3
0
    def get_valid_moves(self, dic, dic_opp, dic_org):

        ##### A LOT OF REPEATED CODE IN THIS FUNCTION, HOWEVER THIS IS FOR SPEED. PUTTING IT IN ANOTHER FUNCTIONS SLOWS IT DOWN CONSIDERABLY

        for piece in dic:  # go thru dictionary, piece by piece

            loc = dic[piece]  # get the location of each piece(list)

            if loc != 'dead':  # if piece isn't dead...

                board = grid.recreate_grid(
                    dic)  # trying to optimize speed, boards are slighty faster
                opp_board = grid.recreate_grid(dic_opp)
                org_board = grid.recreate_grid(dic_org)

                if piece[1] == 'K':  # if piece is a knight

                    for km in KNIGHT_MOVES:

                        can_move = True

                        new_row = loc[0] + km[0]
                        new_col = loc[1] + km[1]

                        #print(piece, new_row, new_col, loc[0], loc[1])

                        if 0 <= new_col < grid.GRID_WIDTH and 0 <= new_row < grid.GRID_HEIGHT:  # if move is on board

                            # if move is not overlapping another piece
                            if board[new_row][new_col] != grid.b:
                                can_move = False

                            if can_move:

                                # add child and teach it what I know
                                child = self.add_child()
                                child.dic_human = dict(self.dic_human)
                                child.dic_ai = dict(self.dic_ai)
                                child.last_piece = piece
                                child.new_row = new_row
                                child.new_col = new_col

                                # if i am human
                                if self.state == 'human':

                                    # update child and switch turn
                                    child.dic_human[piece] = [new_row, new_col]
                                    child.state = 'ai'

                                elif self.state == 'ai':  # if i am ai

                                    # ^^^^^^^^^^^^^^^^^^ (line 180)
                                    child.dic_ai[piece] = [new_row, new_col]
                                    child.state = 'human'

                elif piece[1] == 'P':  # if I am pawn

                    can_move = False

                    # which way is forward???
                    if piece[0] == 'W':
                        forward = -1
                    elif piece[0] == 'B':
                        forward = 1

                    # move ^
                    new_row = loc[0] + forward
                    new_col = loc[1]

                    if 0 <= new_col < grid.GRID_WIDTH and 0 <= new_row < grid.GRID_HEIGHT:  # if move is on board

                        # if move is not overlapping another piece
                        if board[new_row][new_col] == grid.b and opp_board[
                                new_row][new_col] == grid.b and org_board[
                                    new_row][new_col] == grid.b:
                            can_move = True

                        if can_move:

                            child = self.add_child()
                            child.dic_human = dict(self.dic_human)
                            child.dic_ai = dict(self.dic_ai)
                            child.last_piece = piece
                            child.new_row = new_row
                            child.new_col = new_col

                            if self.state == 'human':
                                child.dic_human[piece] = [new_row, new_col]
                                child.state = 'ai'

                            elif self.state == 'ai':
                                child.dic_ai[piece] = [new_row, new_col]
                                child.state = 'human'

                    can_move = False

                    # attack ->^
                    new_row = loc[0] + forward
                    new_col = loc[1] + 1

                    if 0 <= new_col < grid.GRID_WIDTH and 0 <= new_row < grid.GRID_HEIGHT:  # if move is on board

                        # if move is not overlapping another piece and there is enemy
                        if board[new_row][new_col] == grid.b and opp_board[
                                new_row][new_col] != grid.b and org_board[
                                    new_row][new_col] != grid.b:
                            can_move = True

                        if can_move:

                            child = self.add_child()
                            child.dic_human = dict(self.dic_human)
                            child.dic_ai = dict(self.dic_ai)
                            child.last_piece = piece
                            child.new_row = new_row
                            child.new_col = new_col

                            if self.state == 'human':
                                child.dic_human[piece] = [new_row, new_col]
                                child.state = 'ai'

                            elif self.state == 'ai':
                                child.dic_ai[piece] = [new_row, new_col]
                                child.state = 'human'

                    can_move = False

                    #for k in board:
                    #print(k)

                    # attack ^<-
                    new_row = loc[0] + forward
                    new_col = loc[1] - 1

                    if 0 <= new_col < grid.GRID_WIDTH and 0 <= new_row < grid.GRID_HEIGHT:  # if move is on board

                        # if move is not overlapping another piece and there is enemy
                        if board[new_row][new_col] == grid.b and opp_board[
                                new_row][new_col] != grid.b and org_board[
                                    new_row][new_col] != grid.b:
                            can_move = True

                        if can_move:
                            #print('yaya')
                            child = self.add_child()
                            child.dic_human = dict(self.dic_human)
                            child.dic_ai = dict(self.dic_ai)
                            child.last_piece = piece
                            child.new_row = new_row
                            child.new_col = new_col

                            if self.state == 'human':
                                child.dic_human[piece] = [new_row, new_col]
                                child.state = 'ai'

                            elif self.state == 'ai':
                                child.dic_ai[piece] = [new_row, new_col]
                                child.state = 'human'
Exemplo n.º 4
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! <(;-;)>