Tic Tac Toe Menu Problems

  • (5 Pages)
  • +
  • « First
  • 3
  • 4
  • 5

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

#61 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10180
  • View blog
  • Posts: 37,585
  • Joined: 27-December 08

Re: Tic Tac Toe Menu Problems

Posted 14 April 2011 - 04:39 PM

Two things. First, post your revised code. Second, properly indent your code so we can read it. Otherwise, we are the blind leading the blind.
Was This Post Helpful? 1
  • +
  • -

#62 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 14 April 2011 - 04:47 PM

I just posted my code and the error/problem and still seems the Clear isnt working. On Page 4
Was This Post Helpful? -2
  • +
  • -

#63 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 14 April 2011 - 05:08 PM

Compiles but doesnt work..any idea why?

clearAction.addActionListener(new ActionListener(){
	public void actionPerformed(ActionEvent g){
		String arg= g.getActionCommand();{
		if (arg.equals("Clear")){
		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].setLabel("");

Was This Post Helpful? 0
  • +
  • -

#64 pbl  Icon User is offline

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

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Tic Tac Toe Menu Problems

Posted 14 April 2011 - 07:54 PM

This is coming RIDICULOUS !!!
63 replies for a simple actionPerformed() method
Nobody will pass through all of them: nothing to learn anyhow

and despite multiple warnings you still do not correctly indent your code

button[i] = new TicTacToeButton[3];

NO !! you buttons already exist why creating new one

You should seriously think of re-orienting your carreer... no offense :)

This post has been edited by pbl: 14 April 2011 - 07:55 PM

Was This Post Helpful? 0
  • +
  • -

#65 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 14 April 2011 - 08:11 PM

Ok well with anybody's help i added a few things and the "Clear" works but know need it to reset all the variables after it clears, which would happen in this block of code.

	clearAction.addActionListener(new ActionListener(){
	public void actionPerformed(ActionEvent g){
	String arg= g.getActionCommand();{
	if (arg.equals("Clear")){
	for (TicTacToeButton current : allButtons){
	current.setLabel(" ");
	playerTurn.setText("");
				}
			}
		}
	}
});





import java.awt.*;
import java.awt.event.*;
import java.awt.font.*;
import javax.swing.*;
import java.util.ArrayList;////////added
//import java.text.*;

public class tictactoe extends Frame{


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
public static ArrayList<TicTacToeButton> allButtons= new ArrayList<TicTacToeButton>();//////added



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 clearAction = new MenuItem("Clear");
MenuItem aboutAction = new MenuItem("About");
MenuItem exitAction = new MenuItem("Exit");

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



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

	aboutAction.addActionListener(new ActionListener(){
	public void actionPerformed(ActionEvent f){
	String arg= f.getActionCommand();{
	if (arg.equals("About")){
	String message = " Shawn's TicTacToe \n Structured Programming Java";
	JOptionPane.showMessageDialog(null,message,
	"About Tic Tac Toe", JOptionPane.INFORMATION_MESSAGE);
			}
		}
	}
});

	clearAction.addActionListener(new ActionListener(){
	public void actionPerformed(ActionEvent g){
	String arg= g.getActionCommand();{
	if (arg.equals("Clear")){
	for (TicTacToeButton current : allButtons){//////added
	current.setLabel(" ");//////added
	playerTurn.setText("");/////added
				}
			}
		}
	}
});


/* 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();
	allButtons.add(button[i][j]);////////added



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



/* 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*/
	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 = "";


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")){
	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();
	}
}

Was This Post Helpful? -1
  • +
  • -

#66 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 14 April 2011 - 08:22 PM

Just wanted to say good luck because it doesn't appear I will be here much longer. I guess I am a really bad troll or something.

Can you define reset and how you want this to show up on the screen?
Was This Post Helpful? 0
  • +
  • -

#67 pbl  Icon User is offline

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

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Tic Tac Toe Menu Problems

Posted 14 April 2011 - 08:22 PM

You are complicating your life a lot
Make an array of int[3][3] in parallel (sorry macosxnerd101) with your button[] array
Put 10 in it when O and 100 when X
If the sum of a column/row/diagonal is 300 the X won
If the sum of a column/row/diagonal is 30 the O won

no need to make all your comparaisons
Was This Post Helpful? 0
  • +
  • -

#68 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 14 April 2011 - 08:28 PM

I am confused as will this help reset my variables after the board is cleared?
Even though clear won't really help in this situation because I am removing the ActionListener:
Ugg all I want is for the board to reset all variables after someone has won.

for (int i = 0; i < 3; i++) {
	for (int j = 0; j < 3; j++) {
	button[i][j].removeActionListener(bH);
	}

Was This Post Helpful? 0
  • +
  • -

#69 CodeNameNick  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 15-April 11

Re: Tic Tac Toe Menu Problems

Posted 15 April 2011 - 06:23 AM

You should really take pbl's idea about using an array of ints to hold your game board spot values.

Then you could just leave the array of buttons out when clearing the board. You would just be getting the values of the ints to change the labels of the buttons.

For example, as pbl said, whenever the user wants an x, you fill that spot in the array with 100. I would try to figure out how to make it so the buttons read the values from the int array. That way you wouldn't need to have any clearing of the butttons, just their values in the int array.

Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#70 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 15 April 2011 - 10:04 AM

Completed, but having a problem now with draw
Was This Post Helpful? 0
  • +
  • -

#71 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 15 April 2011 - 10:29 AM

Done
Was This Post Helpful? 0
  • +
  • -

#72 pbl  Icon User is offline

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

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Tic Tac Toe Menu Problems

Posted 15 April 2011 - 08:09 PM

View PostBig Bundy, on 15 April 2011 - 12:29 PM, said:

Done

Congratulations
Was This Post Helpful? 1
  • +
  • -

  • (5 Pages)
  • +
  • « First
  • 3
  • 4
  • 5