Chess Project build with java programming language tools used Atom Hackable Editor, some initial code already provided by college.
This is the Screenshot at the begining and only the white pawn is functional and the rest is to be done from here.
(Please click the image above to see it in correct orientation).
Knights moves:
Knight can only move in a L direction(meaning if movement on X-axis(xMovement) == 1, then the movement on Y-axis(yMovement) == 2 (must) and also the other way around.
Also, We need to check the square that we are moving to and make sure that if there is a piece present that its not our own piece.
Bishop's moves:
The bishop may move any number of squares on the same colured squares in a diagonal direction until it is prevented from continuing by another piece. It may then capture the opposing piece by landing on the square. Bishop can't jump pieces unlike Knight.
Rook's Moves:
Rook can either move horizontally or vertically. It can move any number of squares but cannot pass through a piece.
Queen's moves:
The Queen (most powerful piece) can move in a horizontal, vertical or diagonal direction as long as there are no pieces in the way.
Pseudo Code:
King's Moves:
The King can move any direction 1 square at a time it can be horizontal, vertical or diagonal, the king cannot jump over pieces & cannot kills its own pieces and their must be 1 square distance between the opposing Kings else its an invalid move.
Pseudo Code:
- Variables initialX,initialY Stores initial position of piece on the board
int initialX; // Initial position of the piece on x-axis of the board at the start of the game
int initialY; // Initial position of the piece on y-axis of the board at the start of the game.
- startX, startY stores the starting x,y position of the piece before it landed
int startX;
int startY;
- Booleans - possible: doesn't allow piece movement until other color piece has moved , whitePieceMoveFirst: return true initially and allow the white piece to move first to start the game, once the white piece is moved the black piece can move and so on.
Boolean whitePieceMoveFirst;
Boolean possible;
if(whitePieceMoveFirst){
if(pieceName.contains("White")){
possible = true;
}
}
else{
if(pieceName.contains("Black")){
possible = true;
}
}
- ensureOnlyEnemyPieceCanBeKilled() method
Usage:
validMove = ensureOnlyEnemyPieceCanBeKilled(e.getX(), e.getY(), pieceName);
// Method returns a boolean true or false
// It returns true if the piece to be killed is opponent piece and false if its own piece.
Implematation:
private Boolean ensureOnlyEnemyPieceCanBeKilled(int newX, int newY, String pieceName){
Boolean validMove = false;
System.out.println(pieceName);
if(piecePresent(newX, newY)){
if(pieceName.contains("White")){
if(checkWhiteOponent(newX, newY)){
validMove = true;
return validMove;
}
else{
validMove = false;
return validMove;
}
}
else{
if(checkBlackOponent(newX, newY)){
validMove = true;
return validMove;
}
else{
validMove = false;
return validMove;
}
}
}
else{
validMove = true;
return validMove;
}
}
- Math.abs() method gives the absolute value of the argument (Absolute Value: the magnitude of a real number without regard to its sign) Source: https://www.tutorialspoint.com/java/number_abs.htm
int distance = Math.abs(startX-landingX);
Actual Gameplay Images(Me against the computer)
(The pics are about 2-3 weeks old(from the day i started coding), game played during research phrase, I won the game, I loved playing chess since I was 15 years old and also loving this project so far and very excited to finish it and play on my own programmed chess board).
Copyrights - Navjot Singh Virk (https://navjot.mrvirk.com & https://mrvirk.com)