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

Thread: Array problem

  1. #1
    Junior Member
    Join Date
    Jul 2014
    Posts
    14
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Question Array problem

    ok so im doing a treasure hunt thing game and the situation is this:
    i already made a 5 by 5 array of integers and i placed the "treasure" on the 4th row of 3rd column.
    The problem is this
    i am asked to print the cell where the user visits. it should look like this

    * * * * *
    * * * * *
    * * * * *
    * * * * *
    * * * * *

    and when the user inputs 12 it should look like this
    1 represents the row and 2 represents the column
    * 34 * * *
    * * * * *
    * * * * *
    * * * * *
    * * * * *

    the 34 represents the clue on where should the user go.

    my problem is i can print the * and i already made the array pool

    int[][] array  = new int [][] {
            {21, 34, 42, 23, 14}, 
            {12, 31, 55, 32, 24},
            {15, 51, 53, 13, 41},
            {25 ,22, 44, 33, 54},
            {45, 35, 53, 43, 52}
            };

    but i cant replace the * with what cell it is representing

    btw i printed the * like this

    for (i = 1; i <=5; i++) {
    for (j = 1; j <=5; j++) {
    System.out.print("* \t");
    }
    System.out.println ();
    }

    thanks in advance and more power for those who will answer !


  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: Array problem

    Where is your logic for checking whether a user has visited a particular cell?
    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
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Array problem

    but i cant replace the * with what cell it is representing
    I'm not sure I understand what that means, but I'll take a stab at an answer:

    If you have a loop that prints the rows and columns of asterisks, then add an if() statement to print something other than an asterisk when certain conditions exist. For example, if the user selects 12 (meaning row 1 column 2) then when those conditions exist in the for() loop as in:

    if ( row == 1 && column == 2 )

    then print the desired replacement for the '*'.

    To print the elements of the array[][], you'll simply:

    System.out.println( " " + array[row][column] ); // includes leading space (optional)

    Terminology:

    I suggest the treasure is on the 3rd column of the 4th row. Also, you need to keep in mind that the array coordinates are zero-based, so your row 1 is really row 0 and same for the columns. You can translate those coordinates to be one-based, but you should comment that in the code and make it clear when you talk about it. That fact will make the nested loop you posted awkward for printing the array[][] elements, but a translation is possible.

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Array problem

    That would require an if statement for each co-ord.
    How about having a char array filled with *'s. Each time player visits a cell change it to some other char. Then when printing if current cell contains other char print corresponding value in int array instead.
    Improving the world one idiot at a time!

  5. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Array problem

    That would require an if statement for each co-ord.
    No, the desired row/column coordinates would be set before entering the nested for() loops in the print method. (1 and 2 in my example above would be values input by the user stored in variables.) If multiple coordinates had to be printed each time the current state of the field is printed, then it gets more complicated but still doable, but that requirement isn't clear.

    That's probably why we're looking at it differently. I admit I don't understand the requirements.

  6. #6
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Array problem

    Maybe I'm being dense but i am not seeing your solution. What if the user enters (5,5)? Has that cell been visited before? What should it display (* or hint)? How do you determine that?
    Improving the world one idiot at a time!

  7. #7
    Junior Member
    Join Date
    Jul 2014
    Posts
    14
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Array problem

    Quote Originally Posted by KevinWorkman View Post
    Where is your logic for checking whether a user has visited a particular cell?
    such i dont implement actually because when a user input an answer(guess) that cell that used to be * will be replaced with the clue so logically all * that had been replaced with the clue is "visited" thus no need to be checked.

    Quote Originally Posted by GregBrannon View Post
    I'm not sure I understand what that means, but I'll take a stab at an answer:

    If you have a loop that prints the rows and columns of asterisks, then add an if() statement to print something other than an asterisk when certain conditions exist. For example, if the user selects 12 (meaning row 1 column 2) then when those conditions exist in the for() loop as in:

    if ( row == 1 && column == 2 )

    then print the desired replacement for the '*'.

    To print the elements of the array[][], you'll simply:

    System.out.println( " " + array[row][column] ); // includes leading space (optional)

    Terminology:

    I suggest the treasure is on the 3rd column of the 4th row. Also, you need to keep in mind that the array coordinates are zero-based, so your row 1 is really row 0 and same for the columns. You can translate those coordinates to be one-based, but you should comment that in the code and make it clear when you talk about it. That fact will make the nested loop you posted awkward for printing the array[][] elements, but a translation is possible.
    then in every intersection i would have ifs? since it's a 5 by 5 it would be 25 ifs? i have thought of that too but i do think there is a workaround here i just cant figure out the logic >.<

    Quote Originally Posted by Junky View Post
    Maybe I'm being dense but i am not seeing your solution. What if the user enters (5,5)? Has that cell been visited before? What should it display (* or hint)? How do you determine that?
    if the cell is not yet visited it's * and if visited then it displays the clue

    and btw the program PRINTS THE * every time the user guesses ex.

    the program will print like this at start:

    * * * * *
    * * * * *
    * * * * *
    * * * * *
    * * * * *

    then when a user input 12 then the program will print like this

    * 34 * * * *
    * * * * *
    * * * * *
    * * * * *
    * * * * *

    then when he inputs the 34 the program prints like this

    * 34 * * *
    * * * * *
    * * * 13 *
    * * * * *

    here is the code by the way.

            int[][] array  = new int [][] {
            {21, 34, 42, 23, 14}, 
            {12, 31, 55, 32, 24},
            {15, 51, 53, 13, 41},
            {25 ,22, 44, 33, 54},
            {45, 35, 53, 43, 52}
            }; 
            int i=0;
            int j=0;
    int guess;
    int rowGuess;
    int columGuess;
    int counter = 0;
    for (i = 1; i <=5; i++) {
    for (j = 1; j <=5; j++) {
    System.out.print("* \t");
    }
    System.out.println ();
    }
     
    do {
    System.out.print ("Enter your guess: ");
    guess = input.nextInt();
     
    //this convert's the user input to the array index where row 1 actually was 0 index in the array//
    rowGuess = guess/10;
    columGuess = guess%10;
     
    if (rowGuess == 1) {
    i = 0;
    }
    else if (rowGuess ==2) {
    i = 1;
    }
    else if (rowGuess == 3) {
    i=2;
    }
    else if (rowGuess ==4) {
    i=3;
    }
    else if (rowGuess ==5) {
    i =4;
    }
    if (columGuess == 1) {
    j = 0;
    }
    else if (columGuess ==2) {
    j=1;
    }
    else if (columGuess ==3) {
    j=2;
    }
    else if (columGuess ==4) {
    j=3;
    }
    else if (columGuess ==5) {
    j=4;
    }
    System.out.print (array [i][j]);
    System.out.println ();
    counter++;
    } while (counter<10);
    }
    }

Similar Threads

  1. Replies: 2
    Last Post: February 24th, 2014, 10:48 PM
  2. 2D array problem
    By tylin in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 25th, 2013, 12:50 PM
  3. Replies: 2
    Last Post: May 13th, 2011, 03:08 AM
  4. Problem with array
    By mingming8888 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: October 27th, 2010, 12:17 PM
  5. Java program for 2-D Array Maze
    By Peetah05 in forum Collections and Generics
    Replies: 11
    Last Post: May 8th, 2009, 04:30 AM