School Java Project Chinese Chess (1)
Printing out an empty game board
Recently I helped a high school student complete his homework project, which is a simple Java board game Chinese Chess. It looks like the following when finished.
I’ll cover from Java “Hello World” to printing out an empty Chinese Chess board on console in this post.
We worked on MacOS for this project.
We’ll achieve the following empty board in this post:
0 1 2 3 4 5 6 7 8
0 . . . . . . . . .
1 . . . . . . . . .
2 . . . . . . . . .
3 . . . . . . . . .
4 . . . . . . . . .
5 . . . . . . . . .
6 . . . . . . . . .
7 . . . . . . . . .
8 . . . . . . . . .
9 . . . . . . . . .
I also created a YouTube video to show how to write the same Java code in Eclipse. So you can also try this Java project on a Windows computer, as long as Eclipse is installed.
First, bring up Terminal and make sure we do have Java:
> 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 new directory named, e.g “cchess”, and go into it:
> mkdir cchess
> cd cchess
Now let’s write the simplest working program to print out “Hello CChess”. You can use any text editor to create a file named e.g “CChess.java” inside the current directory(folder). We use vim as our text editor.
> vim CChess.java
This is program:
class CChess {
public static void main(String[] args) {
System.out.println("Hello CChess");
}
}
Compile and run this tiny Java program:
> javac CChess.java
> java CChess
Hello CChess
We put all classes in this single source code file CChess.java for convenience. Let’s create class CChessBoard to represent the logical Chinese Chess board. Modify the existing code so it looks like:
class CChess {
public static void main(String[] args) {
CChessBoard brd = new CChessBoard();
System.out.println(brd);
}
}class CChessBoard {
public String toString() {
String brdStr = "";
brdStr += " . . . . . . . . .";
return brdStr;
}
}
Compile and run it we can see the first row of our 10x9 empty(no pieces yet) board:
> javac CChess.java
> java CChess
. . . . . . . . .
Refactor the hardcoded ” . . . . . . . . .”
with a for loop:
class CChessBoard {
public String toString() {
String brdStr = "";
for (int col = 0; col < 9; col++) { // col for "column"
brdStr += " .";
}
return brdStr;
}
}
Compile and run we should get the same output ” . . . . . . . . .”
.
Now add another for loop to out put 10 rows of ” . . . . . . . . .”
.
class CChessBoard {
public String toString() {
String brdStr = "";
for (int row = 0; row < 10; row++) {
for (int col = 0; col < 9; col++) {
brdStr += " .";
}
brdStr += "\n"; // line break
}
return brdStr;
}
}
Compile and run to see the complete 10x9 empty board:
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
Add the top 0 1 2 3 4 5 6 7 8
:
class CChessBoard {
public String toString() {
String brdStr = "";
for (int i = 0; i < 9; i++) {
brdStr += " " + i;
}
brdStr += "\n"; for (int row = 0; row < 10; row++) {
for (int col = 0; col < 9; col++) {
brdStr += " .";
}
brdStr += "\n";
}
return brdStr;
}
}
Now it looks like:
0 1 2 3 4 5 6 7 8
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
Keep going to add vertical coordinate label from 0 to 9:
class CChessBoard {
public String toString() {
String brdStr = "";
brdStr += " ";
for (int i = 0; i < 9; i++) {
brdStr += " " + i;
}
brdStr += "\n"; for (int row = 0; row < 10; row++) {
brdStr += row + "";
for (int col = 0; col < 9; col++) {
brdStr += " .";
}
brdStr += "\n";
}
return brdStr;
}
}
And we get:
0 1 2 3 4 5 6 7 8
0 . . . . . . . . .
1 . . . . . . . . .
2 . . . . . . . . .
3 . . . . . . . . .
4 . . . . . . . . .
5 . . . . . . . . .
6 . . . . . . . . .
7 . . . . . . . . .
8 . . . . . . . . .
9 . . . . . . . . .
One more thing. Refactor the hardcoded numbers 9 and 10 with constants cols
and rows
and here is the complete code so far:
class CChess {
public static void main(String[] args) {
CChessBoard brd = new CChessBoard();
System.out.println(brd);
}
}class CChessBoard {
final static int rows = 10;
final static int cols = 9; @Override
public String toString() {
String brdStr = "";
brdStr += " ";
for (int i = 0; i < cols; i++) {
brdStr += " " + i;
}
brdStr += "\n"; for (int row = 0; row < rows; row++) {
brdStr += row + "";
for (int col = 0; col < cols; col++) {
brdStr += " .";
}
brdStr += "\n";
}
return brdStr;
}
}
And we are done. :-)
Next, let’s deploy initial 32 pieces on board.
These programming concepts were used in this post:
command line
We type in commands, instead of using a mouse, to tell computer what to do. Like in Terminal of MacOS. E.g, we type javac CChess.java
to compile our Java source code, and java CChess
to run our program.
compile a Java program
Use command javac CChess.java
to generate Java class file CChess.class, which contains platform-neutral Java bytecode ready for execution. See Java compiler.
run a Java program
Use command java CChess
to run our CChess program. If we use an IDE like Eclipse to develop this app, you may click a button to run it, without the need to manually compile it first. Behind the scene, Eclipse does the similar thing as java CChess
for us.
class
A class is a template for creating objects. It’s also a key word used in Java programs to create a class. Inheritance means a class can have super classes and subclasses. It’s huge topic however as beginners don’t worry too much about it. Down the road it’ll be our best friend eventually.
main method of a Java program
It’s the entry point of any Java program. Unfortunately the signature of main method it out of our control. Again, don’t worry too much about the details for now, e.g. how it accepts command line arguments when being called, why it’s final, why it’s static etc.
final
This keyword can be used to define constants, instead of a variable.
static
This keyword can be used to define class, instead of instance, variables and methods. Unlike an instance variable, a class variable is shared by all the instances of the class.
for loop
There are 2 things we have to master during the first few days or weeks of our coding journey. One is if else
and the other is for loop
. For loop helps us simplify duplicated code. E.g. with a for loop, it’s easy to print out “1, 2, 3, …, 20”, or draw 15 parallel lines on screen.
nested for loop
When we just start to learn programming, understanding how a program works if much more important than memorizing syntax. Some people call it “embedded for loop”, nested for loop simply means putting a for loop inside another for loop, or wrapping an inner for loop with an outer for loop. In our app we use an inner for loop to generate a row of dots, an outer for loop to generate multiple rows of dots. That’s how we print out the empty game board.
string concatenation
Like many popular programming languages, Java use plus sign “+” to concatenate two strings to generate another new result string. E.g. with “abcd” + “123”
we get “abcd123”
.
method overriding
This is an important concept in Object Oriented programming language like Java. Don’t try to get a sense of it until you use it more than 10 times. Simply accept the fact that we have to override some method(s) of super class to get our job done, e.g. we have to override a specific method when we use JPanel to draw our game board on the app window, like what we will do in future posts of this series.
Java toString method
We get something like “returns a string representation of the object” if we google those words. By overriding this method of class Object, when we print out an instance of our own class, the output is whatever we return in our toString method. In our app, we build up a string representation of our game board. So we can use code like System.out.println(brd);
to print the CChessBoard
object brd
.
We’ll show some common commands which are often used in Terminal. Note we don’t type the>
part, the prompt, in the following examples. Only type the text after it, then press enter/return key to actually run the command.
To view where we are:
> pwd
To create a new directory named abcd:
> mkdir abcd
To list the content of current directory:
> ls -l
To change directory from current to the newly created abcd:
> cd abcd
To change directory back to the parent directory of abcd:
> cd ..
To create an empty text file, e.g. a Java source file like HelloWorld.java:
> touch HelloWorld.java
To start editing it with TextEdit:
> open -e HelloWorld.java
To remove file HelloWorld.java:
> rm HelloWorld.java
To remove directory abcd:
> rm -d abcd