School Java Project Chinese Chess (1)

Printing out an empty game board

  0 1 2 3 4 5 6 7 8
0 . . . . . . . . .
1 . . . . . . . . .
2 . . . . . . . . .
3 . . . . . . . . .
4 . . . . . . . . .
5 . . . . . . . . .
6 . . . . . . . . .
7 . . . . . . . . .
8 . . . . . . . . .
9 . . . . . . . . .
> 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)
> mkdir cchess
> cd cchess
> vim CChess.java
class CChess {
public static void main(String[] args) {
System.out.println("Hello CChess");
}
}
> javac CChess.java
> java CChess
Hello CChess
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;
}
}
> javac CChess.java
> java CChess
. . . . . . . . .
class CChessBoard {
public String toString() {
String brdStr = "";
for (int col = 0; col < 9; col++) { // col for "column"
brdStr += " .";
}

return brdStr;
}
}
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;
}
}
 . . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
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;
}
}
 0 1 2 3 4 5 6 7 8
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
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;
}
}
  0 1 2 3 4 5 6 7 8
0 . . . . . . . . .
1 . . . . . . . . .
2 . . . . . . . . .
3 . . . . . . . . .
4 . . . . . . . . .
5 . . . . . . . . .
6 . . . . . . . . .
7 . . . . . . . . .
8 . . . . . . . . .
9 . . . . . . . . .
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;
}
}

command line

compile a Java program

run a Java program

class

main method of a Java program

final

static

for loop

nested for loop

string concatenation

method overriding

Java toString method

> pwd
> mkdir abcd
> ls -l
> cd abcd
> cd ..
> touch HelloWorld.java
> open -e HelloWorld.java
> rm HelloWorld.java
> rm -d 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