Tic-Tac-Toe game

+1 Steven the awesome · May 31, 2014
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class Main implements ActionListener {
/*Instance Variables*/
private int[][] winCombinations = new int[][]{
{0, 1, 2}, {3, 4, 5}, {6, 7, 8}, //horizontal wins
{0, 3, 6}, {1, 4, 7}, {2, 5, 8}, //vertical wins
{0, 4 ,8}, {2, 4, 6} //diagonal wins
};
private JFrame window = new JFrame("Tic-Tac-Toe");
private JButton buttons[] = new JButton[9];
private int count = 0;
private String letter = "";
private boolean win = false;

public Main(){
//Create Window
window.setSize(300,300);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLayout(new GridLayout(3,3));

//Add Button to the window
for(int i=0; i<=8; i++){
buttons = new JButton();
window.add(buttons);
buttons.addActionListener(this);
}

//Make the window Visible
window.setVisible(true);
}

//When an object is clicked, perform an action.
public void actionPerformed(ActionEvent a){
count++;

//Calculate whose turn it is
if(count % 2 == 0){
letter = "0";
} else {
letter = "x";
}

//Write the letter to the button and deactivate it
JButton pressedButton = (JButton)a.getSource();
pressedButton.setText(letter);
pressedButton.setEnabled(false);

//Determine who won
for(int i=0; i<=7; i++){
if(buttons[winCombinations[0]].getText().equals(buttons[winCombinations[1]].getText()) &&
buttons[winCombinations[1]].getText().equals(buttons[winCombinations[2]].getText()) &&
buttons[winCombinations[0]].getText() != ""){
win = true;
}
}

//Show when the game is over
if(win == true){
JOptionPane.showMessageDialog(null, letter + " wins the game");
System.exit(0);
} else if(count == 9 && win == false){
JOptionPane.showMessageDialog(null, "The game was tie");
System.exit(0);
}
}


public static void main(String args[]){
Main starter = new Main();
}
}

Post a Reply

Replies

Oldest  Newest  Rating
+1 Bucky Roberts · May 31, 2014
Use the code tags.


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

public class Main implements ActionListener {
/*Instance Variables*/
private int[][] winCombinations = new int[][]{
{0, 1, 2}, {3, 4, 5}, {6, 7, 8}, //horizontal wins
{0, 3, 6}, {1, 4, 7}, {2, 5, 8}, //vertical wins
{0, 4 ,8}, {2, 4, 6} //diagonal wins
};
private JFrame window = new JFrame("Tic-Tac-Toe");
private JButton buttons[] = new JButton[9];
private int count = 0;
private String letter = "";
private boolean win = false;

public Main(){
//Create Window
window.setSize(300,300);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLayout(new GridLayout(3,3));

//Add Button to the window
for(int i=0; i<=8; i++){
buttons = new JButton();
window.add(buttons);
buttons.addActionListener(this);
}

//Make the window Visible
window.setVisible(true);
}

//When an object is clicked, perform an action.
public void actionPerformed(ActionEvent a){
count++;

//Calculate whose turn it is
if(count % 2 == 0){
letter = "0";
} else {
letter = "x";
}

//Write the letter to the button and deactivate it
JButton pressedButton = (JButton)a.getSource();
pressedButton.setText(letter);
pressedButton.setEnabled(false);

//Determine who won
for(int i=0; i<=7; i++){
if(buttons[winCombinations[0]].getText().equals(buttons[winCombinations[1]].getText()) &&
buttons[winCombinations[1]].getText().equals(buttons[winCombinations[2]].getText()) &&
buttons[winCombinations[0]].getText() != ""){
win = true;
}
}

//Show when the game is over
if(win == true){
JOptionPane.showMessageDialog(null, letter + " wins the game");
System.exit(0);
} else if(count == 9 && win == false){
JOptionPane.showMessageDialog(null, "The game was tie");
System.exit(0);
}
}


public static void main(String args[]){
Main starter = new Main();
}
}

0 Steven the awesome · May 31, 2014
Yep your right much better :)
0 Jay Deshaun · June 8, 2014
This works, I just finished editing it for you

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

public class Main implements ActionListener {
/*Instance Variables*/
private int[][] winCombinations = {
{0, 1, 2}, {3, 4, 5}, {6, 7, 8}, //horizontal wins
{0, 3, 6}, {1, 4, 7}, {2, 5, 8}, //vertical wins
{0, 4 ,8}, {2, 4, 6} //diagonal wins
};
private JFrame window = new JFrame("Tic-Tac-Toe");
private JButton buttons[] = new JButton[9];
private int count = 0;
private String letter = "";
private boolean win = false;

public Main(){
//Create Window
window.setSize(300,300);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLayout(new GridLayout(3,3));

//Add Button to the window
for(int i=0; i<=8; i++){
buttons = new JButton();
window.add(buttons);
buttons.addActionListener(this);
}

//Make the window Visible
window.setVisible(true);
}

//When an object is clicked, perform an action.
public void actionPerformed(ActionEvent a){
count++;

//Calculate whose turn it is
if(count % 2 == 0)
letter = "0";
else
letter = "x";

//Write the letter to the button and deactivate it
JButton pressedButton = (JButton)a.getSource();
pressedButton.setText(letter);
pressedButton.setEnabled(false);

//Determine who won
for(int i=0; i<=7; i++){
/*if(buttons[winCombinations[0]].getText().equals(buttons[winCombinations[1]].getText()) &&
buttons[winCombinations[1]].getText().equals(buttons[winCombinations[2]].getText()) &&
buttons[winCombinations[0]].getText() != ""){
win = true;
} */
int temp_win = 0;
for(int v = 0;v<=2;v++){
int b = winCombinations[v];
if(buttons.getText().equals(letter))
temp_win++;
}
if(temp_win == 3){
win = true;
break;
}
}

//Show when the game is over
if(win == true){
JOptionPane.showMessageDialog(null, letter + " wins the game");
System.exit(0);
} else if(count == 9 && win == false){
JOptionPane.showMessageDialog(null, "The game was tie");
System.exit(0);
}
}


public static void main(String args[]){
new Main();
}
}


0 Daquaris Chadwick · June 13, 2014
I'm looking over this code and in several instances you try to use arrays that invoke errors. 

Example:
<!--?prettify lang=html linenums=true?--><!--?prettify lang=html linenums=true?-->
	if(buttons.getText().equals(letter))			temp_win++;

you gave buttons the object of an arrayed JButton. And now you want to use the entire buttons to get text. That's not possible. Is it possible that you want to loop through all the buttons? if so, there is already a loop that you can use that is over the if statement. I ran it and i added:
<!--?prettify lang=html linenums=true?--><!--?prettify lang=html linenums=true?-->
	if(buttons.getText().equals(letter))				temp_win++;



But then there is a problem that ends the game immediately when the user chooses a square. It returns the winner clause and I'm assuming that is unwanted. I have't found out why yet, my thought is its because in the loop:
<!--?prettify lang=html linenums=true?--><!--?prettify lang=html linenums=true?-->
for(int i=0; i<=7; i++){	/*if(buttons[winCombinations[0]].getText().equals(buttons[winCombinations[1]].getText()) &&	buttons[winCombinations[1]].getText().equals(buttons[winCombinations[2]].getText()) &&	buttons[winCombinations[0]].getText() != ""){	win = true;	} */	int temp_win = 0;	for(int v = 0;v<=2;v++){		int[] b = winCombinations[v];		if(buttons.getText().equals(letter))			temp_win++;	}	if(temp_win == 3){		win = true;		break;	}}




it'll give temp_win the value 3 immediately because you're asking if the text in i is equal to the letter which is always true if I'm not mistaken. If I'm getting anything wrong can you explain it to me? 
-2 Daquaris Chadwick · June 15, 2014
My previouse post got Fked up. basically in the second code snippet i added "" after the "button"
0 Parijit Kedia · June 24, 2014
i dont think the original code is also working
  • 1

Java / Android Development

106,964 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator