Monday July 24, 2000


This short lab is designed to let you see how a multi-dimensional array can be used. It also gives you a preview of topics you will encounter if you take CIS 121.

Consider the simple game of TicTacToe and how we could use multidimensional arrays to play the game. Note that the code below does not complete the game. It merely allows you to place colors on a board representing xs and os.

import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class TicTacToe extends Applet implements ActionListener {
   Panel[][] panels;
   String[][] board; 
   String[][] used;
   Button place;
   TextField row;
   TextField column;
   String current;

   public TicTacToe() {
      setLayout(new GridLayout(4,3));
      panels = new Panel[3][3];
      for (int i=0;i<3;i++)
         for (int j=0;j<3;j++) {
            panels[i][j] = new Panel(null);
            panels[i][j].setBackground(Color.white);
            add(panels[i][j]);
         }
      board = new String[3][3];
      for (int i=0;i<3;i++)
         for (int j=0;j<3;j++)
            board[i][j] = "";
      used = new String[3][3];
      for (int i=0;i<3;i++)
         for (int j=0;j<3;j++)
            used[i][j] = "no";
      row = new TextField(5);
      column = new TextField(5);
      add(row);
      add(column);
      place = new Button("Place");
      add(place);
      place.addActionListener(this);     
      current = "x";
   }

   public void actionPerformed(ActionEvent e) {
      if (e.getSource() == place) {
         int therow = new Integer(row.getText()).intValue();
         int thecolumn = new Integer(column.getText()).intValue();
         System.out.println(therow + " " + thecolumn);
         System.out.println(current);
         if (used[therow-1][thecolumn-1].equals("no")) {
            if (current.equals("x"))
               panels[therow-1][thecolumn-1].setBackground(Color.red);
            else
               panels[therow-1][thecolumn-1].setBackground(Color.yellow);
            board[therow-1][thecolumn-1] = current;
            used[therow-1][thecolumn-1] = "yes";
            current = current.equals("x") ? "o" : "x";
         }
      }
   }

}
In order to see this program work, right click on the two links below and save each one into the c:\temp directory.

TicTacToe.java

TicTacToe.html

Open a DOS prompt.

Change into the c:\temp directory.

cd c:\temp

Now compile the program.

javac TicTacToe.java

If this doesn't work, then use the full pathname of javac.

c:\jbuilder2\java\bin\javac TicTacToe.java

A class file will now be generated. Unlike the class files we have been writing we cannot execute this program with the interpreter because it doesn't have a main method.

Since this program is an applet it must be run with a special program. First HTML code must be written that will instruct an HTML interpreter to load a Java applet. Then we must use a special program to view the HTML code. Either we can use a web browser or we can use the appletviewer program that comes with the JDK.

At the prompt run appletviewer to see the program in action.

appletviewer TicTacToe.html

If this doesn't work, then use the full pathname of appletviewer.

c:\jbuilder2\java\bin\appletviewer TicTacToe.html