// CS 170 - Spring 2002 // Homework # 8, Arrays // Simple TicTacToe import java.awt.*; import java.applet.*; import java.awt.event.*; public class TicTacToe extends Applet implements ActionListener, MouseListener { // ------------------------------------------------------------ // 1. Create your fields // ------------------------------------------------------------ // a. Arrays // -- A 3 x 3 array of Label objects. Call it grid // -- A 3 x 3 array of chars. Call it game // b. Other graphical fields // -- A Button named restart // -- A Panel named p // -- A Label named status // c. Primitive variables // -- an int named numClicks (number of clicks in game) // -- a boolean named isDone (set to false) // -- a boolean named isXTurn (set to true) // COMPILER CHECK # 1 // ------------------------------------------------------------ // 2. The init() method // ------------------------------------------------------------ public void init() { // a. Set the layout for the applet (BorderLayout()) // b. Add and hook up the restart button // c. Add your status label to the top of the applet // -- Set background to yellow, foreground to blue // -- Set font to 12 point bold Helvetica // d. Initialize your main Panel // -- set the layout (GridLayout(3, 3, 3, 3)); // -- set a nice font // -- initialize the background to black // -- add the Panel to the center of your applet // e. Create and initialize your Label objects // -- Start with a nested loop (row, col) // -- Inside the inner loop // > Create a new Label object (blank), place in array // grid[row][col] = new Label(" ", Label.CENTER); // > Attach a MouseListener to it // > Set the background color (white) // > Add it to your panel // > Put a blank in each element of your game array } // COMPILER CHECK # 2 // ------------------------------------------------------------ // 3. Handle the Mouse Clicked Event // ------------------------------------------------------------ public void mouseClicked(MouseEvent e) { if (isDone) resetGame(); Label clicked = (Label) e.getSource(); next: for (int row = 0; row < 3; row++) { for (int col = 0; col < 3; col++) { if (clicked == grid[row][col]) { // a. if text in clicked is not " " // then print "Invalid Move" using status() // and break next; [a labeled break] // b. if text in clicked is " " and isXTurn is true // -- set the text in clicked to "X" // -- set the foreground in clicked to red // -- set the game[row][col] to 'X' // c. Otherwise (previous two false) // -- set the text in clicked to "O" // -- set the foreground to blue // -- set the game[row][col] to 'O' // d. Toggle isXTurn, incrment numClicks // Check to see if game is over after each click gameOver(); } } } } // COMPILER CHECK # 3 // ------------------------------------------------------------ // 4. The resetGame() method // ------------------------------------------------------------ void resetGame() { // a. Write a nested for loop (row, col) // b. For each element in grid, set text to " " // c. For each element in game, set value to ' ' // d. Set numClicks to zero // e. Set isXTurn to true } // COMPILER CHECK # 4 // ------------------------------------------------------------ // The actionPerformed() method (resets the game) // ------------------------------------------------------------ public void actionPerformed(ActionEvent ae) { resetGame(); } // ------------------------------------------------------------ // The gameOver() method [see if there is a winner] // ------------------------------------------------------------ void gameOver() { char winner = ' '; // Check values in array named game // a. Check the first diagonal (upper-left to lower right) // Set winner to upper-left if all are equal. if (game[0][0] == game[1][1] && game[1][1] == game[2][2]) winner = game[0][0]; // b. if no winner, then check second diagonal // (lower-left to upper-right). If all three are the // same then winner is lower-right corner else if (game[2][0] == game[1][1] && game[1][1] == game[0][2]) winner = game[2][0]; // c. if no winner on diagonals, then check rows // -- start by creating a loop for rows // -- on each row check if all three columns are same // -- if they are, then winner is game[i][0] // -- if not, use rows to represent the columns // and check if each row is the same else { for (int row = 0; row < 3; row++) { if (game[row][0] != ' ' && game[row][0] == game[row][1] && game[row][1] == game[row][2]) winner = game[row][0]; else if (game[0][row] != ' ' && game[0][row] == game[1][row] && game[1][row] == game[2][row]) winner = game[0][row]; } } // Assume game is finished isDone = true; // Check for tie if (winner == ' ' && numClicks == 9) status.setText("Tie Game"); // Check for game done else if (winner != ' ') status.setText("Game Over: " + winner + " Won!!!"); // Otherwise a continuing game else { status.setText((isXTurn ? "X's Turn" : "O's Turn")); isDone = false; } } // ------------------------------------------------------- // Dummy methods for MouseListener interface // ------------------------------------------------------- public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } public void mousePressed(MouseEvent e) { } public void mouseReleased(MouseEvent e) { } }