static int[][] board = {{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0}};
// "names" of the ranks and files
static char ranks[] = {'1', '2', '3', '4', '5', '6', '7',
'8'};
static char files[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g',
'h'};
// void method uses Screen commands to display the board
in the
// center of the screen. Screen numbers the rows from top
to bottom;
// conventional chess notation numbers the ranks from bottom
to top.
static void displayBoard()
{
int screenRow = 1; // starting row
// paint ranks from top to bottom of screen
for (int rank = 7; rank >= 0; rank--)
{
// side caption
Screen.moveCursor(screenRow, 20);
System.out.print(" " +
ranks[rank]
+ " ");
// display all files for one rank
for (int file = 0; file <= 7;
file++)
{
if (board[rank][file]
== 0)
{
System.out.print("..
");
}
else
{
if
(board[rank][file]
<= 9) // 1 digit
{
System.out.print(" "); // need leading blank
}
System.out.print(board[rank][file]
+ " ");
}
}
screenRow +=
2;
// use alternate lines
}
// bottom caption
Screen.moveCursor(screenRow, 20);
System.out.print("
");
for (int file = 0; file <= 7; file++)
{
System.out.print(files[file]
+ " ");
}
} // end of displayBoard
// skeletal main method, just to demonstrate
public static void main (String[] args)
{
// initialize variables
int aRank = 1;
char aFile = 'b';
int moveNumber = 1;
board[aRank-1][aFile - 'a'] = moveNumber;
Screen.clearScreen();
displayBoard();
// now read a single move; assume it's valid, and place the knight
Screen.moveCursor(20, 15);
System.out.print("Please enter a move: file
first, then rank >");
aFile = Keyboard.readChar();
aRank = Keyboard.readInt();
moveNumber++;
board[aRank-1][aFile - 'a'] = moveNumber;
displayBoard();
Screen.moveCursor(22,1);
}
}