Illegal rook moves in chess

Language: Java


To create a chess board that checks if rook moves are legal. This does not include making multiple moves or moving the pieces around. This will follow later. A ‘setUp’ class should be created to make board that can be altered in the code, allowing different positions to be tested.


package com.Hywelsprogram;

import java.util.Map;

public class Main {

public static void main(String[] args) {

//Set up the chess board creating an map called board that shows the square position of the pieces
setUp start = new setUp();
//Say whose move it is
char whoseMove = 'w';
while (true) {
//Player makes a move giving variables move.moveArray (e.g. e2e4), move.moveFrom (e.g. e2) and move.moveTo
// (e.g. e4)
moveCreator move = new moveCreator();
//Create boolean to represent whether the move is legal or not
boolean legal = false;
//runs checks if the piece is a rook
if (board.get(move.moveFrom) == "wrook" || board.get(move.moveFrom) == "brook") {
//runs check to see if it is moving in the correct direction
if (!Rook.moveDirectionChecker(move.moveArray)) {
System.out.println("Illegal move.");
if (!Rook.pieceJumpingChecker(move.moveArray, move, board, whoseMove)) {
System.out.println("Illegal move.");
} else {
System.out.println("Not an illegal move");
import javax.swing.*;

public class ChessMove {

public static char[] makeMove() {
//Create loop to repeat process until a legal format is made. **Currently doesn't work for no move being made
while (true){
String move = JOptionPane.showInputDialog("What is your chess move? Please give it in the format b1-c3 (name and" +
" number of square moved from, a hyphen, and the name and number of the square moved to.");
move = move.toLowerCase();
//Check to see if letters are between a and h, that numbers are between 1 and 8, that there is a hyphen in the
//middle and there are five characters e.g. e2-e4
if ((Character.getNumericValue(move.charAt(0))<10) ||
(Character.getNumericValue(move.charAt(3))<10) ||
move.length()<5 || move.length()>5 ||
(Character.getNumericValue(move.charAt(1))<1) ||
(Character.getNumericValue(move.charAt(1))>8) ||
(Character.getNumericValue(move.charAt(4))<1) ||
//repeat process if the move is illegal
System.out.println("Illegal move");
else {
//create an array of the characters, removing hyphen
return moveArray;
import java.util.HashMap;
import java.util.Map;

public class setUp {

//class creates a HashMap that gives the piece on each square

Map<String, String> board = new HashMap<String, String>();

public setUp() {

board.put("a1", "wrook");
board.put("a2", "empty");
board.put("a3", "empty");
board.put("a4", "empty");
board.put("a5", "empty");
board.put("a6", "empty");
board.put("a7", "empty");
board.put("a8", "brook");
board.put("b1", "empty");
board.put("b2", "empty");
board.put("b3", "empty");
board.put("b4", "empty");
board.put("b5", "empty");
board.put("b6", "empty");
board.put("b7", "empty");
board.put("b8", "empty");
board.put("c1", "wbishop");
board.put("c2", "empty");
board.put("c3", "empty");
board.put("c4", "empty");
board.put("c5", "empty");
board.put("c6", "empty");
board.put("c7", "empty");
board.put("c8", "bbishop");
board.put("d1", "empty");
board.put("d2", "empty");
board.put("d3", "empty");
board.put("d4", "empty");
board.put("d5", "empty");
board.put("d6", "empty");
board.put("d7", "empty");
board.put("d8", "empty");
board.put("e1", "empty");
board.put("e2", "empty");
board.put("e3", "empty");
board.put("e4", "empty");
board.put("e5", "empty");
board.put("e6", "empty");
board.put("e7", "empty");
board.put("e8", "empty");
board.put("f1", "wbishop");
board.put("f2", "empty");
board.put("f3", "empty");
board.put("f4", "empty");
board.put("f5", "empty");
board.put("f6", "empty");
board.put("f7", "empty");
board.put("f8", "bbishop");
board.put("g1", "empty");
board.put("g2", "empty");
board.put("g3", "empty");
board.put("g4", "empty");
board.put("g5", "empty");
board.put("g6", "empty");
board.put("g7", "empty");
board.put("g8", "empty");
board.put("h1", "wrook");
board.put("h2", "empty");
board.put("h3", "empty");
board.put("h4", "empty");
board.put("h5", "empty");
board.put("h6", "empty");
board.put("h7", "empty");
board.put("h8", "brook");

public Map<String,String>getBoard(){
return board;

public class moveCreator {

char[] moveArray = ChessMove.makeMove();
String moveTo = moveCreator.setMoveTo(moveArray);
String moveFrom = moveCreator.setMoveFrom(moveArray);

public moveCreator() {

public static String setMoveFrom(char[]moveArray) {
StringBuilder tempFrom = new StringBuilder();
String moveFrom = tempFrom.toString();

return moveFrom;

public static String setMoveTo(char[]moveArray) {
StringBuilder tempTo = new StringBuilder();
String moveTo = tempTo.toString();

return moveTo;

import java.util.Map;

public class Rook {

public Rook() {

public static boolean moveDirectionChecker(char[] moveArray) {
//variable to see if the piece is moving in the correct direct
boolean correct = true;
//checks to see if the letter or the number has stayed the same
if (moveArray[0] != moveArray[2] && moveArray[1] != moveArray[3] || moveArray[0] == moveArray[2] && moveArray[1] ==
moveArray[3]) {
correct = false;
return correct;

public static boolean pieceJumpingChecker(char[] moveArray, moveCreator move, Map<String, String> board,
char whoseMove) {
//variable to state if the move is legal
boolean correct = true;
//create numeric values for the characters to cut down code
int squareOne = Character.getNumericValue(moveArray[0]) + 87;
int squareTwo = Character.getNumericValue(moveArray[1]);
int squareThree = Character.getNumericValue(moveArray[2]) + 87;
int squareFour = Character.getNumericValue(moveArray[3]);
//check to see if the rook is moving left
if (squareOne > squareThree) {
//loop through each square
for (int i = squareOne - 1; i >= squareThree; i--) {
correct = examine(i, squareTwo, squareOne, board, whoseMove, move, "horizontal");
if (correct == false){
return false;
//check to see if the rook is moving right
if (squareThree > squareOne) {
//loop through each square
for (int i = squareOne + 1; i <= squareThree; i++) {
correct = examine(i, squareTwo, squareOne, board, whoseMove, move, "horizontal");
if (correct == false){
return false;
//check to see if the rook is moving up
if (squareFour > squareTwo) {
//loop through each square
for (int i = squareTwo + 1; i <= squareFour; i++) {
correct = examine(i, squareTwo, squareOne, board, whoseMove, move, "vertical");
if (correct == false){
return false;
if (squareTwo > squareFour) {
//loop through each square to see if (i) a piece is in the way and (ii) the piece landing on is not the
//colour of the piece being moved
//create square that i is currently on
for (int i = squareTwo - 1; i >= squareFour; i--) {
correct = examine(i, squareTwo, squareOne, board, whoseMove, move, "vertical");
if (correct == false){
return false;
return true;

public static boolean examine(int i, int squareTwo, int squareOne, Map<String, String> board, char whoseMove, moveCreator move,
String direction) {
//create a string of the square currently being looked at as we work our way through. This will allow
//us to check for pieces blocking the way etc
StringBuilder sb = new StringBuilder();
if ("horizontal".equals(direction)) {
sb.append((char) i);
String currentSquare = sb.toString();
//create variables to make the if statement below easier. These include any piece that is on the square
//and the colour of the piece on the square
char colourOnSquare = (board.get(currentSquare)).charAt(0);
//returns false (illegal move) if the current square the piece is going on is the same colour, has a
//piece there but it is not the square they are trying to land on.
if (colourOnSquare == whoseMove || colourOnSquare!=whoseMove && !move.moveTo.equals(currentSquare) &&
return false;
return true;
From: Own project