Giter Club home page Giter Club logo

chess-with-oop's Introduction

Chess-With-C-Plus-Plus-OOP

This is a comprehensive implementation of a chess engine using SDL2 and object-oriented programming (OOP) principles in C++.

The First Glimps::

image

Key Classes and Structure

  • Piece Class Hierarchy: Piece class is the base class for all chess pieces (Pawn, Rook, Knight, Bishop, Queen, King). Each piece class (Pawn, Rook, etc.) inherits from Piece and implements its own getValidMoves() method to calculate valid moves based on chess rules specific to that piece.
  • Game Class: Manages the overall game state, including the board (m_board), window (m_window), renderer (m_renderer), and game loop (run()). Handles initialization (init()), loading of pieces (loadPieces()), event handling (handleEvents()), rendering (render()), and logic for moves and game state (isKingInCheck(), isCheckmate(), isStalemate()).
  • Piece Interaction: Each piece knows its position (m_x, m_y) on the board and whether it's white or black (m_isWhite). Pieces interact with each other through getValidMoves(), which checks potential moves and captures based on board state.
  • SDL2 Integration: Uses SDL2 for window creation, rendering, and handling events (SDL_Window, SDL_Renderer). Loads piece images (SDL_Texture) onto the board and manages rendering using SDL functions (SDL_RenderCopy).
  • Game Logic: Implements chess-specific rules like castling for the King, pawn double-step and en passant for the Pawn, and movement patterns for all other pieces (Rook, Knight, Bishop, Queen).

GameShots::

image

The game goes with a medieval theme and the pieces were designed by smalllikeart (https://www.flaticon.com/authors/smalllikeart) and modified by me::

Screenshot from 2024-05-14 19-49-21

Usage and Flow

  • Initialization (init()): Initializes SDL2, creates window and renderer, loads piece images, and sets up initial game state.
  • Game Loop (run()): Handles game events (mouse clicks for piece selection and move execution), updates game state, and renders the board continuously.
  • Event Handling (handleEvents()): Polls SDL events (like mouse clicks and window closure) and delegates actions accordingly (e.g., selecting a piece, executing a move).
  • Piece Movement and Validation:
    • Each piece calculates its valid moves based on its specific rules (getValidMoves()).
    • Board state (m_board) is updated after valid moves are verified.
  • Endgame Conditions:
    • Checks for check, checkmate, and stalemate conditions using isKingInCheck(), isCheckmate(), and isStalemate() methods.

The documentaions I used:

chess-with-oop's People

Contributors

ju4700 avatar

Stargazers

ju avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.