Originally Posted by
KevinWorkman
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.
Originally Posted by
GregBrannon
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 >.<
Originally Posted by
Junky
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);
}
}