1

i'm having trouble about getting my output right. Basically, i'm writing a basic search game, my program has multidimensional char array (every index has been initialize to a single space ' '). There are 2 players that take turn to guess (enter 2 numbers seperated by a space), if player 1 guesses it wrong, it will store '1' into that char array; and if player 2 guesses it wrong, it will be 2. But when one of them guess it right, it will display their number plus a char 'P' (example: 1P, or 2P) that has been stored in that char array (i used random class to randomize the location of the object to be searched for). I have everything done, except i just don't really know how to display the player number and the 'P' in the same index in a char array. i know that we can't have 2 char together since it's char.

Here are the sample run from my code (i actually tried, but it displays a '?'):

enter image description here

and this is the output i suppose to get when one of them player found the 'P':

enter image description here

Here is the code when i used in the toString() method to output the array:

 for(int top = 0; top < totalRooms * 2; top++) {
           a+= "__";
     }
     for(int row = hidingPlaces.length - 1; row >= 0; row--) {
        a += "\n";
        for(int col = 0; col < hidingPlaces[row].length; col++) {
           a += "| " + hidingPlaces[row][col] + " ";
        }
        a += "|\n";
        for(int bot = 0; bot < hidingPlaces[row].length; bot++) {
           a += "|___";
        }
        a += "|";
     }

This is the code where i initialize the char array with player #:

   public boolean searchRoom(int floor, int room, char players) {
  if (hidingPlaces[floor][room] != hidingPlaces[floorLocation][roomLocation]) {
     hidingPlaces[floor][room] = players; // fill out those empty spots in the array with players' index
     aArray[floor][room] = players; // samething here, except that this array doesn't show 'P'   
     found = false;
  } else { 
     winner = players; // char winner is now the players' index.
     found = true;
  }        
  return found;       

}

enter image description here

 //output this array if puppy is found.
     for(int top = 0; top < totalRooms * 2; top++) {
           a+= "__";
     }
     for(int row = hidingPlaces.length - 1; row >= 0; row--) {
        a += "\n";
        for(int col = 0; col < hidingPlaces[row].length; col++) {
           a += "| " + hidingPlaces[row][col] + " ";
            if(hidingPlaces[row][col] == '3') {
              a += "1P";   
           } else if (hidingPlaces[row][col] == '4') {
              a += "2P";
           }
        }
        a += "|\n";
        for(int bot = 0; bot < hidingPlaces[row].length; bot++) {
           a += "|___";
        }
        a += "|";
     }
  }
  return a;
3
  • in fact, the only way i could think of placing the player number + the P in the same cell has to do with String? if so, is there a way to convert a 2d char array to string array? Commented Jan 14, 2014 at 2:05
  • Any reason you can't use a 3 to store 1P finding the puppy and 4 for 2P finding the puppy? If not, then you can just use a condition on the output loop (if it equals 3 output "1P") Commented Jan 14, 2014 at 2:10
  • I rolled back your edit. You can't remove the question after you ask it, that makes it useless to anyone else who comes along. Please do not try to remove it again. Commented Jan 14, 2014 at 3:05

1 Answer 1

2

I can think of two solutions to the problem.

1) '1P' cannot be stored in a character array because it is made up of two characters. Changing this to a String array would allow you to store and easily display multiple characters.

2) If you really want to use a character array, you can store another symbol/letter/number that stands for '1p', and a different symbol to stand for '2p'. Then you would add something like this to your output (instead of displaying the contents of the array):

For example, if 'q' stands for 1p:

 if (hidingPlaces[row][col] == 'q')
      System.out.print ("1p"); //plus the lines and spaces as appropriate
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you. i can only use char array in this assignment. I'll try #2
It probably has something to do with how you're setting up the if/else conditions in your toString() method. Can you post the updated version?
Sorry about that. Updated!
I'm pretty sure your problem is in this line: a += "| " + hidingPlaces[row][col] + " "; You are adding the contents of the array to the string a in every case. You will want to find a way to add this to the if/elseif chain so that the contents of the array are only added to the string if they are not a '3' or a '4'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.