Tic Tac Toe Menu Problems

  • (5 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »

71 Replies - 3603 Views - Last Post: 15 April 2011 - 08:09 PM Rate Topic: ***-- 2 Votes

#1 Big Bundy  Icon User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • Posts: 43
  • Joined: 04-April 11

Tic Tac Toe Menu Problems

Posted 13 April 2011 - 03:10 PM

Everything works wonderful but having problems with a certain part of the code that allows me to exit when I click the MenuItem (exit) nothing happens. I believe it has to do with the actionlistener but I cant seem to figure it out. Anybody who looks over my code maybe can see whats wrong?

import java.awt.*;
import java.awt.event.*;
import java.awt.font.*;
import javax.swing.*;
import java.text.*;

public class tictactoe extends Frame{

//Color red = new Color(153,255,255);;
Font font = new Font("Dialog", Font.BOLD, 18);
Font font2 = new Font("Dialog", Font.ITALIC, 20);
String turn = "O"; //value is either "O" or "X"
Panel board;
TicTacToeButton button[][];
Label playerTurn;
ButtonHandler bH = new ButtonHandler();
int usedCells = 0; //number of cells in use

tictactoe(String title) {
super(title);
board = new Panel();
button = new TicTacToeButton[3][];
playerTurn = new Label("Player 1's Turn");
playerTurn.setFont(font2);
playerTurn.setForeground(Color.white);
}

void launchGame() {
setBackground(Color.red);
board.setLayout(new GridLayout(3, 3, 6, 6));

//setMenuBar//
MenuBar menuBar = new MenuBar();
setMenuBar(menuBar);

Menu fileMenu = new Menu("File");
menuBar.add(fileMenu);

MenuItem newAction = new MenuItem("New");
MenuItem aboutAction = new MenuItem("About");
MenuItem exitAction = new MenuItem("Exit");

fileMenu.add(newAction);
fileMenu.add(aboutAction);
fileMenu.add(exitAction);


//newAction.addActionListener(this);
//aboutAction.addActionListener(this);
//exitAction.addActionListener(this);

newAction.setActionCommand("New");
aboutAction.setActionCommand("About");
exitAction.setActionCommand("Exit");


/* initialize buttons */
for (int i = 0, count = 0; i < 3; i++) {
button[i] = new TicTacToeButton[3];
for (int j = 0; j < 3; j++, count++) {
button[i][j] = new TicTacToeButton();

/* inialize button values to 0,1, 2,...,8 */
button[i][j].value = (new Integer(count)).toString();
board.add(button[i][j]);

/* add listeners to the buttons */
button[i][j].addActionListener(bH);
button[i][j].setFont(font);
button[i][j].setForeground(Color.green);
button[i][j].setBackground(Color.black);

}
}

add(board);
board.setSize(300,300);
add(playerTurn, BorderLayout.SOUTH);


/* add listeners for closing the frame */
addWindowListener( new WindowAdapter() {
public void windowClosing( WindowEvent event ) {
System.exit( 0 );
}
} );

/* display */
pack();
setVisible(true);
}

class ButtonHandler implements ActionListener {
void changeButtonLabel(TicTacToeButton B)/> {
b.setLabel(turn);
b.value = turn;

/* remove listener */
b.removeActionListener(bH);
usedCells++;
}


public void actionPerformed(ActionEvent ae) {


boolean win = false;
boolean draw = false;
int player = 0;
TicTacToeButton b = (TicTacToeButton) ae.getSource();

/* change content of button to O or X */
changeButtonLabel(B)/>;

/* check for a win */
/* horizontal */
for (int i = 0; i < 3; i++) {
if ((button[i][0].value).equals(button[i][1].value) &&
(button[i][0].value).equals(button[i][2].value)) {
win = true;
}
}

/* vertical */
for (int i = 0; i < 3; i++) {
if ((button[0][i].value).equals(button[1][i].value) &&
(button[0][i].value).equals(button[2][i].value)) {
win = true;
}
}

/* diagonal */
if ((button[0][0].value).equals(button[1][1].value) &&
(button[0][0].value).equals(button[2][2].value)) {
win = true;
} else if ((button[0][2].value).equals(button[1][1].value) &&
(button[0][2].value).equals(button[2][0].value)) {
win = true;
}

/* check for a draw */
if (!win) {
if (usedCells==9) {
draw = true;
}
}

/* Change message */
String message = "";

//continued on next page
if (win) {
if (turn.equals("O")) {
player = 1;
} else {
player = 2;
}
message += "Player " + player + " wins!";

/* remove all listeners */
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
button[i][j].removeActionListener(bH);
}
}
} else if (draw) {
message += "It's a draw!";
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
button[i][j].removeActionListener(bH);
}
}
} else {

/* change turn */
if (turn.equals("O")){
//setBackground(Color.red);
turn = new String("X");
player = 2;
message += "Player 2's Turn";
} else {
turn = new String("O");
player = 1;
message += "Player 1's Turn";
}
}
playerTurn.setText(message);
}
}

class TicTacToeButton extends Button {
String value;
}



public static void main(String args[]) {
tictactoe game = new tictactoe("Tic-Tac-Toe");
game.launchGame();
}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Tic Tac Toe Menu Problems

#2 softwareEngineer()  Icon User is offline

  • D.I.C Head

Reputation: -28
  • View blog
  • Posts: 247
  • Joined: 08-April 11

Re: Tic Tac Toe Menu Problems

Posted 13 April 2011 - 03:16 PM

I see that you add an action listener, but you never call an action performed method on it.

This is why nothing is happening; you have not set it to do anything.
Was This Post Helpful? 0
  • +
  • -

#3 Big Bundy  Icon User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • Posts: 43
  • Joined: 04-April 11

Re: Tic Tac Toe Menu Problems

Posted 13 April 2011 - 06:13 PM

Thanks, and whereabouts would I add this Action Performed method?
Was This Post Helpful? 0
  • +
  • -

#4 Big Bundy  Icon User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • Posts: 43
  • Joined: 04-April 11

Re: Tic Tac Toe Menu Problems

Posted 13 April 2011 - 06:20 PM

A little further down the code I have an ActionPerformed Method already in my code and I added:

public void actionPerformed(ActionEvent ae) {
	String arg = ae.getActionCommand();{
		if (arg.equals("Exit")){
		System.exit(0);
}
}



But this doesnt seem to do anything? It compiles but nothing happens..what am I doing wrong?
Was This Post Helpful? 0
  • +
  • -

#5 softwareEngineer()  Icon User is offline

  • D.I.C Head

Reputation: -28
  • View blog
  • Posts: 247
  • Joined: 08-April 11

Re: Tic Tac Toe Menu Problems

Posted 13 April 2011 - 06:39 PM

You would add this action listener - action performed according to where you want your action to be performed. It would be smart to do it right after where you declared the action listeners.

If you need syntax help, let me know.
Was This Post Helpful? 0
  • +
  • -

#6 Big Bundy  Icon User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • Posts: 43
  • Joined: 04-April 11

Re: Tic Tac Toe Menu Problems

Posted 13 April 2011 - 07:24 PM

Yes, syntax help would be great. By the way the ActionListeners for each I have remed out because they do not work i get a few errors.

This post has been edited by Big Bundy: 13 April 2011 - 07:27 PM

Was This Post Helpful? 0
  • +
  • -

#7 softwareEngineer()  Icon User is offline

  • D.I.C Head

Reputation: -28
  • View blog
  • Posts: 247
  • Joined: 08-April 11

Re: Tic Tac Toe Menu Problems

Posted 13 April 2011 - 07:26 PM

You could declare your menuItems and then add this code to whichever item:

        MENUITEM.addActionListener(new ActionListener()
        {
 
            public void actionPerformed(ActionEvent e)
            {
                //PLACE YOUR ACTION HERE
                System.exit(0);
                
            }
        });


This post has been edited by softwareEngineer(): 13 April 2011 - 07:27 PM

Was This Post Helpful? 1
  • +
  • -

#8 Big Bundy  Icon User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • Posts: 43
  • Joined: 04-April 11

Re: Tic Tac Toe Menu Problems

Posted 13 April 2011 - 07:37 PM

Ok I added a few things and the Exit works. Now do you have any idea on how to clear the board?

This post has been edited by Big Bundy: 13 April 2011 - 07:41 PM

Was This Post Helpful? 0
  • +
  • -

#9 softwareEngineer()  Icon User is offline

  • D.I.C Head

Reputation: -28
  • View blog
  • Posts: 247
  • Joined: 08-April 11

Re: Tic Tac Toe Menu Problems

Posted 13 April 2011 - 07:42 PM

Well, the second error means that you have one too many closing curly brackets (}).

The first one I cannot help you with since the line number that the compiler gives is not in the code you posted.

Can you find the line number on the code you just posted so I can help?
Was This Post Helpful? 0
  • +
  • -

#10 Big Bundy  Icon User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • Posts: 43
  • Joined: 04-April 11

Re: Tic Tac Toe Menu Problems

Posted 13 April 2011 - 07:54 PM

Fixed the Exit thanks :). Now how would I begin to start a clear or reset button most likely similar to the exit one? Last thing I really need help with.
Was This Post Helpful? 0
  • +
  • -

#11 softwareEngineer()  Icon User is offline

  • D.I.C Head

Reputation: -28
  • View blog
  • Posts: 247
  • Joined: 08-April 11

Re: Tic Tac Toe Menu Problems

Posted 13 April 2011 - 08:04 PM

ou are asking about clearing the game board, no?

Well in this case, use the same format to add an action listener as before, but now where you put System.exit();

Put all of your symbols as null. For example, if all of your X's were a JLabel, you would set it to equal null along with the other game symbols.
Was This Post Helpful? 0
  • +
  • -

#12 Big Bundy  Icon User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • Posts: 43
  • Joined: 04-April 11

Re: Tic Tac Toe Menu Problems

Posted 13 April 2011 - 08:17 PM

Sorry something doesnt look right. Am I putting the right things in order? Still having problems when you talk about the X's and O's because I didn't use any JLabel. Any advice regarding my code?

exitAction.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
   {
String arg = e.getActionCommand();{
if (arg.equals("Exit")){
System.exit(0);
}
}
}
});
clearAction.addActionListener(new ActionListener()
{
String arg= e.getActionCommand();{
if (arg.equals("Clear")){

This post has been edited by Big Bundy: 13 April 2011 - 08:32 PM

Was This Post Helpful? 0
  • +
  • -

#13 softwareEngineer()  Icon User is offline

  • D.I.C Head

Reputation: -28
  • View blog
  • Posts: 247
  • Joined: 08-April 11

Re: Tic Tac Toe Menu Problems

Posted 13 April 2011 - 08:43 PM

Unfortunately I am on a mobile device so I cannot quite see your code.

PM me it in plain text and then check back here with my response.

I will be able to help you then.
Was This Post Helpful? 0
  • +
  • -

#14 macosxnerd101  Icon User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12222
  • View blog
  • Posts: 45,296
  • Joined: 27-December 08

Re: Tic Tac Toe Menu Problems

Posted 13 April 2011 - 08:50 PM

First, start by properly indenting your code. It will make it much easier for you, as well as others, to read and maintain.

Since you're using anonymous listeners, you do not need a getSource() or getActionCommand() comparison. You are associating the Listeners with unique JComponents. Also, this extra brace at the end of this line is unnecessary: String arg = e.getActionCommand();{.
Was This Post Helpful? 0
  • +
  • -

#15 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Tic Tac Toe Menu Problems

Posted 13 April 2011 - 08:56 PM

Indent your code properly, it is a nightmare to read
Discourage us to help you
Was This Post Helpful? 0
  • +
  • -

  • (5 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »