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

Thread: tic tac toe single array code help

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default tic tac toe single array code help

    Hi. I am having trouble writing the code to create a tic tac toe game using a single array. I don't know where to start with getting the rest of the code to work. Can anyone please help me.

    This is my code so far:

    ///*
     
    */
     
    import java.io.*;
     
    public class H2_tictactoe
    {
       public static void main(String args[]) throws IOException
     
        {
     
           String welcome = "Welcome to Tic Tac Toe Player 1\n";
             char[] board = {0, 0, 0, 0, 0, 0, 0, 0 ,0};
     
     
             String inputPlayer;
             char movesArray;
             char player1 = 'X';
             char player2 = 'O';
     
     
     BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));
     
            movesArray = 9;
     
     {
     
    }
     
    showboard(board);//Call method showboardGame
        menu();//Call method menu
    inputPlayer = dataIn.readLine(); //read in input from player
     
     
     
    }
     
    public static void showboard(char theboard[])
     
    {
            System.out.println("------------");
            System.out.println(" 0| 1 | 2 ");
            System.out.println("------------");
            System.out.println(" 3| 4 | 5 ");
            System.out.println("------------");
            System.out.println(" 6| 7 | 8 ");
            System.out.println("------------");
            System.out.println("");
    }
     
    public static void menu()
        {
              System.out.println("Choose the number associated with the space where you want to move:");
              System.out.println("0. Space 0");
              System.out.println("1. Space 1");
              System.out.println("2. Space 2");
              System.out.println("3. Space 3");
              System.out.println("4. Space 4");
              System.out.println("5. Space 5");
              System.out.println("6. Space 6");
              System.out.println("7. Space 7");
              System.out.println("8. Space 8");
    }
     
    {
     
     
    }
    }
    Last edited by angelus2402004; February 8th, 2012 at 05:10 PM.


  2. #2
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: tic tac toe single array code help

    First of all, could you surround your code in highlight tags please? Do so like this:

    [highlight=Java]//your code here[/highlight]

    Secondly, I don't know how to help you because I don't know what the code does so far. Does it work, or are there errors? If there are, could you post them? Otherwise, are you stuck on making a GUI? Code logic? Please be more specific.
    Last edited by snowguy13; February 6th, 2012 at 05:39 PM.
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  3. #3
    Junior Member
    Join Date
    Feb 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: tic tac toe single array code help

    So far the program shows the menu of where player 1 is supposed to choose where the x goes. When I enter a number 1-8 it shows the number I entered and that's it so far. I can't figure out where/how I am supposed to start the code so that when the user enters a number an x is supposed to place that spot.screen1.jpg screen2.jpg

    And then I have to figure out how to test for a winner and display who won; i.e. X won or O won; display if there is a draw; no winner and not allow the users to enter any more moves after all the moves are exhausted.

    And I have to display the "new" tic-tac-toe board each time a move is made and also the display "spaces" board so the user knows what number is associated with which space.


    I'm so frustrated.

  4. #4
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: tic tac toe single array code help

    Okay. You have an array of characters to store what moves are made where, so next maybe have the program check the value of the array at the index the user specified (make sure that spot isn't already occupied), and if the move is valid, set the value of the char[] at the index to that user's symbol (player 1 = x, player 2 = o, as I think you had it).

    As for displaying the board, I'd make a method that checks the value of the char[] and puts in the correct symbol (or nothing) based on the values. That way, you can call the method whenever you need. So, you'd basically just need to change your showboard() method so that it actually DOES something with the char[] you put in (use if statements!).

    Note, you should probably also create a method that checks if either user has won, and then wrap everything in a loop. So, have some structure like this:

    while(!playerHasWon()) //playerHasWon() would be a method you'd write
    {
       showBoard(board); //your char[] board
       //Do stuff to get input
       showBoard(board); //show board again for player 2
       //Do stuff for player 2 input
    }

    That would be the general flow I would use for this program.
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  5. #5
    Junior Member
    Join Date
    Feb 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: tic tac toe single array code help

    I worked on it some more. But I still can't figure out how to get the input the player enters to go to the designated space and for that space to be an X.

    /*
     
    import java.io.*;
     
    public class H2_tictactoe
    {
       public static void main(String[] args)throws IOException
       {
     
     
        char board[] = {0,0,0,0,0,0,0,0,0};
     
     
     
        int n;
        String inputPlayer;
        BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));
     
     
     
          menu();//call method menu
          showBoard(board);//call method showBoard and pass board array from main
          inputPlayer = dataIn.readLine();
          n = Integer.parseInt(inputPlayer);
          addMove(n, board);
     
     
     
    }
            public static void menu()
        {
     
              System.out.println("------------");
              System.out.println(" 0| 1 | 2 |");
              System.out.println("------------");
              System.out.println(" 3| 4 | 5 |");
              System.out.println("------------");
              System.out.println(" 6| 7 | 8 |");
              System.out.println("------------");
              System.out.println("\t");
     
     
              System.out.println("Choose the number associated with the space where you want to move:");
              System.out.println("0. Space 0");
              System.out.println("1. Space 1");
              System.out.println("2. Space 2");
              System.out.println("3. Space 3");
              System.out.println("4. Space 4");
              System.out.println("5. Space 5");
              System.out.println("6. Space 6");
              System.out.println("7. Space 7");
              System.out.println("8. Space 8");
              System.out.println("\t");
    }
     
    public static void showBoard(char theboard[])
    {
                System.out.println("------------");
                System.out.println (theboard[0] + " | " + theboard[1] + " | " + theboard[2]);
     
                System.out.println("------------");
     
                System.out.println (theboard[3] + " | " + theboard[4] + " | " + theboard[5]);
     
                System.out.println("------------");
     
                System.out.println (theboard[6] + " | " + theboard[7] + " | " + theboard[8]);
                System.out.println("------------");
     
    }
     public static void addMove( int n, char theboard[])
     {
     
    }
    }

Similar Threads

  1. Replies: 4
    Last Post: November 14th, 2011, 09:00 PM
  2. Please help with array code
    By senecawolf in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 12th, 2011, 01:01 AM
  3. Replies: 3
    Last Post: April 11th, 2011, 09:51 PM
  4. Array Code Help
    By whattheeff in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 21st, 2011, 04:44 PM
  5. Single Dimensional Array Help!
    By Allicat in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 15th, 2011, 12:01 PM

Tags for this Thread