Jump to content

How to make Tic Tac Toe with GUI's?

- - - - -

  • Please log in to reply
23 replies to this topic

#1
needhelp

needhelp

    Newbie

  • Members
  • PipPip
  • 12 posts




import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.JFrame;



public class TicTacToeGUI implements ActionListener

{

	private JFrame game = new JFrame("Tic Tac Toe");

	private JButton button1 = new JButton("");

	private JButton button2 = new JButton("");

	private JButton button3 = new JButton("");

	private JButton button4 = new JButton("");

	private JButton button5 = new JButton("");

	private JButton button6 = new JButton("");

	private JButton button7 = new JButton("");

	private JButton button8 = new JButton("");

	private JButton button9 = new JButton("");

	private JButton button10 = new JButton("Play Again");

	private JButton button11 = new JButton("Player X's Score");

	private JButton button12 = new JButton("Player O's Score");

	private String letter = "";

	private int count = 0;

	private int XWins, OWins = 0;

	private boolean win = false;



	public TicTacToeGUI()

	{

	//create game

	game.setSize(300, 300);

	game.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

	game.setLayout(new GridLayout(4, 3));

	game.setLocation(500, 300);


	//Add Buttons

	game.add(button1);

	game.add(button2);

	game.add(button3);

	game.add(button4);

	game.add(button5);

	game.add(button6);

	game.add(button7);

	game.add(button8);

	game.add(button9);

	game.add(button10);

	game.add(button11);

	game.add(button12);


	button1.addActionListener(this);

	button2.addActionListener(this);

	button3.addActionListener(this);

	button4.addActionListener(this);

	button5.addActionListener(this);

	button6.addActionListener(this);

	button7.addActionListener(this);

	button8.addActionListener(this);

	button9.addActionListener(this);

	button10.addActionListener(this);

	button11.addActionListener(this);

	button12.addActionListener(this);


	game.setVisible(true);

	}


	public void actionPerformed(ActionEvent z)

	{

		count++;


		//Who's Turn It is

		if (count == 1 || count == 3 || count == 5 || count == 7 || count == 9)

		{

			letter = "X";

		}

		else if (count == 2 || count == 4 || count == 6 || count == 8 || count == 10)

		{

			letter = "O";

		}



		//Displaying an X or O on the buttons

		if (z.getSource() == button1)

		{

			button1.setText(letter);

			button1.setEnabled(false);

		}

		else if (z.getSource() == button2)

		{

			button2.setText(letter);

			button2.setEnabled(false);

		}

		else if (z.getSource() == button3)

		{

			button3.setText(letter);

			button3.setEnabled(false);

		}

		else if (z.getSource() == button4)

		{

			button4.setText(letter);

			button4.setEnabled(false);

		}

		else if (z.getSource() == button5)

		{

			button5.setText(letter);

			button5.setEnabled(false);

		}

		else if (z.getSource() == button6)

		{

			button6.setText(letter);

			button6.setEnabled(false);

		}

		else if (z.getSource() == button7)

		{

			button7.setText(letter);

			button7.setEnabled(false);

		}

		else if (z.getSource() == button8)

		{

			button8.setText(letter);

			button8.setEnabled(false);

		}

		else if (z.getSource() == button9)

		{

			button9.setText(letter);

			button9.setEnabled(false);

		}

		else if (z.getSource() == button10)

		{

			int answer = JOptionPane.showConfirmDialog(null, "Would you like to play again?", "Confirm", JOptionPane.YES_NO_OPTION);

			if(answer == JOptionPane.NO_OPTION)

			{

				System.exit(0);

			}

			else

			{

				new TicTacToeGUI();

			}

		}

		else if (z.getSource() == button11)

		{

			JOptionPane.showMessageDialog(null, "Player X's Score" + " " + Scores());

		}

		else if (z.getSource() == button12)

		{

			JOptionPane.showMessageDialog(null, "Player O's Score" + " " );

		}


		//Checking who wins

		//Horizontal

		if (button1.getText() == button2.getText() && button2.getText() == button3.getText() && button1.getText() != "")

		{

			win = true;

		}

		else if (button4.getText() == button5.getText() && button5.getText() == button6.getText() && button4.getText() != "")

		{

			win = true;

		}

		else if (button7.getText() == button8.getText() && button8.getText() == button9.getText() && button7.getText() != "")

		{

			win = true;

		}


		//Win vertically

		else if (button1.getText() == button4.getText() && button4.getText() == button7.getText() && button1.getText() != "")

		{

			win = true;

		}

		else if (button2.getText() == button5.getText() && button5.getText() == button8.getText() && button2.getText() != "")

		{

			win = true;

		}

		else if (button3.getText() == button6.getText() && button6.getText() == button9.getText() && button3.getText() != "")

		{

			win = true;

		}


		//Win diagonally

		else if (button1.getText() == button5.getText() && button5.getText() == button9.getText() && button1.getText() != "")

		{

			win = true;

		}

		else if (button3.getText() == button5.getText() && button5.getText() == button7.getText() && button3.getText() != "")

		{

			win = true;

		}

		else

		{

			win = false;

		}


		//Show who wins or if a tie

		if (win == true)

		{

			JOptionPane.showMessageDialog(null, letter +" " + "Wins!");

		}

		else if (count == 9 && win == false)

		{

			JOptionPane.showMessageDialog(null, "It's a tie!");

		}


	}

	//track scores

	void Scores()

	{

		if(letter.equals("X"))

		{

			XWins++;

		}

		else

		{

			OWins++;

		}

	}



	public static void main(String args[])

	{

		 TicTacToeGUI tictactoeGUI = new TicTacToeGUI();

	}

}


this is my cod so far. what i am supposed to do is create a functional tic tac toe board, have a play again button, and to keep track of how many times each player has won. the board is fine, but i am unable to find a way to keep track of how many times each player has won. i thought i did, but i'm not able to implement what i have to make it work. also, when someone wins, and i click on one of the three buttons, it also says that the other play won as well. for example, if X wins, it will display the message "X Wins!" but when i click on play again, the confirmation box pops up asking if i would like to play again, and when i click yes, it will then display the message the "O Wins!" i am not sure why this is happening at all. any help/suggestions would be greatly appreciated.

#2
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
One thing that helps us is compilable code. This code doesn't compile.

After a player wins, you should disable the buttons that aren't already disabled.

Most of this code is from http://forum.codecal...ic-tac-toe.html.
I'd recommend reading the 2nd part of the tutorial and replacing the JButton calls with an array that references the JButtons.

One done, you can more easily loop through each button, check to see if it's enabled, then disable it.
If you don't want to do this, you'll have to manually check after each event to see if the game has been won yet. If it has, then you need to ignore the event.

Once this is done, after a player wins, and the user presses the play again button, I'd reset count to zero, and set all of the JButtons text back to "" (instead of new TicTacToeGUI()).
Why do this instead of TicTacoToeGUI()? You'll keep using the same panel instead of creating a new one that overlaps your old one.

The reason the O wins X wins continuously pops up is because each time you fire off an event, your actionPerformed method check to see if there is a valid win. So when you want to check scores, or play a new game, you're firing off events, and the method checks for a valid win. It finds one and prints O wins or X wins.

#3
SterAllures

SterAllures

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 203 posts
For the error that Lethalwire (and me) got, the following:

An erorr is generated at
else if (z.getSource() == button11) 
        { 
            
            JOptionPane.showMessageDialog(null, "Player X's Score" + " " + Scores()); 
        } 
        else if (z.getSource() == button12) 
        { 
            JOptionPane.showMessageDialog(null, "Player O's Score" + " " ); 
        } 

this is because your method Score() is a void method which doesn't return anything, thus it can't return anything if you call it.

I've changed the Score() method to this:

    int Scores() 
    { 
        if(letter.equals("X")) 
        { 
            XWins++; 
            return XWins;
        } 
        else 
        { 
            OWins++;
            return OWins;
        } 

This way your code will compile. It now returns an Integer, so when you call the Method it will return an int value.

Hope you sort it out!
4d 65 6c 76 69 6e 0d 0a
"If happiness was the national currency, what kind of work would make you rich?"

#4
needhelp

needhelp

    Newbie

  • Members
  • PipPip
  • 12 posts
yea, i forgot to update the thread and put the code that i have now. i just took out the scores function completely and just basically put it in the jbuttons. but i am still having a problem. when i click on the player score, i thought i fixed the problem, but it still says that the other player wins as well. i can get the original winner, but after clicking on the player score, it says the other player won. here is my updated code.



import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.JFrame;



public class TicTacToeGUI implements ActionListener

{

	private JFrame game = new JFrame("Tic Tac Toe");

	private JButton button1 = new JButton("");

	private JButton button2 = new JButton("");

	private JButton button3 = new JButton("");

	private JButton button4 = new JButton("");

	private JButton button5 = new JButton("");

	private JButton button6 = new JButton("");

	private JButton button7 = new JButton("");

	private JButton button8 = new JButton("");

	private JButton button9 = new JButton("");

	private JButton button10 = new JButton("Play Again");

	private JButton button11 = new JButton("Player X's Score");

	private JButton button12 = new JButton("Player O's Score");

	private String letter = "";

	private int count = 0;

	private int XWins, OWins = 0;

	private boolean win = false;



	public TicTacToeGUI()

	{

	//create game

	game.setSize(300, 300);

	game.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

	game.setLayout(new GridLayout(4, 3));

	game.setLocation(500, 300);


	//Add Buttons

	game.add(button1);

	game.add(button2);

	game.add(button3);

	game.add(button4);

	game.add(button5);

	game.add(button6);

	game.add(button7);

	game.add(button8);

	game.add(button9);

	game.add(button10);

	game.add(button11);

	game.add(button12);


	button1.addActionListener(this);

	button2.addActionListener(this);

	button3.addActionListener(this);

	button4.addActionListener(this);

	button5.addActionListener(this);

	button6.addActionListener(this);

	button7.addActionListener(this);

	button8.addActionListener(this);

	button9.addActionListener(this);

	button10.addActionListener(this);

	button11.addActionListener(this);

	button12.addActionListener(this);


	game.setVisible(true);

	}


	public void actionPerformed(ActionEvent z)

	{

		count++;


		//Who's Turn It is

		if (count == 1 || count == 3 || count == 5 || count == 7 || count == 9)

		{

			letter = "X";

		}

		else if (count == 2 || count == 4 || count == 6 || count == 8 || count == 10)

		{

			letter = "O";

		}




		//Displaying an X or O on the buttons

		if (z.getSource() == button1)

		{

			button1.setText(letter);

			button1.setEnabled(false);

		}

		else if (z.getSource() == button2)

		{

			button2.setText(letter);

			button2.setEnabled(false);

		}

		else if (z.getSource() == button3)

		{

			button3.setText(letter);

			button3.setEnabled(false);

		}

		else if (z.getSource() == button4)

		{

			button4.setText(letter);

			button4.setEnabled(false);

		}

		else if (z.getSource() == button5)

		{

			button5.setText(letter);

			button5.setEnabled(false);

		}

		else if (z.getSource() == button6)

		{

			button6.setText(letter);

			button6.setEnabled(false);

		}

		else if (z.getSource() == button7)

		{

			button7.setText(letter);

			button7.setEnabled(false);

		}

		else if (z.getSource() == button8)

		{

			button8.setText(letter);

			button8.setEnabled(false);

		}

		else if (z.getSource() == button9)

		{

			button9.setText(letter);

			button9.setEnabled(false);

		}

		else if (z.getSource() == button10)

		{

			int answer = JOptionPane.showConfirmDialog(null, "Would you like to play again?", "Confirm", JOptionPane.YES_NO_OPTION);

			if(answer == JOptionPane.NO_OPTION)

			{

				System.exit(0);

			}

			else

			{

				reset();

				count = 0;

			}


		}

		else if (z.getSource() == button11)

		{

			if(letter.equals("X"))

			{

				XWins++;

				JOptionPane.showMessageDialog(null, "Player X's number of Wins" + " " + XWins);

			}

		}

		else if (z.getSource() == button12)

		{

			if(letter.equals("O"))

			{

				OWins++;

				JOptionPane.showMessageDialog(null, "Player O's number of Wins" + " " + OWins);

			}

		}


		//Checking who wins

		//Horizontal

		if (button1.getText() == button2.getText() && button2.getText() == button3.getText() && button1.getText() != "")

		{

			win = true;

		}

		else if (button4.getText() == button5.getText() && button5.getText() == button6.getText() && button4.getText() != "")

		{

			win = true;

		}

		else if (button7.getText() == button8.getText() && button8.getText() == button9.getText() && button7.getText() != "")

		{

			win = true;

		}


		//Win vertically

		else if (button1.getText() == button4.getText() && button4.getText() == button7.getText() && button1.getText() != "")

		{

			win = true;

		}

		else if (button2.getText() == button5.getText() && button5.getText() == button8.getText() && button2.getText() != "")

		{

			win = true;

		}

		else if (button3.getText() == button6.getText() && button6.getText() == button9.getText() && button3.getText() != "")

		{

			win = true;

		}


		//Win diagonally

		else if (button1.getText() == button5.getText() && button5.getText() == button9.getText() && button1.getText() != "")

		{

			win = true;

		}

		else if (button3.getText() == button5.getText() && button5.getText() == button7.getText() && button3.getText() != "")

		{

			win = true;

		}

		else

		{

			win = false;

		}


		//Show who wins or if a tie

		if (win == true)

		{

			JOptionPane.showMessageDialog(null, letter +" " + "Wins!");


		}

		else if (count == 9 && win == false)

		{

			JOptionPane.showMessageDialog(null, "Tie game!!");


		}


	}


	public void reset()

	{

		button1.setText("");

		button1.setEnabled(true);

		button2.setText("");

		button2.setEnabled(true);

		button3.setText("");

		button3.setEnabled(true);

		button4.setText("");

		button4.setEnabled(true);

		button5.setText("");

		button5.setEnabled(true);

		button6.setText("");

		button6.setEnabled(true);

		button7.setText("");

		button7.setEnabled(true);

		button8.setText("");

		button8.setEnabled(true);

		button9.setText("");

		button9.setEnabled(true);

		button10.setText("Play Again");

		button10.setEnabled(true);

		button11.setText("Player X's number of Wins");

		button11.setEnabled(true);

		button12.setText("Player O's number of Wins");

		button12.setEnabled(true);

		win = false;

		count = 0;

	}


	public static void main(String args[])

	{

		 TicTacToeGUI tictactoeGUI = new TicTacToeGUI();

	}

}




#5
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
There are several occasions where your logic is wrong.


        else if (z.getSource() == button11)

        {

            if(letter.equals("X"))

            {

                XWins++;

                JOptionPane.showMessageDialog(null, "Player X's number of Wins" + " " + XWins);

            }

        }

        else if (z.getSource() == button12)

        {

            if(letter.equals("O"))

            {

                OWins++;

                JOptionPane.showMessageDialog(null, "Player O's number of Wins" + " " + OWins);

            }

        }


Here, your code states that:
Each time you hit button 11 or 12, increment X wins or O wins and display the amount of wins.
But only do this for X if the currentLetter = X or do this for Y if the currentLetter = Y.
So... If the currentLetter is X and player X keeps hitting button 11, his wins keep growing and growing.
Same for O.
To fix this, you'll need to add the X/OWins++ statement somewhere else. Also, there is no need to check if it is X's turn or O's turn, the player just wants to see a score.


If you want the X/O Wins dialog to stop popping up after a player wins, you'll need to set a boolean variable to true once you've displayed the message.
Now you can check to see if the message has been displayed and if it has, don't display it again... until a reset().

You'll also need to move your count++ statement somewhere else. There is a bug where if Player X chooses a square, then presses button 11, they the turn changes back to Player X without Player O even making a move.

#6
needhelp

needhelp

    Newbie

  • Members
  • PipPip
  • 12 posts
I must be doing something wrong then. because even after i added the code for the X/O Wins++ somewhere else, i'm still getting the same result. i did this instead. and it was still having the same problem.



else if (z.getSource() == button11){

		{

			JOptionPane.showMessageDialog(null, "Player X's number of Wins" + " " + Scores());

		}

		else if (z.getSource() == button12)

		{

			JOptionPane.showMessageDialog(null, "Player O's number of Wins" + " " + Scores());

		}

}


int Scores()

	{

		if(letter.equals("X"))

		{

			XWins++;

			return XWins;

		}

		else

		{

			OWins++;

			return OWins;

		}

	}


and i also tried having the boolean variable there as well, but still the same problem was occurring. I know I'm doing something wrong, I just don't know exactly what i am doing wrong.

I'm not sure where else to put the count++ statement. i was aware of that problem, but that had slipped my mind when i wrote the thread here.

#7
SterAllures

SterAllures

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 203 posts
I would give points the same time you check for win == true
if (win == true) 
Within that statement I would add to X or O scores. then when you want to present the score just call XWins or getXScore()

that way you don't need the Scores() method because you can just call the int or call it through a getter.

I also don't fully understand what you're doing here:

else if (z.getSource() == button11)         { 
            if(letter.equals("X")) 
            { 
            }
        }

you already got an ActionListener on the button itself, so I think you don't need an extra check for "X" or "O"
4d 65 6c 76 69 6e 0d 0a
"If happiness was the national currency, what kind of work would make you rich?"

#8
needhelp

needhelp

    Newbie

  • Members
  • PipPip
  • 12 posts
i am not fully seeing how i would add to X or O scores win the if(win == true). Maybe it's because i'm really tired, i'm not sure, but i'm just not seeing how i would add that part in. for the part that you were having a hard time with, i replaced that part of the code with what I had posted in the post that you had responded to. to make things easier, I will repost the rest of the code that im working with right now.




import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.JFrame;



public class TicTacToeGUI implements ActionListener

{

	private JFrame game = new JFrame("Tic Tac Toe");

	private JButton button1 = new JButton("");

	private JButton button2 = new JButton("");

	private JButton button3 = new JButton("");

	private JButton button4 = new JButton("");

	private JButton button5 = new JButton("");

	private JButton button6 = new JButton("");

	private JButton button7 = new JButton("");

	private JButton button8 = new JButton("");

	private JButton button9 = new JButton("");

	private JButton button10 = new JButton("Play Again");

	private JButton button11 = new JButton("Player X's Score");

	private JButton button12 = new JButton("Player O's Score");

	private String letter = "";

	private int count = 0;

	private int XWins, OWins = 0;

	private boolean win = false;



	public TicTacToeGUI()

	{

	//create game

	game.setSize(300, 300);

	game.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

	game.setLayout(new GridLayout(4, 3));

	game.setLocation(500, 300);


	//Add Buttons

	game.add(button1);

	game.add(button2);

	game.add(button3);

	game.add(button4);

	game.add(button5);

	game.add(button6);

	game.add(button7);

	game.add(button8);

	game.add(button9);

	game.add(button10);

	game.add(button11);

	game.add(button12);


	button1.addActionListener(this);

	button2.addActionListener(this);

	button3.addActionListener(this);

	button4.addActionListener(this);

	button5.addActionListener(this);

	button6.addActionListener(this);

	button7.addActionListener(this);

	button8.addActionListener(this);

	button9.addActionListener(this);

	button10.addActionListener(this);

	button11.addActionListener(this);

	button12.addActionListener(this);


	game.setVisible(true);

	}


	public void actionPerformed(ActionEvent z)

	{

		count++;


		//Who's Turn It is

		if (count == 1 || count == 3 || count == 5 || count == 7 || count == 9)

		{

			letter = "X";

		}

		else if (count == 2 || count == 4 || count == 6 || count == 8 || count == 10)

		{

			letter = "O";

		}




		//Displaying an X or O on the buttons

		if (z.getSource() == button1)

		{

			button1.setText(letter);

			button1.setEnabled(false);

		}

		else if (z.getSource() == button2)

		{

			button2.setText(letter);

			button2.setEnabled(false);

		}

		else if (z.getSource() == button3)

		{

			button3.setText(letter);

			button3.setEnabled(false);

		}

		else if (z.getSource() == button4)

		{

			button4.setText(letter);

			button4.setEnabled(false);

		}

		else if (z.getSource() == button5)

		{

			button5.setText(letter);

			button5.setEnabled(false);

		}

		else if (z.getSource() == button6)

		{

			button6.setText(letter);

			button6.setEnabled(false);

		}

		else if (z.getSource() == button7)

		{

			button7.setText(letter);

			button7.setEnabled(false);

		}

		else if (z.getSource() == button8)

		{

			button8.setText(letter);

			button8.setEnabled(false);

		}

		else if (z.getSource() == button9)

		{

			button9.setText(letter);

			button9.setEnabled(false);

		}

		else if (z.getSource() == button10)

		{

			int answer = JOptionPane.showConfirmDialog(null, "Would you like to play again?", "Confirm", JOptionPane.YES_NO_OPTION);

			if(answer == JOptionPane.NO_OPTION)

			{

				System.exit(0);

			}

			else

			{

				reset();

				count = 0;

			}


		}

		else if (z.getSource() == button11)

		{

			JOptionPane.showMessageDialog(null, "Player X's number of Wins" + " " + Scores());

		}

		else if (z.getSource() == button12)

		{

			JOptionPane.showMessageDialog(null, "Player O's number of Wins" + " " + Scores());

		}


		//Checking who wins

		//Horizontal

		if (button1.getText() == button2.getText() && button2.getText() == button3.getText() && button1.getText() != "")

		{

			win = true;

		}

		else if (button4.getText() == button5.getText() && button5.getText() == button6.getText() && button4.getText() != "")

		{

			win = true;

		}

		else if (button7.getText() == button8.getText() && button8.getText() == button9.getText() && button7.getText() != "")

		{

			win = true;

		}


		//Win vertically

		else if (button1.getText() == button4.getText() && button4.getText() == button7.getText() && button1.getText() != "")

		{

			win = true;

		}

		else if (button2.getText() == button5.getText() && button5.getText() == button8.getText() && button2.getText() != "")

		{

			win = true;

		}

		else if (button3.getText() == button6.getText() && button6.getText() == button9.getText() && button3.getText() != "")

		{

			win = true;

		}


		//Win diagonally

		else if (button1.getText() == button5.getText() && button5.getText() == button9.getText() && button1.getText() != "")

		{

			win = true;

		}

		else if (button3.getText() == button5.getText() && button5.getText() == button7.getText() && button3.getText() != "")

		{

			win = true;

		}

		else

		{

			win = false;

		}


		//Show who wins or if a tie

		if (win == true)

		{

			JOptionPane.showMessageDialog(null, letter +" " + "Wins!");

			boolean win = true;

		}

		else if (count == 9 && win == false)

		{

			JOptionPane.showMessageDialog(null, "Tie game!!");


		}


	}


	int Scores()

	{

		if(letter.equals("X"))

		{

			XWins++;

			return XWins;

		}

		else

		{

			OWins++;

			return OWins;

		}

	}


	public void reset()

	{

		button1.setText("");

		button1.setEnabled(true);

		button2.setText("");

		button2.setEnabled(true);

		button3.setText("");

		button3.setEnabled(true);

		button4.setText("");

		button4.setEnabled(true);

		button5.setText("");

		button5.setEnabled(true);

		button6.setText("");

		button6.setEnabled(true);

		button7.setText("");

		button7.setEnabled(true);

		button8.setText("");

		button8.setEnabled(true);

		button9.setText("");

		button9.setEnabled(true);

		button10.setText("Play Again");

		button10.setEnabled(true);

		button11.setText("Player X's number of Wins");

		button11.setEnabled(true);

		button12.setText("Player O's number of Wins");

		button12.setEnabled(true);

		win = false;

		count = 0;

	}


	public static void main(String args[])

	{

		 TicTacToeGUI tictactoeGUI = new TicTacToeGUI();

	}

}




#9
SterAllures

SterAllures

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 203 posts
I added this to the statement:

if(letter.equalsIgnoreCase("X"))
            {
            	XWins++;
            }
            else
            {
            	OWins++;
            }

Don't know if this is the correct way to do it. I didn't look over the code fully because I'm kind of in a hurry. Will look over it tonight and try figure the other problems out :).

Try to get some sleep ;)
4d 65 6c 76 69 6e 0d 0a
"If happiness was the national currency, what kind of work would make you rich?"

#10
needhelp

needhelp

    Newbie

  • Members
  • PipPip
  • 12 posts
where would that statement go. i can test it out, but i'm trying to figure out if there were to go in the if(win == true) statement or if that goes somewhere else.

i would sleep, but i still had a days worth of classes to still go through

#11
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
Here is where you check for a win:

        if (win == true)

        {

            JOptionPane.showMessageDialog(null, letter +" " + "Wins!");

            boolean win = true;

        }

        else if (count == 9 && win == false)

        {

            JOptionPane.showMessageDialog(null, "Tie game!!");


        } 
So here you need to increment wins for X or O depending on who won.
This is also where the annoying "X wins" or "O Wins" message comes from. So we need to setup a boolean that can control this message.
How? If this message has been displayed, don't show it again!


... somewhere with your field variables

boolean messageDisplayed = false;


        if (win == true)

        {

        	if(!displayedMessage) {

	            JOptionPane.showMessageDialog(null, letter +" " + "Wins!");

	            displayedMessage = true;

	            if(letter.equals("X"))

	            	XWins++;

	            else

	            	OWins++;

        	}


        }
Don't forget the tie message...

        else if (count == 9 && win == false)

        {

        	if(!displayedMessage) {

	            JOptionPane.showMessageDialog(null, "Tie game!!");

	            displayedMessage = true;

        	}

        }
And don't forget to set messageDisplayed to false in your reset() method.

As for count++, you'll need to add it every if{...} where getSource == button[1-9]...
I'm thinking you can also condense the if's to a bunch of if(getSource == || getSource == || getSource ==...
It's nasty coding either way... which is why I originally told you to keep following the tic tac toe tutorial and change all of your JButtons into a JButton[] array.

#12
needhelp

needhelp

    Newbie

  • Members
  • PipPip
  • 12 posts
ok, thank you very much for your help. that did solve the annoying pop up message. but now i'm running into a few more problems. when i click on the button for the scores, it already says that that player already has one win, even if a game has not been played. ive been trying to figure that out, but i cant seem to think of how to set it to 0. and also, is there any way to freeze/disable any unclicked buttons on the actual 3x3 playing grid? for example, lets say that player X wins in 5 moves. then there are still buttons/tiles that have not been clicked. is there any way to disable those buttons without affecting the last row of buttons?




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users