This project is part of the The Odin Project's Ruby curriculum.
Table Of Contents
Chess is a board game played between 2 players. More on Chess details here.
It's the capstone project of The Odin Project's Ruby Programming course to practice and utilize what I learned throughout the course.
It's definitely a big project. I thought chess is a complicated game because I don't have much experience with chess before starting to work on this project. But after doing some research it clicked, it felt simple. Planning and Refactoring helped me stay in the right direction. I spent more time refactoring my code than writing the code. Honestly I enjoyed it. And here comes the important part, Tests. Without tests I can't imagine how I might approached this project. So overall I gained more experience in Planning, Refactoring, Writing Unit Tests, Serialization, OOP concepts like SOLID, Inheritance, Composition, etc.
Tool | Use Case |
---|---|
Ruby | Programming Language |
RSpec | Unit Tests |
Git & GitHub | Source Code Management |
You can try in mobile also... Here's the LINK to try online.
ruby 3 - Recommended
ruby 2.7.4 or higher - Will work
git clone [email protected]:Maheshkumar-novice/chess.git
cd chess
ruby main.rb
Bonus: Pass Custom FEN
ruby main.rb 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1'
This will load the given FEN to create the game state.
gem install rspec
rspec
- Run the game
- Choose Mode
- Create names if asked
- Board will be shown
- First prompt will ask you to choose a
source
, that is the cell of the piece you want to move - You can enter
cmd
in this step to entercommand
mode - Second prompt will ask you to choose a
destination
, that is the cell you want your piece to go
- Available Commands:
draw
,resign
,save
,fen
,exit
draw
- It'll propose draw to your opponent after the completion of your current moveresign
- It'll announce opponent as a winnersave
- Save your current game, named asyour-name-vs-opponent-name-date-time.yml
formatfen
- Shows the current FEN notation of the gameexit
- Exits from the command mode to the game
- Research & Plan the feature by writing it down in a text editor
- Implement the feature
- Refactor
- Write Tests
- Refactor
- If the game playable, test it by playing
- Long Running Branches workflow
main
branch with most stable versiondev
branch with new feature updates, partially stable, will be merged intomain
once become stablefeature
branches to create new features and will be merged intodev
- Basic movements and captures
- Check
- Checkmate
- Stalemate
- Castling
- En Passant
- Promotion
- 50 move rule Draw
- Manual Draw
- Resignation
- Command Mode
- Save & Load games
- Copy FEN
- Load a FEN game of your wish by passing it as a command line argument
- Colorful UI
- Mobile support
- Refactor Game class
- Validator for FEN
- 5 fold repetition Draw
- Insufficient material Draw
- Bot player improvement
Board is inspired from the Graph data strucutre. Board holds 64 cell objects, where each cell object know who is their neighbour. It is, each cell object stores the location of their row_right
, row_left
, column_above
, column_below
, top_left_diagonal
, top_right_diagonal
, bottom_right_diagonal
, bottom_left_diagonal
neighbours.
- Hash with 64 pieces
- Key: Symbol represent the cell coordinate (e.g. :a8)
- Value: A cell object represents the board cell
- Object that holds the piece
- It stores the location of their neighbours
flowchart LR
cell --- row_right
cell --- row_left
cell --- column_above
cell --- column_below
cell --- top_left_diagonal
cell --- top_right_diagonal
cell --- bottom_right_diagonal
cell --- bottom_left_diagonal
Whenever a player make a move their king shouldn't be in check.
I designed knight's move as, one step forward in the four directions it can step and the diagonal moves related to that forward step. For example, If the knight generate moves for it's left side,
- It can step on the left side
- It can go to either the top left diagonal or the bottom left diagonal
Then we use other objects to operate and manipulate this board and the pieces in the cells to play the game of chess.
- Importance of Refactoring
- Importance of Planning
- Importance of Tests
- SOLID priniciples of OOP
- More about Composition
- More about Inheritance
- And more....
Found an issue? Click here to raise an issue.