This program complies but it won't run?
Anyone know why?
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class cross extends JFrame implements ActionListener
{
/**
*
*/
private static final long serialVersionUID = 1L;
private boolean gameOver; // to keep track of the game status
private String player; // to keep track of the current player
private JPanel board = new JPanel(); // to hold the nine cells
private JButton [][]cell= new JButton [3][3]; // the cells
// these next label provides a border around the cells
private JLabel blank = new JLabel(" ");
// the next two labels are centre alligned
private JLabel error = new JLabel(" ", JLabel.CENTER);
private JLabel info = new JLabel ("player X to start", JLabel.CENTER);
// the constructor
public cross()
{
gameOver = false;
player = "X"; // player X to start the game
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Noughts and Crosses");
setSize(230, 230);
setLocation(300, 100);
getContentPane().setBackground(Color.yellow);
board.setLayout(new GridLayout(3,3)); // discussed later
// creates and adds nine buttons to the board
for (int r = 0; r<3; r++)
{
for (int c=0; c<3; c++)
{
cell[r][c] = new JButton();
cell[r][c].addActionListener(this);
board.add(cell[r][c]);
}
}
// positions the items on the screen
add("Center", board);
add ("West", blank);
add("East", blank);
add("North", info);
add("South", error);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if (!gameOver)// process mouse click only if game is not over
{
for (int r = 0; r<3; r++)
{
for (int c=0; c<3; c++)
{
if (e.getSource()== cell[r][c])
{
processEvent(r,c); // call helper method to process event
}
}
}
}
}
// helper method to process a given button press
private void processEvent(int rowIn, int colIn)
{
// check no attempt made to move into an occupied cell
if (cell[rowIn][colIn].getText().equals("X") ||
cell[rowIn][colIn].getText().equals("O"))
{
error.setText("This cell already taken!!");
}
else
{
// clear any error messages
error.setText(" ");
// change button caption to current player
cell[rowIn][colIn].setText(player);
// check whether this moves results in game over
if (hasWon(rowIn, colIn))// process game over
{
info.setText(" player " + player + " has won!");
gameOver = true;
}
else // process game not over
{
// change player
if (player.equals("X"))
{
player = "O";
}
else
{
player = "X";
}
info.setText("player "+player+" to go next");
}
}
}
// helper method to check if game over
private boolean hasWon(int rowIn, int colIn)
{
boolean won;
// check current row
won = true;
for(int c = 0; c <3; c++)
{
if (!cell[rowIn][c].getText().equals(player))
{
won = false;
}
}
if (!won)
{
// check current column
won = true;
for(int r = 0; r <3; r++)
{
if (!cell[r][colIn].getText().equals(player))
{
won = false;
}
}
}
if (!won)
{
// check left diagonal
won = true;
for(int num = 0; num<3; num++)
{
if (!cell[num][num].getText().equals(player))
{
won = false;
}
}
}
if (!won)
{
// check right diagonal
won = true;
for(int c = 0; c<3; c++)
{
if (!cell[2-c][c].getText().equals(player))
{
won = false;
}
}
}
return won;
}
}

New Topic/Question
Reply



MultiQuote



|