Reputation: 23
I've recently started learning Arrays, and I need help from you experts to solve these problems. I'm making a simple Mine program, where the user jumps from square to square on a 3x3 grid until there is only square left: the one with the mine.
int choice;
String action;
String square[]={"1A","1B","1C","2D","3E","3F","1G","2H"};
int counter = 0;
Random random = new Random();
int mine = random.nextInt(square.length);
System.out.println(square[mine]);
Since the square array is a String, I make it so that the random mine generated is from the squares, and it displays the mine just for the sake of testing. But when the user gets to choose the squares they want to move, there are so many possibilities, for example if the user chooses square[1], then there are options of choosing square[0], square[2], ...., square[7], and so on. So, I need help on making these codes a lot simpler by using Arrays (remember, I'm new to Arrays).
Second, how do I parse a String array to an Int array, so when the player jumps on a square with the mine, let's say square[0], which would then have to check if it "equals" the mine. The code I have doesn't work:
System.out.println("You will start in the middle of a 5 x 5 square.");
System.out.println("[" + square[0] + "]\t[" + square[1] + "]\t[" + square[2] + "]");
System.out.println("[" + square[7] + "]\t[" + character + "]\t[" + square[3] + "]");
System.out.println("[" + square[6] + "]\t[" + square[5] + "]\t[" + square[4] + "]");
System.out.print("Hey. Character " + character + ", make your move. ");
action = (input.readLine());
if (action.equalsIgnoreCase("1A"))
{
if (square[0].equals(mine))
{
System.out.println("Sorry. You stepped on a mine!");
break;
}
else
{
System.out.println("[" + character + "]\t[" + square[1] + "]\t[" + square[2] + "]");
System.out.println("[" + square[7] + "]\t[" + "O" + "]\t[" + square[3] + "]");
System.out.println("[" + square[6] + "]\t[" + square[5] + "]\t[" + square[4] + "]");
counter++;
System.out.print("Make your next move: ");
action = input.readLine();
if (action.equalsIgnoreCase("1B"))
{
if (square[1].equals(mine))
{
System.out.println("Sorry. You stepped on a mine!");
break;
}
else
{
System.out.println("[" + "O" + "]\t[" + character + "]\t[" + square[2] + "]");
System.out.println("[" + square[7] + "]\t[" + "O" + "]\t[" + square[3] + "]");
System.out.println("[" + square[6] + "]\t[" + square[5] + "]\t[" + square[4] + "]");
counter++;
System.out.print("Make your next move: ");
action = input.readLine();
}
}
Apparently, the "if (square[x].equals(mine))" doesn't work, and I don't know how to compare String to Int arrays, and another problem is, as mentioned earlier, my code is getting too long. It's simply because I am counting out the possibilities of choosing each square (out of 8) and the square after that the user will choose (out of 8-1), and so on, which would be 8! factorial (because user starts on the middle square, leaving out eight squares). But I don't want 40320 possibilities all coded, and I believe that there is a MUCH simpler way that you guys could tell me. Thanks!
On a side note How would I do it for a 5x5 square? With String arrays? Or Int arrays? Or merged?
Upvotes: 2
Views: 169
Reputation: 5614
Yes, there is a much simpler way. What you need to do is to be able to answer the question "is there a mine at coordinate X,Y?" This is done with a two dimensional array and the only thing you need is a yes/no boolean at each spot.
boolean[][] minePos = new boolean[5][5];
You set the mines like this (both parameters are numbers):
minePos[1][1] = true; //this is 1A
minePos[1][2] = true; //this is 1B
and so on. You have been using A, B, C, for the Y coordinate, and you need to use 1 for A, 2 for B, etc. The places where you set to true will be the places of mines. All the rest are false, indicating no mine is there. When the user want to move to position xpos, ypos, you test whether there is a mine there with this:
if (minePos[xpos,ypos]) {
//tell them they were blown up
}
else {
//you are OK, not blown up
}
If they are safe at xpos,ypos, and you want to restrict the movement to the squares around them, then it is easy to enumerate the eight possible jumps:
(xpos-1,ypos-1)
(xpos, ypos-1)
(xpos+1, ypos-1)
(xpos-1,ypos)
(xpos+1, ypos)
(xpos-1,ypos+1)
(xpos, ypos+1)
(xpos+1, ypos+1)
and so on. They choose one of the eight directions, and based on this you find out where they are going to land. Then you test whether the landing position has a mine.
It is not clear how you want to prompt for user movement. Do they indicate "I want to move one square up"? You should already know where they are (in a couple variables) so given a direction it is easy to calculate where they land. Or are you going to ask them to enter a coordinate, like "A1"? Then you need to make sure that the coordinate they enter is next to the one that you know they are at. or you could show all the possible coordinates they might move to and let them select one. However you do it, you figure out what the destination numeric coordinate is, and then check if there is a mine there.
Start with a mines in fixed position, but later it will be easy to write a small loop that places mines at random X and Y positions. Something like this:
Random rand = new Random();
for (int count:=1; count<8; count++) {
int randx = rand.nextInt(5);
int randy = rand.nextInt(5);
minePos[randx, randy] = true;
}
That will place 8 mines at a random x value (0 thru 4) and random y position (0 thru 4). You can change the size of the board, and the number of mines you want to place.
Upvotes: 1