Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is how I would do it. You have up to 2^48 different games which you can replay by using the same seed. This avoids the need to read files or store an index in memory. You can recreate any board game as required.</p> <pre><code>public class Baord { private static final int[] SHIP_SIZES = {4, 4, 3, 3, 3, 2, 2, 2, 2, 1, 1}; public static final char EMPTY = '.'; private final Random random; private final char[][] grid; private char letter = 'a'; private final int width; private final int height; public Baord(int height,int width, int seed) { this.width = width; this.height = height; this.random = new Random(seed); this.grid = new char[height][width]; for (char[] chars : grid) Arrays.fill(chars, EMPTY); for (int len : SHIP_SIZES) placeShip(len); } private void placeShip(int len) { OUTER: while (true) { if (random.nextBoolean()) { // across int x = random.nextInt(width - len + 1); int y = random.nextInt(height); for (int j = Math.max(0, y - 1); j &lt; Math.min(height, y + 2); j++) for (int i = Math.max(0, x - 1); i &lt; Math.min(width, x + len + 2); i++) if (grid[j][i] &gt; EMPTY) continue OUTER; for (int i = 0; i &lt; len; i++) grid[y][x + i] = letter; } else { // down int y = random.nextInt(height - len + 1); int x = random.nextInt(width); for (int j = Math.max(0, x - 1); j &lt; Math.min(width, x + 2); j++) for (int i = Math.max(0, y - 1); i &lt; Math.min(height, y + len + 2); i++) if (grid[i][j] &gt; EMPTY) continue OUTER; for (int i = 0; i &lt; len; i++) grid[y + i][x] = letter; } break; } letter++; } public String toString() { StringBuilder ret = new StringBuilder(); for (int y = 0; y &lt; grid.length; y++) ret.append(new String(grid[y])).append("\n"); return ret.toString(); } public static void main(String... args) { for (int i = 0; i &lt; 3; i++) System.out.println(new Baord(8, 16, i)); } } </code></pre> <p>prints</p> <pre><code>..........ddd... .....aaaa....... eee.......j..... .............i.. ..bbbb.......i.c .......g.......c .k.h...g...f...c ...h.......f.... ii.............. .....ccc..ff...b ...............b aaaa...........b .........j..h..b ....gg......h... ..............k. eee..ddd........ .d.....eee.....f .d.............f .d.............. ......j.ccc....g .............b.g .............b.. h.i..........b.. h.i...aaaa.k.b.. .......h....aaaa .......h........ .e.........ccc.. .e.............. .e.bbbb...ii..ff ........d....... .g..j...d....... .g......d.....k. ............ccc. .....bbbb....... .ii.......k..d.. .....f.......d.. eee..f.......d.. ................ .j.g........hh.. ...g.aaaa....... </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

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