/***
* TicTac is the start of a program to play tic tac toe.
*
* This example shows the syntax for a 2-dimensional array,
* with two subscripts, board[row][col]
*
* @version 0.1alpha, Oct 25 2002
* @author Jim Mahoney
***/
class TicTac {
final private static int BLANK = 0;
final private static int XX = 1;
final private static int OO = 2;
private static int board[][] = new int[3][3];
// Java syntax would also allow this form:
// int[][] board; // declaration
// board = new int[3][3]; // creation
public static void main(String[] args){
print(" -- Tic Tac Toe -- \n");
initBoard();
board[0][1] = XX; // Plop down a few marks
board[1][2] = OO;
printBoard(); // and print the board.
}
private static void printBoard(){
println(" "); // start with a blank line
for (int row=0; row<3; row++){
print(" "); // 4 spaces start each line
for (int col=0; col<3; col++){
if ( board[row][col] == XX ){
print(" X ");
}
else if ( board[row][col] == OO ){
print(" O ");
}
else {
print(" ");
}
if (col<2) {
print("|"); // after columns 0 and 1
}
} // end loop over columns
println(" "); // carriagee return
if (row<2) {
println(" ---+---+---"); // after lines 0 and 1
}
} // end loop over rows
println(" "); // blank line afterward.
} // end printBoard
private static void initBoard(){
for (int row=0; row<3; row++){
for (int col=0; col<3; col++){
board[row][col] = BLANK;
}
}
}
private static void println(String s){
System.out.println(s);
}
private static void print(String s){
System.out.print(s);
}
}
syntax highlighted by Code2HTML, v. 0.9.1