Note that there are some explanatory texts on larger screens.

plurals
  1. POJava: Concatenate String-Int array & using array for possibilities
    text
    copied!<p>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.</p> <pre><code>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]); </code></pre> <p>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). </p> <p>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:</p> <pre><code>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(); } } </code></pre> <p>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!</p> <p><em>On a side note</em> How would I do it for a 5x5 square? With String arrays? Or Int arrays? Or merged?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload