Skip to content

Play chess in your computer terminal programmed with the ruby language. The chess game has a number of features such as single player against the computer, 2 players at the same terminal, or watch the computer play against itself.

Notifications You must be signed in to change notification settings

RaspberryRat/chess

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

chess


   _____   _    _   ______    _____    _____
  / ____| | |  | | |  ____|  / ____|  / ____|
 | |      | |__| | | |__    | (___   | (___
 | |      |  __  | |  __|    \___ \   \___ \
 | |____  | |  | | | |____   ____) |  ____) |
  \_____| |_|  |_| |______| |_____/  |_____/

Terminal Chess in Ruby

Play chess in your computer terminal programmed with the ruby language. The chess game has a number of features such as single player against the computer, 2 players at the same terminal, or watch the computer play against itself.
· Report Bug · Request Feature

readme-top

Table of Contents
  1. About The Project
  2. Running the Game
  3. Built With
  4. Getting Started
  5. Starting the Game
  6. Game Play
  7. Move Log
  8. Fen Notation Board State
  9. Game Rules
  10. Special Rules
  11. Contact

About The Project

I programmed this to practice and develop my programming while I am learning.

Screenshot of gameboard

(back to top)

Built With

  • Ruby

(back to top)

Running the Game

To run the project, download the code. Make sure you have ruby >= 3.2 installed.

From the command line type:

ruby lib/main.rb

Getting Started

To play go to replit => https://replit.com/@SmellyCosby/chess?v=1

(back to top)

Starting the Game

The game allows you to start a new game.

Load the game from a save file.

Start a game using fen notation.

Screen of start of game options

Option 1 will allow you to start a new game

Game type options



Option 2 will allow you to load a game from a file. To choose this option, you must have saved a game previously.

Option 3 will allow you to enter fen notation.

For details about fen notation check out this website: https://www.chess.com/terms/fen-chess

To begin a game with option 3, the user must enter notation with the following format: 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq -'

The first part of the notation represents the game board, with the placement of pieces on each row being marked with a letter and empty spaces with a number.

The letters represent the pieces: R: rook, N: knight, B: Bishop, Q: queen, K: king, and P: pawn.

Capital letters are white game pieces and lowercase letters are black game pieces

The break between rows represented by a /.

A number equals the amount of squares in that row that are empty. For example P7 means the row has a white pawn in the first (leftest most) square followed by 7 empty squares. A second example: r2KQ2r, starting with the left square is a black rook, two empty squares, a white king, a white queen, two empty squares, and a second black rook.

There will be a space following the 8 rows then a lowercase 'w' or 'b'. This represents the current players turn. 'w' means it is the white player's turn and 'b' means it is the black player's turn.

A game can not be loaded this way without all 8 rows represented and a turn indicator ('w' or 'b'). You can also have castling notation and En Passant notation but neither of it is required to load a game with this method.

Following the turn indicator is another space and will have up to the following letters 'KQkq', but also may contain no letters here. This represents if there is a castling option available. 'K' or 'k' represents a kingside castle. 'Q' or 'q' represents the queenside castle for the white and black player resepctively.

The appropriate letter will be removed from the notation if a rook or the king moves during the game, meaning that castling option is no longer available.

The final part of the notation is followed by another space. This is the En Passant notation and is not required to start a game.

This En Passant Notation can be a '-' or 'a3' through 'h3' or 'a6' through 'h6'.

En Passant notation indicates if a pawn made a double move on the previous players turn. If so, that piece can be taken by a pawn.

Game Play

Game type

After choosing how to start the game you will be given three choices:

Screenshot of gametype

You can play single player, multiplayer or watch the computer duke it out!

If you choose option 1: single player, you will play by yourself against a computer AI opponent on easy. Hard mode coming soon...

If you choose option 2: multiplayer, you will play against another human opponent. Both players must use the same terminal and keyboard to play.

If you choose option 3: simulate game, both players will be computer AIs on easy. Watch the computer take on itself and try to guess who will win!

After choosing option 1 or option 2, the human players choose what colour they will be. White player is always player 1 and goes first. Black player is always player 2 and plays second.

Screenshot of colour choice

Human players can then choose their name which will show up when it is their turn:

Screenshot of name choice

Screenshot of game board showing name

Move Log

Enter 'move log' instead of inputting a piece location to pull up all past moves by both players'

Screenshot of move log

Fen Notation Board State

Enter 'fen' instead of inputting a piece location to display the current board state as fen notation. You can copy this fen notation and enter it to restart you game from the same spot

Screenshot of move log

(back to top)

Game Rules

To win the game, you must put your opponent's king in checkmate. This means the opponent has no legal moves available where their king will not be captured on your next turn. Essentially the goal of the game is to capture the opponent's king.

Piece Movement

To move a piece, enter the coordinates of a piece on the board using the format column row. For example, to choose a pawn at 'a2' simply enter 'a2'.

After entering a piece that has legal moves, the piece selected will be highlighted in blue and all squares that are legal moves will be highlighted in red.

Screenshot of piece selection

Then enter the coordinates of the square you want to move to, legel moves will be listed below the game board. For example in the screenshot above you could choose 'b2', 'c3', 'c5', or 'b6'.

All pieces follow the movement rules for chess.

(back to top)

Pawn movement

Pawns can move forward a single space. White pawns can only move in the driection where the row is a higher value (for example, 'a2' to 'a3'). Black pawns can only move in the direciton where rows are a decreasing number (for example, 'a7' to 'a6').

When pawns are at their starting position, row 2 for white pawns and row 7 for black pawns, they can move forward 2 squares for the pawns first move. Example, pawn 'c2' can move to square 'c3' or 'c4'

Screenshot of pawn initial move

Pawns can only capture an opponents piece that is at a diagonal forward direction to the pawn's square. For example pawn at a 'c4', there an opponent piece at locations 'b5', 'c5', and 'd5'. The white pawn can capture the pieces at location 'b5' or 'd5'. The pawn can not capture the piece at location 'c5'.

Screenshot of pawn capture

(back to top)

Rook movement

A rook can move horizontally or vertically up to as many squares that are empty. Rooks cannot pass through another piece during its movement. It cannot end up on the same square as a piece of the same colour. If a rook ends its movement on a piece of the enemy colour, the player captures that piece and removes it from the gameboard. Rooks cannot move diagonally.

(back to top)

Knight movemenet

A knight moves in an 'L' shape. This means it can move 1 square in any direction, say verticlaly, than 2 squares horizontally. Or 1 square horizontal direction, than 2 squares vertically. For example, a knight at 'a4' could move to 'b2', 'c3', 'c5', or 'b6'.

Screenshot of piece selection

(back to top)

Bishop movement

A bishop can move diagonally up to as many squares that are empty, it cannot end pass through a piece during its movement and must end its movement on an empty square or a square with an opponent's piece. If the bishop ends its movement on a square where there is an opponent's piece, it captures that piece and removes it from the gameboard.

(back to top)

Queen movement

Queens have all movement options, the queen can move, horizontally, vertically, or diagonally up to as many squares that are empty. The queen cannot end pass through a piece during its movement and must end its movement on an empty square or a square with an opponent's piece. If the queen ends its movement on a square where there is an opponent's piece, it captures that piece and removes it from the gameboard.

(back to top)

King movement

Kings have all movement options, however the king is limited to moving only a single square. The king can move, horizontally, vertically, or diagonally, but cannot ends its movement on a piece of the same colour. If the king ends its movement on a square where there is an opponent's piece, it captures that piece and removes it from the gameboard.

Screenshot of king move

(back to top)

Check

A king is in check when your opponent can capture it on their next turn. When a king is in check, the only moves you can make result with your king no longer in check. This could be by moving either the king that in in check, or another piece that blocks, or captures the piece that is causing the king to be in check. The game will not let you make any other move. Additionally, you cannot make a move that results with your king being in check at the end of the move.

Screenshot of king check

(back to top)

Checkmate

A king is in checkmate when there are no legal moves available that don't result in the current player's king being out of check. In the screenshot below, the black player's king is in checkmate. The black player has no legal moves where the king is not in check. When a player is in checkmate the other player is the winner and the game ends

Screenshot of checkmate

(back to top)

Special Rules

There are three additional special rules in chess that are important for a player to know. Promotion which effects a pawn that makes it to the other end of the board, castling, which is a special king movememnt, and En Passant, which is a special pawn capture move.

Promotion

If a pawn moves it to the last square of the opposite side of the board, meaning row 8 for the white player, or row 1 for the black player, the pawn can be promoted to a queen, bishop, knight, or rook. Contrary to popular belief, a player can choose any of these options even if the player hasn't captured that piece from their opponent. This means you could have 2 or more queens! After choosing a piece, the pawn transforms into that piece for the rest of the game.

Screenshot of promotion option

Screenshot of after promotion

(back to top)

Castling

Castling is a special move available to the king which results in the king moving two squares, while simultaneously moving a rook to the far side of the king, protecting it.

A king can castle on either the right or left side of the board which is called a 'kingside castle' or 'queenside castle' respectively. A castle results in moving the king two squares either left or right. Meaning it will end up in column 'c' or 'g'. The rook will move past the king and end up in column 'd' or 'g' respectively.

You can only castle if there is a clear path to castle, and your king is not put in check on any of the squares on the way to the castle location. Addiitonally, castling, must be the first move a king makes in the game, and the first move the respective rook makes in the game. If he king moves at all, you can no longer castle. If a rook moves, you can no longer castle on that side of the board.

To castle, choose your king piece, for example enter 'e1', if you can castle you will have a move 'kingside castle' and/or 'queenside castle'. Enter in either 'k' or 'kingside castle' or 'q' or 'queenside castle' to choose that move. Both pieces will then be moved to the correct squares.

Screenshot of castle option

Screenshot of castle after

(back to top)

En Passant

En Passant, mean means 'in passing' in french is a special move available only to pawns. This speical move allows you to capture a pawn with your own pawn when the opponent move's their pawn 2 squares. If an opponent's pawn moves 2 squares, and you would have been able to capture it if the opponent only moved a single square, you will then have the option to capture that pawn as if it only moved the 1 square. See the screenshots below to see how it works in the game.

The white player's pawn at 'a5' is within two squares of the black player.

Screenshot of enpassant

At this point the black player moves their pawn from 'b7' to 'b5' thinking they can avoid getting it captured.

Screenshot of enpassant

However, the rule of En Passant, which means in passing in french, allows the white player to still capture the black player's pawn.

Screenshot of enpassant

The white player has move to 'b6' and capture the pawn at 'b5'

Screenshot of enpassant

(back to top)

Contact

Project Link: https://github.com/RaspberryRat/chess

(back to top)

About

Play chess in your computer terminal programmed with the ruby language. The chess game has a number of features such as single player against the computer, 2 players at the same terminal, or watch the computer play against itself.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages