https://github.com/maheshkumar-novice/chess
A command line game written in Ruby
https://github.com/maheshkumar-novice/chess
chess colors object-oriented-programming rspec rubocop ruby tests
Last synced: 2 months ago
JSON representation
A command line game written in Ruby
- Host: GitHub
- URL: https://github.com/maheshkumar-novice/chess
- Owner: Maheshkumar-novice
- License: mit
- Created: 2022-01-08T09:52:20.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2022-03-02T15:58:36.000Z (over 3 years ago)
- Last Synced: 2025-02-01T03:45:18.765Z (4 months ago)
- Topics: chess, colors, object-oriented-programming, rspec, rubocop, ruby, tests
- Language: Ruby
- Homepage: https://replit.com/@MaheshkumarP/Chess?lite=1&outputonly=1
- Size: 1000 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# [Chess](https://en.wikipedia.org/wiki/Chess)
This project is part of the [The Odin Project](https://www.theodinproject.com/paths/full-stack-ruby-on-rails/courses/ruby-programming/lessons/ruby-final-project)'s Ruby curriculum.
Table Of Contents
Description
Demo
Built With
Try Yourself
How To Play
Workflow
Features
Implementation Details
Possible Enhancements
What I Learned
Acknowledgements
## Description
### What is Chess?
Chess is a board game played between 2 players. More on Chess details [here](https://en.wikipedia.org/wiki/Chess).### Why did I create this?
It's the capstone project of The Odin Project's Ruby Programming course to practice and utilize what I learned throughout the course.### My experience
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.**## Demo

## Built With| Tool | Use Case |
| ------------ | ----------------------- |
| Ruby | Programming Language |
| RSpec | Unit Tests |
| Git & GitHub | Source Code Management |## Try Yourself
### Online
You can try in mobile also...
Here's the [LINK](https://replit.com/@MaheshkumarP/Chess?lite=1&outputonly=1) to try online.### Local
#### Prerequisites
```sh
ruby 3 - Recommended
ruby 2.7.4 or higher - Will work
```#### Step 1: Clone the Repository
```sh
git clone [email protected]:Maheshkumar-novice/chess.git
```#### Step 2: Move to Chess Directory
```sh
cd chess
```#### Step 3: Run the main ruby file
```sh
ruby main.rb
```#### Bonus: Pass Custom [FEN](https://www.chessprogramming.org/Forsyth-Edwards_Notation)
```sh
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.#### To Run the Test Suite
#### Step 1: Install RSpec
```sh
gem install rspec
```#### Step 2: Run the Test Suite
```sh
rspec
```## How To Play
### General Instructions
* 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 enter `command` mode
* Second prompt will ask you to choose a `destination`, that is the cell you want your piece to go### Command Mode
* Available Commands: `draw`, `resign`, `save`, `fen`, `exit`
* `draw` - It'll propose draw to your opponent after the completion of your current move
* `resign` - It'll announce opponent as a winner
* `save` - Save your current game, named as `your-name-vs-opponent-name-date-time.yml` format
* `fen` - Shows the current FEN notation of the game
* `exit` - Exits from the command mode to the game## Workflow
### General
* 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### Git
* [Long Running Branches](https://git-scm.com/book/en/v2/Git-Branching-Branching-Workflows) workflow
* `main` branch with most stable version
* `dev` branch with new feature updates, partially stable, will be merged into `main` once become stable
* `feature` branches to create new features and will be merged into `dev`## Features
### Core
* Basic movements and captures
* Check
* Checkmate
* Stalemate
* Castling
* En Passant
* Promotion### Additional
* 50 move rule Draw
* Manual Draw
* Resignation
* Command Mode
* Save & Load games
* Copy [FEN](https://www.chessprogramming.org/Forsyth-Edwards_Notation)
* Load a FEN game of your wish by passing it as a command line argument### Others
* Colorful UI
* Mobile support## Possible Enhancements
* Refactor Game class
* Validator for FEN
* 5 fold repetition Draw
* Insufficient material Draw
* Bot player improvement## Implementation Details
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.### Board
* Hash with 64 pieces
* **Key:** Symbol represent the cell coordinate (e.g. :a8)
* **Value:** A cell object represents the board cell### Cell
* Object that holds the piece
* It stores the location of their neighbours
```mermaid
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
```### Check
Whenever a player make a move their king shouldn't be in check.### Knight Move
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 diagonalThen we use other objects to operate and manipulate this board and the pieces in the cells to play the game of chess.
## What I Learned
* Importance of Refactoring
* Importance of Planning
* Importance of Tests
* SOLID priniciples of OOP
* More about Composition
* More about Inheritance
* And more....
## Acknowledgements
* [The Odin Project](https://theodinproject.com)
* [Lichess](https://lichess.org)
* [Chess.com](https://chess.com)
* [Replit](https://replit.com)
* [Wikipedia](https://wikipedia.org)
* [Chess Variants](http://www.chessvariants.org/d.chess/chess.html)
* [Chess Programming](https://www.chessprogramming.org/Forsyth-Edwards_Notation)Found an issue? [Click](https://github.com/Maheshkumar-novice/Chess/issues) here to raise an issue.
[Move To Top](#chess)