-
Notifications
You must be signed in to change notification settings - Fork 0
Home
The Game class manages the entire game logic and rendering, including the user interface (UI) for the main menu and victory screen. It also handles gameplay, user input, and interaction between the game board and pieces.
-
Game(): Initializes the game, including setting up the window, loading textures, and initializing the board. -
loadButtonsTextures(): Loads the textures for the UI buttons in the menu (e.g., the "Play" button). -
loadIcon(): Loads and sets the window icon. -
run(): Starts the main game loop, handling events, updating the game, and rendering the UI. -
stopGame(): Ends the game and shows the victory screen. -
restartGame(): Resets the game to its initial state, showing the menu again.
-
loadTexture(const std::string& filePath, sf::Texture& texture): Loads a texture from a file path into ansf::Textureobject. -
configureSprite(sf::Sprite& sprite, sf::Texture& texture, sf::Vector2f position = { 0, 0 }, sf::Vector2f scale = { 1.0f, 1.0f }): Configures a sprite's texture, position, and scale. -
processInput(): Handles user input, such as clicks on the play button or piece movement. -
update(): Updates the game state, checking for player turns, piece movement, and win conditions. -
render(): Draws the game board, pieces, and UI elements to the screen. -
hideMenu(): Hides the menu to start the game.
The implementation of the Game class includes logic for setting up textures, handling user input, updating the game state, rendering the UI, and managing game states.
-
Constructor:
Game()- Initializes the game window (
sf::RenderWindow), board, textures for buttons, and icons. The constructor also sets the initial state (e.g., which player is starting).
- Initializes the game window (
-
Method:
loadButtonsTextures()- Loads the texture for the "Play" button from a file and configures the sprite for the button. This sprite is rendered on the menu screen.
-
Method:
loadIcon()- Loads and sets the game window icon using an image file, which appears in the top-left corner of the application window.
-
Method:
loadTexture()- A helper method that loads a texture from a file and returns true if successful. This method is used in other parts of the class to load textures for the game UI and pieces.
-
Method:
configureSprite()- A helper method that configures the sprite’s texture, position, and scale. It’s used for rendering UI components such as the play button and the game board.
-
Method:
processInput()- Handles user input (mouse clicks or keyboard presses). This method listens for window events and checks if any interactive UI elements (like buttons) were clicked, triggering appropriate actions like starting or restarting the game.
-
Method:
update()- Contains the game logic for checking for win conditions. If the game is over, it triggers the victory screen.
-
Method:
render()- Renders the game's current state to the window. It handles drawing the game board, pieces, and UI components (backgrounds, buttons) based on the current screen (menu, game, or victory screen).
-
Method:
hideMenu()- Hides the menu, which is typically displayed at the start, and initiates the game view.
-
Method:
stopGame()- Closes window and ends game process.
-
Method:
restartGame()- Resets the game state, including the board and player turns.
This class represents individual checkers pieces, which belong to either the white or black player. The pieces are displayed on the board and moved based on player input.
-
Type: Enum representing the piece type (White,Black). -
isKing: A boolean flag indicating whether the piece has been promoted to a king.
-
move(): Handles the movement of a piece, ensuring it follows the game rules (e.g., diagonal moves). -
promoteToKing(): Promotes a piece to a king when it reaches the opponent's side of the board.
The Board class manages the game board, piece movement, and user interactions. It includes functionality for rendering the board, checking move validity, and handling turn-based game logic.
-
Board(Game* game):- Constructor that initializes the game board and associates it with an instance of the
Gameclass.
- Constructor that initializes the game board and associates it with an instance of the
-
void initializeBoard():- Initializes the board with the correct layout of pieces at the start of the game.
-
bool isValidMove(int startX, int startY, int endX, int endY, Piece::Type currentPlayer) const:- Checks whether a move from
(startX, startY)to(endX, endY)is valid based on the rules for the specified player.
- Checks whether a move from
-
bool movePiece(int startX, int startY, int endX, int endY, Piece::Type currentPlayer):- Moves a piece from the start coordinates to the end coordinates, ensuring the move is valid and updating the board accordingly.
-
bool isValidKingMove(int startX, int startY, int endX, int endY, Piece::Type currentPlayer) const:- Checks whether a move for a king (a promoted piece) is valid.
-
bool isDiagonalMove(int startX, int startY, int endX, int endY) const:- Checks if the move is a diagonal move, which is a basic requirement for all valid checkers moves.
-
bool canContinueTurn(int gridX, int gridY):- Checks if the current player can continue their turn after moving a piece, especially in cases where multiple captures are possible.
-
void handleClick(int gridX, int gridY, Piece::Type& currentPlayer):- Handles a click event on the board, selecting and moving a piece or ending the turn, based on the game state.
-
void render(sf::RenderWindow& window):- Renders the board and all pieces to the screen using SFML's rendering capabilities.
-
void switchPlayer():- Switches the turn to the next player, alternating between the two sides (e.g., White and Black).
-
void loadTextures():- Loads the textures for the board squares and pieces, used in the rendering process.
-
bool checkWinCondition(Piece::Type currentPlayer) const:- Checks if the current player has won the game by evaluating the board state (e.g., if the opponent has no more pieces or cannot make a valid move).
-
bool pieceSelected:- Flag that tracks whether a piece is currently selected for movement.
-
int selectedX, selectedY:- Coordinates for the selected piece on the board.
-
bool captureMade:- Flag that indicates whether a capture (jump) has been made during the current turn.
-
Piece board[8][8]:- A 2D array representing the game board, where each element stores a
Pieceobject.
- A 2D array representing the game board, where each element stores a
-
Game* gameInstance:- A pointer to the
Gameclass instance, allowing interaction with the game state and other components (e.g., handling the win condition).
- A pointer to the
- SFML (Simple and Fast Multimedia Library) is used for rendering graphics, handling user input, and managing window events.
-
Textures and Sprites: Game assets such as buttons and backgrounds are handled through SFML’s
sf::Textureandsf::Spriteclasses. -
Game Logic: The game logic for moving pieces, checking win conditions, and switching turns is handled in the
update()andprocessInput()methods of theGameclass.