Creating Tic Tac Toe GUI game.
Okay so can someone please help fix my code for Tic Tac Toe i have no idea how to! =/
I'm new to programming so help would be appreciated..
thanx.
<code>
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TicTacToe implements ActionListener {
/*Instance Variables*/
private JFrame window = 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 String letter = "";
private ImageIcon O = new ImageIcon("LET-O.jpg");
private ImageIcon X = new ImageIcon("LET-X.jpg");
private int count = 0;
private boolean win = false;
public TicTacToe(){
/*Create Window*/
window.setSize(300,300);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLayout(new GridLayout(3,3));
/*Add Buttons To The Window*/
window.add(button1);
window.add(button2);
window.add(button3);
window.add(button4);
window.add(button5);
window.add(button6);
window.add(button7);
window.add(button8);
window.add(button9);
/*Add The Action Listener To The Buttons*/
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);
/*Make The Window Visible*/
window.setVisible(true);
public void actionPerformed(ActionEvent e) {
count++;
/*Calculate Who's Turn It Is*/
if(count == 1 || count == 3 || count == 5 || count == 7 || count == 9)
X = new ImageIcon("LET-X.jpg");
else if(count == 2 || count == 4 || count == 6 || count == 8 || count == 10)
O = new ImageIcon("LET-O.jpg");
/*Display X's or O's on the buttons*/
if(e.getSource() == button1){
button1.setIcon();
button1.setEnabled(false);
} else if(e.getSource() == button2){
button2.setIcon();
button2.setEnabled(false);
} else if(e.getSource() == button3){
button3.setIcon();
button3.setEnabled(false);
} else if(e.getSource() == button4){
button4.setIcon();
button4.setEnabled(false);
} else if(e.getSource() == button5){
button5.setIcon();
button5.setEnabled(false);
} else if(e.getSource() == button6){
button6.setIcon();
button6.setEnabled(false);
} else if(e.getSource() == button7){
button7.setIcon();
button7.setEnabled(false);
} else if(e.getSource() == button8){
button8.setIcon();
button8.setEnabled(false);
} else if(e.getSource() == button9){
button9.setIcon();
button9.setEnabled(false);
//Determine who won
//horizontal wins
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;
//vertical wins
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;
//diagonal wins
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 a dialog if someone wins or the game is tie*/
if(win == true)
JOptionPane.showMessageDialog(null, letter + " WINS!");
else if(count == 9 && win == false)
JOptionPane.showMessageDialog(null, "Tie Game!");
public static void main(String[] args)
new TicTacToe();
</code>
Yeah Thanks.
So I'm just stuck here. Like I need to program a Tic Tac Toe GUI with the X's and O's as images but I have no clue on what to do next. I have the images for both but I don't know how to make them work once the buttons are clicked.
Sorry here it is again..
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TicTacToe implements ActionListener {
/*Instance Variables*/
private JFrame window = 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 String letter = "";
private ImageIcon O = new ImageIcon("LET-O.jpg");
private ImageIcon X = new ImageIcon("LET-X.jpg");
private int count = 0;
private boolean win = false;
public TicTacToe(){
/*Create Window*/
window.setSize(300,300);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLayout(new GridLayout(3,3));
/*Add Buttons To The Window*/
window.add(button1);
window.add(button2);
window.add(button3);
window.add(button4);
window.add(button5);
window.add(button6);
window.add(button7);
window.add(button8);
window.add(button9);
/*Add The Action Listener To The Buttons*/
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);
/*Make The Window Visible*/
window.setVisible(true);
public void actionPerformed(ActionEvent e) {
count++;
/*Calculate Who's Turn It Is*/
if(count == 1 || count == 3 || count == 5 || count == 7 || count == 9)
X = new ImageIcon("LET-X.jpg");
else if(count == 2 || count == 4 || count == 6 || count == 8 || count == 10)
O = new ImageIcon("LET-O.jpg");
/*Display X's or O's on the buttons*/
if(e.getSource() == button1){
button1.setIcon();
button1.setEnabled(false);
} else if(e.getSource() == button2){
button2.setIcon();
button2.setEnabled(false);
} else if(e.getSource() == button3){
button3.setIcon();
button3.setEnabled(false);
} else if(e.getSource() == button4){
button4.setIcon();
button4.setEnabled(false);
} else if(e.getSource() == button5){
button5.setIcon();
button5.setEnabled(false);
} else if(e.getSource() == button6){
button6.setIcon();
button6.setEnabled(false);
} else if(e.getSource() == button7){
button7.setIcon();
button7.setEnabled(false);
} else if(e.getSource() == button8){
button8.setIcon();
button8.setEnabled(false);
} else if(e.getSource() == button9){
button9.setIcon();
button9.setEnabled(false);
//Determine who won
//horizontal wins
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;
//vertical wins
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;
//diagonal wins
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 a dialog if someone wins or the game is tie*/
if(win == true)
JOptionPane.showMessageDialog(null, letter + " WINS!");
else if(count == 9 && win == false)
JOptionPane.showMessageDialog(null, "Tie Game!");
public static void main(String[] args)
new TicTacToe();
}
Similar Messages
-
Get blank screen each time? tic tac toe GUI...
I get a blank screen each time i run this...I'm trying to display a tic tac toe board...the x's and o's are randomly generated...any idea why it's blank each time??
public class TicTacToe extends JFrame
private ImageIcon x = new ImageIcon("cross.gif");
private ImageIcon o = new ImageIcon("not.gif");
Random random = new Random(2);
public TicTacToe()
setLayout(new GridLayout(3,3));
for(int y = 0; y<= 9; y++)
Random r = new Random(2);
int randint = r.nextInt();
if (randint == 0)
add(new JLabel(x));
if (randint == 2)
add(new JLabel(o));
public static void main(String args[])
TicTacToe frame = new TicTacToe();
frame.setTitle("Tic Tac Toe");
frame.setSize(450,450);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}Timm017 wrote:
Interested, what was wrong?He used Random and random.nextInt() incorrectly. Given that nextInt() without arguments can return any int between Integer.MIN_VALUE and Integer.MAX_VALUE with equal probability, the chance of hitting 0 or 2 is fairly low. So, most times (for most Random seeds), his code would never add any buttons to the panel. And, actually, since he used a '2' to seed the Random generator, he always got the same sequence of "random" numbers every time. So, his code would always give the same 9 numbers, and, apparently, those numbers don't include either 0 or 2.
By the way, rollarace, besides picking the proper nextInt(...) method [you want the one that requires a parameter, as I assume you figured out], you should create the Random object once BEFORE the loop starts, and then just reuse it.
Also:
for(int y = 0; y<= 9; y++)should be:
for(int y = 0; y< 9; y++)Otherwise, once the nextInt(...) part was fixed, he'd add 10 buttons. -
Hey can someone please tell me how to hide the text in my code so that when I run it, the images only appear no text.
thanks.
Here's what I have so far..
public void actionPerformed(ActionEvent a) {
count++;
/*Calculate Who's Turn It Is*/
if(count == 1 || count == 3 || count == 5 || count == 7 || count == 9){
letter = "X";
Icon = new ImageIcon("LET-X.jpg");
} else if(count == 2 || count == 4 || count == 6 || count == 8 || count == 10){
letter = "O";
Icon = new ImageIcon("LET-O.jpg");
/*Display X's or O's on the buttons*/
if(a.getSource() == button1){
button1.setText(letter);
button1.setIcon(Icon);
button1.setEnabled(false);
} else if(a.getSource() == button2){
button2.setText(letter);
button2.setIcon(Icon);
button2.setEnabled(false);
} else if(a.getSource() == button3){
button3.setText(letter);
button3.setIcon(Icon);
button3.setEnabled(false);
} else if(a.getSource() == button4){
button4.setText(letter);
button4.setIcon(Icon);
button4.setEnabled(false);
} else if(a.getSource() == button5){
button5.setText(letter);
button5.setIcon(Icon);
button5.setEnabled(false);
} else if(a.getSource() == button6){
button6.setText(letter);
button6.setIcon(Icon);
button6.setEnabled(false);
} else if(a.getSource() == button7){
button7.setText(letter);
button7.setIcon(Icon);
button7.setEnabled(false);
} else if(a.getSource() == button8){
button8.setText(letter);
button8.setIcon(Icon);
button8.setEnabled(false);
} else if(a.getSource() == button9){
button9.setText(letter);
button9.setIcon(Icon);
button9.setEnabled(false);
/*Determine who won*/
//horizontal wins
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;
//virticle wins
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;
//diagonal wins
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;
}Yeah but then it messes up the whole code. It won't determine who wins if I stop calling the setText method.
Is there a better way to code this??
public void actionPerformed(ActionEvent a) {
count++;
/*Calculate Who's Turn It Is*/
if(count == 1 || count == 3 || count == 5 || count == 7 || count == 9){
letter = "X";
Icon = new ImageIcon("LET-X.jpg");
} else if(count == 2 || count == 4 || count == 6 || count == 8 || count == 10){
letter = "O";
Icon = new ImageIcon("LET-O.jpg");
/*Display X's or O's on the buttons*/
if(a.getSource() == button1){
button1.setText(letter);
button1.setIcon(Icon);
button1.setEnabled(false);
} else if(a.getSource() == button2){
button2.setText(letter);
button2.setIcon(Icon);
button2.setEnabled(false);
} else if(a.getSource() == button3){
button3.setText(letter);
button3.setIcon(Icon);
button3.setEnabled(false);
} else if(a.getSource() == button4){
button4.setText(letter);
button4.setIcon(Icon);
button4.setEnabled(false);
} else if(a.getSource() == button5){
button5.setText(letter);
button5.setIcon(Icon);
button5.setEnabled(false);
} else if(a.getSource() == button6){
button6.setText(letter);
button6.setIcon(Icon);
button6.setEnabled(false);
} else if(a.getSource() == button7){
button7.setText(letter);
button7.setIcon(Icon);
button7.setEnabled(false);
} else if(a.getSource() == button8){
button8.setText(letter);
button8.setIcon(Icon);
button8.setEnabled(false);
} else if(a.getSource() == button9){
button9.setText(letter);
button9.setIcon(Icon);
button9.setEnabled(false);
/*Determine who won*/
//horizontal wins
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;
//virticle wins
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;
//diagonal wins
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;
} -
Need some help with Tic Tac Toe game
hey. hope u guys can help me out...
having a problem with my tic tac toe program...
i have 2 problems.
1) got the "X" and "O" to appear using a flag(teacher didn't explain)
using code like this
if (jb == button[0]){
if (flag==0)
button[0].setText("X");
else
button[0].setText("O");
//toggle
flag = (flag==0)?1:0;
my problem is how do i get it to stop.(For example button[1] is selected as "X"..how do i stop it from albe to switch to "O")
2) found this code in javascript online and i want to do what it does in java code
if(button[0] == " X " && button[1] == " X " && button[2] == " X ")
alert("You Win!");
reset();
how would i do that?
thanks for the help.ok here's my code:
//TTT.java
import javax.swing.*;
import java.awt.*;
import java .awt.event.*;
public class TTT extends JFrame
implements WindowListener, ActionListener
private JFrame frFirst;
private Container cnFirst,cnSecond;
private JButton button[]=new JButton[9];
private JButton btnNewGame,btnExit;
private FlowLayout southLayout;
private JPanel southPanel;
private int flag;
public TTT()
frFirst=new JFrame ("Michael's Tic Tac Toe Game!");
cnFirst=frFirst.getContentPane();
cnFirst.setLayout (new GridLayout (4,4));
cnFirst.setBackground(Color.green);
for(int i=0;i<button.length;i++)
button[i] = new JButton();
cnFirst.add(button);
flag=0;
southPanel = new JPanel ();
btnNewGame = new JButton ("New Game");
southPanel.add (btnNewGame);
btnExit = new JButton ("EXIT");
southPanel.add (btnExit);
frFirst.add (southPanel,BorderLayout.SOUTH);
frFirst.setLocation(200, 200);
frFirst.setSize(400,400);
frFirst.setResizable(false);
frFirst.setVisible(true);
this.frFirst.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// listeners
frFirst.addWindowListener(this);
button[0].addActionListener(this);
button[1].addActionListener(this);
button[2].addActionListener(this);
button[3].addActionListener(this);
button[4].addActionListener(this);
button[5].addActionListener(this);
button[6].addActionListener(this);
button[7].addActionListener(this);
button[8].addActionListener(this);
btnNewGame.addActionListener (this);
btnExit.addActionListener (this);
//define methods of WindowListener
public void windowOpened(WindowEvent we)
public void windowClosing(WindowEvent we)
System.exit(0);
public void windowClosed(WindowEvent we)
public void windowIconified(WindowEvent we)
public void windowDeiconified(WindowEvent we)
public void windowActivated(WindowEvent we)
public void windowDeactivated(WindowEvent we)
public void actionPerformed(ActionEvent ae)
//making the X and O to appear on JButtons
Object obj = ae.getSource();
if (obj instanceof JButton){
JButton jb = (JButton)obj;
if (jb == button[0])
if (flag==0)
button[0].setText("X");
else
button[0].setText("O");
//toggle
flag = (flag==0)?1:0;
if(jb==button[1])
if (flag==0)
button[1].setText("X");
else
button[1].setText("O");
//toggle
flag = (flag==0)?1:0;
if(jb==button[2])
if (flag==0)
button[2].setText("X");
else
button[2].setText("O");
//toggle
flag = (flag==0)?1:0;
if(jb==button[3])
if (flag==0)
button[3].setText("X");
else
button[3].setText("O");
//toggle
flag = (flag==0)?1:0;
if(jb==button[4])
if (flag==0)
button[4].setText("X");
else
button[4].setText("O");
//toggle
flag = (flag==0)?1:0;
if(jb==button[5])
if (flag==0)
button[5].setText("X");
else
button[5].setText("O");
//toggle
flag = (flag==0)?1:0;
if(jb==button[6])
if (flag==0)
button[6].setText("X");
else
button[6].setText("O");
//toggle
flag = (flag==0)?1:0;
if(jb==button[7])
if (flag==0)
button[7].setText("X");
else
button[7].setText("O");
//toggle
flag = (flag==0)?1:0;
if(jb==button[8])
if (flag==0)
button[8].setText("X");
else
button[8].setText("O");
//toggle
flag = (flag==0)?1:0;
//New Game To Reset
if (ae.getSource () == btnNewGame)
/* String text = JOptionPane.showInputDialog(null,"Do You Want To Start A New Game?","Michael's Tic Tac Toe Game!",JOptionPane.WARNING_MESSAGE);
String YES;
if (text == YES)
JOptionPane.showMessageDialog(null,"Do You","Michael's Tic Tac Toe Game!",JOptionPane.WARNING_MESSAGE);
else{
add code to reset game
//Exit Button to Exit
if (ae.getSource () == btnExit)
JOptionPane.showMessageDialog(null,"Thanks For Playing!","Michael's Tic Tac Toe Game!",JOptionPane.INFORMATION_MESSAGE);
this.setVisible(false);
System.exit(0);
} //end of if instanceof
public static void main(String[]args)
//instantiate GUI
new TTT(); -
Gui tris (tic tac toe)
Hi,
i am trying to create the tris game in java (maybe you know this game as tic tac toe). I created windows and the alghoritm to play. But now i have the problem to create the board. My idea is to create a board with nine clickable cells, the player clicks on the cell he wants (if it's free), then the computer plays and so on...but i don't have the knowledges of making complex graphic things in java. How can I do?ok, and how can i manage the player's click?MouseListener on the JLabel.
remember
also that when the player clicks, the program must
draw a symbol (x or o depending from the choice of
player). how can i do all this things? For a noob
like me this is very difficultRead the tutorials. I'm not going to explain to you how to create your app if you can read it up yourself. -
Tic - Tac - Toe Game - Please Help!
Hi everyone, I am attempting to create a tic - tac - toe game (O's & X's).
I would like there to be two playing modes to the game, the first mode will be one player where the user plays against the computer. The second mode is a two - player game, where two users play, one being X's and the other being O's.
Can anyone help me get started with this game, I know there is source code on the internet but I would rather not use this as I would like to learn as I create the game.
Thanks Everyoneits amazing how much code a simple game like tic-tac-toe can require.. well, only if you program the AI of the computer.. i wrote one in c++ last year, for my computer science class, and the whole class had trouble with it, mainly the AI.. a suggestion to you: write it in an object-oriented manner. i ended up writing it procedurally and in the end, it looked very bad, but it worked!
i would prob. setup some objects like: Board (the 3x3 grid), Mark (a user mark, such as an "x" or an "o")..
i dunno.. hope that helps some, if you need help with the AI at all.. i do have the tic-tac-toe program i wrote on my website (http://www.angelfire.com/blues/smb/ ) in the C++ section.. i used a graphics package, but the logic is all there and the code is well commented.
anyway, ill talk to ya later, good luck,
Steven Berardi -
Hello,
I am working on a Tic Tac Toe game, I have already started woking on it put i am
stuck on the array part. This is what is required in the project: Create a nine-member array to hold results of the game.. Initialize the array before the start of the game...Allow the user input to indicate which square ther user selects...
Thank You
help will be appreciated..
import javax.swing.JOptionPane;
import java.awt.*;
import java.applet.*;
public class TicTacToe extends Applet
public void init()
// declaration aand created array
int Array[];
Array = new int[8];
String input1;
String input2;
int choice1;
int choice2;
input1 =
JOptionPane.showInputDialog
"Please select your symbol\n"+
"1= O \n"+
"2= X \n");
// converts string to integer
choice1 = Integer.parseInt( input1 );
// draws the TicTacToe Board...
public void paint (Graphics g)
Dimension d = getSize();
g.setColor(Color.blue);
int X_coordinate = d.width / 3;
int Y_coordinate = d.height / 3;
g.drawLine(X_coordinate, 0, X_coordinate, d.height);
g.drawLine(2*X_coordinate, 0, 2*X_coordinate, d.height);
g.drawLine(0, Y_coordinate, d.width, Y_coordinate);
g.drawLine(0, 2*Y_coordinate, d.width, 2*Y_coordinate);
} //end of paitn method
}// end of classTry this:
import javax.swing.*;
import java.awt.*;
import java.applet.*;
public class TicTac extends JApplet
public void init()
// declaration aand created array
int Array[] = new int[8];
String input1;
String input2;
int choice1 = 0;
int choice2;
for(int i=0; i < Array.length; i++) Array[i] = 0;
while (choice1 == 0)
input1 = JOptionPane.showInputDialog
("Please select your symbol\n"+
"1= O \n"+
"2= X \n");
if (input1.equals("1")) choice1 = 1;
if (input1.equals("2")) choice1 = 2;
// converts string to integer
// choice1 = Integer.parseInt( input1 );
// draws the TicTacToe Board...
public void paint(Graphics g)
Dimension d = getSize();
g.setColor(Color.blue);
int sz = 0;
if (d.height/4 - 10 < d.width/4) sz = d.height / 4;
else sz = d.width / 4;
for (int j=0; j < 4; j++)
g.drawLine(10,sz*j+10,sz*3+10,sz*j+10);
g.drawLine(j*sz+10,10,j*sz+10,sz*3+10);
g.setColor(Color.black);
g.drawRect(9,9,sz*3+2,sz*3+2);
} //end of paitn method
}// end of class
Noah
import javax.swing.*;
import java.awt.*;
import java.applet.*;
public class TicTac extends JApplet
public void init()
// declaration aand created array
int Array[] = new int[8];
String input1;
String input2;
int choice1 = 0;
int choice2;
for(int j=0; j < Array.length; j++) Array[j] = 0;
while (choice1 == 0)
input1 = JOptionPane.showInputDialog
("Please select your symbol\n"+
"1= O \n"+
"2= X \n");
if (input1.equals("1")) choice1 = 1;
if (input1.equals("2")) choice1 = 2;
// converts string to integer
// choice1 = Integer.parseInt( input1 );
// draws the TicTacToe Board...
public void paint(Graphics g)
Dimension d = getSize();
g.setColor(Color.blue);
int sz = 0;
if (d.height/4 - 10 < d.width/4) sz = d.height / 4;
else sz = d.width / 4;
for (int j=0; j < 4; j++)
g.drawLine(10,sz*j+10,sz*3+10,sz*j+10);
g.drawLine(j*sz+10,10,j*sz+10,sz*3+10);
g.setColor(Color.black);
g.drawRect(9,9,sz*3+2,sz*3+2);
} //end of paitn method
}// end of class -
Hi,
can any one give me the source code of tic tac toe game..
below is the requirement
1. Multi-Player online Tic-Tac-Toe game
2. Configurable to set from 3 to 6 sides
3. Able to create game, select a list of created open game, join the selected game
Please can i have the distributable war file...
Thanks in advanceHi,
can any one give me the source code of tic tac toe game..
below is the requirement
1. Multi-Player online Tic-Tac-Toe game
2. Configurable to set from 3 to 6 sides
3. Able to create game, select a list of created open game, join the selected game
Please can i have the distributable war file...
Thanks in advance -
Need help/advice with tic tac toe game
Hello all. I am working on a tic tac toe game. I was able to program the first 4 moves fine, but ame having trouble with moves 5 and 6 for reasons that are unknown to me. Everything complies fine, its just that the move is displayed int the wrong space (B1) instead of in B2 or B3. Also the move that is supposed to be in A1 disapppears when B2 or B3 is clicked. Also, I need advice as to how to keep the prior moves from being over written.
At this point I ahve gone over the code on-screen, printed it out, and stared at my drawings... and I'm not having any luck. I'm sure its a small, stupid thing that I'm missing, that anyone else would easily catch. Once again, thx for all your help.
import java.awt.event.*;
import java.awt.*;
import java.applet.*;
public class game3 extends Applet implements MouseListener{
String move = "";
boolean player1 = true;
String gameBoard[][] = new String [3][3];
public void spaceA1(Graphics g){ // MOVE IS A1
if(gameBoard[0][0] == "X")
g.drawString("X",65,65);
if(gameBoard[0][0] == "O")
g.drawString("O",65,65);
public void spaceA2(Graphics g){ // MOVE IS A2
if(gameBoard[0][1] == "X")
g.drawString("X",95,65);
if(gameBoard[0][1] == "O")
g.drawString("O",95,65);
public void spaceA3(Graphics g){ // MOVE IS A3
if(gameBoard[0][2] == "X")
g.drawString("X",125,65);
if(gameBoard[0][2] == "O")
g.drawString("O",125,65);
public void spaceB1(Graphics g){ // MOVE IS B1
if(gameBoard[1][0] == "X")
g.drawString("X",65,95);
if(gameBoard[1][0] == "O")
g.drawString("O",65,95);
public void spaceB2(Graphics g){ // MOVE IS B2
if(gameBoard[1][1] == "X")
g.drawString("X",95,95);
if(gameBoard[1][1] == "O")
g.drawString("O",95,95);
public void spaceB3(Graphics g){ // MOVE IS B3
if(gameBoard[1][2] == "X")
g.drawString("X",125,95);
if(gameBoard[1][2] == "O")
g.drawString("O",125,95);
public void spaceC1(Graphics g){ // MOVE IS C1
if(gameBoard[2][0] == "X")
g.drawString("X",65,125);
if(gameBoard[2][0] == "O")
g.drawString("O",65,125);
public void spaceC2(Graphics g){ // MOVE IS C2
if(gameBoard[2][1] == "X")
g.drawString("X",95,125);
if(gameBoard[2][1] == "O")
g.drawString("O",95,125);
public void spaceC3(Graphics g){ // MOVE IS C3
if(gameBoard[2][2] == "X")
g.drawString("X",125,125);
if(gameBoard[2][2] == "O")
g.drawString("O",125,125);
public void init(){
addMouseListener(this);
public void paint(Graphics g){
g.drawString(" 1 2 3", 50,45);
g.drawString("A",40,70);
g.drawString("B",40,100);
g.drawString("C",40,130);
// first row of boxes
g.drawRect(50,50,30,30);
g.drawRect(80,50,30,30);
g.drawRect(110,50,30,30);
// second row of boxes
g.drawRect(50,80,30,30);
g.drawRect(80,80,30,30);
g.drawRect(110,80,30,30);
// third row of boxes
g.drawRect(50,110,30,30);
g.drawRect(80,110,30,30);
g.drawRect(110,110,30,30);
if(move == "A1"){
spaceA2(g);
spaceA3(g);
spaceB1(g);
spaceB2(g);
spaceB3(g);
spaceC1(g);
spaceC2(g);
spaceC3(g);
if(player1){
gameBoard[0][0] = "X";
g.drawString("X",65,65);
player1 = false;
return;
else
if(!player1){
gameBoard[0][0] = "O";
g.drawString("O",65,65);
player1 = true;
return;
} // end of A1
else
if(move == "A2"){
spaceA1(g);
spaceA3(g);
spaceB1(g);
spaceB2(g);
spaceB3(g);
spaceC1(g);
spaceC2(g);
spaceC3(g);
if(player1){
gameBoard[0][1] = "X";
g.drawString("X",95,65);
player1 = false;
return;
else
if(!player1){
gameBoard[0][1] = "O";
g.drawString("O",95,65);
player1 = true;
return;
} // end of A2
else
if(move == "A3"){
spaceA1(g);
spaceA2(g);
spaceB1(g);
spaceB2(g);
spaceB3(g);
spaceC1(g);
spaceC2(g);
spaceC3(g);
if(player1){
gameBoard[0][2] = "X";
g.drawString("X",125,65);
player1 = false;
return;
else
if(!player1){
gameBoard[0][2] = "O";
g.drawString("O",125,65);
player1 = true;
return;
} // end of A3
else
if(move == "B1")
spaceA1(g);
spaceA2(g);
spaceA3(g);
spaceB2(g);
spaceB3(g);
spaceC1(g);
spaceC2(g);
spaceC3(g);
if(player1){
gameBoard[1][0] = "X";
g.drawString("X",65,95);
player1 = false;
return;
else
if(!player1){
gameBoard[1][0] = "O";
g.drawString("O",65,95);
player1 = true;
return;
} // end of B1
else
if(move == "B2"){
spaceA1(g);
spaceA2(g);
spaceA3(g);
spaceB1(g);
spaceB3(g);
spaceC1(g);
spaceC2(g);
spaceC3(g);
if(player1){
gameBoard[1][1] = "X";
g.drawString("X",95,95);
player1 = false;
return;
else
if(!player1){
gameBoard[1][1] = "O";
g.drawString("O",95,95);
player1 = true;
return;
} // end of B2
else
if(move == "B3"){
spaceA1(g);
spaceA2(g);
spaceA3(g);
spaceB1(g);
spaceB2(g);
spaceC1(g);
spaceC2(g);
spaceC3(g);
if(player1){
gameBoard[1][2] = "X";
g.drawString("X",125,95);
player1 = false;
return;
else
if(!player1){
gameBoard[1][2] = "O";
g.drawString("O",125,95);
player1 = true;
return;
}// end of B3
}// end of graphics
public void mouseReleased(MouseEvent me){}
public void mousePressed(MouseEvent me){}
public void mouseDragged(MouseEvent me){}
public void mouseEntered(MouseEvent me){}
public void mouseExited(MouseEvent me){}
public void mouseClicked(MouseEvent me){
int x = me.getX();
int y = me.getY();
if((x >=51) && (x<= 79) && (y >= 51) && (y <= 79)) //MOVE IS A1
move = "A1";
else
if((x >=81) && (x<=109) && (y >=51) && (y <= 79)) //MOVE IS A2
move = "A2";
else
if((x >=111) && (x<=139) && (y >=51) && (y <= 79)) //MOVE IS A3
move = "A3";
else
if((x >=51) && (x<= 79) && (y >= 81) && (y <= 109)) //MOVE IS B1
move = "B1";
else
if((x >=81) && (x<=109) && (y >=81) && (y <= 109)) //MOVE IS B2
move = "B2";
else
if((x >=111) && (x<=139) && (y >=81) && (y <= 109)) //MOVE IS B3
move = "B3";
repaint();
//<applet code = "game3.class" height =300 width=300> </applet>writing a tic-tac-toe is harder than it sounds.. i wrote one last year in my computer science class.. i have it on my website, if you want to look at code. i wrote it in c++, but the logic is all that matters :)
btw-last year, i wasnt too good of an OOP programmer, so the program is procedurely written. heres the url:
http://www.angelfire.com/blues/smb
also, to tell if a box is already taken, you can just add an if statement: if ( gameBoard[selX][selY] == null ) //not taken, fill box:many people resort to a boolean matrix of the same idea, but with booleans that store which boxes are taken. i prefer the way above, saves code, memory, and makes it more understandable.
hope it helps. -
Tic tac toe 3x3 multiplaye​r game
How can i build a tic tac toe game in labview
What im interested in is the following
1) A 3x3 multiplayer game
2) A boolean to move left in the tictactoe grid and another to move down
3) A boolean array to display the grid an another string array to show the X and O's
4) A boolean to confirm player's choice
5) A way to choose whether player 1 or player 2 begins
6) A way to show which player wins or loses, or if it is a draw.
Thank You in Advance
I appreciate your helpSome of your classmates with the same assignment already asked this quesiton. Why don't you do a quick search before starting a new thread?
LabVIEW Champion . Do more with less code and in less time . -
Does anyone know of an open-source implementation of Tic-Tac-Toe using Graphs? I'm reading through Michael Mann's Data Structures and Other Objects Using Java book and in chapter 14, he briefly mentions the use of directed graphs in implementing a game of Tic Tac Toe. I'm interested in seeing how one would implement something of this sort (and no, it's not a homework assignment).
If someone would be kind enough as to post a link to a site that offers mini-java applications, along with code that novice developers such as myself can traverse, please do so.
Thanks in advance.As an added though, there's really no need to for each node to keep track of incoming connections, only outgoing ones. What I, personally would do is keep an array of 8 outgoing connections, one for each direction. If no connection was there, then that element in the array would be null. If a connection was there, then that element would be an edge connecting to another node, or the other node itself (depending on whether or not you explicitly implemented 'edges' in your graph).
That way, you could do something like:
Node s = getOneOfTheEdgeNodes();
boolean hasWinner = false;
try {
for (int i = 0; i < 8; i ++) {
if (s.getOutgoing(i).getNode().getOutgoing(i).getNode() != null) {
hasWinner = true;
break;
} catch (NullPointerException npx) {
// No winner.
if (hasWinner)
System.out.println(s.getPlayer() + " has won");Yeah, that's a shitty example, but hopefully you get my drift.
Jason -
Tic-tac-toe multithreaded server game
i have written the code for tic-tac-toe game partially.i am stuck at writing the code for "who is the winner"? and adding a button to start the new game ?please help me out to write the remaining?
iam unable to attach the code now;
Message was edited by:
vamshijupallyi have written the code for tic-tac-toe game
partially.i am stuck at writing the code for "who is
the winner"? and adding a button to start the new
game ?please help me out to write the remaining?That question is almost impossible to answer because you didn't provide two vital pieces of information.
1) What specific problems you're having.
2) Relevant code.
iam unable to attach the code now;So, you call the doctor on the phone, say, "I'm not feeling well. Please make me better. I can't come to your office or tell you anything about what's wrong with me now." -
Test out this Tic Tac Toe game please
I know this is suppose to be a real java forum, but this is stripped down version of java, but it's still java. Before using real java I used this Judo. So can you guys download the stuff and play it a few times. Give me any suggestions or bug reports. This is v. 1.0 and there isn't a winner decider yet. Thanks and hope you have fun!
You will need to download JUDO
http://judo.sourceforge.net/#download
Open JUDO and paste this code into it and run.
void main () {
String Intro = "Tic Tac Toe v 1.0 only funcutions under lower case letters.";
String Start = "Execute y/n...";
drawString(Intro, 150,50);
drawString(Start, 150,70);
String Go = "y";
String go;
go = readString();
if(equal(go,Go)){
setColor(blue);
for(int loading = 0 ; loading < 460; loading++)
int lpercent = loading;
//printLine("Loading percent... "+lpercent/460+"%");
fillRectangle(loading*1,250, 25,25);
delay(0.01);
String complete = "Loading Complete...";
drawString(complete,0,235);
//Ask what user would like [X or O]
//get player name
String PlayerName;
String PlayerName2;
printLine("Enter Player 1 name...");
PlayerName = readString();
printLine("Enter Player 2 name...");
PlayerName2 = readString();
//Recycle program loop here
String execute = "y";
String yes = "y";
//While user would still like to run program
while(equal(execute, yes)) {
//decide player symbol
String PlayerOne;
String PlayerTwo;
printLine("Select player 1 character...[x or o]");
PlayerOne = readString();
String O = "o";
String X = "x";
if(equal(PlayerOne, X) ) {
PlayerTwo = O;
else{
PlayerTwo = X;
//Printout inputed data
printLine("Player 1 : ("+ PlayerOne +") " + PlayerName + " Player 2 : (" + PlayerTwo +") "+ PlayerName2);
//Draw playing board
String info = "Player 1 : ("+ PlayerOne +") " + PlayerName + " | Player 2 : (" + PlayerTwo +") "+ PlayerName2;
//clearDrawing();
for(int roundTime = 0 ; roundTime < 6; roundTime++) {
setBackgroundColor(azure);
drawString(info, 150,50);
//horizontal lines
setColor(black);
drawLine(100,100, 400,100);
drawLine(100,101, 400,101);
drawLine(100,141, 400,141);
drawLine(100,142, 400,142);
drawLine(100,182, 400,182);
drawLine(100,183, 400,183);
//verticle lines
drawLine(200,60, 200,223);
drawLine(201,60, 201,223);
drawLine(300,60, 300,223);
drawLine(301,60, 301,223);
//code boxed
String a = "A";
String b = "B";
String c = "C";
String d = "D";
String one = "1";
String two = "2";
String three = "3";
drawString(a, 100,80);
drawString(b, 100,120);
drawString(c, 100,160);
drawString(d, 100,200);
drawString(one, 150,230);
drawString(two, 250,230);
drawString(three, 350,230);
//Let X go first then O (continue alternating)
String pOne = PlayerName + "'s turn to move...";
String pTwo = PlayerName2 + "'s turn to move...";
if(equal(PlayerOne,X)) {
drawString(pOne, 50,243);
else{
drawString(pTwo, 50,243);
//ask user to make move here, then alternate character moves.
String move;
String move2;
String A1 = "a1";
String A2 = "a2";
String A3 = "a3";
String B1 = "b1";
String B2 = "b2";
String B3 = "b3";
String C1 = "c1";
String C2 = "c2";
String C3 = "c3";
String D1 = "d1";
String D2 = "d2";
String D3 = "d3";
printLine("Please enter your character position player(X)...");
move = readString();
String CX = "X";
String CO = "O";
if(equal(move,A1)) {
setColor(blue);
drawString(CX, 42, 140,99);
else{
//continue program execution
if(equal(move,A2)) {
setColor(blue);
drawString(CX, 42, 240,99);
else{
//continue program execution
if(equal(move,A3)) {
setColor(blue);
drawString(CX, 42, 340,99);
else{
//continue program execution
if(equal(move,B1)) {
setColor(blue);
drawString(CX, 42, 140,140);
else{
//continue program execution
if(equal(move,B2)) {
setColor(blue);
drawString(CX, 42, 240,140);
else{
//continue program execution
if(equal(move,B3)) {
setColor(blue);
drawString(CX, 42, 340,140);
else{
//continue program execution
if(equal(move,C1)) {
setColor(blue);
drawString(CX, 42, 140,181);
else{
//continue program execution
if(equal(move,C2)) {
setColor(blue);
drawString(CX, 42, 240,181);
else{
//continue program execution
if(equal(move,C3)) {
setColor(blue);
drawString(CX, 42, 340,181);
else{
//continue program execution
if(equal(move,D1)) {
setColor(blue);
drawString(CX, 42, 140,222);
else{
//continue program execution
if(equal(move,D2)) {
setColor(blue);
drawString(CX, 42, 240,222);
else{
//continue program execution
if(equal(move,D3)) {
setColor(blue);
drawString(CX, 42, 340,222);
else{
//continue program execution
//clearDrawing();
if(equal(PlayerOne,X)) {
drawString(pTwo, 50,243);
else{
drawString(pOne, 50,243);
//O 's movement here
printLine("Please enter your character position player(O)...");
move2 = readString();
if(equal(move2,A1)) {
setColor(red);
drawString(CO, 42, 140,99);
else{
//continue program execution
if(equal(move2,A2)) {
setColor(red);
drawString(CO, 42, 240,99);
else{
//continue program execution
if(equal(move2,A3)) {
setColor(red);
drawString(CO, 42, 340,99);
else{
//continue program execution
if(equal(move2,B1)) {
setColor(red);
drawString(CO, 42, 140,140);
else{
//continue program execution
if(equal(move2,B2)) {
setColor(red);
drawString(CO, 42, 240,140);
else{
//continue program execution
if(equal(move2,B3)) {
setColor(red);
drawString(CO, 42, 340,140);
else{
//continue program execution
if(equal(move2,C1)) {
setColor(red);
drawString(CO, 42, 140,181);
else{
//continue program execution
if(equal(move2,C2)) {
setColor(red);
drawString(CO, 42, 240,181);
else{
//continue program execution
if(equal(move2,C3)) {
setColor(red);
drawString(CO, 42, 340,181);
else{
//continue program execution
if(equal(move2,D1)) {
setColor(red);
drawString(CO, 42, 140,222);
else{
//continue program execution
if(equal(move2,D2)) {
setColor(red);
drawString(CO, 42, 240,222);
else{
//continue program execution
if(equal(move2,D3)) {
setColor(red);
drawString(CO, 42, 340,222);
else{
//continue program execution
/*setBackgroundColor(azure);
drawString(info, 150,50);
//horizontal lines
setColor(black);
drawLine(100,100, 400,100);
drawLine(100,101, 400,101);
drawLine(100,141, 400,141);
drawLine(100,142, 400,142);
drawLine(100,182, 400,182);
drawLine(100,183, 400,183);
//verticle lines
drawLine(200,60, 200,223);
drawLine(201,60, 201,223);
drawLine(300,60, 300,223);
drawLine(301,60, 301,223);
//code boxed
drawString(a, 100,80);
drawString(b, 100,120);
drawString(c, 100,160);
drawString(d, 100,200);
drawString(one, 150,230);
drawString(two, 250,230);
*/ drawString(three, 350,230);
printLine("would you like to execute program further? Y/N");
execute = readString();
else{
clearDrawing();
String Fail = "User declined program execution...";
drawString(Fail, 150,60);please format the code..
RK -
Hi, I have been trying to create a simple Tic Tac Toe game using Java. I'm currently having problems as to how to create the board using arrays (sorry I don't understand them very well). All I have so far is this,
public class ticTacToe {
private int [] [] theBoard = new int [3] [3];
// *'N' is no winner
// 'X' is X won
// 'O' is O won
// 'C' is a cat's game
public static char didSomeoneWin(int [] [] currentBoard)
int winForX = 8000, winForO = 1, checkIfItsAcatsGame = 1, product;
char winner = 'N';
for(int column = 0; column < 3; column++) // Check the columns
product = currentBoard [0] [column] * currentBoard [1] [column] * currentBoard [2] [column];
if(product == winForX) winner = 'X';
if(product == winForO) winner = 'O';
for(int row = 0; row < 3; row++) // Check the rows
product = currentBoard [row] [0] * currentBoard [row] [1] * currentBoard [row] [2];
if(product == winForX) winner = 'X';
if(product == winForO) winner = 'O';
product = currentBoard [0] [0] * currentBoard [1] [1] * currentBoard [2] [2]; // Check one diagonal
if(product == winForX) winner = 'X';
if(product == winForO) winner = 'O';
product = currentBoard [0] [2] * currentBoard [1] [1] * currentBoard [2] [0]; // Check the other diagonal
if(product == winForX) winner = 'X';
if(product == winForO) winner = 'O';
if(winner == 'N') // If nobody's won, Check for a cat's game
for(int row = 0; row < 3; row++)
for(int column = 0; column < 3; column++)
checkIfItsAcatsGame *=currentBoard [row] [column];
if(checkIfItsAcatsGame != 0) winner = 'C'; // any empty space is a zero. So product is zero if there is space left.
return winner;
public void markX(int row, int column)
myBoard [row] [column] = 20;
public void markO(int row, int column)
myBoard [row] [column] = 1;
That is all so far, could someone help me print out the board? And help me out on the arrays? I don't think they are quite right. Thanks in Advance!
Message was edited by:
matthews1.7.2.0Hi,
Do you need a GUI ?
If yes see http://java.sun.com/docs/books/tutorial/uiswing/index.html
You can also see this : http://www.apl.jhu.edu/~hall/java/Swing-Tutorial/Swing-Tutorial-Printing.html about printing.
--Marc (http://jnative.sf.net) -
I need help on a Tic Tac Toe game I am making for a class. I had finished a "2 Player" version and my instructor wants me to simulate a computer "AI" now. I cannot figure out what I'm doing wrong. The order of precedence is this:
1. X moves first (the player)
2. Computer moves to win.
3. If it can't win, it moves to block a win
4. If it can't block a win, it moves to the best possible spot to set up a win.
I think my problems are with several methods, but I cannot figure out what I'm doing wrong. Every time I look at my code, it just makes "sense". I think my problems could probably be found in 4 methods, setButtonText(), findOpen(), findBestOpen() and tempCheck(). I would like hints or maybe if I could get a second look at it for me.
public class VangTicTacToe extends javax.swing.JFrame {
private boolean isX;
private int turnCount;
private boolean gameWon;
private boolean isTemp;
/** Creates new form VangTicTacToe */
public VangTicTacToe() {
initComponents();
resetGame();
isX = true;
turnCount = 0;
private void checkWinner(String pressedButton){
//an multi-dimensional array of winning combinations
JButton[][] winningCombos = {{oneJButton, twoJButton,
threeJButton}, {oneJButton, fourJButton,
sevenJButton}, {oneJButton, fiveJButton, nineJButton},
{twoJButton, fiveJButton, eightJButton}, {threeJButton, sixJButton,
nineJButton}, {fourJButton, fiveJButton, sixJButton}, {sevenJButton,
eightJButton, nineJButton}, {sevenJButton, fiveJButton, threeJButton}};
String buttonPressed = pressedButton;
//loops and determines if any of the possible winning combinations have been
//met for X and displays output accordingly
for(JButton[] row : winningCombos){
for(JButton button : row){
if(button.getText().equals(buttonPressed)){
gameWon = true;
else{
gameWon = false;
break;
}//end inner for
if(gameWon == true && isTemp == false){
for(JButton button : row){
button.setBackground(Color.green);
}//end inner for
if(pressedButton.equals("X"))
outputJLabel.setText("Congratulations! Player 1 (\"X\") Wins!");
else
outputJLabel.setText("Sorry, computer wins.");
disableButtons();
break;
}//end if
else{
continue;//go to next outer loop and keep searching
}//end outer for
}//end method checkWinner
private void setButtonText(JButton buttonPressed){
if(isX == true){
buttonPressed.setText("X");
isX = false;
checkWinner("X");
outputJLabel.setText("Computer's turn");
isTemp = true; //sets isTemp to true so that the test is temporary
findOpen();//calls findOpen to start computer move
//disable the button so it cannot be pressed again
buttonPressed.setEnabled(false);
//increment the turn count number
turnCount++;
if(turnCount == 9)
outputJLabel.setText("Cats Game! You both are losers!");
}//end method setText
//the following 3 methods are for a computer move
//find next open space
private void findOpen(){
//array of buttons
JButton[] buttons = {oneJButton, twoJButton, threeJButton, fourJButton,
fiveJButton, sixJButton, sevenJButton, eightJButton, nineJButton};
//moves through array of buttons and looks for empty. If empty,
//it calls temporary select method.
for (int count = 0; count < buttons.length; count++){
if(buttons[count].getText().isEmpty())
tempCheck(buttons[count]);
}//end for loop
//if gameWon is false, call findBestButton to find the best open spot
if(gameWon == false){
findBestButton();
}//end method findOpen
private void findBestButton(){
//an multi-dimensional array of winning combinations
JButton[][] winningCombos = {{oneJButton, twoJButton,
threeJButton}, {oneJButton, fourJButton,
sevenJButton}, {oneJButton, fiveJButton, nineJButton},
{twoJButton, fiveJButton, eightJButton}, {threeJButton, sixJButton,
nineJButton}, {fourJButton, fiveJButton, sixJButton}, {sevenJButton,
eightJButton, nineJButton}, {sevenJButton, fiveJButton, threeJButton}};
boolean placeO = false;
int buttonCount = 0;
for(JButton[] row : winningCombos){
for(JButton button : row){
if(button.getText().equals("O") || button.getText().equals("")){
placeO = true;
else{
placeO = false;
buttonCount = 0;
if(placeO == true){
++buttonCount;
else{
break;
if(buttonCount == 3 && placeO == true){
button.setText("O");
}//end inner for
if(placeO == true){
isX = true;
break;
else{
continue;
}//end outer for
}//end method findBestButton
//checks if temp move would win
private void tempCheck(JButton tempButton){
//temporarily assigns "O" to a square and checks if it wins
tempButton.setText("O");
checkWinner("O");
if(gameWon == true){//if it wins then set temp to false and call
isTemp = false;//checkWinner();
checkWinner("0");
else{
tempButton.setText("");//else, set buttonText to empty
}//end if
if(gameWon == false){//if gameWon is false, check if "X" would win
tempButton.setText("X");
isTemp = true;
checkWinner("X");
if(gameWon == true){//if x is going to win,
tempButton.setText("O");//block the player from winning
else{
tempButton.setText("X");
}//end method tempCheck()
}touandcim wrote:
I click to make "X"'s (the player's move). X appears but O never does, although a button is highlighted. If I keep pressing the buttons, all X's appear until the last move, when an "O" appears.I don't know if it's the problem, but
checkWinner("0");looks suspicious. (2nd invocation in your tempCheck() method).
Your program seems awfully brittle. How would you do it if you had a 4 x 4 board? Or 5 x 5?
Also, your 'winningCombos' array is repeated everywhere it's used. There's no need for that; just make it an instance variable/constant.
Winston
Maybe you are looking for
-
How to change font size in Notification center?
Good day I purchased new monitor with BIG resolution an dfount what font in notification message from Notification Center stay very small I try to find place where possible change font siza but don't found anything Please help
-
Cannot sycn iTunes using my new laptop (home sharing is turned on). I no longer have access to the old HP desktop that iTunes was originally on. How can I get all the songs in iTunes on my iPhone?
-
SE54 - Recording Routine Radio Button - Transport Question
Greetings! We have an issue in our development system where in T-Code SE54 the Recording Routine radio button selection was changed for a particular table so it's affecting one of our custom transactions. We are looking to change the setting but ther
-
Handle errors when makeing doc.submitForm()
Hello, I am trying to submit PDF form by method doc.submitForm, example: doc.submitForm cURL: "http://localhost/reader_test.php?name=" + name.rawValue, cSubmitAs: "XML" A) With this approach, is it possible to handle connection errors
-
NetWeaver EHP1 ce 7.1 Oracle (JAVA)
DB Oracle 10g (10.2) DB Oracle Patch (10.2.04) VMWARE environment OS Windows Server 2003 x64 64bit SAP EHP1 Netweaver ce 7.1 SAP MII 12.1 Oracle: When prepping the environment, we installed Oracle on the D:\ using a vb script that uses our file locat