package boardgame; /** * A board game using JApplet that consists of a JPanel for the board and a JLabel for * displaying game status; a nested class Cell is used to implement individual cells * on the board */ import java.lang.Math; import javax.swing.JApplet; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.border.LineBorder; import javax.swing.JOptionPane; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Graphics; import java.awt.GridLayout; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; /** * BoardGame Class is our applet and main class. Initializes the Board, asks user for input, and creates * the cells. It is also in charge of painting and redrawing the game board when needed. * * @author Hector Colon * */ public class BoardGame extends JApplet { private final int MAX_ROW = 10, MAX_COLUMN = 10; // maximum rows and columns of the board private int row, column, circles; // actual numbers of rows, columns, to be input from user private boolean won = false; //keeps track of whether the user has won private String user; // user input from dialog messages private Cell[][] cells = new Cell[MAX_ROW][MAX_COLUMN]; private int noMoves = 0; //the number of moves the user makes private JOptionPane dialog; //dialog box to ask user for inputs private JPanel panel; // hold the game board private JLabel gameStatus; // display game status /** * Called exactly once when the JApplet is started. Sets up the game board. * * @throws Exception */ public void init() { this.setLayout(new BorderLayout()); // prompt user for number of rows and columns (at most 10), and number of initially marked cells dialog = new JOptionPane(); boolean loop; do { try { //ask user for the game board layout user = JOptionPane.showInputDialog(null, "How many rows would you like the boad to have?"); row = Integer.parseInt(user); user = dialog.showInputDialog(null, "How many columns would you like the boad to have?"); column = Integer.parseInt(user); user = dialog.showInputDialog(null, "How many circles would you like the boad to have?"); circles = Integer.parseInt(user); //check if there are invalid input if(row > MAX_ROW || column > MAX_COLUMN || circles > (row*column)) { loop = true; dialog.showMessageDialog(null, "Invalid Input!"); } else loop = false; } //catches any exception. most likely a string that cannot be parsed catch(Exception e) { dialog.showMessageDialog(null, "Invalid Input!"); loop = true; } } while(loop); // set line border for the applet’s window panel = new JPanel(new GridLayout(row, column, 0, 0)); panel.setBorder(new LineBorder(Color.red, 2)); // create Cell objects and add to the JPanel panel for (int i = 0; i < row; i++) { for (int j = 0; j < column; j++) { cells[i][j] = new Cell(i,j); panel.add(cells[i][j]); } } this.add(panel); // use java.lang.Math.random() to mark randomly selected cells with ‘O’, // e.g., the statement cells[3][4].cellStatus = 'O' will set a cell’s status for(int i = 0; i < circles; i++) { int randi = (int)(row * java.lang.Math.random()); int randj = (int)(column * java.lang.Math.random()); //check if the cell already has a circle if(cells[randi][randj].cellStatus != 'O') { cells[randi][randj].cellStatus = 'O'; } //try a different random location for the circle else i--; } // use BorderLayout to place the JPanel panel on top of JLabel gameStatus gameStatus = new JLabel("Number of Moves: " + noMoves); this.add(gameStatus, BorderLayout.PAGE_END); // set initial JApplet size to 400 by 400 this.setSize(400, 400); } public void start() { } public void stop() { } public void destroy() { } /** * called by a Cell object requesting repaint() of the board * need to change cellStatus for those cells in the same row or column * as the calling cell. It also keeps track if the user has won. * * @param r the row of the cell * @param c the column of the cell */ private void redraw(int r, int c) { for (int i = 0; i < row; i++) { for (int j = 0; j < column; j++) { if(cells[i][j].rowIndex == r ||cells[i][j].columnIndex == c) { cells[i][j].cellStatus = 'X'; } } } //check to see if the user has won //assume the user has won, then prove otherwise won = true; for (int i = 0; i < row; i++) { for (int j = 0; j < column; j++) { if(cells[i][j].cellStatus == 'O') { won = false; } } } // display gameStatus text if(won) gameStatus.setText("GAME OVER Number of Moves: " + noMoves); else gameStatus.setText("Number of Moves: " + noMoves); // will cause the entire JApplet including Cell object be repainted this.repaint(); } /** * A nested class implementing individual cells on the board. Each cell knows its * position on the game board and it's status. */ private class Cell extends JPanel implements MouseListener { private char cellStatus = ' '; // display in the cell, initialized to a space character ' ' private int rowIndex, columnIndex; // row and column numbers of the cell, respectively /** * constructor, passing row and column indexes * @param i the row of the cell * @param j the column of the cell */ public Cell(int i, int j) { // add Cell object to the MouseListener addMouseListener(this); //set the location of the cell rowIndex = i; columnIndex = j; } /** * Called by the JApplet environment whenever the display requires redrawn * e.g., moved or resized), and called by the repaint() method */ public void paint(Graphics g) { // draw a line border around cell g.drawRect(0, 0, getWidth()-1, getHeight()-1); // draw 'X' or 'O' on display switch (cellStatus) { case 'X': g.drawLine(10, 10, getWidth()-10, getHeight()-10); g.drawLine(getWidth()-10, 10, 10, getHeight()-10); break; case 'O': g.drawOval(0, 0, getWidth(), getHeight()); break; } } /** * Handles mouse clicks on a cell. Increments the number of moves and calls * the redraw method using the cells location. */ public void mouseClicked(MouseEvent e) { //increment the number of moves noMoves++; // call redraw() which will change the status and draw the X marks redraw(rowIndex, columnIndex); } // added by Eclipse public void mouseEntered(MouseEvent e) { // TODO Auto-generated method stub } // added by Eclipse public void mouseExited(MouseEvent e) { // TODO Auto-generated method stub } // added by Eclipse public void mousePressed(MouseEvent e) { // TODO Auto-generated method stub } // added by Eclipse public void mouseReleased(MouseEvent e) { // TODO Auto-generated method stub } } }