AKIRA
Tank
- Joined
- Feb 6, 2006
- Messages
- 3,000
- Reaction score
- 2
ok..pretty much screwed myself over with this assignment..thought it was going to be easy...turned out i was dead wrong.
gotta make a tic tac toe game
got the grid to the left and a text field to the right that should display game stats like:
wins losses
player1
player2
when you click on a button in teh grid it should randomly display either an x or o...
im stuck here...please help asap i will provide my code but yea i know im an idiot and i shouldnt have been cocky and assumed it was easy but my assignment is due tmw and you lose 20% each day that it;s late
im really desperate here for any help...can someone tell me how to code the actual tic tac toe algorithm and make it so that when i click on the buttons int he grid it'll display either an x or o??
thanks in advance!!!!
gotta make a tic tac toe game
got the grid to the left and a text field to the right that should display game stats like:
wins losses
player1
player2
when you click on a button in teh grid it should randomly display either an x or o...
im stuck here...please help asap i will provide my code but yea i know im an idiot and i shouldnt have been cocky and assumed it was easy but my assignment is due tmw and you lose 20% each day that it;s late
im really desperate here for any help...can someone tell me how to code the actual tic tac toe algorithm and make it so that when i click on the buttons int he grid it'll display either an x or o??
Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/***************************************************************/
public class example1 extends JFrame
{
//Declare objects
JButton jbtNewGame, jbtPlayGame[];
JPanel main, button, display;
JTextArea jtaDisplay;
int x = 0,y = 0,gameTurn;
//Constructor
public example1(String title)
{
super(title);
setSize(200,200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel p = new JPanel();
p.add(createMainPanel());
p.add(createButtonPanel());
p.add(createDisplayPanel());
add(p);
main.add(display);
main.add(jbtNewGame,BorderLayout.SOUTH);
display.add(button);
add(main);
jtaDisplay = new JTextArea();
display.add(jtaDisplay);
for (int i =0 ;i <9 ;i++ )
{
jbtPlayGame[i] = new JButton();
button.add(jbtPlayGame[i]);
jbtPlayGame[i].addActionListener(new ButtonAction());
}
setLocationRelativeTo(null);
setVisible(true);
}
public JPanel createMainPanel(){
main = new JPanel();
main.setLayout(new BorderLayout());
return main;
}
public JPanel createButtonPanel(){
button = new JPanel();
button.setLayout(new GridLayout(3, 3));
jbtNewGame = new JButton("New Game");
jbtPlayGame = new JButton[9];
return button;
}
public JPanel createDisplayPanel(){
display = new JPanel();
display.setLayout(new GridLayout(1, 2));
return display;
}
/***************************************************************/
private class ButtonAction implements ActionListener{
public void actionPerformed(ActionEvent e){
gameTurn = 2;
for (int i=0; i<9; i++){
if(jbtPlayGame[i] == e.getSource()){
if(gameTurn%2 == 0)
jbtPlayGame[i].setText("x");
else
jbtPlayGame[i].setText("o");
}
gameTurn++;
}
if(jbtNewGame == e.getSource()){
for (int i=0; i<9; i++ ){
jbtPlayGame[i].setText("");
}
}
}
}
/***************************************************************/
public static void main(String[] args)
{
example1 game = new example1("Tic Tac Toe");
}
}
thanks in advance!!!!