7 Replies - 974 Views - Last Post: 10 June 2012 - 08:32 PM Rate Topic: -----

#1 11joe4  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 10-June 12

Tic Tac Toe Game

Posted 10 June 2012 - 01:25 PM

I am trying to develop a Tic Tac Toe game but the compiler has stopped throwing errors at me i have tried everything i could possibly think of to try and fix the code, and i am to the point where i am thinking about just rewriting the whole thing.

The problem i am faced with is getting the program to start up, and i am new to GUI's. so if anyone could tell me why it will not start up or may be the cause of it would be appreciated greatly.

The code is a little sloppy because i have messed around with it so much, and tried fixing the problem myself. but i have run out of ideas and if anyone could tell what is wrong with it so i can fix it would again be greatly appreciated. (i am not asking you to write the code for me i just want to know where the problem is located, since i am new to GUI's.)

The first class is called TTT

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

public class TTT extends JFrame{
//0 = blank, 1 = x, 2 = o
	JFrame frame;
	JButton button1, button2, button3, button4, button5, button6, button7, button8, button9, start, computerAI, replay, exit;
	JLabel title, label;
	JOptionPane pane;
	int count =0;
	int[][] game;
	int player;
	boolean gameWin;
	Icon xImage;
	Icon oImage;
	String plyone = "Player 1 Wins";
	String plytwo = "Player 2 Wins";
	String cat = "CATS GAME";
	
	public TTT(){
		super("Tic Tac Toe");
		
		//images for x and o
		//xImage = new ImageIcon(/*getClass().getResource()*/ "");
		
		gameWin = false;
		frame.setLayout(new GridLayout(3,3));
		
		add(start);
		add(computerAI);
		
		
		frame = new JFrame("Tic Tac Toe");
		button1 = new JButton();
		button1.setPreferredSize(new Dimension(75, 75));
		frame.add(button1);
		button2 = new JButton();
		button2.setPreferredSize(new Dimension(75, 75));
		frame.add(button2);
		button3 = new JButton();
		button3.setPreferredSize(new Dimension(75, 75));
		frame.add(button3);
		button4 = new JButton();
		button4.setPreferredSize(new Dimension(75, 75));
		frame.add(button4);
		button5 = new JButton();
		button5.setPreferredSize(new Dimension(75, 75));
		frame.add(button5);
		button6 = new JButton();
		button6.setPreferredSize(new Dimension(75, 75));
		frame.add(button6);
		button7 = new JButton();
		button7.setPreferredSize(new Dimension(75, 75));
		frame.add(button7);
		button8 = new JButton();
		button8.setPreferredSize(new Dimension(75, 75));
		frame.add(button8);
		button9 = new JButton();
		button9.setPreferredSize(new Dimension(75, 75));
		frame.add(button9);
		
		
		//if player 1 click: button 1 is clicked make Array[0][0] = 1. if a row is all 1 make game win = true
		game = new int[2][2];
		for(int i = 0;i < game.length;i++){
			for(int j = 0;j < game[i].length;j++){
				game[i][j] = 0;
			}
		}
		
		
	

	}
	public void actionPerformed(ActionEvent e){
		count++;
				
		//Player turns
		if(count == 1 || count == 3 || count == 5 || count == 7 || count == 9){ 
			player = 1;
		}
		if(count == 2 || count == 4 || count == 6 || count == 8){
			player = 2;
		}
		else if(count == 10){
			pane.showMessageDialog(null, cat);
			
			
		}
		
		//button pushes
		if(e.getSource() == button1 && player == 1){
			game[0][0] = 1;
			button1.setIcon(xImage);
			button1.setEnabled(false);
		}
		if(e.getSource() == button2 && player == 1){
			game[0][1] = 1;
			button2.setIcon(xImage);
			button2.setEnabled(false);
		}
		if(e.getSource() == button3 && player == 1){
			game[0][2] = 1;
			button3.setIcon(xImage);
			button3.setEnabled(false);
		}
		if(e.getSource() == button4 && player == 1){
			game[1][0] = 1;
			button4.setIcon(xImage);
			button4.setEnabled(false);
		}
		if(e.getSource() == button5 && player == 1){
			game[1][1] = 1;
			button5.setIcon(xImage);
			button5.setEnabled(false);
		}
		if(e.getSource() == button6 && player == 1){
			game[1][2] = 1;
			button6.setIcon(xImage);
			button6.setEnabled(false);
		}
		if(e.getSource() == button7 && player == 1){
			game[2][0] = 1;
			button7.setIcon(xImage);
			button7.setEnabled(false);
		}
		if(e.getSource() == button8 && player == 1){
			game[2][1] = 1;
			button8.setIcon(xImage);
			button8.setEnabled(false);
		}
		if(e.getSource() == button9 && player == 1){
			game[2][2] = 1;
			button9.setIcon(xImage);
			button9.setEnabled(false);
		}
		//__________________________________________
		if(e.getSource() == button1 && player == 2){
			game[0][0] = 2;
			button1.setIcon(oImage);
			button1.setEnabled(false);
		}
		if(e.getSource() == button2 && player == 2){
			game[0][1] = 2;
			button2.setIcon(oImage);
			button2.setEnabled(false);
		}
		if(e.getSource() == button3 && player == 2){
			game[0][2] = 2;
			button3.setIcon(oImage);
			button3.setEnabled(false);
		}
		if(e.getSource() == button4 && player == 2){
			game[1][0] = 2;
			button4.setIcon(oImage);
			button4.setEnabled(false);
		}
		if(e.getSource() == button5 && player == 2){
			game[1][1] = 2;
			button5.setIcon(oImage);
			button5.setEnabled(false);
		}
		if(e.getSource() == button6 && player == 2){
			game[1][2] = 2;
			button6.setIcon(oImage);
			button6.setEnabled(false);
		}
		if(e.getSource() == button7 && player == 2){
			game[2][0] = 2;
			button7.setIcon(oImage);
			button7.setEnabled(false);
		}
		if(e.getSource() == button8 && player == 2){
			game[2][1] = 2;
			button8.setIcon(oImage);
			button8.setEnabled(false);
		}
		if(e.getSource() == button9 && player == 2){
			game[2][2] = 2;
			button9.setIcon(oImage);
			button9.setEnabled(false);
		}
		
		// the winning spaces for player 1
		if(game[0][0] == 1 && game[0][1] == 1 && game[0][2] == 1){
			gameWin = true;
			//set button editable false 
		}
		if(game[1][0] == 1 && game[1][1] == 1 && game[1][2] == 1){
			gameWin = true;
		}
		if(game[2][0] == 1 && game[2][1] == 1 && game[2][2] == 1){
			gameWin = true;
		}
		if(game[0][0] == 1 && game[1][0] == 1 && game[2][0] == 1){
			gameWin = true;
		}
		if(game[0][1] == 1 && game[1][1] == 1 && game[2][1] == 1){
			gameWin = true;
		}
		if(game[0][2] == 1 && game[1][2] == 1 && game[2][2] == 1){
			gameWin = true;
		}
		if(game[0][0] == 1 && game[1][1] == 1 && game[2][2] == 1){
			gameWin = true;
		}
		if(game[0][2] == 1 && game[1][1] == 1 && game[2][0] == 1){
			gameWin = true;
		}
		
		
		// the winning spaces for player 2
		if(game[0][0] == 2 && game[0][1] == 2 && game[0][2] == 2){
			gameWin = true;
		}
		if(game[1][0] == 2 && game[1][1] == 2 && game[1][2] == 2){
			gameWin = true;
		}
		if(game[2][0] == 2 && game[2][1] == 2 && game[2][2] == 2){
			gameWin = true;
		}
		if(game[0][0] == 2 && game[1][0] == 2 && game[2][0] == 2){
			gameWin = true;
		}
		if(game[0][1] == 2 && game[1][1] == 2 && game[2][1] == 2){
			gameWin = true;
		}
		if(game[0][2] == 2 && game[1][2] == 2 && game[2][2] == 2){
			gameWin = true;
		}
		if(game[0][0] == 2 && game[1][1] == 2 && game[2][2] == 2){
			gameWin = true;
		}
		if(game[0][2] == 2 && game[1][1] == 2 && game[2][0] == 2){
			gameWin = true;
		}
		
		//win or lose?
		if(gameWin = true && count == 1 || count == 3 || count == 5 || count == 7 || count == 9){
			JOptionPane.showInputDialog(replay, plyone, "Play Again?", DISPOSE_ON_CLOSE);
		}
		
		if(gameWin = true && count == 2 || count == 4 || count == 6 || count == 8){
			JOptionPane.showInputDialog(replay, plytwo, "Play Again?", DISPOSE_ON_CLOSE);
			//JOptionPane with play again? or play with computer AI? Select Level of AI? restart game?
		}
		
		//Player AI Chosen from menu
		//...
		
		
	}
}
]

then here is my Main class

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

public class Main {

	public static void main(String[] args){
		
		TTT ttt = new TTT();
		ttt.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		ttt.setSize(350, 350);
		ttt.setVisible(true);
		
		
	}
}



The Error the compiler gives me when i try to execute my program

Exception in thread "main" java.lang.NullPointerException
at TTT.<init>(TTT.java:28)
at Main.main(Main.java:9)

What the program is supposed to do is make 9 buttons, whenever a button is pushed it will change the value of the 2 dimensional array. as shown in the many if statements above the code will know when there is 3 in a row. once there is 3 in a row the game will set gameWin to true, and check the count, and if the count is 1,3,5,7,9 player1 will win and it will display the appropriate message for player1, if count is 2,4,6,8 then the same thing will happen with player2 instead of player one. if count gets to 10 it will be a cats game and then display a JOptionPane asking if you would like to play again.

Thank you.

This post has been edited by no2pencil: 10 June 2012 - 07:51 PM
Reason for edit:: Added code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Tic Tac Toe Game

#2 natecat  Icon User is offline

  • D.I.C Head

Reputation: 53
  • View blog
  • Posts: 225
  • Joined: 19-December 11

Re: Tic Tac Toe Game

Posted 10 June 2012 - 01:27 PM

Use code tags please.

And also, you need to initilize button variables by doing *ButtonName*=new JButton();
Was This Post Helpful? 0
  • +
  • -

#3 11joe4  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 10-June 12

Re: Tic Tac Toe Game

Posted 10 June 2012 - 01:35 PM

i thought i did initialize the buttons, and apologies for not using the tags

frame = new JFrame("Tic Tac Toe");
button1 = new JButton();
button1.setPreferredSize(new Dimension(75, 75));
frame.add(button1);
button2 = new JButton();
button2.setPreferredSize(new Dimension(75, 75));
frame.add(button2);
button3 = new JButton();
button3.setPreferredSize(new Dimension(75, 75));
frame.add(button3);
button4 = new JButton();
button4.setPreferredSize(new Dimension(75, 75));
frame.add(button4);
button5 = new JButton();
button5.setPreferredSize(new Dimension(75, 75));
frame.add(button5);
button6 = new JButton();
button6.setPreferredSize(new Dimension(75, 75));
frame.add(button6);
button7 = new JButton();
button7.setPreferredSize(new Dimension(75, 75));
frame.add(button7);
button8 = new JButton();
button8.setPreferredSize(new Dimension(75, 75));
frame.add(button8);
button9 = new JButton();
button9.setPreferredSize(new Dimension(75, 75));
frame.add(button9);

Was This Post Helpful? 0
  • +
  • -

#4 natecat  Icon User is offline

  • D.I.C Head

Reputation: 53
  • View blog
  • Posts: 225
  • Joined: 19-December 11

Re: Tic Tac Toe Game

Posted 10 June 2012 - 02:12 PM

add(start);
add(computerAI);

Where did you initialize these two variables?
Was This Post Helpful? 0
  • +
  • -

#5 11joe4  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 10-June 12

Re: Tic Tac Toe Game

Posted 10 June 2012 - 02:40 PM

View Postnatecat, on 10 June 2012 - 02:12 PM, said:

add(start);
add(computerAI);

Where did you initialize these two variables?


those variables where never initialized because the program is not finished yet, I'm just trying to get my program to execute so i can see the GUI.
from the code that is written there i can only assume that it should launch the main part of my program which is the tic tac toe game, the variables computerAI and start are all menu items not the game items.
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Tic Tac Toe Game

Posted 10 June 2012 - 07:28 PM

Please learn how to use array before trying a program like Tic Tac Toe
Was This Post Helpful? 0
  • +
  • -

#7 11joe4  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 10-June 12

Re: Tic Tac Toe Game

Posted 10 June 2012 - 08:14 PM

View Postpbl, on 10 June 2012 - 07:28 PM, said:

Please learn how to use array before trying a program like Tic Tac Toe

is that where i had gone wrong, because i have tried learning arrays by asking my professor but i still have a very hard time using them?
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Tic Tac Toe Game

Posted 10 June 2012 - 08:32 PM

Try that

class TTT extends JFrame implements ActionListener {
   JButton[][] b;

   TTT() {
      super("TicTacToe");
      setLayout(new GridLayout(3,3));

      b = new JButton[3][3];
      for(int i= 0; i < 3; ++i) {
        for(int j = 0; j < 3; ++j) {
           b[i][j] = new JButton();
           b[i][j].addActionListener(this);
           add(b[i][j])
        }
      }
    }

    public void actionPerformed(ActionEvent e) {
         Object o = e.getSource();
         for(int i = 0; i < 3;l i++) {
            for(int j = 0; j < 3; j++) {
               if(o == b[i][j]) {
                  System.out.println("Button " + I + "," + j + " was clicked");
                  return;
               }
            }
          }
    }

    public static void main(String[] arg) {
        TTT ttt = new TTT();
        ttt.setSize(300,300);
        ttt.setVisible(true);
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1