Skip to content
Branch: master
Go to file

Latest commit


Failed to load latest commit information.
Latest commit message
Commit time

CLI Chess

CLI Chess is a command-line chess game written in Ruby.

alt text


  1. Ensure Ruby is installed
  2. Clone this repository by running the command $ git clone
  3. Navigate into the game folder $ cd CLI-Chess
  4. Install dependencies $ bundle install
  5. To start the game, run $ ruby game.rb
  6. When prompted type 'yes' or 'no' to play against the computer.


CLI Chess is split up into four basic pieces:

  1. A Game class which handles the overlying gameplay
  2. A Board class which houses the board state and piece positions
  3. A Display class that handles the rendering of the board to the command-line and handles the input of moves
  4. Different piece classes that house information about each piece and how they move


Each piece (Pawn, Rook, Queen, etc...) is split up into its own class. To keep code DRY, shared logic and methods are housed in a parent Piece class which every other piece inherits from. To help with the internal logic for each piece, a reference to the Board is also passed in and stored as an instance variable.


In chess, pieces can move by either sliding or stepping (with the exception of the Pawn). Sliding pieces are able to move along a straight line in a given direction. Stepping pieces can only move to a fixed number of spaces around them depending on the piece.

To replicate this behavior and to keep code reusable, I created a SteppingPiece Module and a SlidingPiece Module.

Stepping Pieces

Each stepping piece holds a reference to the spaces they can move, relative to [0,0]. These move_diffs are then given actual positions using the reference to the board, being sure to only keep moves that are on the board.

def moves
  possible_moves = []
  move_diffs.each do |diff|
    move_position = Piece.get_new_position(@position, diff)
    possible_moves << move_position
  end do |move|
    Board.in_bounds?(move) && @board[move].color != self.color


Sliding Pieces

Each sliding piece holds a reference to which directions they are able to slide to. Using these move_dirs, possible moves are found by moving along that direction until they hit the end of the board or another piece.

def moves
  possible_moves = []
  move_dirs.each do |diff|
    new_position = Piece.get_new_position(@position, diff)
    until !Board.in_bounds?(new_position) ||  @board[new_position].color == self.color
      possible_moves << new_position
      break unless @board[new_position].is_a?(NullPiece)
      new_position = Piece.get_new_position(new_position, diff)

Computer Player (AI)

Players are able to play against a computer player. The computer player decides which moves to make by weighting all possible moves in terms of piece value. Currently the AI will either take a piece if it is able to, or randomly select a move from its possible moves. Future improvements will implement more complex logic by using branching trees and algorithms.


Command line implementation of the class game Chess using Ruby.



No releases published


You can’t perform that action at this time.