Hey im sort of new to java. i took a class in school called JAVA and it talked about the basics. And know that the class is over i wanted to create something on my own and i decided to try and make tic tac toe the game… it started out going good but once i made the board and set the turns i got stuck :-[. I can’t figure out how to tell which player won the game and i also want to cheat proof my game , you can click the box twice and make it go from an x to an o or vice versa. Your help would be greatly appreciated and if you could could you explain what your doing so i can do it right the next time i try. Thanks for your help!
Scott.
import javax.swing.;
import BreezySwing.;
import java.awt.;
import java.awt.event.;
public class Tictactoe extends GBFrame{
//Declare varialbe for the window objects
private JLabel title;
private JButton topleft;
private JButton topmiddle;
private JButton topright;
private JButton middleleft;
private JButton middlemiddle;
private JButton middleright;
private JButton bottomleft;
private JButton bottommiddle;
private JButton bottomright;
private JButton newgame;
private int x, y=0;
//constructor
public Tictactoe(){
//instantiate and add window objects to the window
title = addLabel ("Tic Tac Toe" ,1,2,1,1);
topleft = addButton (" " ,2,1,1,1);
topmiddle = addButton (" " ,2,2,1,1);
topright = addButton (" " ,2,3,1,1);
middleleft = addButton (" " ,3,1,1,1);
middlemiddle = addButton (" " ,3,2,1,1);
middleright = addButton (" " ,3,3,1,1);
bottomleft = addButton (" " ,4,1,1,1);
bottommiddle = addButton (" " ,4,2,1,1);
bottomright = addButton (" " ,4,3,1,1);
newgame = addButtom ("New Game" , 5,2,1,1);
}
//respond to button click events
public void buttonClicked (JButton buttonObj){
if (y=o){
y=1;
}
if (buttonObj == topleft){
if ((x%2)==o){
topleft.setText("X");
}else{
topleft.setText("O");
}
x+=1;
}
if (buttonObj == topmiddle){
if ((x%2)==o){
topmiddle.setText("X");
}else{
topmiddle.setText("O");
}
x+=1;
}
if (buttonObj == topright){
if ((x%2)==o){
topright.setText("X");
}else{
topright.setText("O");
}
x+=1;
}
if (buttonObj == middleleft){
if ((x%2)==o){
middleleft.setText("X");
}else{
middleleft.setText("O");
}
x+=1;
}
if (buttonObj == middlemiddle){
if ((x%2)==o){
middlemiddle.setText("X");
}else{
middlemiddle.setText("O");
}
x+=1;
}
if (buttonObj == middleright){
if ((x%2)==o){
middleright.setText("X");
}else{
middleright.setText("O");
}
x+=1;
}
if (buttonObj == bottomleft){
if ((x%2)==o){
bottomleft.setText("X");
}else{
bottomleft.setText("O");
}
x+=1;
}
if (buttonObj == bottommiddle){
if ((x%2)==o){
bottommiddle.setText("X");
}else{
bottommiddle.setText("O");
}
x+=1;
}
if (buttonObj == bottomright){
if ((x%2)==o){
bottomright.setText("X");
}else{
bottomright.setText("O");
}
x+=1;
}
if (buttonObj == newgame){
topleft.setText(" ");
topmiddle.setText(" ");
topright.setText(" ");
middleleft.setText(" ");
middlemiddle.setText(" ");
middleright.setText(" ");
bottomleft.setText(" ");
bottommiddle.setText(" ");
bottomright.setText(" ");
x = 0;
}
}
public static void main (String[] args){
Tictactoe theGUI = new Tictactoe();
theGUI.setSize (275, 205);
theGUI.setLookAndFeel("Metal");
theGUI.setVisible (true);
}
}