Skip to content

D-Arnav/Chess-in-JS

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Chess-in-JS

Introduction

Chess is a two player strategy game played on a 64 x 64 square chessboard, with the goal of capturing the opponent's king. Although this may seem simple, the underlying rules and strategies make chess a very complex game.

Moves

Pawn

  • 1 or 2 squares forward on the first move
  • 1 square forward on every other move
  • Captures diagonally forwards

Rook

  • Moves horizontally or vertical

Bishop

  • Moves along the two diagonals

Knight

  • Moves in a L shape 2 squares in 1 direction and 1 square in a perpendicular direction

Queen

  • Moves horizontally, vertically or diagonally

King

  • Moves one square either horizontally, vertically or diagonally

Plan

Key Point 1

Give an cell id based on the location of each cell. We can then mathematically determine the legality of a move based on the from-cell, to-cell and piece-type.

Key Point 2

Add special moves such as enpassant, castle, long castle, pawn promotion

Key Point 3

For every move, check for legality, see if the move checks the king, or if the game is won.

Developer Notes: Some Problems and Solutions

Problem

Castle cannot be done when a king moves once, this means that for the same board state, a move may or may not be possible. A similar but more challenging problem is also present for enpassant.

Solution

Add variables to store board data - kingMoved and enpassant. kingMoved will decide if castle() and longCastle() is possible and enpassant will decide if enpassant() is possible.

Problem

Rook, Queen, Pawn, and Bishop moves require there to be no pieces in the way

Solution

Iterate through all the cells between fromCell and toCell and if a piece exists, the move is not possible.

Problem

Pins and Discovered checks pose a huge problem in detecting checks.

Solution

create an isCheck() function to detect if the board is in a checked state, and also create a causesCheck() function to see if the particular move causes the board state to be in check. causesCheck() would move the piece, store the state, move back the piece, then return the state.

Setup Instructions

  1. Create any directory on your computer
  2. Download the 3 program files: index.html, Chess.css, Chess.js and place them in the directory
  3. Download all the png images in Chess Pieces, and create a directory called Chess Pieces within the previously created one and store all the images there
  4. Find the index.html file on your computer and right click and open it with a browser such as Chrome

About

I made chess using only HTML, CSS and JS

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published