Hey guys~! I am working on a code and I am having some difficulties with it -- Basically I have a 3x3 grid of JButtons that represent the squares on a TicTacToe grid. The code seems to run fine but then after it determines a victor it crashes and says something about AWT Event Dispatch and a class casting error. Does anyone know how to get rid of this? Here is my code so far:
import javax.swing.JButton;
/**
* This class was designed to give me extra capabilities for my buttons that the
* JButton does not seem to offer. I used the variable "tempText" to try to assign a
* string to this Tile that can be evaluated in my code because setText() creates a
* thread to update the button and it might not have finished by the time my own code
* gets to the evaluation part.
*/
public class Tile extends JButton
{
private int row;
private int col;
private String tempText;
private static int counter = 0; //for testing
public Tile(int row, int col)
{
super(" ");
tempText = " ";
this.row = row;
this.col = col;
setSize(10,10);
}
public void clear()
{
tempText = " ";
setText(" ");
}
public boolean isEmpty()
{
return (tempText.equals(" "));
}
public void setTempText(String s)
{
tempText = s;
}
public void setText(String s)
{
setTempText(s);
super.setText(s);
}
public String getTempText()
{
return tempText;
}
public boolean isFilled()
{
return !isEmpty();
}
public int getRow()
{
return row;
}
public int getCol()
{
return col;
}
/**
* Precondition: other is an instance of Tile
*/
public boolean equals(Object other)
{
Tile temp = (Tile)other;
return tempText.equals(temp.tempText);
}
}
package games;
/**
* This class is designed to create a 2D grid for any object type.
* It is mainly designed for gaming purposes. It works correctly.
*/
public class Grid<T>
{
private Object[][] grid;
/**
* Creates a new grid based on a given number of rows and columns
* @param rows Number of rows in the grid
* @param cols Number of columns in the grid
*/
public Grid(int rows, int cols)
{
grid = new Object[rows][cols];
}
/**
* Sets an element in the grid based on the inputted coordinate and item provided
* @param row The row index
* @param col The column index
* @param item The element to place
*/
public void set(int row, int col, T item)
{
grid[row][col] = item;
}
/**
* Retrieves the element stored at the inputted coordinate
* @param row The row index
* @param col The column index
* @return The element at the specified location
*/
public T get(int row, int col)
{
return (T)grid[row][col];
}
/**
* Prints the grid in a crude format
*/
public void print()
{
for (int r=0;r<grid.length;r++)
{
for (int c=0;c<grid[0].length;c++)
{
System.out.print(get(r,c)+"\t");
}
System.out.println();
}
}
/**
* Empties the grid
*/
public void empty()
{
for (int r=0;r<grid.length;r++)
{
for (int c=0;c<grid[0].length;c++)
{
set(r,c,null);
}
System.out.println();
}
}
/**
* Generates an ArrayList of objects in the grid
* @return List of objects in grid
*/
public java.util.ArrayList<T> list()
{
java.util.ArrayList<T> temp = new java.util.ArrayList<T>();
for (int r=0;r<grid.length;r++)
{
for (int c=0;c<grid[0].length;c++)
{
temp.add(get(r,c));
}
}
return temp;
}
}
import java.util.*;
/**
* Board extending the Grid class specifically for Tic-Tac-Toe
*/
public class Board extends games.Grid<Tile>
{
/**
* Constructs a new board from a certain number of rows and columns
* @param rows Number of rows
* @param cols Number of columns
*/
public Board(int rows, int cols)
{
super(rows,cols);
for (int i=0;i<rows;i++)
{
for (int j=0;j<cols;j++)
{
set(i,j,new Tile(i,j));
}
}
}
/**
* Scans for a row with a certain number of a certain tile present.
* If the number is 2, it will return a Tile where the empty space is.
* If the number is not 2, it will return a Tile of that type that is not on the board.
* @param symbol String representation of the symbol for the Tile
* @param num Number of tiles that should be in the set
* @return Tile to be returned.
*/
public Tile numPresent(String symbol, int num)
{
ArrayList<Tile> symbolList = new ArrayList<Tile>();
symbolList.add(new Tile(-1,-1));
symbolList.get(0).setText(symbol);
Tile[] temp = new Tile[3];
ArrayList<Tile> tempList;
temp[0] = get(0,0);
temp[1] = get(0,1);
temp[2] = get(0,2);
tempList = new ArrayList<Tile>(Arrays.asList(temp));
tempList.retainAll(symbolList);
if (tempList.size()==num)
{
if (num==2)
{
if (tempList.get(0).isEmpty())
return tempList.get(0);
}
else
return symbolList.get(0);
}
temp[0] = get(1,0);
temp[1] = get(1,1);
temp[2] = get(1,2);
tempList = new ArrayList<Tile>(Arrays.asList(temp));
tempList.retainAll(symbolList);
if (tempList.size()==num)
{
if (num==2)
{
if (tempList.get(0).isEmpty())
return tempList.get(0);
}
else
return symbolList.get(0);
}
temp[0] = get(2,0);
temp[1] = get(2,1);
temp[2] = get(2,2);
tempList = new ArrayList<Tile>(Arrays.asList(temp));
tempList.retainAll(symbolList);
if (tempList.size()==num)
{
if (num==2)
{
if (tempList.get(0).isEmpty())
return tempList.get(0);
}
else
return symbolList.get(0);
}
temp[0] = get(0,0);
temp[1] = get(1,0);
temp[2] = get(2,0);
tempList = new ArrayList<Tile>(Arrays.asList(temp));
tempList.retainAll(symbolList);
if (tempList.size()==num)
{
if (num==2)
{
if (tempList.get(0).isEmpty())
return tempList.get(0);
}
else
return symbolList.get(0);
}
temp[0] = get(0,1);
temp[1] = get(1,1);
temp[2] = get(2,1);
tempList = new ArrayList<Tile>(Arrays.asList(temp));
tempList.retainAll(symbolList);
if (tempList.size()==num)
{
if (num==2)
{
if (tempList.get(0).isEmpty())
return tempList.get(0);
}
else
return symbolList.get(0);
}
temp[0] = get(0,2);
temp[1] = get(1,2);
temp[2] = get(2,2);
tempList = new ArrayList<Tile>(Arrays.asList(temp));
tempList.retainAll(symbolList);
if (tempList.size()==num)
{
if (num==2)
{
if (tempList.get(0).isEmpty())
return tempList.get(0);
}
else
return symbolList.get(0);
}
temp[0] = get(0,0);
temp[1] = get(1,1);
temp[2] = get(2,2);
tempList = new ArrayList<Tile>(Arrays.asList(temp));
tempList.retainAll(symbolList);
if (tempList.size()==num)
{
if (num==2)
{
if (tempList.get(0).isEmpty())
return tempList.get(0);
}
else
return symbolList.get(0);
}
temp[0] = get(2,0);
temp[1] = get(1,1);
temp[2] = get(0,2);
tempList = new ArrayList<Tile>(Arrays.asList(temp));
tempList.retainAll(symbolList);
if (tempList.size()==num)
{
if (num==2)
{
if (tempList.get(0).isEmpty())
return tempList.get(0);
}
else
return symbolList.get(0);
}
return null;
}
/**
* Checks who the winner is.
* @return The symbol of the winner, null if there is no winner.
*/
public String winner()
{
Tile tile = numPresent("O",3);
if (tile != null)
return "O";
tile = numPresent("X",3);
if (tile != null)
return "X";
for (Tile t : list())
{
if (t.isEmpty())
return null;
}
return "D";
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Driver implements ActionListener
{
private Board board;
private String compSymbol;
private String userSymbol;
private boolean userFirst;
private JFrame frame;
private JPanel panel;
public Driver()
{
frame = new JFrame("Tic Tac Toe");
panel = new JPanel();
frame.setSize(150,150);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
board = new Board(3,3);
for (Tile t : board.list())
{
panel.add(t);
t.addActionListener(this);
}
frame.add(panel);
if ((int)(Math.random()*2)==0)
{
userFirst = false;
compSymbol = "X";
userSymbol = "O";
}
else
{
userFirst = true;
compSymbol = "O";
userSymbol = "X";
}
}
public static void main (String[] args)
{
Driver d = new Driver();
d.frame.setVisible(true);
d.frame.requestFocus();
if (!d.userFirst)
d.computerTurn();
}
public void actionPerformed(ActionEvent e)
{
Tile temp = null;
for (Tile t : board.list())
if (e.getSource() == t)
temp = t;
temp.setTempText(userSymbol);
scanWinner();
Tile temp2 = computerTurn();
scanWinner();
temp.setText(userSymbol);
if (temp2!=null)
temp2.setText(compSymbol);
}
private void scanWinner()
{
String winner = board.winner();
if (winner == null)
return;
else if (winner.equals("D"))
JOptionPane.showMessageDialog(null,"Draw.");
else if (winner.equals(userSymbol))
JOptionPane.showMessageDialog(null,"You Are Victorious!");
else
JOptionPane.showMessageDialog(null,"You have been defeated...");
System.exit(0);
}
private Tile computerTurn()
{
//Scan Dangers and place accordingly
Tile danger = board.numPresent(userSymbol,2);
if (danger!=null)
{
danger.setTempText(compSymbol);
return danger;
}
/*Unfinished code for the computer's turn here...*/
return null;
}
}
I know this is a lot of code, but I really don't know where I'm going wrong -- I am getting an error that I have never seen before so I don't really understand it. Also, the error doesn't appear when it occurs for some reason -- it seems to be waiting for me to finish the code before displaying.