Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Tic Tac Toe problem help!

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    6
    My Mood
    Sad
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Tic Tac Toe problem help!

    Hello guys! This is what my code looks like at the moment:


    public class TicClass

    {

    private int playerTurn;

    private char playerMarker;


    public TicClass()

    {

    playerTurn = 1;

    playerMarker = ' ';

    }


    public char playerMarker() //Player assignments.

    {

    if(playerTurn == 1)

    {

    playerMarker = 'X';

    return 'X';

    }

    else

    {

    playerMarker = 'O';

    return 'O';

    }


    }


    public int getPlayer()

    {

    return playerTurn;

    }


    public void swapPlayers()

    {

    if(playerTurn == 1)

    {

    playerTurn = 2;

    }

    else

    {

    playerTurn = 1;

    }

    }

    }

    and:


    import javax.swing.JOptionPane;

    public class Tic

    {

    public static void main(String[] args)

    {

    TicClass t = new TicClass();

    char one = '1'; //Numbers for matrix.

    char two = '2';

    char three = '3';

    char four = '4';

    char five = '5';

    char six = '6';

    char seven = '7';

    char eight = '8';

    char nine = '9';

    int gameOver = 0;


    System.out.println("[" + one + "] [" + two + "] [" + three + "]");

    System.out.println("[" + four + "] [" + five + "] [" + six + "]");

    System.out.println("[" + seven + "] [" + eight + "] [" + nine + "]");

    System.out.println("");

    if(gameOver != 1)

    {


    for (int i = 0; i < 9; i++) // Game Loop

    {


    String playerMove = JOptionPane.showInputDialog("Choose a square player: " + t.getPlayer());

    int choice = Integer.parseInt(playerMove);


    if(choice == 1 && one != 'X'&& one != 'O') // Checks for valid moves

    {

    one = t.playerMarker();

    t.swapPlayers();

    }

    else if (choice == 2 && two != 'X'&& two != 'O')

    {

    two = t.playerMarker();

    t.swapPlayers();

    }

    else if (choice == 3 && three != 'X'&& three != 'O')

    {

    three = t.playerMarker();

    t.swapPlayers();

    }

    else if (choice == 4 && four != 'X'&& four != 'O')

    {

    four = t.playerMarker();

    t.swapPlayers();

    }

    else if (choice == 5 && five != 'X'&& five != 'O')

    {

    five = t.playerMarker();

    t.swapPlayers();

    }

    else if (choice == 6 && six != 'X'&& six != 'O')

    {

    six = t.playerMarker();

    t.swapPlayers();

    }


    else if (choice == 7 && seven != 'X'&& seven != 'O')

    {

    seven = t.playerMarker();

    t.swapPlayers();

    }

    else if (choice == 8 && eight != 'X'&& eight != 'O')

    {

    eight = t.playerMarker();

    t.swapPlayers();

    }

    else if (choice == 9 && nine != 'X'&& nine != 'O')

    {

    nine = t.playerMarker();

    t.swapPlayers();

    }


    else


    {

    System.out.println("Invalid Choice");

    i--;

    }


    System.out.println("[" + one + "] [" + two + "] [" + three + "]"); // Prints char. board

    System.out.println("[" + four + "] [" + five + "] [" + six + "]");

    System.out.println("[" + seven + "] [" + eight + "] [" + nine + "]");

    System.out.println("");

    // Checks for winners

    if((one == 'O' && four == 'O' && seven == 'O') || (one == 'X' && four == 'X' && seven == 'X')) //Vertical rows

    {

    gameOver = 1;

    System.out.println("Game Over!");

    System.exit(0);

    }

    else if((two == 'O' && five == 'O' && eight == 'O') || (two == 'X' && five == 'X' && eight == 'X'))

    {

    gameOver = 1;

    System.out.println("Game Over!");

    System.exit(0);

    }

    else if((three == 'O' && six == 'O' && nine == 'O') || (three == 'X' && six == 'X' && nine == 'X'))

    {

    gameOver = 1;

    System.out.println("Game Over!");

    System.exit(0);

    }

    else if((one == 'X' && two == 'X' && three == 'X') || (one == 'O' && two == 'O' && three == 'O')) //Horizontal rows

    {

    gameOver = 1;

    System.out.println("Game Over!");

    System.exit(0);

    }

    else if((four == 'X' && five == 'X' && six == 'X') || (four == 'O' && five == 'O' && six == 'O'))

    {

    gameOver = 1;

    System.out.println("Game Over!");

    System.exit(0);

    }

    else if((seven == 'X' && eight == 'X' && nine == 'X') || (seven == 'O' && eight == 'O' && nine == 'O'))

    {

    gameOver = 1;

    System.out.println("Game Over!");

    System.exit(0);

    } //Diagonal rows

    else if((one == 'X' && five == 'X' && nine == 'X') || (one == 'O' && five == 'O' && nine == 'O'))

    {

    gameOver = 1;

    System.out.println("Game Over!");

    System.exit(0);

    }

    else if((seven == 'X' && five == 'X' && three == 'X') || (seven == 'O' && five == 'O' && three == 'O'))

    {

    gameOver = 1;

    System.out.println("Game Over!");

    System.exit(0);

    }

    }


    }

    }


    }

    What I would like to do is instead of having two players, make the program select a random move for player two!

    I am guessing that I will need to use the java random generator, which would look something like:


    Random rand = new Random();
    int num = 1 + rand.nextInt(9);

    The problem is that I have no idea if this is the right code, where to put it, and how to implement the rest of it for my program to work!

    Any help or suggestions are appreciated!


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Tic Tac Toe problem help!

    Yikes, that sure is a lot of unformatted code- make sure you use the highlight tags to make code easier to read.

    Think about the flow of the program. When does the player take a turn? When does the computer make a turn? When should you check for wins? Sketch out a rough flow of the program and then fill in the pieces from there.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Sep 2012
    Posts
    6
    My Mood
    Sad
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Tic Tac Toe problem help!

    Ok! So I tried inserting the following if statement:

    if (t.getPlayer()==1)
    {

    String playerMove = JOptionPane.showInputDialog("Choose a square player: " + t.getPlayer());
    int choice = Integer.parseInt(playerMove);

    }

    else

    {

    Random rand = new Random();
    int choice = 1 + rand.nextInt(9);

    }

    But i get the following error: "choice cannot be resolved to a variable"

    How would I make it possible for choice to have different values depending on the player turn?

  4. #4
    Junior Member
    Join Date
    Sep 2012
    Posts
    6
    My Mood
    Sad
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Tic Tac Toe problem help!

    Oh and by the way, sorry that my code is not very neat and not highlighted, but I'm a beginner to java and the forum!

  5. #5
    Junior Member
    Join Date
    Sep 2012
    Posts
    6
    My Mood
    Sad
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Tic Tac Toe problem help!

    I found my mistake! I had to declare "choice" before the if statement. The next part of my assignment is to make this client-server but I'm guessing this isn't the right part of the forum for that!

  6. #6
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Tic Tac Toe problem help!

    Quote Originally Posted by vess28 View Post
    Oh and by the way, sorry that my code is not very neat and not highlighted, but I'm a beginner to java and the forum!
    Hi and welcome.
    See the announcements page for helpful forum tips.

Similar Threads

  1. help needed (tic tac toe problem)
    By vess28 in forum Java Networking
    Replies: 1
    Last Post: September 21st, 2012, 05:14 PM
  2. Tic-Tac-Toe Help
    By coke32 in forum Object Oriented Programming
    Replies: 13
    Last Post: March 12th, 2012, 07:59 AM
  3. tic tac toe game problem with X and O
    By woohooXX in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 20th, 2011, 07:34 AM
  4. Tic-Tac-Toe program problem help
    By MuffinMcFluffin in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 25th, 2009, 10:14 PM
  5. [SOLVED] Tic Toe java code exists when we enter 0 row and 0 column
    By big_c in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: May 13th, 2009, 07:16 AM