Results 1 to 5 of 5
  1. #1
    rahul_swe23 is offline Member
    Join Date
    Jan 2009
    Posts
    3
    Rep Power
    0

    Default please help in java code (simple)

    hi i am very new in java programing.i try to write java tic-tac-toe game with help of book where i am not able to understand some code(High Lighted).
    Pls help me to explain

    Thanks

    public class TicTacToe extends JApplet implements
    ActionListener{

    /**
    * Initialization method that will be called after the applet is loaded
    * into the browser.
    */
    // Assign different JJButton and Label
    JButton squares[];
    //JButton NewGameJButton;
    JLabel score;

    int emptySquaresLeft= 9;
    private JButton newGameButton;

    @Override
    public void init() { // this method is applet constructor


    // create applicant content pane all window go there
    Container appletContent = this.getContentPane();
    // BorderLayout bd = new BorderLayout()
    //this.setLayout(bd);

    // set applet layout manager with font and color
    appletContent.setLayout(new BorderLayout());
    appletContent.setBackground(Color.cyan);

    // change font and make bold 2o
    Font appletFont = new Font("Monospased", Font.BOLD, 20 );

    appletContent.setFont(appletFont);

    // create new game JButton
    JButton newGameButton = new JButton("New Gamee");
    // rgister this JButton with action listener
    newGameButton.addActionListener( this);

    // create JPanel for NewGameJButton
    JPanel topJPanel = new JPanel();
    // add this JButton with this pane
    topJPanel.add(newGameButton);

    // place the top JPanel
    appletContent.add(topJPanel,"North");

    // JPanel for JButton
    JPanel centerPanel = new JPanel();
    // set layout for this JPanel
    centerPanel.setLayout(new GridLayout(3,3));
    //palce this JPanel
    appletContent.add(centerPanel , "Center");

    // create new label
    score = new JLabel("----YOUR TURN---");
    appletContent.add(score,"South");

    // create the array to hold 9 JButton
    squares = new JButton[9];

    //JButton squares[];
    //squares = new JButton[9]; we can define this way also

    // initiate the JButton store the references to them in the array
    // register them with listener, paint them

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

    squares[i]= new JButton(); // register with JButton
    squares[i].addActionListener( this);
    //Set the color to each square
    squares[i].setBackground(Color.ORANGE);

    // now add that square (button)in centerpanel
    centerPanel.add(squares[i]);


    }



    }
    // thsi method will perform and action

    public void actionPerformed(ActionEvent e ){

    // create new button for game and add get source so that it can inform

    JButton theButton = (JButton)e.getSource();

    // place all button

    if (theButton == newGameButton){ // if this is the new game button its mean
    // if we press newgame button the later condition will perfome
    for (int i= 0; i < 9 ; i++){
    squares[i].setEnabled(true) ;
    squares[i].setText(" ");
    squares[i].setBackground(Color.ORANGE);


    }

    emptySquaresLeft = 9;
    score.setText("You won");
    newGameButton.setEnabled(false);
    return; // exit method here

    }

    // winner define as a string
    String winner = " ";



    // is this one of the square
    for (int i = 0; i < 9 ; i++){ // check each square
    if (theButton == squares[i] ){
    squares[i].setLabel("X");
    winner = lookForWinner(); //

    if(!"".equals(winner)){

    endTheGame();
    }
    else {
    computerMove();
    winner = lookForWinner();

    if (!"".equals(winner));{
    endTheGame();

    }


    }
    break;

    }
    } // end of loop

    if(winner.equals("O")){
    score.setText("you won");
    } else if (winner.equals("Y")){

    score.setText("Ypu lost ");
    }else if ( winner.equals("T")){
    score.setText("Tie");
    }

    } // end the action performed



    /**
    * This method is called after every move to see
    * if we have a winner. It checks every row, column
    * and diagonal to find out three squares with the
    * same label (other than blank)
    * @return "X", "O", "T" for tie or "" for no winner
    */
    String lookForWinner() {

    String theWinner = " ";
    emptySquaresLeft-- ;

    if (emptySquaresLeft == 0){

    return " T"; // its tie
    }
    // first row

    if(!squares[0].getLabel().equals("") &&
    squares[0].getLabel().equals(squares[1].getLabel())

    && squares[0].getLabel().equals(squares[2].getLabel())){

    theWinner = squares[0].getLabel();
    highlightWinner(0,1,2);

    }
    // second row
    else if(! squares [3].getLabel().equals("") &&
    squares[3].getLabel().equals(squares[4].getLabel())
    && squares[3].getLabel().equals(squares[5].getLabel())) {

    theWinner = squares[3].getLabel();
    highlightWinner (3,4,5);

    }

    else if (!squares[6].getLabel().equals("") &&
    squares[6].getLabel().equals(squares[7].getLabel())
    && squares[6].getLabel().equals(squares[8].getLabel())){

    theWinner = squares[6].getLabel();
    highlightWinner(6,7,8);


    }
    else if (! squares[0].getLabel().equals("") &&
    squares[0].getLabel().equals(squares[4].getLabel()) &&
    squares[0].getLabel().equals(squares[8].getLabel())) {

    theWinner = squares[0].getLabel();
    highlightWinner(0,4,8);

    }

  2. #2
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    much better... somebody should have told you to use code tags as well...

    what exactly about this code that you don't understand?
    it seems pretty straight forward.
    Java Code:
    if(!squares[0].getLabel().equals("") &&                       // condition
         squares[0].getLabel().equals(squares[1].getLabel())  &&     // condition
         squares[0].getLabel().equals(squares[2].getLabel()))         // condition
    {
         theWinner = squares[0].getLabel();          // assignment
         highlightWinner(0,1,2);                          // method call
    }
    * oh, i broke the two space-tab rule!
    Last edited by angryboy; 01-26-2009 at 04:01 AM.
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Hm, this doesn't compile for me as it appears to be missing some methods, perhaps more. Also, as noted above, you would help us by posting code tags so that your code will retain its formatting and be readable. To do this, you will need to paste already formatted code into the forum, highlight this code, and then press the "code" button at the top of the forum Message editor prior to posting the message. Another way is to place the tag &#91;code] at the top of your block of code and the tag &#91;/code] at the bottom, like so:

    Java Code:
    &#91;code]
      // your code block goes here.
      // note the differences between the tag at the top vs the bottom.
    &#91;/code]

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    the "highlighted" code is where you're checking a JLabel array to see if all of the text on the 3 labels are the same -- either all "O" or all "X" to see if you have a winner. the JLabel method getText() returns the text on the label as a String. The String method equals(...) checks to see if the one String holds the same characters as the other String. You'll want to read up on Swing at the Sun Swing tutorial to learn more about this. You can find this here:

    Lesson: Using Swing Components (The Java™ Tutorials > Creating a GUI with JFC/Swing)

  5. #5
    rahul_swe23 is offline Member
    Join Date
    Jan 2009
    Posts
    3
    Rep Power
    0

Similar Threads

  1. Simple code. Could you help pls..?
    By Iliyas in forum New To Java
    Replies: 8
    Last Post: 12-26-2008, 03:17 AM
  2. Simple code help
    By rednose in forum New To Java
    Replies: 11
    Last Post: 11-30-2008, 06:02 AM
  3. Please help me this is simple bit of code
    By BlitzAcez in forum New To Java
    Replies: 4
    Last Post: 11-27-2008, 05:52 AM
  4. A simple Java source code viewer
    By Java Tip in forum SWT
    Replies: 0
    Last Post: 07-11-2008, 04:34 PM
  5. problem with a simple java code
    By boy22 in forum New To Java
    Replies: 2
    Last Post: 08-03-2007, 02:46 AM

Posting Permissions

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