import javax.swing.*; import javax.swing.border.*; import java.awt.*; import java.awt.event.*; /* * Created on Jul 20, 2005 * * To change the template for this generated file go to * Window - Preferences - Java - Code Generation - Code and Comments */ public class TicTacApp extends JApplet implements ActionListener { TicTacBoard board; JButton[][] bttn = new JButton[3][3]; JLabel MessageText; public void init() { // Initialize class variables board = new TicTacBoard(Piece.Player1); MessageText = new JLabel(" "); // Make a main panel that holds everything. This only has the // usefulness of allowing a border (I think). JPanel MainPanel = new JPanel(); MainPanel.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED)); getContentPane().add(MainPanel); // The BorderLayout layout manager lets me put the board in the middle // and expand as much as necessary while keeping the message at the // bottom always the same. MainPanel.setLayout(new BorderLayout()); //Make JPanel to hold board (buttons) JPanel BoardPanel = new JPanel(); BoardPanel.setBorder(BorderFactory.createEtchedBorder()); MainPanel.add(BoardPanel, BorderLayout.CENTER); //Add buttons in 3x3 array int i, j; for (i=0; i<3; i++) for (j=0; j<3; j++){ bttn[i][j] = new JButton(""); bttn[i][j].addActionListener(this); bttn[i][j].setActionCommand("move:"+i+":"+j); BoardPanel.add(bttn[i][j]); } BoardPanel.setLayout(new GridLayout(3,3)); //Make JPanel to hold new game button and message JPanel BottomPanel = new JPanel(); BottomPanel.setLayout(new BoxLayout(BottomPanel, BoxLayout.PAGE_AXIS)); MainPanel.add(BottomPanel, BorderLayout.PAGE_END); //Add buttons for new games JButton NewGame1 = new JButton("New Game (You start)"); NewGame1.addActionListener(this); NewGame1.setActionCommand("newgame:1"); NewGame1.setAlignmentX(Component.CENTER_ALIGNMENT); BottomPanel.add(Box.createRigidArea(new Dimension(0,5))); BottomPanel.add(NewGame1); JButton NewGame2 = new JButton("New Game (I start)"); NewGame2.addActionListener(this); NewGame2.setActionCommand("newgame:2"); NewGame2.setAlignmentX(Component.CENTER_ALIGNMENT); BottomPanel.add(Box.createRigidArea(new Dimension(0,5))); BottomPanel.add(NewGame2); BottomPanel.add(Box.createRigidArea(new Dimension(0,5))); //Add text box MessageText.setHorizontalAlignment(JLabel.CENTER); MessageText.setAlignmentX(Component.CENTER_ALIGNMENT); //MessageText.setBorder(BorderFactory.createEtchedBorder()); BottomPanel.add(MessageText); } public void actionPerformed(ActionEvent evt) { int i, j; String[] cmdparsed = evt.getActionCommand().split(":"); if(cmdparsed.length<1) return; if(cmdparsed[0].equals("move")) { if(cmdparsed.length<3) return; i = Integer.parseInt(cmdparsed[1]); j = Integer.parseInt(cmdparsed[2]); // if someone already won, return without doing anything // this way the buttons can stay enabled but don't continue the game if(board.checkWin(Piece.Player1) || board.checkWin(Piece.Player2)) return; // on error exit without doing anything else if(board.makeMove(new Piece(Piece.Player1, i, j))) return; bttn[i][j].setEnabled(false); // disable button bttn[i][j].setLabel(""+Piece.Player1); // See if user won if(board.checkWin(Piece.Player1)) { MessageText.setText("You Win!!!"); return; } // See if it is cat's game if(board.checkCat()) { MessageText.setText("Cat's Game..."); return; } // Let computer make move Piece NextMove = board.decideMove(); i = NextMove.getX(); j = NextMove.getY(); // on error exit if(board.makeMove(new Piece(Piece.Player2, i, j))) return; bttn[i][j].setEnabled(false); // disable button bttn[i][j].setLabel(""+Piece.Player2); // See if computer won if(board.checkWin(Piece.Player2)) { MessageText.setText("I Win!!!"); return; } // See if it is cat's game if(board.checkCat()) { MessageText.setText("Cat's Game..."); return; } return; } if(cmdparsed[0].equals("newgame")) { if(cmdparsed.length<2) return; // Reset the buttons for (i=0; i<3; i++) for (j=0; j<3; j++){ bttn[i][j].setLabel(""); bttn[i][j].setEnabled(true); } // Reset the message MessageText.setText(" "); //If player 1 (human) starts, get board for him to start if(cmdparsed[1].equals("1")) board = new TicTacBoard(Piece.Player1); //If Player 2 starts (computer) make the first move if(cmdparsed[1].equals("2")) { board = new TicTacBoard(Piece.Player2); Piece NextMove = board.decideMove(); i = NextMove.getX(); j = NextMove.getY(); // on error exit if(board.makeMove(new Piece(Piece.Player2, i, j))) return; bttn[i][j].setEnabled(false); // disable button bttn[i][j].setLabel(""+Piece.Player2); } } } }