How to use multidimensional arrays in your code.
You can run this technique code from:
NOTE First make sure that database is running on your localhost SilverStream Server | |
See the chapter on coding Java for SilverStream applications in the Programmer's Guide |
This example loads multidimensional arrays with objects and values, then navigates through them to retrieve values. It uses the game Tic Tac Toe.
The form also contains a timer to delay processing so that it appears the computer is thinking.
The following code shows how the game's user moves and computer moves are stored. The display characters representing the moves are stored as text in the elements of an AgcLabel array.
protected void formLoaded() { // Load each cell in the array with the appropriate label. cell [0][0] = lblRow1Col1; cell [0][1] = lblRow1Col2; cell [0][2] = lblRow1Col3; cell [1][0] = lblRow2Col1; cell [1][1] = lblRow2Col2; cell [1][2] = lblRow2Col3; cell [2][0] = lblRow3Col1; cell [2][1] = lblRow3Col2; cell [2][2] = lblRow3Col3; }
In the following code, the user must have clicked on the cell in (row 1, column 1) for this event to occur. The code for the other events (generated from clicking the other cells in the form) is similar. The following code sets the text of a particular label element in the cell array, thereby displaying an "X" in the corresponding cell on the form. It also sets the corresponding element in the board array, which is used for pattern checking.
private void handle_lblRow1Col1_mousePressed(MouseEvent mouseEvent) { // No move allowed, if game over if (gbWin) return; if (lblRow1Col1.getText() == "") { lblRow1Col1.setText("X"); board [0][0] = 'x'; evaluateBoard (); } }
setText()
.
The following code uses board to check for the 8 possible winning patterns. If it identifies one of the winning patterns, it sets the gbWin flag to true. In this method, the input parameter ('x' or 'o') determines the player for which it evaluates the winning pattern.
public boolean checkForWin(char charCode) { // Make sure the flag is false to begin with gbWin = false; // Check the top left to bottom right diagonal for a win if ( (board[0][0] == charCode) && (board[1][1] == charCode) && (board[2][2] == charCode) ) gbWin = true; // Check the top right to bottom left diagonal for a win else if ( (board[0][2] == charCode) && (board[1][1] == charCode) && (board[2][0] == charCode) ) gbWin = true; // Now check the rows else for (int i = 0; i < 3; i++) { // Check rows across for a win if ( (board[i][0] == charCode) && (board[i][1] == charCode) && (board[i][2] == charCode) ) { gbWin = true; break; } // Check columns down for a win if ( (board[0][i] == charCode) && (board[1][i] == charCode) && (board[2][i] == charCode) ) { gbWin = true; break; } } // Return the result of checking for a win return gbWin; }
The following code checks if the user has made the winning move. If not, it uses a timer to pause the computer before it makes the next move.
public void evaluateBoard() { // Check if the users last move won if (checkForWin ('x')) { taMessage.setText (GS_YOU_WON); return; } // Wait a little before making our move. tmrWait.startTimer(); }
checkForWin()
and providing 'x' as the parameter. The user always plays "X". This method returns true if the user has won. In this case, display a winning message.
tmrWait.startTimer()
.