oh god hl2.net please help - programmers

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??

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!!!!
 
Ha, I actually made a tic tac toe game where you could play against the computer. Once for DOS and once for Windows3. I still have the floppy discs but don't know if they work.

Unfortunately, I can't help you because I used different languages and it's been more than 15 years.

Should be pretty easy for some of these guys, but IMO the most rewarding part of programing is figuring how to get things to work on your own.

PRINTSCREEN "X"
GOTO 20
 
UPDATE:

ok...i figured some stuff out, figured out to do the JTextArea and figured out how to add the x's and o's when i click the buttons...but the only thing is that i couldn't figure out the logic behind the winning condition so my buddy sent his code to me...problem is i cant figure it out how to make it different than mine because his makes so much sense :(...I'll provide the code now, can you guys maybe help me with another way to find out the winCondition()?

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 o = 0;
	int	x = 0;
	int	playerTurn = 2; 
	boolean winCondition;




	//Constructor
	public example1(String title)
	{
		super(title);
		setSize(200,200);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setLocationRelativeTo(null);

		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);
		jbtNewGame.addActionListener(new ButtonAction());
		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());
		}
		setText();
		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){
			
			for (int i=0; i<9; i++){

				if(jbtPlayGame[i] == e.getSource()){

					playerTurn = ++playerTurn;
					//Prevent changing symbol
					if(jbtPlayGame[i].getText() != "x" && jbtPlayGame[i].getText()!="o" ){

						if(playerTurn%2 == 0)
						jbtPlayGame[i].setText("x");
						else
						jbtPlayGame[i].setText("o");
					}
					else 
						playerTurn = --playerTurn;
					}
			}

			if(jbtNewGame == e.getSource()){

				for (int i=0; i<9; i++ ){
				jbtPlayGame[i].setText("");
				jbtPlayGame[i].setEnabled(true);

				}
			}
			
			
			
			winCondition();
			
			if (winCondition == true){

				for (int x=0; x<9; x++){

				jbtPlayGame[x].setEnabled(false);

				}

				setText();
			}
			
			winCondition = false;
			
			
		}
	}
/***************************************************************/
		public void winCondition(){
		
		//Checking to see if there is a winner in a horizontal fashion
		for (int i=0; i<3; i++){
			if (jbtPlayGame[0+(i*3)].getText() =="o" && jbtPlayGame[0+(i*3)].getText() == jbtPlayGame[1+(i*3)].getText() && 
				jbtPlayGame[1+(i*3)].getText() == jbtPlayGame[2+(i*3)].getText()){
				o = o + 1;
				winCondition = true;
			}
			else if (jbtPlayGame[0+(i*3)].getText() =="x" && jbtPlayGame[0+(i*3)].getText() == jbtPlayGame[1+(i*3)].getText() && 
				jbtPlayGame[1+(i*3)].getText() == jbtPlayGame[2+(i*3)].getText()){
				x = x + 1;
				winCondition = true;
			}	
		
		}
		//Checking to see if there is a winner in a vertical fashion
		for (int i=0; i<3; i++){
			if (jbtPlayGame[0+i].getText() =="o" && jbtPlayGame[0+i].getText() == jbtPlayGame[3+i].getText() && 
				jbtPlayGame[3+i].getText() == jbtPlayGame[6+i].getText()){
				o = o + 1;
				winCondition = true;
			}
			else if (jbtPlayGame[0+i].getText() =="x" && jbtPlayGame[0+i].getText() == jbtPlayGame[3+i].getText() && 
				jbtPlayGame[3+i].getText() == jbtPlayGame[6+i].getText()){
				x = x + 1;
				winCondition = true;
			}	
		
		}
		//Checking to see if there is a winner in a diagonal fashion
		for (int i=0; i<2; i++){
			if (jbtPlayGame[0+(2*i)].getText() =="o" && jbtPlayGame[0+(2*i)].getText() == jbtPlayGame[4].getText() && 
				jbtPlayGame[4].getText() == jbtPlayGame[8-(2*i)].getText()){
				o = o + 1;
				winCondition = true;
			}
			else if (jbtPlayGame[0+(2*i)].getText() =="x" && jbtPlayGame[0+(2*i)].getText() == jbtPlayGame[4].getText() && 
				jbtPlayGame[4].getText() == jbtPlayGame[8-(2*i)].getText()){
				x = x + 1 ;
				winCondition = true;
			}	
		
		}
		
	}
/***************************************************************/
public void setText(){

		String display = "Player\tWins\nPlayer X\t" + x +"\nPlayer O\t" + o;
		jtaDisplay.setText(display);


	}
/***************************************************************/
	public static void main(String[] args) 
	{
		example1 game = new example1("Tic Tac Toe");

		}
	

}

really appreciate the help so far :)
 
Why does he write 0 + i everywhere ? , 0 + i = i . another way to do it is to determine if they are all the same and then determine the winner based on whether it was a 0 row or an X row .

so instead of

Code:
		for (int i=0; i<3; i++){
			if (jbtPlayGame[0+i].getText() =="o" && jbtPlayGame[0+i].getText() == jbtPlayGame[3+i].getText() && 
				jbtPlayGame[3+i].getText() == jbtPlayGame[6+i].getText()){
				o = o + 1;
				winCondition = true;
			}
			else if (jbtPlayGame[0+i].getText() =="x" && jbtPlayGame[0+i].getText() == jbtPlayGame[3+i].getText() && 
				jbtPlayGame[3+i].getText() == jbtPlayGame[6+i].getText()){
				x = x + 1;
				winCondition = true;
			}	
		
		}

you would have

Code:
		for (int i=0; i<3; i++){
			if (jbtnPlayGame[i]!=""&&jbtPlayGame[i].getText() == jbtPlayGame[3+i].getText() && 
				jbtPlayGame[3+i].getText() == jbtPlayGame[6+i].getText()){
				winCondition = true;
                                winPlayer = (jbtPlayGame[i].getText()=='x')?player1:player2 ;
                                break;
			}
		}

you can also combine the first two loops into one .

I don't care whether the button is an x or a 0 all i care about in the if is whether 3 of them are the same in either horizontal , vertical or diagonal directions. If they are the same i THEN check whether it started with an X or a 0 to determine which player won, i did this in a ternary assignment but you could just aswelll do it with an if statement, also always remember to break your forloops. Another way to check it is to propagate from the last player click , this will reduce the number of searches but in a 9 button grid this really isn't a big deal.

edit : I can elaborate a bit more if you want .

edit 2: you can also combine the first two loops into one.

edit 3: don't use global variables make winCondition() return true or false . This will make your life easier trust me . It will be a lot harder to break your code.

edit 4 : why don't you use a 2d array for the buttons ? , this is standard in any grid based game.
 
I'm a Progamer. You gotta micro your lings and lurkers, that way when the marines come in, they can't shoot the lurkers. Be sure to run from tanks, they are assholes.

Wait.
 
Also make it return from winCondition() every time you find that the player has won , because once the player has won you don't need to continue checking the other cases.
 
Ha, I actually made a tic tac toe game where you could play against the computer.

We had to make tic-tac-toe in my c++ class, 2 human players, but I took it one step further and made some A.I. for it, which I was pretty proud of. I did it because I wanted to learn and because I thought I'd get extra-credit, but of course the teacher gave me 0 extra credit and then told me not to deviate from the assignments -_-

Sorry for trying to learn, oh great teacher.
 
I wrote Connect 4 in javascript earlier this year.

Also AKIRA you might be interested to know that if your teacher suspects you didn't do your work and searches the code online, Halflife2.net has a very high google ranking and this thread will probably pop up on the first page of results (if not the top result). Just food for thought, but if you aren't in a uni class and you usually do well you will probably be fine.
 
We had to make tic-tac-toe in my c++ class, 2 human players, but I took it one step further and made some A.I. for it, which I was pretty proud of. I did it because I wanted to learn and because I thought I'd get extra-credit, but of course the teacher gave me 0 extra credit and then told me not to deviate from the assignments -_-

Sorry for trying to learn, oh great teacher.

I have a very similar story. I've already told it here a year or two ago, but we were told we could use the professional edition of Vbasic several times, so I was using it.

For the final grade I was told that they didn't have the pro edition so they couldn't test my program, so he just assumed it worked and gave me a B.

WTF I had spent much of the summer working on that shit for community college class. It was like really over the top, like a commercial video game.
 
Back
Top