Results 1 to 6 of 6
  1. #1
    Laythe's Avatar
    Laythe is offline Member
    Join Date
    Mar 2009
    Posts
    28
    Rep Power
    0

    Default can you tell me what this line mean in this code?

    I found a Tic-Tac-Toe game code and i am tring to understand how it works, but i can understand and visualize how some lines works.

    This is the hole code of the game:
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    import java.util.*;
    
    public class TicTacToeOrigineCode implements ActionListener	{
    	final String VERSION = "1.0";
    //Setting up ALL the variables
    	JFrame window = new JFrame("Tic-Tac-Toe " + VERSION);
    
    	JMenuBar mnuMain = new JMenuBar();
    	JMenuItem 	mnuNewGame = new JMenuItem("New Game"),
    					mnuInstruction = new JMenuItem("Instructions"),
    					mnuExit = new JMenuItem("Exit"),
    					mnuAbout = new JMenuItem("About");
    
    	JButton 	btn1v1 = new JButton("Player vs Player"),
    				btn1vCPU = new JButton("Player vs CPU"),
    				btnBack = new JButton("<--back");
    	JButton btnEmpty[] = new JButton[10];
    
    	JPanel 	pnlNewGame = new JPanel(),      // pnlNewGame.add(btn1v1);  pnlNewGame.add(btn1vCPU);
    				pnlNorth = new JPanel(),    // pnlNorth.add(mnuMain); 1
    				pnlSouth = new JPanel(),    // pnlSouth.add(lblTitle);  pnlSouth.add(pnlTop);   pnlSouth.add(pnlBottom);
    				pnlTop = new JPanel(),      // pnlTop.add(pnlNewGame);  pnlTop.add(txtMessage);
    				pnlBottom = new JPanel(),   // pnlBottom.add(btnBack);
    				pnlPlayingField = new JPanel();
    	JLabel lblTitle = new JLabel("Tic-Tac-Toe");
    	JTextArea txtMessage = new JTextArea();
    
    	final int winCombo[][] = new int[][]	{
    		{1, 2, 3}, 			{1, 4, 7}, 		{1, 5, 9},
    		{4, 5, 6}, 			{2, 5, 8}, 		{3, 5, 7},
    		{7, 8, 9}, 			{3, 6, 9}
    /*Horizontal Wins*/	/*Vertical Wins*/ /*Diagonal Wins*/
    	};
    	final int X = 412, Y = 268, color = 190;
    	boolean inGame = false;
    	boolean win = false;
    	boolean btnEmptyClicked = false;
    	String message;
    	int turn = 1;
    	int wonNumber1 = 1, wonNumber2 = 1, wonNumber3 = 1;
    
    
    	public TicTacToeOrigineCode()	{	//Setting game properties and layout and sytle...
    	//Setting window properties:
    		window.setSize(X, Y);
    		window.setLocation(450, 260);
    		window.setResizable(false);
    		window.setLayout(new BorderLayout());
    		window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    	//Setting Panel layouts and properties
    		pnlNewGame.setLayout(new GridLayout(2, 1, 2, 10));
    		pnlNorth.setLayout(new FlowLayout(FlowLayout.CENTER));
    		pnlSouth.setLayout(new FlowLayout(FlowLayout.CENTER));
    
    		pnlNorth.setBackground(new Color(color-20, color-20, color-20));
    		pnlSouth.setBackground(new Color(color, color, color));
    
    		pnlTop.setBackground(new Color(color, color, color));
    		pnlBottom.setBackground(new Color(color, color, color));
    
    		pnlTop.setLayout(new FlowLayout(FlowLayout.CENTER));
    		pnlBottom.setLayout(new FlowLayout(FlowLayout.CENTER));
    		pnlNewGame.setBackground(Color.blue);
    
    	//Adding menu items to menu bar
    		mnuMain.add(mnuNewGame);
    		mnuMain.add(mnuInstruction);
    		mnuMain.add(mnuAbout);
    		mnuMain.add(mnuExit);//---->Menu Bar Complete
    
    	//Adding buttons to NewGame panel
    		pnlNewGame.add(btn1v1);
    		pnlNewGame.add(btn1vCPU);
    
    	//Adding Action Listener to all the Buttons and Menu Items
    		mnuNewGame.addActionListener(this);
    		mnuExit.addActionListener(this);
    		mnuInstruction.addActionListener(this);
    		mnuAbout.addActionListener(this);
    		btn1v1.addActionListener(this);
    		btn1vCPU.addActionListener(this);
    		btnBack.addActionListener(this);
    
    	//Setting up the playing field
    		pnlPlayingField.setLayout(new GridLayout(3, 3, 2, 2));
    		pnlPlayingField.setBackground(Color.black);
    		for(int i=1; i<=9; i++)	{
    			btnEmpty[i] = new JButton();
    			btnEmpty[i].setBackground(new Color(220, 220, 220));
    			btnEmpty[i].addActionListener(this);
    			pnlPlayingField.add(btnEmpty[i]);
    		}
    
    	//Adding everything needed to pnlNorth and pnlSouth
    		pnlNorth.add(mnuMain);
    		pnlSouth.add(lblTitle);
    
    	//Adding to window and Showing window
    		window.add(pnlNorth, BorderLayout.NORTH);
    		window.add(pnlSouth, BorderLayout.CENTER);
    		window.setVisible(true);
    	}
    
    
    //-------------------START OF ACTION PERFORMED CLASS-------------------------//
    	public void actionPerformed(ActionEvent click)	{
    		Object source = click.getSource();
    		for(int i=1; i<=9; i++)	{
    			if(source == btnEmpty[i] && turn <	10)	{
    				btnEmptyClicked = true;
    				if(!(turn % 2 == 0))
    					btnEmpty[i].setText("X");
    				else
    					btnEmpty[i].setText("O");
    				btnEmpty[i].setEnabled(false);
    				pnlPlayingField.requestFocus();
    				turn++;
    			}
    		}
    		if(btnEmptyClicked)	{
    			checkWin();
    			btnEmptyClicked = false;
    		}
    		if(source == mnuNewGame)	{
    			clearPanelSouth();
    			pnlSouth.setLayout(new GridLayout(2, 1, 2, 5));
    			pnlTop.add(pnlNewGame);
    			pnlBottom.add(btnBack);
    			pnlSouth.add(pnlTop);
    			pnlSouth.add(pnlBottom);
    
    		}
    		else if(source == btn1v1)	{
    			if(inGame)	{
    				int option = JOptionPane.showConfirmDialog(null, "If you start a new game," +
    				 	"your current game will be lost..." + "\n" +
    					"Are you sure you want to continue?",
    					"Quit Game?" ,JOptionPane.YES_NO_OPTION);
    				if(option == JOptionPane.YES_OPTION)	{
    					inGame = false;
    				}
    			}
    			if(!inGame)	{
    				btnEmpty[wonNumber1].setBackground(new Color(220, 220, 220));
    				btnEmpty[wonNumber2].setBackground(new Color(220, 220, 220));
    				btnEmpty[wonNumber3].setBackground(new Color(220, 220, 220));
    				turn = 1;
    				for(int i=1; i<10; i++)	{
    					btnEmpty[i].setText("");
    					btnEmpty[i].setEnabled(true);
    				}
    				win = false;
    				showGame();
    
    			}
    		}
    		else if(source == btn1vCPU)	{
    			JOptionPane.showMessageDialog(null, "Coming Soon!!");
    		}
    		else if(source == mnuExit)	{
    			int option = JOptionPane.showConfirmDialog(null, "Are you sure you want to exit?",
    			"Exit Game" ,JOptionPane.YES_NO_OPTION);
    			if(option == JOptionPane.YES_OPTION)
    				System.exit(0);
    		}
    		else if(source == mnuInstruction || source == mnuAbout)	{
    			clearPanelSouth();
    			String message = "";
    			txtMessage.setBackground(new Color(color, color, color));
    			if(source == mnuInstruction)	{
    				message = 	"Instructions:\n\n" +
    								"Your goal is to be the first player to get 3 X's or O's in a\n" +
    								"row. (horizontally, diagonally, or vertically)";
    			} else	{
    				message = 	"about:\n\n" +
    								"Title: Tic-Tac-Toe\n" +
    								"Author: Blmaster\n" +
    								"Version: " + VERSION + "\n";
    			}
    			txtMessage.setEditable(false);
    			txtMessage.setText(message);
    			pnlSouth.setLayout(new GridLayout(2, 1, 2, 5));
    			pnlTop.add(txtMessage);
    			pnlBottom.add(btnBack);
    			pnlSouth.add(pnlTop);
    			pnlSouth.add(pnlBottom);
    		}
    		else if(source == btnBack)	{
    			if(inGame)
    				showGame();
    			else	{
    				clearPanelSouth();
    				pnlSouth.setLayout(new FlowLayout(FlowLayout.CENTER));
    				pnlNorth.setVisible(true);
    				pnlSouth.add(lblTitle);
    			}
    		}
    		pnlSouth.setVisible(false);
    		pnlSouth.setVisible(true);
    	}
    //-------------------END OF ACTION PERFORMED CLASS-------------------------//
    
    
    /*
    		----------------------------------
    		Start of all the other methods.	|
    		----------------------------------
    */
    	public void showGame()	{	//	Shows the Playing Field
    										//	*IMPORTANT*- Does not start out brand new (meaning just shows what it had before)
    		clearPanelSouth();
    		inGame = true;
    		pnlSouth.setLayout(new BorderLayout());
    		pnlSouth.add(pnlPlayingField, BorderLayout.CENTER);
    		pnlPlayingField.requestFocus();
    	}
    
    	public void checkWin()	{	//	checks if there are 3 symbols in a row vertically, diagonally, or horizontally.
    										//	then shows a message and disables buttons.
    		for(int i=0; i<7; i++)	{
    			if(
    					!btnEmpty[winCombo[i][0]].getText().equals("") &&
    					btnEmpty[winCombo[i][0]].getText().equals(btnEmpty[winCombo[i][1]].getText()) &&
    					//								if {1 == 2 && 2 == 3}
    					btnEmpty[winCombo[i][1]].getText().equals(btnEmpty[winCombo[i][2]].getText())
    					/*
    						The way this checks the if someone won is:
    						First: it checks if the btnEmpty[x] is not equal to an empty string-	x being the array number
    							inside the multi-dementional array winCombo[checks inside each of the 7 sets][the first number]
    						Secong: it checks if btnEmpty[x] is equal to btnEmpty[y]- x being winCombo[each set][the first number]
    							y being winCombo[each set the same as x][the second number] (So basically checks if the first and
    							second number in each set is equal to each other)
    						Third: it checks if btnEmtpy[y] is eual to btnEmpty[z]- y being the same y as last time and z being
    							winCombo[each set as y][the third number]
    						Conclusion:	So basically it checks if it is equal to the btnEmpty is equal to each set of numbers
    					*/
    				)	{
    				win = true;
    				wonNumber1 = winCombo[i][0];
    				wonNumber2 = winCombo[i][1];
    				wonNumber3 = winCombo[i][2];
    				btnEmpty[wonNumber1].setBackground(Color.white);
    				btnEmpty[wonNumber2].setBackground(Color.white);
    				btnEmpty[wonNumber3].setBackground(Color.white);
    				break;
    			}
    		}
    		if(win || (!win && turn>9))	{
    			if(win)	{
    				if(turn % 2 == 0)
    					message = "X has won!";
    				else
    					message = "O has won!";
    				win = false;
    			}	else if(!win && turn>9)	{
    				message = "Both players have tied!\nBetter luck next time.";
    			}
    				JOptionPane.showMessageDialog(null, message);
    			for(int i=1; i<=9; i++)	{
    				btnEmpty[i].setEnabled(false);
    			}
    		}
    	}
    
    	public void clearPanelSouth()	{	//Removes all the possible panels
    												//that pnlSouth, pnlTop, pnlBottom
    												//could have.
    		pnlSouth.remove(lblTitle);
    		pnlSouth.remove(pnlTop);
    		pnlSouth.remove(pnlBottom);
    		pnlSouth.remove(pnlPlayingField);
    		pnlTop.remove(pnlNewGame);
    		pnlTop.remove(txtMessage);
    		pnlBottom.remove(btnBack);
    	}
    
    	public static void main(String[] args)	{
    		new TicTacToeOrigineCode();//	Calling the class construtor.
    
    	}
    }


    I cant figureout what the condition in the first if statement means
    Java Code:
    public void checkWin()	
    {	
    		for(int i=0; i<7; i++)	
            {
    			if(
    				   // what is this means?? :
                                       !btnEmpty[winCombo[i][0]].getText().equals("") &&
    			            btnEmpty[winCombo[i][0]].getText().equals(btnEmpty[winCombo[i][1]].getText()) &&
    				    btnEmpty[winCombo[i][1]].getText().equals(btnEmpty[winCombo[i][2]].getText())
    					
    				)	
                {
    				win = true;
    				wonNumber1 = winCombo[i][0];
    				wonNumber2 = winCombo[i][1];
    				wonNumber3 = winCombo[i][2];
    				btnEmpty[wonNumber1].setBackground(Color.white);
    				btnEmpty[wonNumber2].setBackground(Color.white);
    				btnEmpty[wonNumber3].setBackground(Color.white);
    				break;
    			}
    		}
    		
            if(win || (!win && turn>9))	
            {
    			if(win)	
                {
    				if(turn % 2 == 0)
                    {
                        message = "X has won!";
                    }
    					
    				else
                    {
                        message = "O has won!";
                    }
    					
    				win = false;
    			}	
                else 
                    if(!win && turn>9)	
                    {
                        message = "Both players have tied!\nBetter luck next time.";
                    }
                
                JOptionPane.showMessageDialog(null, message);
                
                for(int i=1; i<=9; i++)
                {
                    btnEmpty[i].setEnabled(false);
                }
    		}
    	}

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Er, read the comments in the code.

  3. #3
    Laythe's Avatar
    Laythe is offline Member
    Join Date
    Mar 2009
    Posts
    28
    Rep Power
    0

    Default

    Quote Originally Posted by r035198x View Post
    Er, read the comments in the code.
    come on dont you think i read it and i couldnt understands it so thats why i posted this thread.

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    The comment for the if statement is pretty comprehensive.
    If I were to explain that if I would be posting what is already in those comments so you wouldn't understand that explanation as well.

  5. #5
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    5

    Default

    Java Code:
    if(
    // what is this means?? :
    !btnEmpty[winCombo[i][0]].getText().equals("") &&
    btnEmpty[winCombo[i][0]].getText().equals(btnEmpty[winCombo[i][1]].getText()) &&
    btnEmpty[winCombo[i][1]].getText().equals(btnEmpty[winCombo[i][2]].getText()))
    Well, break it down.

    First off, you have winCombo[i][0..2]. This must be an int[][] since it is used as the index for btnEmpty.

    Then, you have btnEmpty. The "btn" part would suggest that it is a Button of some form, and you have an array of them.

    The variable "i" is usually used to mean a row. Since, we're talking about a tic-tac-toe board, it has three rows and three columns, so the [0..2] for winCombo means we are talking about the entire row.


    Now, break down the if. The first part says the first button's text is not empty (i.e. an "X" or "O", since we are talking about tic-tac-toe). The second part says that the button's text for the first column, equals the button's text for the second. Finally, the third part says that the button's text for the second column equals the button's text for the third. In other words, all three values are the same (and not empty) - ex. "XXX" or "OOO".

    So, the above if statement is to check whether that row is a win for either X or O.
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

  6. #6
    Laythe's Avatar
    Laythe is offline Member
    Join Date
    Mar 2009
    Posts
    28
    Rep Power
    0

    Default

    Thank you Sir CodesAway for the explanation, it's awesome :D
    A +rep from me to Sir CodesAway :)

Similar Threads

  1. Replies: 4
    Last Post: 09-10-2009, 10:18 AM
  2. Replies: 9
    Last Post: 04-07-2009, 03:51 AM
  3. How to return to next line of code after calling a public class?
    By devdevi@comcast.net in forum New To Java
    Replies: 1
    Last Post: 02-02-2009, 06:46 PM
  4. what does this line of code mean?
    By diggitydoggz in forum New To Java
    Replies: 7
    Last Post: 01-25-2009, 04:52 PM
  5. What does this line of code mean?
    By naxalyte in forum New To Java
    Replies: 1
    Last Post: 11-23-2007, 09:28 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •