// // JButtonSquare.java // FrilJavaDemo - Fril - Java noughts and crosses (tic-tac-toe) demo // // Created by entpm on Wed May 16 2001. // Copyright (c) 2001. All rights reserved. // /* * Description * class to handle a square on the noughts-and-crosses grid. * Each square is numbered (0-8) and this number is used to identify the square. * If a square is unoccupied, a click places a cross in the square and the * computer's response is found - see JButtonSquareActionPerformed(). * Otherwise, code is mainly concerned with the square's state * * */ public class JButtonSquare extends javax.swing.JButton { private int myID; private int myState; private static final String compSYMBOL = "O"; private static final String humSYMBOL = "X"; PlayingWindow mainWindow; JButtonSquare(int id, int xcoord, int ycoord, PlayingWindow theMainWindow) { super(); setVisible(true); setBackground(new java.awt.Color(255, 255, 255)); setBorderPainted(false); setFont(new java.awt.Font("DialogInput", 0, 20)); setSize(new java.awt.Dimension(50, 50)); setLocation(new java.awt.Point(xcoord, ycoord)); myID = id; resetState(); addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent e) { JButtonSquareActionPerformed(e); } }); mainWindow = theMainWindow; } public void resetState() { setText(" "); setToolTipText("click to go here"); myState = FrilJavaDemo.BLANK; setEnabled(true); } public void occupySquare(int occupier) { if (myState == FrilJavaDemo.BLANK) { myState = occupier; setToolTipText("this square is occupied"); if(myState == FrilJavaDemo.HUMAN) setText(humSYMBOL); else setText(compSYMBOL); setEnabled(false); mainWindow.jFrilTextInput.requestFocus(); // otherwise focus goes to an adjacent square } } public void JButtonSquareActionPerformed(java.awt.event.ActionEvent e) { // System.out.println("occupy square " + myID); int compSquare = FrilJavaDemo.BLANK; boolean dummy; occupySquare(FrilJavaDemo.HUMAN); if(mainWindow.checkGameEnded() == false) compSquare = mainWindow.chooseComputerSquare(); if(compSquare != FrilJavaDemo.BLANK) { mainWindow.computerMove(compSquare); dummy = mainWindow.checkGameEnded(); } } public int getState() { int ret = myID; if (myState != FrilJavaDemo.BLANK) ret = myState; return ret; } public int equalsState(int theState) { int ret=0; if (myState == theState) { ret=1; } return ret; } }