Start learning free

LESSON 70 OF 70 · PROJECTS: OBJECT-ORIENTED

Chess Move Validator in Scala

Practice O(1)

This is a design round. You write the classes, and the interviewer checks that they behave.

Here you check moves on a chess board. There are no turns, no check and no pawns. Any piece may move at any time.

A square is written like "e4". It has a column letter from a to h, then a row digit from 1 to 8.

Each kind of piece is its own class, and all of them share an abstract class Piece:

  • A piece keeps its color and its letter. Its label() returns both, like "white Q".
  • canReach(x, y, toX, toY, board) tells whether the piece's own rule allows a move. Each kind writes its own.
  • The piece stands on column x and row y. The square it wants is at column toX and row toY.
  • A column becomes a number from 0 for a to 7 for h. A row becomes 0 for 1 up to 7 for 8.
  • Five classs extend it, one per kind (King, Queen, Rook, Bishop and Knight).

Implement them and a class Board:

  • Board() makes an empty board of 8 by 8 squares.
  • place(kind, color, square) puts a new piece on an empty square. The kind is one letter ("K", "Q", "R", "B" or "N"). The color is "white" or "black".
  • canMove(from, to) returns true if the piece on from may move to to.
  • move(from, to) makes the move if it is allowed. It returns "capture" when it takes a piece and "ok" for any other allowed move. Otherwise it returns "illegal".
  • at(square) returns the label of the piece on that square, or "empty".

A move is allowed when all four of these hold:

  • A piece stands on from.
  • to is a different square.
  • to is empty or holds a piece of the other color.
  • The move fits the rule of the piece's kind.

The rules of the five kinds:

  • A king moves one square in any direction.
  • A rook moves any number of squares along its row or its column.
  • A bishop moves any number of squares along a diagonal.
  • A queen moves like a rook or like a bishop.
  • A knight moves in an L: two squares one way, then one square to the side.

A rook, a bishop or a queen can't pass a piece. Every square between from and to must be empty. A knight jumps over anything in between.

An allowed move puts the piece on to and empties from. A piece that stood on to is captured and leaves the board. A king can be captured like any other piece. An illegal move changes nothing.

Every call except place writes one line of output. Booleans are written as True and False.

 

Example 1:

Input:
Board()
1. place("R", "white", "a1")
2. place("B", "black", "c3")
3. place("N", "white", "a4")
4. canMove("a1", "a3")
5. canMove("a1", "a6")
6. canMove("a1", "c3")
7. canMove("c3", "a1")
8. move("c3", "a1")
9. at("a1")
10. at("c3")
Output:
True
False
False
True
capture
black B
empty
Explanation:
The rook on a1 can go up its column to a3.
It can't reach a6: the knight on a4 is in the way.
Square c3 is on neither its row nor its column.
The bishop on c3 can reach a1 along a diagonal.
So call 8 captures the white rook.
Now the bishop stands on a1, and c3 is empty.

Example 2:

Input:
Board()
1. place("N", "white", "b1")
2. place("K", "white", "d2")
3. place("B", "white", "c2")
4. place("R", "black", "c3")
5. canMove("b1", "d2")
6. move("b1", "c3")
7. canMove("d2", "d4")
8. move("d2", "e3")
9. move("d2", "d3")
10. canMove("e3", "e3")
11. at("e3")
Output:
False
capture
False
ok
illegal
False
white K
Explanation:
d2 is an L away from b1, but a white king stands there.
In call 6, the knight jumps over the bishop on c2.
It takes the black rook on c3.
A king moves one square, so d4 is too far.
Call 8 moves the king to e3.
Now d2 is empty, so call 9 is illegal.
A piece can't move to its own square.

Example 3:

Input:
Board()
1. place("Q", "black", "d8")
2. place("K", "white", "d1")
3. place("R", "black", "g5")
4. canMove("d8", "d1")
5. canMove("d8", "h4")
6. canMove("d8", "e6")
7. move("d8", "g5")
8. move("d8", "a5")
9. move("a5", "d2")
10. at("d8")
11. at("d2")
Output:
True
False
False
illegal
ok
ok
empty
black Q
Explanation:
The queen can go straight down and take the king on d1.
The rook on g5 blocks the diagonal to h4.
e6 is neither straight nor diagonal from d8.
The queen can't take the rook on g5: it is black too.
Calls 8 and 9 move the queen along two diagonals.

 

Constraints:

  • Every square is on the board, from "a1" to "h8".
  • kind is one of the five letters ("K", "Q", "R", "B" and "N").
  • color is "white" or "black".
  • place is only called on an empty square.
  • At most 50,000 calls follow the constructor.

Note:

Each call should run in O(1) time, since a move crosses at most 6 squares. The board asks the piece and never checks which kind it holds. One shared helper, isPathClear, tells whether every square between two squares is empty. The rook, the bishop and the queen use it.

STEP 1 OF 42
    Ctrl + Enter to check

    Chess Moves in Scala

    Keep going with Scala1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free