School Java Project Chess (2)

Deploying 32 initial pieces

拇指 muzhi.com
Published in
8 min readJan 9, 2020

--

Last time we printed out the empty chess board. Today we’ll populate the board with initial 32 pieces. The board will look like the following when we finish.

  a b c d e f g h
8 r n b q k b n r 8
7 p p p p p p p p 7
6 . . . . . . . . 6
5 . . . . . . . . 5
4 . . . . . . . . 4
3 . . . . . . . . 3
2 P P P P P P P P 2
1 R N B Q K B N R 1
a b c d e f g h

Open Chess.java and create an enum Type to represent chess piece type at the end of the file.

class Chess {
public static void main(String[] args) {
Board brd = new Board();
System.out.println(brd);
}
}
class Board {
public String toString() {
String brdStr = "";
brdStr += " 0 1 2 3 4 5 6 7\n";
for (int row = 0; row < 8; row++) {
brdStr += row + "";
for (int col = 0; col < 8; col++) {
brdStr += " .";
}
brdStr += "\n"; // line break
}
return brdStr;
}
}
enum Type {
P, // pawn
R, // rook
N, // knight
B, // bishop
Q, // queen
K, // king
}

Edit/Compile/Run Chess.java.

🄹 vim Chess.java                
🄹 javac Chess.java && java Chess
0 1 2 3 4 5 6 7
0 . . . . . . . .
1 . . . . . . . .
2 . . . . . . . .
3 . . . . . . . .
4 . . . . . . . .
5 . . . . . . . .
6 . . . . . . . .
7 . . . . . . . .

Now we can design a new class Piece to represent a piece. Add the following code at the end of file Chess.java.

class Piece {
int c; // column
int r; // row
boolean w; // isWhite
Type t;
Piece(int c, int r, boolean w, Type t) {
this.c = c;
this.r = r;
this.w = w;
this.t = t;
}
}

There are two vim commands we can use to speed up editing a little bit. “L” moves cursor to the bottom. “o” enters INSERT mode and inserts a new line below cursor. BTW, “H” moves cursor to the top and “M” to the middle. “O” enters INSERT mode and inserts a new line above cursor.

Edit/Compile/Run Chess.java to make sure everything is fine.

🄹 vim Chess.java                
🄹 javac Chess.java && java Chess
0 1 2 3 4 5 6 7
0 . . . . . . . .
1 . . . . . . . .
2 . . . . . . . .
3 . . . . . . . .
4 . . . . . . . .
5 . . . . . . . .
6 . . . . . . . .
7 . . . . . . . .

In order to use data type Set as the container of all the pieces on board, we need to add two lines of code at the top of Chess.java.

import java.util.Set;
import java.util.HashSet;

We’ll create an instance variable with type Set<Piece> and insert a white queen into it in the constructor Board().

class Board {
private Set<Piece> pieces = new HashSet<>();
Board() {
pieces.add(new Piece(3, 7, true, Type.Q));
}
public String toString() {
String brdStr = "";
brdStr += " 0 1 2 3 4 5 6 7\n";
for (int row = 0; row < 8; row++) {
brdStr += row + "";
for (int col = 0; col < 8; col++) {
brdStr += " .";
}
brdStr += "\n"; // line break
}
return brdStr;
}
}

Make sure it still works by compiling and running the updated Chess.java.

To print out pieces on board we can create the following helper method Piece pieceAt(int c, int r) to return an Piece object at a specific location.

class Board {
private Set<Piece> pieces = new HashSet<>();
Board() {
pieces.add(new Piece(3, 7, true, Type.Q));
}
Piece pieceAt(int c, int r) {
for (Piece p : pieces) {
if (p.c == c && p.r == r) {
return p;
}
}
return null;
}
public String toString() {
String brdStr = "";
brdStr += " 0 1 2 3 4 5 6 7\n";
for (int row = 0; row < 8; row++) {
brdStr += row + "";
for (int col = 0; col < 8; col++) {
brdStr += " .";
}
brdStr += "\n"; // line break
}
return brdStr;
}
}

Now we need to modify toString() method of class Board to show pieces.

class Board {
private Set<Piece> pieces = new HashSet<>();
Board() {
pieces.add(new Piece(3, 7, true, Type.Q));
}
Piece pieceAt(int c, int r) {
for (Piece p : pieces) {
if (p.c == c && p.r == r) {
return p;
}
}
return null;
}
public String toString() {
String brdStr = "";
brdStr += " 0 1 2 3 4 5 6 7\n";
for (int r = 0; r < 8; r++) {
brdStr += r + "";
for (int c = 0; c < 8; c++) {
Piece p = pieceAt(c, r);
if (p == null) {
brdStr += " .";
} else {
switch (p.t) {
case P: brdStr += p.w ? " P" : " p"; break;
case R: brdStr += p.w ? " R" : " r"; break;
case N: brdStr += p.w ? " N" : " n"; break;
case B: brdStr += p.w ? " B" : " b"; break;
case Q: brdStr += p.w ? " Q" : " q"; break;
case K: brdStr += p.w ? " K" : " k"; break;
}
}
}
brdStr += "\n"; // line break
}
return brdStr;
}
}

Edit/Compile/Run Chess.java to see the single piece Queen on board.

🄹 vim Chess.java                
🄹 javac Chess.java && java Chess
a b c d e f g h
8 . . . . . . . . 8
7 . . . . . . . . 7
6 . . . . . . . . 6
5 . . . . . . . . 5
4 . . . . . . . . 4
3 . . . . . . . . 3
2 . . . . . . . . 2
1 . . . Q . . . . 1
a b c d e f g h

Time to add the rest 31 pieces in the constructor of class Board.

  Board() {
pieces.add(new Piece(3, 0, false, Type.Q));
pieces.add(new Piece(3, 7, true , Type.Q));
pieces.add(new Piece(4, 0, false, Type.K));
pieces.add(new Piece(4, 7, true , Type.K));
for (int i = 0; i < 2; i++) {
pieces.add(new Piece(0 + i * 7, 0, false, Type.R));
pieces.add(new Piece(0 + i * 7, 7, true , Type.R));
pieces.add(new Piece(1 + i * 5, 0, false, Type.N));
pieces.add(new Piece(1 + i * 5, 7, true , Type.N));
pieces.add(new Piece(2 + i * 3, 0, false, Type.B));
pieces.add(new Piece(2 + i * 3, 7, true , Type.B));
}
for (int i = 0; i < 8; i++) {
pieces.add(new Piece(i, 1, false, Type.P));
pieces.add(new Piece(i, 6, true , Type.P));
}
}

Edit/Compile/Run Chess.java to see all 32 pieces.

🄹 vim Chess.java                
🄹 javac Chess.java && java Chess
a b c d e f g h
8 r n b q k b n r 8
7 p p p p p p p p 7
6 . . . . . . . . 6
5 . . . . . . . . 5
4 . . . . . . . . 4
3 . . . . . . . . 3
2 P P P P P P P P 2
1 R N B Q K B N R 1
a b c d e f g h

Here is the complete code so far.

import java.util.Set;
import java.util.HashSet;
class Chess {
public static void main(String[] args) {
Board brd = new Board();
System.out.println(brd);
}
}
class Board {
private Set<Piece> pieces = new HashSet<>();
Board() {
pieces.add(new Piece(3, 0, false, Type.Q));
pieces.add(new Piece(3, 7, true , Type.Q));
pieces.add(new Piece(4, 0, false, Type.K));
pieces.add(new Piece(4, 7, true , Type.K));
for (int i = 0; i < 2; i++) {
pieces.add(new Piece(0 + i * 7, 0, false, Type.R));
pieces.add(new Piece(0 + i * 7, 7, true , Type.R));
pieces.add(new Piece(1 + i * 5, 0, false, Type.N));
pieces.add(new Piece(1 + i * 5, 7, true , Type.N));
pieces.add(new Piece(2 + i * 3, 0, false, Type.B));
pieces.add(new Piece(2 + i * 3, 7, true , Type.B));
}
for (int i = 0; i < 8; i++) {
pieces.add(new Piece(i, 1, false, Type.P));
pieces.add(new Piece(i, 6, true , Type.P));
}
}
Piece pieceAt(int c, int r) {
for (Piece p : pieces) {
if (p.c == c && p.r == r) {
return p;
}
}
return null;
}
public String toString() {
String brdStr = "";
brdStr += " a b c d e f g h\n";
for (int r = 0; r < 8; r++) {
brdStr += (8 - r) + "";
for (int c = 0; c < 8; c++) {
Piece p = pieceAt(c, r);
if (p == null) {
brdStr += " .";
} else {
switch (p.t) {
case P: brdStr += p.w ? " P" : " p"; break;
case R: brdStr += p.w ? " R" : " r"; break;
case N: brdStr += p.w ? " N" : " n"; break;
case B: brdStr += p.w ? " B" : " b"; break;
case Q: brdStr += p.w ? " Q" : " q"; break;
case K: brdStr += p.w ? " K" : " k"; break;
}
}
}
brdStr += " " + (8 - r) + "\n";
}
brdStr += " a b c d e f g h\n";
return brdStr;
}
}
enum Type {
P, // pawn
R, // rook
N, // knight
B, // bishop
Q, // queen
K, // king
}
class Piece {
int c; // column
int r; // row
boolean w; // isWhite
Type t;
Piece(int c, int r, boolean w, Type t) {
this.c = c;
this.r = r;
this.w = w;
this.t = t;
}
}

And we are done.

--

--