Image for post
Image for post

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:

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:

Create a new directory named, e.g “cchess”, and go into it:

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 “” inside the current directory(folder). We use vim as our text editor.

This is program:

Compile and run this tiny Java program:

We put all classes in this single source code file for convenience. Let’s create class CChessBoard to represent the logical Chinese Chess board. Modify the existing code so it looks like:

Compile and run it we can see the first row of our 10x9 empty(no pieces yet) board:

Refactor the hardcoded ” . . . . . . . . .” with a for loop:

Compile and run we should get the same output ” . . . . . . . . .” .

Now add another for loop to out put 10 rows of ” . . . . . . . . .” .

Compile and run to see the complete 10x9 empty board:

Add the top 0 1 2 3 4 5 6 7 8 :

Now it looks like:

Keep going to add vertical coordinate label from 0 to 9:

And we get:

One more thing. Refactor the hardcoded numbers 9 and 10 with constants cols and rows and here is the complete code so far:

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 to compile our Java source code, and java CChess to run our program.

compile a Java program

Use command javac 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.


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.


This keyword can be used to define constants, instead of a variable.


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:

To create a new directory named abcd:

To list the content of current directory:

To change directory from current to the newly created abcd:

To change directory back to the parent directory of abcd:

To create an empty text file, e.g. a Java source file like

To start editing it with TextEdit:

To remove file

To remove directory abcd:

1-on-1 tutor of chosen kids

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store