Note that there are some explanatory texts on larger screens.

plurals
  1. POMaking something move in an array
    text
    copied!<p>I have to make this program that will make a 12 by 12 array filled with different letters, then make something like a disease that spreads to the nearby cells of the same letter (it'll be randomly placed in the array). I've managed so far to make a class for the array and for the disease (to select a random char to be diseased) but I don't understand how I can make it see the nearby cells. In addition in the Disease the user can choose how many different letters(2-4). So far i've done: </p> <pre><code>import java.util.Random; public class Disease { public char D ; Random r = new Random(); public void forTwo() { Random r = new Random(); D = (char)(r.nextInt(2) + 'E'); } public void forThree() { Random r = new Random(); D = (char)(r.nextInt(3) + 'E'); } public void forFour() { Random r = new Random(); D = (char)(r.nextInt(4) + 'E'); } } </code></pre> <p>AND THE ARRAY:</p> <pre><code>import java.util.Arrays; import java.util.Random; public class PlayingArea { private String letters; public char[][] grid; public PlayingArea(String letters) { this.letters = letters; } public void populate() { int n = letters.length(); grid = new char[12][12]; Random r = new Random(); for (int j = 0; j &lt; grid.length; j++) { for (int i = 0; i &lt; grid.length; i++) { grid[i][j] = letters.charAt(r.nextInt(n)); } } } public String gridAsString() { StringBuilder sb = new StringBuilder(); for (char[] letterRow : grid) { sb.append(Arrays.toString(letterRow)).append('\n'); } return sb.toString(); } } </code></pre>
 

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