Exemplo n.º 1
0
class Game:
    def __init__(self, win):
        '''
        de facto wywolanie _init() przypisanie zmiennej win która jest naszym oknem
        '''
        self.win = win
        self._init()

    def update(self):
        '''
        funkcja update uruchamiana w każdym obiegu głównej funkcji main,
        rysuje ustawienie szachownicy jak i samą szachownicę
        dozwolone ruchy o ile istnieją
        po części obłsuguje wyjątki poprzez printowanie tekstów "wywoływanych" przez wyjątki  za pomocą zmiany stanów zmiennych
        kontrolowanie czy któryś z graczy nie wygrał meczu poprzez sprawdzanie zmienne winner, jesli tak obsługuje
        printa z informacja kto wygrał
        '''
        self.board.draw(self.win)
        self.printing_moves()
        self.draw_valid_moves(self.valid_moves)
        if self.bad_move_exce:
            self.bad_move_exe()
            pygame.time.delay(1000)
        self.bad_move_exce = False
        if self.outside_board_exce:
            self.outside_board_exe()
            pygame.time.delay(1000)
        self.outside_board_exce = False
        if self.winner() != None:
            if self.winner() == WHITE:
                pygame.draw.rect(
                    self.win, GREY,
                    (1 * SQUARE_SIZE + SQUARE_SIZE // 2, 4 * SQUARE_SIZE,
                     5 * SQUARE_SIZE, SQUARE_SIZE + 5))
                self.show_text("WHITE HAS WON", 4, 1.5, WHITE)
            else:
                pygame.draw.rect(
                    self.win, GREY,
                    (1 * SQUARE_SIZE + SQUARE_SIZE // 2, 4 * SQUARE_SIZE,
                     5 * SQUARE_SIZE, SQUARE_SIZE + 5))
                self.show_text("RED HAS WON", 4, 2, WHITE)
        pygame.display.update()

    def show_text(self, content, x, y, rgb):
        '''
        printowanie tekstu o zadanym kolorze rgb na ekranie w konkretnym miejscu
        '''
        text = FONT2.render(content, True, rgb)
        self.win.blit(text, (y * SQUARE_SIZE + SQUARE_SIZE // 4 + 5,
                             x * SQUARE_SIZE + SQUARE_SIZE // 4 + 5))

    def _init(self):
        '''
        de facto funkcja inicjalizująca wywoływana w __init__(), było to potrzebne aby łatwo robić resety
        mamy takie parametry jak selected - zaznaczony item (typu zwykła bierka/król)
        stworzenie obiektu Board (nasza plansza)
        aktualna tura
        dostępne ruchy do funkcji valid_moves
        pola dla wyjątków do obsługi nieco zawiłej procedury printowania wyjątków na ekranie gry pygame
        '''
        self.selected = None
        self.board = Board(self.win)
        self.turn = RED
        self.valid_moves = {}
        self.bad_move_exce = False
        self.outside_board_exce = False

    def reset(self):
        '''
        resetowanie gry poprzez ponowne wywołanie funkcji _init()
        :return:
        '''
        self._init()

    def select(self, row, col):
        '''
        wybranie konkretnego itemu z naszej tablicy (szachownica ma dostępne pola 0 - puste, Piece - obiekt podsatwowej bierki, King - bierka król)
        wybieranie przebiega na podsatwie zmiennych row,col które symbolizują pola 8x8 planszy
        obsługa dwóch wyjątków - łapanie wyjątku odnośnie "bad move" kliknięcie podczas tury ruchu pole które nie jest dozwolonym ruchem
        oraz podnoszenie wyjątku o kliknięciu w dolny pasek który nie jest responsywny a jest jedynie tłem
        w dodatku metoda obsługuje restart gry gdy przekazane współrzędne odpowiadają naszemu przyciskowi

        '''
        if row != 8:
            # jeśli mamy już coś zaznaczonego (bierkę) próbujemy zrobić ruch
            if self.selected:
                try:
                    result = self._move(row, col)
                    if not result:
                        self.selected = None
                        self.select(row, col)
                except BadMoveExceptions:
                    self.bad_move_exce = True
                    print("bad move")
            # pobieramy wartość z pod konkretnego indeksu na tablicy
            piece = self.board.get_piece(row, col)
            # jeżeli obiekt != znaczy ze jest to jakaś bierka, sprawdzamy czy to nasza tura
            # i czy mamy jakieś ruchy dla tej konkretnej instacji (bierki)
            # jeśli tak zracamy true, jesli nie false
            if piece != 0 and piece.color == self.turn:
                self.selected = piece
                self.valid_moves = self.board.get_valid_moves(piece)
                return True
            elif piece != 0 and piece.color != self.turn:
                print("zly kolor")
            return False
        # jeżeli klikniecie jest w 8 rząd czyli poniżej planszy (0-7 rzędy) sprawdzamy czy kliknęliśmy w guzik
        elif col == 0 or col == 1:
            self.restart_game()
        # jeśli kliknęlismy poza plansze ale nie w guzik wyrzucamy błąd
        else:
            raise OutsideBoardExceptions()

    def bad_move_exe(self):
        '''
        obsługa printu dla niedozwolonego ruchu
        '''
        pygame.draw.rect(self.win, GREY,
                         (1 * SQUARE_SIZE + SQUARE_SIZE // 2, 4 * SQUARE_SIZE,
                          5 * SQUARE_SIZE, SQUARE_SIZE + 5))
        self.show_text("BAD MOVE", 4, 1.5, BLACK)

    def outside_board_exe(self):
        '''
        obsługa printu dla kliknięcie poza obszar planszy który jednak dalej jest w oknie gry
        '''
        pygame.draw.rect(self.win, GREY,
                         (1 * SQUARE_SIZE + SQUARE_SIZE // 2, 4 * SQUARE_SIZE,
                          5 * SQUARE_SIZE, SQUARE_SIZE + 5))
        self.show_text("Outside board", 4, 1.5, BLACK)

    def restart_game(self):
        '''
        metoda restartująca poprzez ponowne wywołanie inita który inicjalizuje wartości
        '''
        print("restart")
        self._init()

    def _move(self, row, col):
        '''
        metoda sprawdzająca czy czy pod podanymi współrzędnymi znajduje się się 0 czyli brak jakiegoś obiektu i
        czy można tam przestawić naszą bierkę
        '''
        piece = self.board.get_piece(row, col)
        if self.selected and piece == 0 and (row, col) in self.valid_moves:
            self.board.move(self.selected, row, col)
            skipped = self.valid_moves[(row, col)]
            if skipped:
                self.board.remove(skipped)
            self.change_turn()
        else:
            if piece == 0:
                raise BadMoveExceptions()
        return True

    def change_turn(self):
        '''
        metoda która zmienia turę bazując na obecnej turze
        :return:
        '''
        self.valid_moves = {}
        if self.turn == RED:
            self.turn = WHITE
        elif self.turn == WHITE:
            self.turn = RED

    def draw_valid_moves(self, moves):
        '''
        na podstawie elementów w valid_moves = możliwe ruchy rysujemy niebieskie kropki w tych punktach
        '''
        for move in moves:
            row, col = move
            pygame.draw.circle(self.win, BLUE,
                               (col * SQUARE_SIZE + SQUARE_SIZE // 2,
                                row * SQUARE_SIZE + SQUARE_SIZE // 2), 12)

    def winner(self):
        '''
        metoda zwracająca zwycięzce na podstawie zmiennych zawartych w klasie
        '''
        return self.board.winner()

    def printing_moves(self):
        '''
        obsługa printowania tury gracza na dole ekranu z grą
        '''
        if self.turn == RED:
            self.show_text("RED's MOVE", 7.8, 2, BLACK)
        else:
            self.show_text("WHITE's MOVE", 7.8, 2, BLACK)
Exemplo n.º 2
0
class Game:
    def __init__(self, win):
        self._init()
        self.win = win

    def update(self):
        self.board.draw(self.win)
        self.draw_valid_moves(self.valid_moves)
        pygame.display.update()

    # _init makes it a private method of the class
    def _init(self):
        self.selected = None
        self.board = Board()
        self.turn = RED
        self.valid_moves = {}

    def winner(self):
        return self.board.winner()

    def reset(self):
        self._init()

    def select(self, row, col):
        if self.selected:
            result = self._move(row, col)
            if not result:
                self.selected = None
                self.select(row, col)

        piece = self.board.get_piece(row, col)
        if piece != 0 and piece.color == self.turn:
            self.selected = piece
            self.valid_moves = self.board.get_valid_moves(piece)
            return True

        return False

    def _move(self, row, col):
        piece = self.board.get_piece(row, col)
        if self.selected and piece == 0 and (row, col) in self.valid_moves:
            self.board.move(self.selected, row, col)
            skipped = self.valid_moves[(row, col)]
            if skipped:
                self.board.remove(skipped)
            self.change_turn()
        else:
            return False

        return True

    def draw_valid_moves(self, moves):
        for move in moves:
            row, col = move
            pygame.draw.circle(self.win, BLUE,
                               (col * SQUARE_SIZE + SQUARE_SIZE // 2,
                                row * SQUARE_SIZE + SQUARE_SIZE // 2), 15)

    def change_turn(self):
        self.valid_moves = {}
        if self.turn == RED:
            self.turn = WHITE
        else:
            self.turn = RED
class Game:
    def __init__(self, win):
        self._init()
        self.win = win

    # Update pygame display using this method
    def update(self):
        self.board.draw(self.win)
        self.draw_valid_moves(self.valid_moves)
        pygame.display.update()

    # Initilizing => private method init
    def _init(self):
        self.selected = None
        self.board = Board()
        self.turn = RED

        # dictionary of valid possible moves that can be taken from a position
        self.valid_moves = {}

    # Reset the game
    def reset(self):
        self._init()

    # When select something => call the select method, tell the row and col selected and do something based on that
    def select(self, row, col):

        # Move the already selected piece to the desired row,col
        if self.selected:
            result = self._move(row, col)

            # If incorrect/invalid move => reset the selection and reselect row and col
            if not result:
                self.selected = None
                self.select(row, col)

        piece = self.board.get_piece(row, col)

        # not selecting an empty piece and the color of the piece is same as the turn
        if piece != 0 and piece.color == self.turn:
            self.selected = piece
            self.valid_moves = self.board.get_valid_moves(piece)
            return True

        return False

    # Move currently selected piece to the row and col
    def _move(self, row, col):
        piece = self.board.get_piece(row, col)

        # If we have selected something and the piece that we selected is empty and the row, col is a valid move then we can move it
        if self.selected and piece == 0 and (row, col) in self.valid_moves:
            self.board.move(self.selected, row, col)
            skipped = self.valid_moves[(row, col)]
            if skipped:
                self.board.remove(skipped)
            self.change_turn()
        else:
            return False

        return True

    # Change the turn after the move
    def change_turn(self):
        self.valid_moves = []
        if self.turn == RED:
            self.turn = WHITE
        else:
            self.turn = RED

    def draw_valid_moves(self, moves):
        for move in moves:
            row, col = move
            pygame.draw.circle(self.win, BLUE,
                               (col * SQUARE_SIZE + SQUARE_SIZE // 2,
                                row * SQUARE_SIZE + SQUARE_SIZE // 2), 15)

    def winner(self):
        return self.board.winner()
Exemplo n.º 4
0
class Game:
    def __init__(self, win):
        self._init()
        self.win = win
        self.removed_counter = 0
        self.valid_moves = {}

    def winner(self):
        return self.board.winner()

    def _is_stuck(self, matchWinner):
        return matchWinner

    def update_board(self, row, col):
        self.board.draw1(self.win, row, col)
        self.draw_valid_moves(self.valid_moves)
        pygame.display.update()

    def update3(self, row3, col3):
        self.board.draw3(self.win, row3, col3)

        self.board.draw_selected(self.win, row3, col3)
        pygame.display.update()

    def _init(self):
        self.selected = None
        self.board = Board()
        self.turn = RED
        self.valid_moves = {}  #a dictionary of current vallid moves

    def reset(self):
        self._init()

    def select(self, row, col):
        """
        get here when the player press on piece
        if selected is vallid move piece if not (=not result) try again
        """
        if self.selected:
            rectOne = (100, 100, 100, 100)
            pygame.draw.circle(self.win, BLUE,
                               (col * SQUARE_SIZE + SQUARE_SIZE // 2,
                                row * SQUARE_SIZE + SQUARE_SIZE // 2), 8)
            pygame.draw.circle(self.win, GRAY,
                               (col * SQUARE_SIZE, row * SQUARE_SIZE), 14)
            result = self.move(row, col)  #try to move
            if not result:
                self.selected = None  #delete invalid selected
                self.select(row, col)  #reselect

        piece = self.board.get_piece(row, col)

        self.board.draw_selected(self.win, row, col)
        if piece != 0 and piece.color == self.turn:
            self.selected = piece
            self.valid_moves = self.board.get_valid_moves(piece)
            if not self.valid_moves:
                self._is_stuck("WHITE")
            return True

        return False

    def move(self, row, col):
        piece = self.board.get_piece(row, col)

        pygame.draw.circle(self.win, YELLOW,
                           (col * SQUARE_SIZE + SQUARE_SIZE // 2,
                            row * SQUARE_SIZE + SQUARE_SIZE // 2), 8)
        #if the player selected an empty square and it's valid move
        if self.selected and piece == 0 and (row, col) in self.valid_moves:
            pygame.draw.circle(self.win, BLUE,
                               (col * SQUARE_SIZE + SQUARE_SIZE // 2,
                                row * SQUARE_SIZE + SQUARE_SIZE // 2), 8)
            self.board.move(self.selected, row, col)
            skipped = self.valid_moves[(row, col)]
            if skipped:
                self.removed_counter = 0
                self.board.remove(skipped)
            else:
                self.removed_counter = self.removed_counter + 1
            self.change_turn()
        else:
            return False
        return True

    def draw_valid_moves(self, moves):
        for move in moves:
            row, col = move
            pygame.draw.circle(self.win, BLUE,
                               (col * SQUARE_SIZE + SQUARE_SIZE // 2,
                                row * SQUARE_SIZE + SQUARE_SIZE // 2), 8)

    def change_turn(self):
        self.valid_moves = {}
        if self.turn == RED:
            self.turn = WHITE
        else:
            self.turn = RED

    def get_board(self):
        return self.board

    def ai_move(
        self, board
    ):  #Since Game is a Controller class, it connects between the Board class (The View)
        # and  the Model- the Minimax Algorithm Class.
        self.board = board
        self.change_turn()
Exemplo n.º 5
0
class Game:
    def __init__(self, win):
        self._init()
        self.win = win

    def update(self):
        self.board.draw(self.win, self.turn)
        self.draw_valid_moves(self.valid_moves)
        pygame.display.update()

    def _init(self):
        self.selected = None
        self.board = Board()
        self.turn = WHITE
        self.valid_moves = {}

    def reset(self):
        self._init()

    def select(self, pos):
        x, y = pos
        row = y // SQUARE_SIZE
        col = x // SQUARE_SIZE
        if self.selected:
            result = self._move(row, col)
            if not result:
                self.selected = None
                self.select(pos)

        piece = self.board.get_piece(row, col)
        if piece != 0 and piece.color == self.turn:
            self.selected = piece
            self.valid_moves = self.board.get_valid_moves(piece)
            return True

        return False

    def _move(self, row, col):
        piece = self.board.get_piece(row, col)
        if self.selected and piece == 0 and (row, col) in self.valid_moves:
            self.board.move(self.selected, row, col)
            skipped = self.valid_moves[(row, col)]
            if skipped:
                self.board.remove(skipped)
            self.change_turn()
        else:
            return False

        return True

    def draw_valid_moves(self, moves):
        for move in moves:
            row, col = move
            pygame.draw.circle(self.win, BLUE,
                               (col * SQUARE_SIZE + SQUARE_SIZE // 2,
                                row * SQUARE_SIZE + SQUARE_SIZE // 2), 15)

    def change_turn(self):
        self.valid_moves = []
        if self.turn == WHITE:
            self.turn = BLACK
        else:
            self.turn = WHITE

    def winner(self):
        return self.board.winner()

    def draw(self):
        for row in self.board.board:
            for piece in row:
                if piece != 0 and piece.color == self.turn and len(
                        self.board.get_valid_moves(piece)) > 0:
                    return self.draw
        self.draw = True
        return self.draw

    def get_board(self):
        return self.board

    def highlight_move(self, new_board):
        old_board = self.board
        for row in range(ROWS):
            for col in range(COLS):
                if old_board.board[row][
                        col] == 0 and new_board.board[row][col] != 0:
                    pygame.draw.circle(self.win,
                                       BLUE,
                                       (col * SQUARE_SIZE + SQUARE_SIZE // 2,
                                        row * SQUARE_SIZE + SQUARE_SIZE // 2),
                                       40,
                                       width=5)
                if new_board.board[row][
                        col] == 0 and old_board.board[row][col] != 0:
                    pygame.draw.circle(self.win,
                                       BLUE,
                                       (col * SQUARE_SIZE + SQUARE_SIZE // 2,
                                        row * SQUARE_SIZE + SQUARE_SIZE // 2),
                                       40,
                                       width=5)
        pygame.display.update()

    def ai_move(self, new_board):
        self.highlight_move(new_board)
        pygame.time.delay(1000)
        self.board = new_board
        self.change_turn()
Exemplo n.º 6
0
class Game:
    def __init__(self, window):
        self._init()
        self.window = window  # the Game window on your device

    def update(self):
        self.board.draw(self.window)
        self.draw_valid_moves(self.valid_moves)
        pygame.display.update()

    def _init(self):
        self.selected = None  # which piece is selected
        self.board = Board(
        )  # the game will happen on the board so we initialize it inside the Game class
        self.turn = YELLOW  # which player's turn
        self.valid_moves = {
        }  # what the current valid moves are for whatever player is playing

    # to reinitialize the game
    def reset(self):
        self._init()

    def winner(self):
        return self.board.winner()

    def select(self, row, col):  # this is a recursive method
        if self.selected:  # in case user pressed on some piece
            result = self._move(row, col)
            # move the selected piece from row and col of the select method to row and col of the move method
            if not result:  # if the result is not valid (an empty square selected for example)
                self.selected = None  # nothing is selected
                self.select(
                    row, col
                )  # user has to select a new piece (so this is to call the method again)

        piece = self.board.get_piece(
            row, col)  # define the piece correspond to selected row and col
        if piece != 0 and piece.color == self.turn:
            # if the selected piece exists and corresponds to the player's color:
            self.selected = piece  # the piece is selected
            self.valid_moves = self.board.get_valid_moves(piece)
            return True  # the selection is valid

        return False  # the selected piece is not valid, try again

    def _move(self, row, col):
        piece = self.board.get_piece(row, col)
        if self.selected and piece == 0 and (row, col) in self.valid_moves:
            #(piece == 0 to make sure that where we are going to move the piece is an empty square with no piece on it already!)
            self.board.move(self.selected, row,
                            col)  # move the valid selected piece
            skipped = self.valid_moves[(row, col)]
            if skipped:
                self.board.remove(
                    skipped)  # remove the eaten pieces from the board
            self.change_turn()  # change turn so the second player can play
        else:
            return False

        return True

    def draw_valid_moves(self, moves):
        for move in moves:
            row, col = move
            # since moves is a dictionary of tuples (row, col)
            pygame.draw.circle(self.window,
                               BLUE, (col * SQUARE_SIZE + SQUARE_SIZE // 2,
                                      row * SQUARE_SIZE + SQUARE_SIZE // 2),
                               radius=15)

    def change_turn(self):
        self.valid_moves = {}
        if self.turn == WHITE:
            self.turn = YELLOW
        else:
            self.turn = WHITE
Exemplo n.º 7
0
class Game:
    def __init__(self, win):
        self._init()
        self.win = win

    def update(self):
        self.board.draw(self.win)
        self.draw_valid_moves(self.valid_moves)
        # pygame.draw.circle(win, GREY, (self.x, self.y), radius+self.OUTLINE)
        pygame.display.update()

    def _init(self):
        self.selected = None
        self.board = Board()
        self.turn = RED
        self.valid_moves = {}

    def winner(self):
        return self.board.winner()

    def reset(self):
        self._init()
        self.board.draw(self.win)
        pygame.display.update()

    def select(self, row, col, win):
        if self.selected:
            result = self._move(row, col)
            if not result:
                self.selected = None
                self.select(row, col, win)

        piece = self.board.get_piece(row, col)
        if piece != 0 and piece.color == self.turn:
            # print(piece.x, piece.y)
            # pygame.draw.circle(win, GREEN, (piece.x, piece.y), 15)
            self.selected = piece

            self.valid_moves = self.board.get_valid_moves(piece)
            return True

        return False

    def _move(self, row, col):
        piece = self.board.get_piece(row, col)
        if self.selected and piece == 0 and (row, col) in self.valid_moves:
            self.board.move(self.selected, row, col)
            skipped = self.valid_moves[(row, col)]
            if skipped:
                self.board.remove(skipped)
            self.change_turn()
        else:
            return False

        return True

    def draw_valid_moves(self, moves):
        # print(self.selected)
        for move in moves:
            row, col = move
            pygame.draw.circle(self.win, BLUE, (col * SQUARE_SIZE +
                                                SQUARE_SIZE // 2, row * SQUARE_SIZE + SQUARE_SIZE // 2), 15)

    def change_turn(self):
        self.valid_moves = {}
        if self.turn == RED:
            self.turn = BLACK
        else:
            self.turn = RED

    def get_board(self):
        return self.board

    def ai_move(self, board):
        self.board = board
        self.change_turn()
Exemplo n.º 8
0
class Game:
    def __init__(self, win):
        self._init()
        self.win = win

    def update(self):
        self.board.draw(self.win)
        self.draw_valid_moves(self.valid_moves)
        pygame.display.update()

    def _init(self):
        self.selected = None
        self.board = Board()
        self.turn = RED
        self.valid_moves = {}

    def winner(self):
        return self.board.winner()

    def reset(self):
        self._init()

    def select(self, row, col):
        if self.selected:
            result = self._move(row, col)
            if not result:
                self.selected = None
                self.select(row, col)

        piece = self.board.get_piece(row, col)
        if piece != 0 and piece.color == self.turn:
            self.selected = piece
            self.valid_moves = self.board.get_valid_moves(piece)
            return True

        return False

    def _move(self, row, col):
        piece = self.board.get_piece(row, col)
        if self.selected and piece == 0 and (row, col) in self.valid_moves:
            self.board.move(self.selected, row, col)
            skipped = self.valid_moves[(row, col)]
            if skipped:
                self.board.remove(skipped)
            self.change_turn()
        else:
            return False

        return True

    def get_board(self):
        return self.board

    def ai_move(self, board):
        #will be passed the changed board the entire board will be returned and the board the player sees will
        #be updated to include the new board
        self.board = board
        self.change_turn()

    def draw_valid_moves(self, moves):
        for move in moves:
            row, col = move
            pygame.draw.circle(self.win, BLUE,
                               (col * SQUARE_SIZE + SQUARE_SIZE // 2,
                                row * SQUARE_SIZE + SQUARE_SIZE // 2), 15)

    def change_turn(self):
        self.valid_moves = {}
        if self.turn == RED:
            self.turn = WHITE
        else:
            self.turn = RED
Exemplo n.º 9
0
class Game:
    def __init__(self, win):
        self._init()
        self.win = win

    def update(self):
        self.board.draw(self.win)
        self.draw_valid_moves(self.valid_moves)
        pygame.display.update()

    def _init(self):
        self.selected = None
        self.board = Board()
        self.turn = RED
        self.valid_moves = {}

    def reset(self):
        self._init()

    def select(self, row, col):
        if self.selected:
            result = self._move(row, col)
            if not result:
                self.selected = None
                self.select(row, col)

        piece = self.board.get_piece(row, col)
        if piece != 0 and piece.color == self.turn:
            self.selected = piece
            self.valid_moves = self.board.get_valid_moves(piece)
            print(self.valid_moves)
            return True
        return False

    def _move(self, row, col):
        piece = self.board.get_piece(row, col)
        if self.selected and piece == 0 and (row, col) in self.valid_moves:
            self.board.move(self.selected, row, col)
            self.change_turn()
        else:
            return False
        return True

    def draw_valid_moves(self, moves):
        for move in moves:
            row, col = move
            pygame.draw.circle(self.win, BLACK,
                               (col * SQUARE_SIZE + SQUARE_SIZE // 2, row * SQUARE_SIZE + SQUARE_SIZE // 2), 15)

    def change_turn(self):
        self.valid_moves = {}
        if self.turn == BLUE:
            self.turn = RED
        else:
            self.turn = BLUE

    def validate_winner(self, row, col, color):
        piece = self.board.get_piece(row, col)
        if piece != 0:
            if piece.color == color:
                return True
        return False

    def winner(self):
        r0_c0 = self.validate_winner(0, 0, RED)
        r1_c0 = self.validate_winner(1, 0, RED)
        r0_c1 = self.validate_winner(0, 1, RED)
        r1_c1 = self.validate_winner(1, 1, RED)
        r0_c2 = self.validate_winner(0, 2, RED)
        r2_c0 = self.validate_winner(2, 0, RED)
        r0_c3 = self.validate_winner(0, 3, RED)
        r3_c0 = self.validate_winner(3, 0, RED)
        r2_c1 = self.validate_winner(2, 1, RED)
        r1_c2 = self.validate_winner(1, 2, RED)
        if r0_c0 and r1_c0 and r0_c1 and r1_c1 and r0_c2 and r2_c0 and r0_c3 and r3_c0 and r2_c1 and r1_c2:
            print('#########################################')
            print('######### Gano el Jugador ROJO ##########')
            print('#########################################')
            return RED

        r8_c8 = self.validate_winner(8, 8, BLUE)
        r7_c8 = self.validate_winner(7, 8, BLUE)
        r8_c7 = self.validate_winner(8, 7, BLUE)
        r7_c7 = self.validate_winner(7, 7, BLUE)
        r8_c6 = self.validate_winner(8, 6, BLUE)
        r6_c8 = self.validate_winner(6, 8, BLUE)
        r8_c5 = self.validate_winner(8, 5, BLUE)
        r5_c8 = self.validate_winner(5, 8, BLUE)
        r6_c7 = self.validate_winner(6, 7, BLUE)
        r7_c6 = self.validate_winner(7, 6, BLUE)
        if r8_c8 and r7_c8 and r8_c7 and r7_c7 and r8_c6 and r6_c8 and r8_c5 and r5_c8 and r6_c7 and r7_c6:
            print('#########################################')
            print('######### Gano el Jugador AZUL ##########')
            print('#########################################')
            return BLUE

        return None

    def get_board(self):
        return self.board

    def ai_move(self, board):
        self.board = board
        self.change_turn()
Exemplo n.º 10
0
class Game:
    def __init__(self, win):
        self._init()
        self.win = win
    
    def update(self):
        self.board.draw(self.win)
        self.draw_valid_moves(self.valid_moves)
        pygame.display.update()

    def _init(self):
        self.selected = None
        self.board = Board()
        self.turn = WHITE
        self.valid_moves = {}

    def winner(self):
        return self.board.winner()

    def reset(self):
        self._init()

    def select(self, row, col):
        if self.selected:
            result = self._move(row, col)
            if not result:
                self.selected = None
                self.select(row, col)
        
        piece = self.board.get_checker(row, col)
        if piece != 0 and piece.color == self.turn:
            self.selected = piece
            self.valid_moves = self.board.get_valid_moves(piece)

            return True
            
        return False

    def _move(self, row, col):
        piece = self.board.get_checker(row, col)
        if self.selected and piece == 0 and (row, col) in self.valid_moves:
            self.board.move(self.selected, row, col)
            skipped = self.valid_moves[(row, col)]
            if skipped:
                self.board.remove(skipped)
            self.change_turn()
        else:
            return False

        return True

    def draw_valid_moves(self, moves):
        for move in moves:
            row, col = move
            pygame.draw.rect(self.win, GREEN, (col * SQUARE_SIZE, row * SQUARE_SIZE, SQUARE_SIZE, SQUARE_SIZE))

    def change_turn(self):
        self.valid_moves = {}
        if self.turn == WHITE:
            self.turn = BLACK
        else:
            self.turn = WHITE
Exemplo n.º 11
0
class Game:
    def __init__(self, win):
        self._init()
        self.win = win
    
    def update(self):
        self.board.draw(self.win)
        self.draw_valid_moves(self.valid_moves)  # to increase difficulty let deactivate this function
        pygame.display.update()

    def _init(self):
        self.selected = None
        self.board = Board()
        self.turn = GREEN
        self.valid_moves = {}

    def winner(self):
        return self.board.winner()

    def reset(self):
        self._init()

    def select(self, row, col):
        if self.selected:
            result = self._move(row, col)
            if not result:
                self.selected = None
                self.select(row, col)
        
        piece = self.board.get_piece(row, col)
        if piece != 0 and piece.color == self.turn:
            self.selected = piece
            self.valid_moves = self.board.get_valid_moves(piece)
            return True
            
        return False

    def _move(self, row, col):
        piece = self.board.get_piece(row, col)
        if self.selected and piece == 0 and (row, col) in self.valid_moves:
            self.board.move(self.selected, row, col)
            skipped = self.valid_moves[(row, col)]
            # if skipped:
            #     self.board.remove(skipped)
            self.change_turn()
        else:
            return False

        return True
    
    # to increase difficulty let deactivate this function():
    def draw_valid_moves(self, moves):
        for move in moves:
            row, col = move
            pygame.draw.circle(self.win, BLUE, (col * SQUARE_SIZE + SQUARE_SIZE//2, row * SQUARE_SIZE + SQUARE_SIZE//2), 15)

    def change_turn(self):
        self.valid_moves = {}
        if self.turn == GREEN:
            self.turn = WHITE
        else:
            self.turn = GREEN
Exemplo n.º 12
0
class Game:
    def __init__(self, win):
        self._init()
        self.win = win

    def update(self):
        self.board.draw(self.win)
        self.draw_valid_moves(self.valid_moves)
        pygame.display.update()

    def _init(self):
        self.selected = None
        self.board = Board()
        self.turn = RED
        self.valid_moves = {}

    def winner(self):

        if self.board.winner() == WHITE:
            winner = 'White'
        elif self.board.winner() == RED:
            winner = 'Red'
        else:
            winner = None
        return winner

    def reset(self):
        self._init()

    def select(self, row, col):
        if self.selected:
            result = self._move(row, col)
            if not result:
                self.selected = None
                self.select(row, col)

        piece = self.board.get_piece(row, col)
        if piece != 0 and piece.color == self.turn:
            self.selected = piece
            self.valid_moves = self.board.get_valid_moves(piece)
            return True

        return False

    def _move(self, row, col):
        piece = self.board.get_piece(row, col)
        if self.selected and piece == 0 and (
                row, col
        ) in self.valid_moves:  #s-a selectat un patrat care nu e gol
            # si linia si coloana la care se va feca mutarea e valida
            self.board.move(self.selected, row, col)
            skipped = self.valid_moves[(row, col)]  #piesa peste care s-a sarit
            if skipped:
                self.board.remove(
                    skipped)  #se elimina piesa peste care s-a sarit
            self.change_turn()
        else:
            return False

        return True

    def draw_valid_moves(self, moves):
        '''
        Marcheaza toate mutarile pe care piesa selectata le poate face
        :param moves: mutarile valide
        :return:
        '''
        for move in moves:
            row, col = move
            pygame.draw.circle(self.win, BLUE,
                               (col * SQUARE_SIZE + SQUARE_SIZE // 2,
                                row * SQUARE_SIZE + SQUARE_SIZE // 2), 15)

    def change_turn(self):
        self.valid_moves = {}
        if self.turn == RED:
            self.turn = WHITE
        else:
            self.turn = RED
Exemplo n.º 13
0
class Game:
    def __init__(self, win):
        self._init()
        self.win = win
    
    #updates the display and draws it
    def update(self):
        self.board.draw(self.win)
        self.draw_valid_moves(self.valid_moves)
        pygame.display.update()


    def _init(self):
        self.selected = None
        self.board = Board()
        self.turn = RED
        self.valid_moves = {}

    #if someone wins the game
    def winner(self):
        return self.board.winner()

    #method to reset the game
    def reset(self):
        self._init()

    #if selection is valid, move the piece
    #otherwise reset the selection and reselect a row and col
    def select(self, row, col):
        if self.selected:
            result = self.move(row, col)
            if not result:
                self.selected = None
                self.select(row, col)
        
        piece = self.board.get_piece(row, col)
        if piece != 0 and piece.colour == self.turn:
            self.selected = piece
            self.valid_moves = self.board.get_valid_moves(piece)
            return True
        
        return False

    def move(self, row, col):
        piece = self.board.get_piece(row, col)
        if self.selected and piece == 0 and (row, col) in self.valid_moves:
            self.board.move(self.selected, row, col)
            skipped = self.valid_moves[(row, col)]
            if skipped:
                self.board.remove(skipped)
            self.change_turn()
        else:
            return False
        
        return True

    def draw_valid_moves(self, moves):
        for move in moves:
            row, col = move
            pygame.draw.circle(self.win, BLUE, (col * SQUARE_SIZE + SQUARE_SIZE//2, row * SQUARE_SIZE + SQUARE_SIZE//2), 15)

    #if red turn, change to black, else change to red
    def change_turn(self):
        self.valid_moves = {}
        if self.turn == RED:
            self.turn = BLACK
        else:
            self.turn = RED
    
    def get_board(self):
        return self.board
    
    def ai_move(self, board):
        self.board = board
        self.change_turn()