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 3 of 3

Thread: Tic Tac Toe = Naming Winning Player & Restarting Program

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Tic Tac Toe = Naming Winning Player & Restarting Program

    Hey,

    So I have been working on a project for a simple console Tic Tac Toe game, and I'm pretty much all set with the code. However, I have two small problems, and that's where I turn to you guys.

    First off, here's my code:

     
    package anthonytictactoe;
    import java.util.Scanner;
     
    public class Main {
     
        public static void main(String[] args) {
            int row;
            int col;
            char X;
            char O;
            boolean winner = false;
            Scanner in = new Scanner(System.in);
            System.out.println("Welcome to Tic Tac Toe!");
            System.out.println("Instructions: The top row is 0, middle is 1, "
                    + "and bottom is 2.");
            System.out.println("The leftmost column is 0, the middle is 1, "
                    + "and the rightmost is 2.");
     
            char[][] x = new char [3][3];
     
     
              for(row = 0; row < 3; row++){
                  for(col = 0; col < 3; col++){
                      x[row][col] = '*';
                  }
            }
              for(row = 0; row < 3; row++){
                   for(col = 0; col < 3; col++){
                      System.out.print(x[row][col] + " ");
     
              }
                   System.out.println();
              }
           do{
             do{
              System.out.print("Player X, what row?: ");
                  row = in.nextInt();
              System.out.print("Player X, what column?: ");
                  col = in.nextInt();
                if (x[row][col] == 'X' || x[row][col] == 'O'){
                 System.out.println("Space is already taken, choose again.");
                 x[row][col] = 10;}
     
     
             } while (x[row][col] == 10);
     
            x[row][col] = 'X';
            for (row = 0; row < 3; row++){
                for(col = 0; col < 3; col++){
                    System.out.print(x[row][col] + " ");
                }
                  System.out.println();
            }
            //Check to see if there is a winner
     
            if (x[0][0] == 'X' && x[0][1] == 'X' && x[0][2] == 'X')
                winner = true;
            else if (x[1][0] == 'X' && x[1][1] == 'X' && x[1][2] == 'X')
                winner = true;
            else if (x[2][0] == 'X' && x[2][1] == 'X' && x[2][2] == 'X')
                winner = true;
            else if (x[0][0] == 'X' && x[1][0] == 'X' && x[2][0] == 'X')
                winner = true;
            else if (x[0][1] == 'X' && x[1][1] == 'X' && x[2][1] == 'X')
                winner = true;
            else if (x[0][2] == 'X' && x[1][2] == 'X' && x[2][2] == 'X')
                winner = true;
            else if (x[0][0] == 'X' && x[1][1] == 'X' && x[2][2] == 'X')
                winner = true;
            else if (x[0][2] == 'X' && x[1][1] == 'X' && x[2][0] == 'X')
                winner = true;
     
            do{
              System.out.print("Player O, what row?: ");
                  row = in.nextInt();
              System.out.print("Player O, what column?: ");
                  col = in.nextInt();
                if (x[row][col] == 'X' || x[row][col] == 'O'){
                 System.out.println("Space is already taken, choose again.");
                 x[row][col] = 10;}
     
             } while (x[row][col] == 10);
     
            x[row][col] = 'O';
            for (row = 0; row < 3; row++){
                for (col = 0; col < 3; col++){
                   System.out.print(x[row][col] + " ");
                }
              System.out.println();
            }
            //check for winner again
     
             if (x[0][0] == 'O' && x[0][1] == 'O' && x[0][2] == 'O')
                winner = true;
            else if (x[1][0] == 'O' && x[1][1] == 'O' && x[1][2] == 'O')
                winner = true;
            else if (x[2][0] == 'O' && x[2][1] == 'O' && x[2][2] == 'O')
                winner = true;
            else if (x[0][0] == 'O' && x[1][0] == 'O' && x[2][0] == 'O')
                winner = true;
            else if (x[0][1] == 'O' && x[1][1] == 'O' && x[2][1] == 'O')
                winner = true;
            else if (x[0][2] == 'O' && x[1][2] == 'O' && x[2][2] == 'O')
                winner = true;
            else if (x[0][0] == 'O' && x[1][1] == 'O' && x[2][2] == 'O')
                winner = true;
            else if (x[0][2] == 'O' && x[1][1] == 'O' && x[2][0] == 'O')
                winner = true;
     
           } while (!winner);
     
            System.out.print("The game is over!");
            System.out.print("Play again? (1 for yes, 2 for no: ");
     
               }
    }

    What I'm looking for is a recommendation on what kind of loop I should use to give the user the option to play again, or exit the program.

    I'm also looking for a way to name the winning player after they win.

    Any suggestions would be helpful.


    Thanks a lot,

    Override


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Tic Tac Toe = Naming Winning Player & Restarting Program

    Sounds like what you want is a while loop (or a do-while loop if you want them to play once before asking them if they want to play again). Create a flag which would denote if the user wants to play again or not. Then ask them if they would like to play again. If they say yes, set the flag to make the loop conditional true. If not, set it to make the loop conditional false.

    Also, it would make it a lot easier to manage your code if you moved the actual playing of the game out to a helper method.

    For example:

    boolean keeyPlaying = true;
    // this is a do-while loop example
    // the user will play the game once before being asked if they want to play again
    do
    {
        playTicTacToe(); // helper method which plays a game of tic-tac-toe
        // game is over here
        // TODO: ask the user if they want to keep playing
        if(/* TODO: some conditional check on the user input saying they want to play again*/)
        {
            keepPlaying = true;
        }
        else
        {
            keepPlaying = false;
        }
    }
    while(keepPlaying);

  3. #3
    Junior Member
    Join Date
    Oct 2010
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Tic Tac Toe = Naming Winning Player & Restarting Program

    Thanks a lot, I used your idea to set a flag, and use another loop. It works great. I also tried putting the playing of the game into a method, but it kept giving me errors. Not sure why. My last question is actually how I would approach setting a stalemate? Would that just be another If statement under each "check for winner" 's?

    Here's my new code:

    package anthonytictactoe;
    import java.util.Scanner;
     
    public class Main {
     
        public static void main(String[] args) {
            int row;
            int col;
            char X;
            char O;
            boolean winner = false;
            boolean keepPlaying = true;
            Scanner in = new Scanner(System.in);
            System.out.println("Welcome to Tic Tac Toe!");
            System.out.println("Instructions: The top row is 0, middle is 1, "
                    + "and bottom is 2.");
            System.out.println("The leftmost column is 0, the middle is 1, "
                    + "and the rightmost is 2.");
     
          do{
            char[][] x = new char [3][3];
     
            //Print the board
              for(row = 0; row < 3; row++){
                  for(col = 0; col < 3; col++){
                      x[row][col] = '*';
                  }
            }
              for(row = 0; row < 3; row++){
                   for(col = 0; col < 3; col++){
                      System.out.print(x[row][col] + " ");
     
              }
                   System.out.println();
              }
           do{
             do{
              System.out.print("Player X, what row?: ");
                  row = in.nextInt();
              System.out.print("Player X, what column?: ");
                  col = in.nextInt();
                if (x[row][col] == 'X' || x[row][col] == 'O'){
                 System.out.println("Space is already taken, choose again.");
                 x[row][col] = 10;}
     
     
             } while (x[row][col] == 10);
     
            x[row][col] = 'X';
            for (row = 0; row < 3; row++){
                for(col = 0; col < 3; col++){
                    System.out.print(x[row][col] + " ");
                }
                  System.out.println();
            }
            //Check to see if there is a winner
     
            if (x[0][0] == 'X' && x[0][1] == 'X' && x[0][2] == 'X'){
                System.out.println("Player X Wins!");
                winner = true;}
            else if (x[1][0] == 'X' && x[1][1] == 'X' && x[1][2] == 'X'){
                winner = true;
                System.out.println("Player X Wins!");}
            else if (x[2][0] == 'X' && x[2][1] == 'X' && x[2][2] == 'X'){
                System.out.println("Player X Wins!");
                winner = true;}
            else if (x[0][0] == 'X' && x[1][0] == 'X' && x[2][0] == 'X'){
           System.out.println("Player X Wins!");
                winner = true;}
            else if (x[0][1] == 'X' && x[1][1] == 'X' && x[2][1] == 'X'){
               System.out.println("Player X Wins!");
                winner = true;}
            else if (x[0][2] == 'X' && x[1][2] == 'X' && x[2][2] == 'X'){
                System.out.println("Player X Wins!");
                winner = true;}
            else if (x[0][0] == 'X' && x[1][1] == 'X' && x[2][2] == 'X'){
                System.out.println("Player X Wins!");
                winner = true;}
            else if (x[0][2] == 'X' && x[1][1] == 'X' && x[2][0] == 'X'){
                System.out.println("Player X Wins!");
                winner = true;}
     
            do{
              System.out.print("Player O, what row?: ");
                  row = in.nextInt();
              System.out.print("Player O, what column?: ");
                  col = in.nextInt();
                if (x[row][col] == 'X' || x[row][col] == 'O'){
                 System.out.println("Space is already taken, choose again.");
                 x[row][col] = 10;}
     
             } while (x[row][col] == 10);
     
            x[row][col] = 'O';
            for (row = 0; row < 3; row++){
                for (col = 0; col < 3; col++){
                   System.out.print(x[row][col] + " ");
                }
              System.out.println();
            }
            //check for winner again
     
             if (x[0][0] == 'O' && x[0][1] == 'O' && x[0][2] == 'O'){
                 System.out.println("Player O Wins!");
                winner = true;}
            else if (x[1][0] == 'O' && x[1][1] == 'O' && x[1][2] == 'O'){
                System.out.println("Player O Wins!");
                winner = true;}
            else if (x[2][0] == 'O' && x[2][1] == 'O' && x[2][2] == 'O'){
                System.out.println("Player O Wins!");
                winner = true;}
            else if (x[0][0] == 'O' && x[1][0] == 'O' && x[2][0] == 'O'){
                System.out.println("Player O Wins!");
                winner = true;}
            else if (x[0][1] == 'O' && x[1][1] == 'O' && x[2][1] == 'O'){
                System.out.println("Player O Wins!");
                winner = true;}
            else if (x[0][2] == 'O' && x[1][2] == 'O' && x[2][2] == 'O'){
                System.out.println("Player O Wins!");
                winner = true;}
            else if (x[0][0] == 'O' && x[1][1] == 'O' && x[2][2] == 'O'){
                System.out.println("Player O Wins!");
                winner = true;}
            else if (x[0][2] == 'O' && x[1][1] == 'O' && x[2][0] == 'O'){
                System.out.println("Player O Wins!");
                winner = true;}
     
               } while (!winner);
     
           System.out.println("Game is over! Do you want to play again?");
           System.out.print("(1 for Yes, 2 for No): ");
           int keepplayinggame = in.nextInt();
     
             if(keepplayinggame == 1){
               keepPlaying = true;
                                     }
             else if(keepplayinggame == 2){
               keepPlaying = false;
                                     }
     
     
     
          }while (keepPlaying);     
    }
    }


    Override

Similar Threads

  1. Java Mp3 player.
    By alex901 in forum Object Oriented Programming
    Replies: 4
    Last Post: November 18th, 2010, 09:12 AM
  2. Restarting at the beginning of an array
    By bonbon242 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 31st, 2010, 10:58 AM
  3. Save the information from an ArrayList after restarting the program
    By noFear in forum Java Theory & Questions
    Replies: 4
    Last Post: August 14th, 2010, 08:53 AM
  4. Music player problem
    By abhinavhardikar in forum What's Wrong With My Code?
    Replies: 6
    Last Post: February 9th, 2010, 08:41 PM
  5. [ASK] JMF Class Player
    By bocahTuaNakalzz in forum Java SE APIs
    Replies: 2
    Last Post: December 8th, 2009, 03:40 AM