I made this simple program for a 2 player TiCTacToe game.
But It doesnt seem to be working properly. I cant understand whats going wrong here!
Please help...

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


public class MainFrame extends JFrame
{
	
	JButton [][] buttons= new JButton[3][3];
	JTextField statusBar;
	GamePanel panel;
	Integer turn;
	GameListener listener=new GameListener();
	
	
	public MainFrame()
	{
		setLayout(new BorderLayout());
		
		panel=new GamePanel();
		add(panel,BorderLayout.CENTER);
		
		statusBar=new JTextField("Player1's Turn");
		statusBar.setEditable(false);
		add(statusBar,BorderLayout.SOUTH);
		
		setTitle("Tic Tac Toe!");
		setVisible(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(400,400,300,300);
	}
	
	class GamePanel extends JPanel
	{
		
		public GamePanel()
		{
			setLayout(new GridLayout(3,3));
			turn =1;
			for(int i=0;i<3;i++)
				for(int j=0;j<3;j++)
				{
					buttons[i][j]=new JButton();
					buttons[i][j].putClientProperty("INDEX", new Integer[]{i,j});
					buttons[i][j].putClientProperty("OWNER", new Integer(i+10));
					buttons[i][j].addActionListener(listener);
					add(buttons[i][j]);
				}
		}
	}
	
	class GameListener implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			JButton b=(JButton)e.getSource();
			System.out.println(turn);	//turn
			Integer[]index=(Integer[]) b.getClientProperty("INDEX");
			buttons[index[0]][index[1]].putClientProperty("OWNER", turn);
			b.setIcon(new ImageIcon(turn.toString()+".gif"));
			b.setEnabled(false);
			boolean result=checkVictoryCondition(index);
			if(result)
			{
				JOptionPane.showMessageDialog(null, "Player "+turn.toString()+" Wins");
				initComponents();
			}
			else
			{
				if(turn==1)
				{
					turn=2;
					statusBar.setText("Player2's Turn");
				}
				else
				{
					assert turn==2;
					turn=1;
					statusBar.setText("Player2's Turn");
				}
			}
				
				
		}
		
		Integer getOwner(JButton b)
		{
			return (Integer)b.getClientProperty("OWNER");
		}
		
		boolean checkVictoryCondition(Integer [] index)
		{
			Integer[][]buttonMap=new Integer[][] { 
					{ getOwner(buttons[0][0]),getOwner(buttons[0][1]),getOwner(buttons[0][2])},
					{ getOwner(buttons[1][0]),getOwner(buttons[1][1]),getOwner(buttons[1][2])},
					{ getOwner(buttons[2][0]),getOwner(buttons[2][1]),getOwner(buttons[2][2])}
			};
			
			Integer a=index[0];
			Integer b=index[1];
			int i;
			
			//check row
			for(i=0;i<3;i++)
			{
				if(buttonMap[a][i]!=buttonMap[a][b])
					break;
			}
			if(i==3)
				return true;
			
			//check column
			for(i=0;i<3;i++)
			{
				if(buttonMap[i][b]!=buttonMap[a][b])
					break;
			}
			if(i==3)
				return true;
			
			//check diagonal
			if(a==2&&b==2)
			{
				//right diagonal
				if((buttonMap[0][2]==buttonMap[a][b])&&(buttonMap[2][0]==buttonMap[a][b]))
					return true;
				//left diagonal
				else if((buttonMap[0][0]==buttonMap[a][b])&&(buttonMap[2][2]==buttonMap[a][b]))
					return true;
			}
			
			return false;
				
		}
	}
	
	void initComponents()
	{
		for(int i=0;i<3;i++)
			for(int j=0;j<3;j++)
			{
				buttons[i][j].putClientProperty("INDEX", new Integer[]{i,j});
				buttons[i][j].putClientProperty("OWNER", new Integer(i+10));
				buttons[i][j].setIcon(null);
				buttons[i][j].setEnabled(true);
				turn=1;
				statusBar.setText("Player2's Turn");
				
			}
	}

}

You're not exactly giving us a lot of info on what it is that's not working "properly". If you want help, please start by posting a detailed and precise description of exactly how the actual behaviour differs from the expected behaviour.

ALright sorry. Was in a hurry. Im back now.

I get a frame with the buttons
and when I press a button, the icon doesnt appear.
And then the game is not functioning properly at all.

eg. the turn is changing but the status bar is not updating at all....
And the victory conditions are not working properly.. But the algorithm/technique used is correct i think...

Put lots of print statements in your action listener so you can see which statements are being executed and what the values of the variables are

oh well, I made an error in initializing the "OWNER" clientProperty. Fixed it and the game is working fine!

here's the code:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.logging.Logger;


public class MainFrame extends JFrame
{
	
	JButton [][] buttons= new JButton[3][3];
	JTextField statusBar;
	GamePanel panel;
	Integer turn;
	GameListener listener=new GameListener();
	Integer count;
	
	
	public MainFrame()
	{
		setLayout(new BorderLayout());
		
		panel=new GamePanel();
		add(panel,BorderLayout.CENTER);
		
		statusBar=new JTextField("Player1's Turn");
		statusBar.setEditable(false);
		add(statusBar,BorderLayout.SOUTH);
		
		setTitle("Tic Tac Toe!");
		setVisible(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(400,400,300,300);
	}
	
	class GamePanel extends JPanel
	{
		
		public GamePanel()
		{
			setLayout(new GridLayout(3,3));
			turn =1;
			count=0;
			for(int i=0;i<3;i++)
				for(int j=0;j<3;j++)
				{
					buttons[i][j]=new JButton();
					buttons[i][j].putClientProperty("INDEX", new Integer[]{i,j});
					buttons[i][j].putClientProperty("OWNER", null);
					buttons[i][j].addActionListener(listener);
					add(buttons[i][j]);
				}
		}
	}
	
	class GameListener implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			count++;			
			JButton b=(JButton)e.getSource();
			Integer[]index=(Integer[]) b.getClientProperty("INDEX");
			
			//System.out.println(turn);	//turn       											//<-----  
			//System.out.println("["+index[0]+"]"+"["+index[1]+"]");  							//<-----
			
			b.putClientProperty("OWNER", turn);
			Icon ico=new ImageIcon(turn.toString()+".gif");
			b.setIcon(ico);
			b.setEnabled(false);
			boolean result=checkVictoryCondition(index);
			if(result)
			{
				JOptionPane.showMessageDialog(null, "Player "+turn.toString()+" Wins");
				initComponents();
			}
			else
			{
				if(turn==1)
				{
					turn=2;
					statusBar.setText("Player2's Turn");
				}
				else
				{
					turn=1;
					statusBar.setText("Player1's Turn");
				}
			}
			if(count==9)
			{
				JOptionPane.showMessageDialog(null, "Match is a draw!");
				initComponents();

			}
				
				
		}
		
		Integer getOwner(JButton b)
		{
			return (Integer)b.getClientProperty("OWNER");
		}
		
//		void printbuttonMap(Integer [][]bMap)
//		{
//			for(int i=0;i<3;i++) {
//				for(int j=0;j<3;j++)
//					System.out.print(bMap[i][j]+" ");
//				System.out.println("");
//			}
//		}
		
		boolean checkVictoryCondition(Integer [] index)
		{
//			Integer[][]buttonMap=new Integer[][] { 
//					{ getOwner(buttons[0][0]),getOwner(buttons[0][1]),getOwner(buttons[0][2])},
//					{ getOwner(buttons[1][0]),getOwner(buttons[1][1]),getOwner(buttons[1][2])},
//					{ getOwner(buttons[2][0]),getOwner(buttons[2][1]),getOwner(buttons[2][2])}
//			};
			
//			printbuttonMap(buttonMap);
			
			Integer a=index[0];
			Integer b=index[1];
			int i;
			
			//check row
			for(i=0;i<3;i++)
			{
				if(getOwner(buttons[a][i])!=getOwner(buttons[a][b]))
					break;
			}
			if(i==3)
				return true;
			
			//check column
			for(i=0;i<3;i++)
			{
				if(getOwner(buttons[i][b])!=getOwner(buttons[a][b]))
					break;
			}
			if(i==3)
				return true;
			
			//check diagonal
			if(a==2&&b==2)
			{
				//right diagonal
				if((getOwner(buttons[0][2])==getOwner(buttons[a][b])&&(getOwner(buttons[2][0])==getOwner(buttons[a][b]))))
					return true;
				//left diagonal
				else if((getOwner(buttons[0][0])==getOwner(buttons[a][b]))&&(getOwner(buttons[2][2])==getOwner(buttons[a][b])))
					return true;
			}
			
			return false;
				
		}
	}
	
	void initComponents()
	{
		for(int i=0;i<3;i++)
			for(int j=0;j<3;j++)
			{
				buttons[i][j].putClientProperty("INDEX", new Integer[]{i,j});
				buttons[i][j].putClientProperty("OWNER",null);
				buttons[i][j].setIcon(null);
				buttons[i][j].setEnabled(true);
				turn=1;
				count=0;
				statusBar.setText("Player1's Turn");
				
			}
	}

}

Now i want to convert it into a network game. I want to use RMI becoz TCP would be a headache. Any suggestions? Tutorials for RMI? Any ideas... please....

did you fix the testing for winner logic?
If you click buttons: 1, 5 and 9 (diagonal upper left to lower right) it says Winner.

Fixed bro...

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.logging.Logger;


public class MainFrame extends JFrame
{
	
	JButton [][] buttons= new JButton[3][3];
	JTextField statusBar;
	GamePanel panel;
	Integer turn;
	GameListener listener=new GameListener();
	Integer count;
	
	
	public MainFrame()
	{
		setLayout(new BorderLayout());
		
		panel=new GamePanel();
		add(panel,BorderLayout.CENTER);
		
		statusBar=new JTextField("Player1's Turn");
		statusBar.setEditable(false);
		add(statusBar,BorderLayout.SOUTH);
		
		setTitle("Tic Tac Toe!");
		setVisible(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(400,400,300,300);
	}
	
	class GamePanel extends JPanel
	{
		
		public GamePanel()
		{
			setLayout(new GridLayout(3,3));
			turn =1;
			count=0;
			for(int i=0;i<3;i++)
				for(int j=0;j<3;j++)
				{
					buttons[i][j]=new JButton();
					buttons[i][j].putClientProperty("INDEX", new Integer[]{i,j});
					buttons[i][j].putClientProperty("OWNER", null);
					buttons[i][j].addActionListener(listener);
					add(buttons[i][j]);
				}
		}
	}
	
	class GameListener implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			count++;			
			JButton b=(JButton)e.getSource();
			Integer[]index=(Integer[]) b.getClientProperty("INDEX");
			
			//System.out.println(turn);	//turn       											//<-----  
			System.out.println("["+index[0]+"]"+"["+index[1]+"]");  							//<-----
			
			b.putClientProperty("OWNER", turn);
			Icon ico=new ImageIcon(turn.toString()+".gif");
			b.setIcon(ico);
			b.setEnabled(false);
			boolean result=checkVictoryCondition(index);
			if(result)
			{
				JOptionPane.showMessageDialog(null, "Player "+turn.toString()+" Wins");
				initComponents();
			}
			else
			{
				if(turn==1)
				{
					turn=2;
					statusBar.setText("Player2's Turn");
				}
				else
				{
					turn=1;
					statusBar.setText("Player1's Turn");
				}
			}
			if(count==9)
			{
				JOptionPane.showMessageDialog(null, "Match is a draw!");
				initComponents();

			}
				
				
		}
		
		Integer getOwner(JButton b)
		{
			return (Integer)b.getClientProperty("OWNER");
		}
		
		void printbuttonMap(Integer [][]bMap)
		{
			for(int i=0;i<3;i++) {
				for(int j=0;j<3;j++)
					System.out.print(bMap[i][j]+" ");
				System.out.println("");
			}
		}
		
		boolean checkVictoryCondition(Integer [] index)
		{
			Integer[][]buttonMap=new Integer[][] { 
					{ getOwner(buttons[0][0]),getOwner(buttons[0][1]),getOwner(buttons[0][2])},
					{ getOwner(buttons[1][0]),getOwner(buttons[1][1]),getOwner(buttons[1][2])},
					{ getOwner(buttons[2][0]),getOwner(buttons[2][1]),getOwner(buttons[2][2])}
			};
			
			printbuttonMap(buttonMap);
			
			Integer a=index[0];
			Integer b=index[1];
			int i;
			
			//check row
			for(i=0;i<3;i++)
			{
				if(getOwner(buttons[a][i])!=getOwner(buttons[a][b]))
					break;
			}
			if(i==3)
				return true;
			
			//check column
			for(i=0;i<3;i++)
			{
				if(getOwner(buttons[i][b])!=getOwner(buttons[a][b]))
					break;
			}
			if(i==3)
				return true;
			
			//check diagonal
			if((a==2&&b==2)||(a==0&&b==0)||(a==1&&b==1)||(a==0&&b==2)||(a==2&&b==0))
			{
				//left diagonal
				for(i=0;i<3;i++)
					if(getOwner(buttons[i][i])!=getOwner(buttons[a][b]))
						break;
				if(i==3)
					return true;
				
				//right diagonal
				if((getOwner(buttons[0][2])==getOwner(buttons[a][b]))&&(getOwner(buttons[1][1])==getOwner(buttons[a][b]))&&(getOwner(buttons[2][0])==getOwner(buttons[a][b])))
					return true;
				
				
				/*
				//right diagonal
				if((getOwner(buttons[0][2])==getOwner(buttons[a][b])&&(getOwner(buttons[2][0])==getOwner(buttons[a][b])))){
					System.out.println("right diag pass");return true;}
				//left diagonal
				else if((getOwner(buttons[0][0])==getOwner(buttons[a][b]))&&(getOwner(buttons[2][2])==getOwner(buttons[a][b])))
					{System.out.println("left diag pass");return true;}
			} */
				}
		//	System.out.println("diagonalFail");
			return false;
				
		}
	}
	
	void initComponents()
	{
		for(int i=0;i<3;i++)
			for(int j=0;j<3;j++)
			{
				buttons[i][j].putClientProperty("INDEX", new Integer[]{i,j});
				buttons[i][j].putClientProperty("OWNER",null);
				buttons[i][j].setIcon(null);
				buttons[i][j].setEnabled(true);
				turn=1;
				count=0;
				statusBar.setText("Player1's Turn");
				
			}
	}

}

stevanity Where is your main class?

I have it in another file. I have no problem with the program. My question is solved. But I want to create a 2 player version with RMI. So It would be nice if anyone could guide me where to start...

An idea. Write a two screen version of the program where X plays on one and O on the other. Doing this would isolate what state each screen needs to be in and what information needs to be passed between the two classes that control each playing screen.

commented: Very helpful and useful advice. +6
commented: Thanks. +0

I see I see. You mean, I need to create a two frame version for my app so that I might know what data to transmit. Thanks thanks. Thats a great start! Ill work on it now.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.