School Java Project Chess (1)

Printing out an empty game board

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

--

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.

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.

Then run brew install java.

Make sure java is installed properly:

Create a chess directory, or folder, somewhere in your file system.

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.

The complete code has 5 lines:

Compile Chess.java with javac and run the app using java.

Congratulations! We got our first Java program working perfectly.

BTW since programmers are lazy the above two commands can by put together like this:

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.

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 “;”.

Java compiler kindly reports that on line 3 “;” expected:

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.

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.

Edit/Compile/Run it.

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

Edit/Compile/Run it to see the same output of 8 dots.

Now add another for loop to print out 8 rows of “ . . . . . . . .”.

Edit/Compile/Run it.

Add the top a b c d e f g h:

Edit/Compile/Run it.

Add vertical coordinate label from 1 to 8:

Edit/Compile/Run it.

Similarly we can add the rank labels and bottom file labels.

Here is the complete source code:

And we are done.

--

--