School Java Project Chess (1)
Printing out an empty game board
Let’s create a simple chess app which can be used like a non-digital chess board, meaning that we can use mouse to move pieces around on board. It will look like the following when finished.
This is the first post of the series so we’ll kick off Java Hello World at the same time. Our goal is to print out a simple empty chess board represented with 64 dots.
a b c d e f g h
8 . . . . . . . . 8
7 . . . . . . . . 7
6 . . . . . . . . 6
5 . . . . . . . . 5
4 . . . . . . . . 4
3 . . . . . . . . 3
2 . . . . . . . . 2
1 . . . . . . . . 1
a b c d e f g h
We simply follow the board style shown on this Chess Wiki page.
The following is the computer I used. If you are using one other than MacOS, you may need to adapt some of your tools to follow, although the Java source code you find here should work on your machine too, compatible I mean.
You can bring up app terminal by pressing Cmd + Space and start typing “term…”.
Terminal window looks like the following. The default command line prompt could be “$” or “%” or something else.
The next step is not necessary. But for fun let’s change the default prompt from “zhijunsheng@mbp2012 ~ %
” to “🄹 ”.
Make sure you have Java installed on your machine. I won’t go to the details about how to install Java on your machine. Here is one way of doing it. If you don’t have homebrew
installed you can run the following.
🄹 ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Then run brew install java
.
Make sure java is installed properly:
🄹 java -version
java version "1.8.0_20"
Java(TM) SE Runtime Environment (build 1.8.0_20-b26)
Java HotSpot(TM) 64-Bit Server VM (build 25.20-b23, mixed mode)
Create a chess directory, or folder, somewhere in your file system.
🄹 mkdir chess
🄹 ls -l
total 0
drwxr-xr-x 2 zhijunsheng staff 64 8 Jan 23:05 chess
🄹 cd chess
🄹 ls -l
🄹
The first program we’ll write is Hello Chess. We use vim text editor to edit source code. Two spaces, instead of tab key, are use for indentation. Run vim Chess.java
to start typing in source code. If you don’t know how to use vim it’s a good time to make friend with it since most of programmers can use this tiny but powerful text editing tool.
🄹 vim Chess.java
The complete code has 5 lines:
class Chess {
public static void main(String[] args) {
System.out.println("Hello, Chess!");
}
}
Compile Chess.java with javac and run the app using java.
🄹 javac Chess.java
🄹 java Chess
Hello, Chess!
Congratulations! We got our first Java program working perfectly.
BTW since programmers are lazy the above two commands can by put together like this:
🄹 javac Chess.java && java Chess
Hello, Chess!
There is no need to type the same command manually every time after you update the source code because Up Arrow key 🔼 brings you through the command history in terminal.
If you check your current directory you’ll find a new file Chess.class which was the output of command javac Chess.java
.
🄹 ls -l
total 16
-rw-r--r-- 1 zhijunsheng staff 417 8 Jan 23:21 Chess.class
-rw-r--r-- 1 zhijunsheng staff 105 8 Jan 23:15 Chess.java
Before we move on let’s see what if we make a mistake in the source code like a typo, for example we forgot the semicolon “;”.
class Chess {
public static void main(String[] args) {
System.out.println("Hello, Chess!")
}
}
Java compiler kindly reports that on line 3 “;” expected:
🄹 vim Chess.java
🄹 javac Chess.java && java Chess
Chess.java:3: error: ';' expected
System.out.println("Hello, Chess!")
^
1 error
So we go back and under vim command mode type “3” then “G” to move the cursor to line 3.
Then type “$” to move the cursor to end of current line.
Now type “a” to enter editing mode and add the missing semicolon.
Press esc key to quit editing mode and enter command mode.
Type “:” and “x” then enter to save and quit vim.
🄹 javac Chess.java && java Chess
Chess.java:3: error: ';' expected
System.out.println("Hello, Chess!")
^
1 error
🄹 vim Chess.java
🄹 javac Chess.java && java Chess
Hello, Chess!
🄹
Again if need help on how to use vim, Google is our best friend.
We can do chess related stuff now. Actually we can do anything, ok almost, with programs.
A logical chess board can be represented with 8x8 dots. The following code can print 8 dots out. We create a new class named Board.
class Chess {
public static void main(String[] args) {
Board brd = new Board();
System.out.println(brd);
}
}class Board {
public String toString() {
String brdStr = "";
brdStr += " . . . . . . . .";
return brdStr;
}
}
Edit/Compile/Run it.
🄹 vim Chess.java
🄹 javac Chess.java && java Chess
. . . . . . . .
Refactor the hardcoded “ . . . . . . . .”
with a for loop:
class Board {
public String toString() {
String brdStr = "";
for (int col = 0; col < 8; col++) {
brdStr += " .";
}
return brdStr;
}
}
Edit/Compile/Run it to see the same output of 8 dots.
🄹 vim Chess.java
🄹 javac Chess.java && java Chess
. . . . . . . .
Now add another for loop to print out 8 rows of “ . . . . . . . .”
.
class Board {
public String toString() {
String brdStr = "";
for (int r = 0; r < 8; r++) {
for (int c = 0; c < 8; c++) {
brdStr += " .";
}
brdStr += "\n"; // line break
}
return brdStr;
}
}
Edit/Compile/Run it.
🄹 vim Chess.java
🄹 javac Chess.java && java Chess
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
Add the top a b c d e f g h
:
class Board {
public String toString() {
String brdStr = "";
brdStr += " a b c d e f g h\n";
for (int r = 0; r < 8; r++) {
for (int c = 0; c < 8; c++) {
brdStr += " .";
}
brdStr += "\n"; // line break
}
return brdStr;
}
}
Edit/Compile/Run it.
🄹 vim Chess.java
🄹 javac Chess.java && java Chess
a b c d e f g h
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
Add vertical coordinate label from 1 to 8:
class Board {
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++) {
brdStr += " .";
}
brdStr += "\n"; // line break
}
return brdStr;
}
}
Edit/Compile/Run it.
🄹 vim Chess.java
🄹 javac Chess.java && java Chess
a b c d e f g h
8 . . . . . . . .
7 . . . . . . . .
6 . . . . . . . .
5 . . . . . . . .
4 . . . . . . . .
3 . . . . . . . .
2 . . . . . . . .
1 . . . . . . . .
Similarly we can add the rank labels and bottom file labels.
Here is the complete source code:
class Chess {
public static void main(String[] args) {
Board brd = new Board();
System.out.println(brd);
}
}class Board {
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++) {
brdStr += " .";
}
brdStr += " " + (8 - r) + "\n";
}
brdStr += " a b c d e f g h\n";
return brdStr;
}
}
And we are done.