Results 1 to 4 of 4
  1. #1
    hoangx87 is offline Member
    Join Date
    Mar 2011
    Posts
    44
    Rep Power
    0

    Default another tic tac toe issue

    THE GAME COMPILE AND RUN FINE. BUT THE ISSUE I HAVE NOW IS THAT I CAN't get it to determines who win.


    import javax.swing.JFrame;

    import javax.swing.*;

    public class TicTacToeGame extends JFrame{

    public TicTacToeGame(){
    JFrame frame = new JFrame();//Create FRAME
    setSize(500, 500);
    setLocationRelativeTo(null);
    setDefaultCloseOperation(EXIT_ON_CLOSE);


    GridLayout layout = new GridLayout(3, 3);//Create 3x3 Grid
    setLayout(layout);
    Listener listener = new Listener();
    for (int i = 0; i < 9; i++) {
    JButton b = new JButton();
    b.addActionListener(listener);
    add(b);
    }

    }



    public static void main (String[]args){
    TicTacToeGame frame = new TicTacToeGame();
    frame.setVisible(true);

    }
    }


    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class Listener implements ActionListener {

    int count = 0;
    ImageIcon[] icon;
    ImageIcon cross = new ImageIcon("cross.gif");
    ImageIcon not= new ImageIcon("not.gif");

    public void actionPerformed(ActionEvent e) {
    JButton b = (JButton)e.getSource();
    if (b.getText().equals("")) {
    if (count % 2 == 0) {

    b.setIcon(cross);
    } else {
    b.setIcon(not);
    }
    count++;

    }
    }
    }

  2. #2
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    1. read the board into a 2d-array
    2. if there is 3 in a row on the board -> the game has ended
    3. whoever got 3 in a row won

  3. #3
    hoangx87 is offline Member
    Join Date
    Mar 2011
    Posts
    44
    Rep Power
    0

    Default

    ozzy, i understand what you mean, but I have no idea how to write the code. I just know that it would be like if {1,2,3} ...... Win

  4. #4
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    i didn't write more than that because it seemed like you were asking someone to do it for you... but here's something to start you off:

    1. compile a List of the contents of each board square.
    by 'contents' i mean some value which can be used to evaluate the winner later on

    Java Code:
        String[][] myBoard = new String[3][3];
        myBoard[0][0] = someObject1.someValue;
        myBoard[0][1] = someObject2.someValue;
        ...
        myBoard[2][2] = someObject9.someValue;

    2. encapsulate the list into some method so that you can get an updated board on demand

    Java Code:
    public String[][] getCurrentBoard() {
        //compile board array
        ...
        return myBoard;
    }

    3. Make a blank method to take your board to check the values
    I've called it evaluateMyBoard() but you can call it for e.g. findWinner()
    or whatever is easiest for you to read

    Java Code:
    public void evaluateMyBoard(String[][] board) {
        //find the winner
    }

    4. To find a winner, you need to search the board using loops
    You can choose to do this in any way you want but the idea is the same:
    compile a sequence of 3 squares in a row, check if all 3 match,
    if they match then go to step 5.

    Java Code:
    //loop through 2d-board
    for (int i=0; i<board.length; i++) {
        for (int j=0; j<board[i].length; j++) {
            //get each square to do something (add to a list?)
        }
        //after each row do something (check each item in the list?)
    }

    Because your board is small, you could just do this...
    Java Code:
    String row1 = board[0][0] + board[0][1] + board[0][2];
    String diag1 = board[0][0] + board[1][1] + board[2][2];
    ...for each combination instead of looping, then proceed to step 5.


    5. if we have a match, determine the winner and end the game
    The winner would be the player whose objects it was that matches,
    and the rest is straight-forward

Similar Threads

  1. JSP issue
    By RamaLakshmi in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 01-21-2011, 02:55 PM
  2. Having an issue with this
    By baap30 in forum New To Java
    Replies: 5
    Last Post: 04-23-2010, 06:32 AM
  3. Issue
    By FlashNinja in forum New To Java
    Replies: 20
    Last Post: 11-28-2009, 10:44 PM
  4. PDF Box issue
    By jazz2k8 in forum Advanced Java
    Replies: 0
    Last Post: 03-20-2009, 12:04 PM
  5. Issue
    By chaitu444 in forum New To Java
    Replies: 2
    Last Post: 11-06-2007, 08:49 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
  •