From 691b82336cb6f438400d3e2adef6f1df1ae7b811 Mon Sep 17 00:00:00 2001 From: Topher Lamey Date: Sat, 27 Feb 2021 18:41:04 -0700 Subject: [PATCH 01/10] Initial take on Amazing for Java, gitignore update for IntelliJ --- .gitignore | 5 + 02 Amazing/java/Amazing.java | 181 +++++++++++++++++++++++++++++++ 02 Amazing/java/AmazingGame.java | 6 + 3 files changed, 192 insertions(+) create mode 100644 02 Amazing/java/Amazing.java create mode 100644 02 Amazing/java/AmazingGame.java diff --git a/.gitignore b/.gitignore index aa42e787..2973d79d 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,8 @@ */csharp/bin */csharp/obj +.idea +*.iws +*.iml +*.ipr +out/ diff --git a/02 Amazing/java/Amazing.java b/02 Amazing/java/Amazing.java new file mode 100644 index 00000000..d6b61f29 --- /dev/null +++ b/02 Amazing/java/Amazing.java @@ -0,0 +1,181 @@ +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Random; +import java.util.Scanner; +import static java.lang.System.in; +import static java.lang.System.out; + +public class Amazing { + + private final Scanner kbScanner; + + public Amazing() { + kbScanner = new Scanner(in); + } + + enum Direction { + GO_LEFT, + GO_UP, + GO_RIGHT, + GO_DOWN, + } + + public void play() { + out.println(tab(28) + "AMAZING PROGRAM"); + out.println(tab(15) + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY"); + out.println(); + + int width = 0; + int length = 0; + + do { + String range = displayTextAndGetInput("WHAT ARE YOUR WIDTH AND LENGTH"); + if (range.indexOf(",") > 0) { + width = getDelimitedValue(range, 0); + length = getDelimitedValue(range, 1); + } + } while (width < 1 || length < 1); + + + Integer[][] used = new Integer[length][width]; + for (int i=0; i < length; i++) { + used[i] = new Integer[width]; + for (int j = 0; j < width; j++) { + used[i][j] = 0; + } + } + + Integer[][] walls = new Integer[length][width]; + for (int i=0; i < length; i++) { + walls[i] = new Integer[width]; + for (int j = 0; j < width; j++) { + walls[i][j] = 0; + } + } + + int EXIT_DOWN = 1; + int EXIT_RIGHT = 2; + + int enterCol = random(0, width); + int col = enterCol; + int row = 0; + int count = 1; + int totalWalls = width * length + 1; + + // set up entrance + used[row][col] = count; + count++; + + while (count != totalWalls) { + ArrayList possibleDirs = new ArrayList<>(Arrays.asList(Direction.values())); + + if (col == 0 || used[row][col - 1] != 0) { + possibleDirs.remove(Direction.GO_LEFT); + } + if (row == 0 || used[row - 1][col] != 0) { + possibleDirs.remove(Direction.GO_UP); + } + if (col == width - 1 || used[row][col + 1] != 0) { + possibleDirs.remove(Direction.GO_RIGHT); + } + if (row == length - 1 || used[row + 1][col] != 0) { + possibleDirs.remove(Direction.GO_DOWN); + } + + if (possibleDirs.size() != 0) { + Direction direction = possibleDirs.get(random(0, possibleDirs.size())); + if (direction == Direction.GO_LEFT) { + col = col - 1; + walls[row][col] = EXIT_RIGHT; + } else if (direction == Direction.GO_UP) { + row = row - 1; + walls[row][col] = EXIT_DOWN; + } else if (direction == Direction.GO_RIGHT) { + walls[row][col] = walls[row][col] + EXIT_RIGHT; + col = col + 1; + } else if (direction == Direction.GO_DOWN) { + walls[row][col] = walls[row][col] + EXIT_DOWN; + row = row + 1; + } + used[row][col] = count; + count++; + } else { + do { + if (col != width - 1) { + col++; + } else if (row != length - 1) { + row++; + col = 0; + } else { + row = 0; + col = 0; + } + } while (used[row][col] == 0); + } + } + + col = random(0, width - 1); + row = length - 1; + walls[row][col] = walls[row][col] + 1; + + for (int i=0; i < width; i++) { + if (i == enterCol) { + out.print(". "); + } else { + out.print(".--"); + } + } + out.println('.'); + + for (int i=0; i < length; i++) { + out.print("I"); + for (int j = 0; j < width; j++) { + if (walls[i][j] < 2) { + out.print(" I"); + } else { + out.print(" "); + } + } + out.println(); + for (int j = 0; j < width; j++) { + if (walls[i][j] == 0 || walls[i][j] == 2) { + out.print(":--"); + } else { + out.print(": "); + } + } + out.println("."); + } + } + + private String displayTextAndGetInput(String text) { + out.print(text); + return kbScanner.next(); + } + + private static int getDelimitedValue(String text, int pos) { + String[] tokens = text.split(","); + try { + return Integer.parseInt(tokens[pos]); + } catch (Exception ex) { + return 0; + } + } + + private static String tab(int spaces) { + char[] spacesTemp = new char[spaces]; + Arrays.fill(spacesTemp, ' '); + return new String(spacesTemp); + } + + public static boolean random() { + Random random = new Random(); + return random.nextBoolean(); + } + + public static int random(int min, int max) { + Random random = new Random(); + return random.nextInt(max - min) + min; + } +} + diff --git a/02 Amazing/java/AmazingGame.java b/02 Amazing/java/AmazingGame.java new file mode 100644 index 00000000..a40056df --- /dev/null +++ b/02 Amazing/java/AmazingGame.java @@ -0,0 +1,6 @@ +public class AmazingGame { + public static void main(String[] args) { + Amazing amazing = new Amazing(); + amazing.play(); + } +} From 59369f93ed327afc3762b7e42ad9bec455867a3c Mon Sep 17 00:00:00 2001 From: Topher Lamey Date: Sat, 27 Feb 2021 18:51:42 -0700 Subject: [PATCH 02/10] Fix up constants --- 02 Amazing/java/Amazing.java | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/02 Amazing/java/Amazing.java b/02 Amazing/java/Amazing.java index d6b61f29..6057e932 100644 --- a/02 Amazing/java/Amazing.java +++ b/02 Amazing/java/Amazing.java @@ -56,6 +56,11 @@ public class Amazing { int EXIT_DOWN = 1; int EXIT_RIGHT = 2; + int FIRST_COL = 0; + int LAST_COL = width - 1; + int FIRST_ROW = 0; + int LAST_ROW = length - 1; + int enterCol = random(0, width); int col = enterCol; int row = 0; @@ -69,33 +74,33 @@ public class Amazing { while (count != totalWalls) { ArrayList possibleDirs = new ArrayList<>(Arrays.asList(Direction.values())); - if (col == 0 || used[row][col - 1] != 0) { + if (col == FIRST_COL || 0 != used[row][col - 1]) { possibleDirs.remove(Direction.GO_LEFT); } - if (row == 0 || used[row - 1][col] != 0) { + if (row == FIRST_ROW || 0 != used[row - 1][col]) { possibleDirs.remove(Direction.GO_UP); } - if (col == width - 1 || used[row][col + 1] != 0) { + if (col == LAST_COL || 0 != used[row][col + 1]) { possibleDirs.remove(Direction.GO_RIGHT); } - if (row == length - 1 || used[row + 1][col] != 0) { + if (row == LAST_ROW || 0 != used[row + 1][col]) { possibleDirs.remove(Direction.GO_DOWN); } if (possibleDirs.size() != 0) { Direction direction = possibleDirs.get(random(0, possibleDirs.size())); if (direction == Direction.GO_LEFT) { - col = col - 1; + col--; walls[row][col] = EXIT_RIGHT; } else if (direction == Direction.GO_UP) { - row = row - 1; + row--; walls[row][col] = EXIT_DOWN; } else if (direction == Direction.GO_RIGHT) { walls[row][col] = walls[row][col] + EXIT_RIGHT; - col = col + 1; + col++; } else if (direction == Direction.GO_DOWN) { walls[row][col] = walls[row][col] + EXIT_DOWN; - row = row + 1; + row++; } used[row][col] = count; count++; From 7c28a2216b8dc3efce6eb5fa2335a9925e9d0fe3 Mon Sep 17 00:00:00 2001 From: Topher Lamey Date: Sat, 27 Feb 2021 22:07:45 -0700 Subject: [PATCH 03/10] Be more explicit with cell values --- 02 Amazing/java/Amazing.java | 86 +++++++++++++++++++----------------- 1 file changed, 46 insertions(+), 40 deletions(-) diff --git a/02 Amazing/java/Amazing.java b/02 Amazing/java/Amazing.java index 6057e932..5cbe5af0 100644 --- a/02 Amazing/java/Amazing.java +++ b/02 Amazing/java/Amazing.java @@ -20,6 +20,13 @@ public class Amazing { GO_DOWN, } + final static int FIRST_COL = 0; + final static int FIRST_ROW = 0; + + final static int EXIT_UNSET = 0; + final static int EXIT_DOWN = 1; + final static int EXIT_RIGHT = 2; + public void play() { out.println(tab(28) + "AMAZING PROGRAM"); out.println(tab(15) + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY"); @@ -37,29 +44,11 @@ public class Amazing { } while (width < 1 || length < 1); - Integer[][] used = new Integer[length][width]; - for (int i=0; i < length; i++) { - used[i] = new Integer[width]; - for (int j = 0; j < width; j++) { - used[i][j] = 0; - } - } + Integer[][] walls = buildGrid(width, length); + Integer[][] used = buildGrid(width, length); - Integer[][] walls = new Integer[length][width]; - for (int i=0; i < length; i++) { - walls[i] = new Integer[width]; - for (int j = 0; j < width; j++) { - walls[i][j] = 0; - } - } - - int EXIT_DOWN = 1; - int EXIT_RIGHT = 2; - - int FIRST_COL = 0; - int LAST_COL = width - 1; - int FIRST_ROW = 0; - int LAST_ROW = length - 1; + int lastCol = width - 1; + int lastRow = length - 1; int enterCol = random(0, width); int col = enterCol; @@ -72,20 +61,7 @@ public class Amazing { count++; while (count != totalWalls) { - ArrayList possibleDirs = new ArrayList<>(Arrays.asList(Direction.values())); - - if (col == FIRST_COL || 0 != used[row][col - 1]) { - possibleDirs.remove(Direction.GO_LEFT); - } - if (row == FIRST_ROW || 0 != used[row - 1][col]) { - possibleDirs.remove(Direction.GO_UP); - } - if (col == LAST_COL || 0 != used[row][col + 1]) { - possibleDirs.remove(Direction.GO_RIGHT); - } - if (row == LAST_ROW || 0 != used[row + 1][col]) { - possibleDirs.remove(Direction.GO_DOWN); - } + ArrayList possibleDirs = getPossibleDirs(used, lastCol, lastRow, col, row); if (possibleDirs.size() != 0) { Direction direction = possibleDirs.get(random(0, possibleDirs.size())); @@ -106,9 +82,9 @@ public class Amazing { count++; } else { do { - if (col != width - 1) { + if (col != lastCol) { col++; - } else if (row != length - 1) { + } else if (row != lastRow) { row++; col = 0; } else { @@ -123,6 +99,7 @@ public class Amazing { row = length - 1; walls[row][col] = walls[row][col] + 1; + // top line for (int i=0; i < width; i++) { if (i == enterCol) { out.print(". "); @@ -135,7 +112,7 @@ public class Amazing { for (int i=0; i < length; i++) { out.print("I"); for (int j = 0; j < width; j++) { - if (walls[i][j] < 2) { + if (walls[i][j] == EXIT_UNSET || walls[i][j] == EXIT_DOWN) { out.print(" I"); } else { out.print(" "); @@ -143,7 +120,7 @@ public class Amazing { } out.println(); for (int j = 0; j < width; j++) { - if (walls[i][j] == 0 || walls[i][j] == 2) { + if (walls[i][j] == EXIT_UNSET || walls[i][j] == EXIT_RIGHT) { out.print(":--"); } else { out.print(": "); @@ -153,6 +130,35 @@ public class Amazing { } } + private Integer[][] buildGrid(int width, int length) { + Integer[][] grid = new Integer[length][width]; + for (int i=0; i < length; i++) { + grid[i] = new Integer[width]; + for (int j = 0; j < width; j++) { + grid[i][j] = EXIT_UNSET; + } + } + return grid; + } + + private ArrayList getPossibleDirs(Integer[][] used, int lastCol, int lastRow, int col, int row) { + ArrayList possibleDirs = new ArrayList<>(Arrays.asList(Direction.values())); + + if (col == FIRST_COL || 0 != used[row][col - 1]) { + possibleDirs.remove(Direction.GO_LEFT); + } + if (row == FIRST_ROW || 0 != used[row - 1][col]) { + possibleDirs.remove(Direction.GO_UP); + } + if (col == lastCol || 0 != used[row][col + 1]) { + possibleDirs.remove(Direction.GO_RIGHT); + } + if (row == lastRow || 0 != used[row + 1][col]) { + possibleDirs.remove(Direction.GO_DOWN); + } + return possibleDirs; + } + private String displayTextAndGetInput(String text) { out.print(text); return kbScanner.next(); From 1dcc302ce74d5e74b21d96f904566020b22d3c15 Mon Sep 17 00:00:00 2001 From: Topher Lamey Date: Sat, 27 Feb 2021 22:17:10 -0700 Subject: [PATCH 04/10] Use single grade rather than two arrays --- 02 Amazing/java/Amazing.java | 78 ++++++++++++++++++++---------------- 1 file changed, 44 insertions(+), 34 deletions(-) diff --git a/02 Amazing/java/Amazing.java b/02 Amazing/java/Amazing.java index 5cbe5af0..ae9e886c 100644 --- a/02 Amazing/java/Amazing.java +++ b/02 Amazing/java/Amazing.java @@ -44,11 +44,7 @@ public class Amazing { } while (width < 1 || length < 1); - Integer[][] walls = buildGrid(width, length); - Integer[][] used = buildGrid(width, length); - - int lastCol = width - 1; - int lastRow = length - 1; + Grid grid = new Grid(length, width); int enterCol = random(0, width); int col = enterCol; @@ -57,47 +53,47 @@ public class Amazing { int totalWalls = width * length + 1; // set up entrance - used[row][col] = count; + grid.cells[row][col].count = count; count++; while (count != totalWalls) { - ArrayList possibleDirs = getPossibleDirs(used, lastCol, lastRow, col, row); + ArrayList possibleDirs = getPossibleDirs(grid, col, row); if (possibleDirs.size() != 0) { Direction direction = possibleDirs.get(random(0, possibleDirs.size())); if (direction == Direction.GO_LEFT) { col--; - walls[row][col] = EXIT_RIGHT; + grid.cells[row][col].exitType = EXIT_RIGHT; } else if (direction == Direction.GO_UP) { row--; - walls[row][col] = EXIT_DOWN; + grid.cells[row][col].exitType = EXIT_DOWN; } else if (direction == Direction.GO_RIGHT) { - walls[row][col] = walls[row][col] + EXIT_RIGHT; + grid.cells[row][col].exitType = grid.cells[row][col].exitType + EXIT_RIGHT; col++; } else if (direction == Direction.GO_DOWN) { - walls[row][col] = walls[row][col] + EXIT_DOWN; + grid.cells[row][col].exitType = grid.cells[row][col].exitType + EXIT_DOWN; row++; } - used[row][col] = count; + grid.cells[row][col].count = count; count++; } else { do { - if (col != lastCol) { + if (col != grid.lastCol) { col++; - } else if (row != lastRow) { + } else if (row != grid.lastRow) { row++; col = 0; } else { row = 0; col = 0; } - } while (used[row][col] == 0); + } while (grid.cells[row][col].count == 0); } } col = random(0, width - 1); row = length - 1; - walls[row][col] = walls[row][col] + 1; + grid.cells[row][col].exitType = grid.cells[row][col].exitType + 1; // top line for (int i=0; i < width; i++) { @@ -112,7 +108,7 @@ public class Amazing { for (int i=0; i < length; i++) { out.print("I"); for (int j = 0; j < width; j++) { - if (walls[i][j] == EXIT_UNSET || walls[i][j] == EXIT_DOWN) { + if (grid.cells[i][j].exitType == EXIT_UNSET || grid.cells[i][j].exitType == EXIT_DOWN) { out.print(" I"); } else { out.print(" "); @@ -120,7 +116,7 @@ public class Amazing { } out.println(); for (int j = 0; j < width; j++) { - if (walls[i][j] == EXIT_UNSET || walls[i][j] == EXIT_RIGHT) { + if (grid.cells[i][j].exitType == EXIT_UNSET || grid.cells[i][j].exitType == EXIT_RIGHT) { out.print(":--"); } else { out.print(": "); @@ -130,30 +126,19 @@ public class Amazing { } } - private Integer[][] buildGrid(int width, int length) { - Integer[][] grid = new Integer[length][width]; - for (int i=0; i < length; i++) { - grid[i] = new Integer[width]; - for (int j = 0; j < width; j++) { - grid[i][j] = EXIT_UNSET; - } - } - return grid; - } - - private ArrayList getPossibleDirs(Integer[][] used, int lastCol, int lastRow, int col, int row) { + private ArrayList getPossibleDirs(Grid grid, int col, int row) { ArrayList possibleDirs = new ArrayList<>(Arrays.asList(Direction.values())); - if (col == FIRST_COL || 0 != used[row][col - 1]) { + if (col == FIRST_COL || 0 != grid.cells[row][col - 1].count) { possibleDirs.remove(Direction.GO_LEFT); } - if (row == FIRST_ROW || 0 != used[row - 1][col]) { + if (row == FIRST_ROW || 0 != grid.cells[row - 1][col].count) { possibleDirs.remove(Direction.GO_UP); } - if (col == lastCol || 0 != used[row][col + 1]) { + if (col == grid.lastCol || 0 != grid.cells[row][col + 1].count) { possibleDirs.remove(Direction.GO_RIGHT); } - if (row == lastRow || 0 != used[row + 1][col]) { + if (row == grid.lastRow || 0 != grid.cells[row + 1][col].count) { possibleDirs.remove(Direction.GO_DOWN); } return possibleDirs; @@ -188,5 +173,30 @@ public class Amazing { Random random = new Random(); return random.nextInt(max - min) + min; } + + public class Cell { + int exitType = EXIT_UNSET; + int count = 0; + } + + public class Grid { + Cell[][] cells; + + int lastCol; + int lastRow; + + public Grid(int length, int width) { + lastCol = width - 1; + lastRow = length - 1; + + cells = new Cell[length][width]; + for (int i=0; i < length; i++) { + cells[i] = new Cell[width]; + for (int j = 0; j < width; j++) { + cells[i][j] = new Cell(); + } + } + } + } } From d0c396b73d4fc3874a7be552fbb66353951aa38a Mon Sep 17 00:00:00 2001 From: Topher Lamey Date: Sat, 27 Feb 2021 22:28:37 -0700 Subject: [PATCH 05/10] Use cell for possible directions --- 02 Amazing/java/Amazing.java | 40 ++++++++++++++++++++++++++++-------- 1 file changed, 32 insertions(+), 8 deletions(-) diff --git a/02 Amazing/java/Amazing.java b/02 Amazing/java/Amazing.java index ae9e886c..cd7c9cdd 100644 --- a/02 Amazing/java/Amazing.java +++ b/02 Amazing/java/Amazing.java @@ -43,7 +43,6 @@ public class Amazing { } } while (width < 1 || length < 1); - Grid grid = new Grid(length, width); int enterCol = random(0, width); @@ -57,7 +56,7 @@ public class Amazing { count++; while (count != totalWalls) { - ArrayList possibleDirs = getPossibleDirs(grid, col, row); + ArrayList possibleDirs = getPossibleDirs(grid, new Cell(row, col)); if (possibleDirs.size() != 0) { Direction direction = possibleDirs.get(random(0, possibleDirs.size())); @@ -126,19 +125,19 @@ public class Amazing { } } - private ArrayList getPossibleDirs(Grid grid, int col, int row) { + private ArrayList getPossibleDirs(Grid grid, Cell cell) { ArrayList possibleDirs = new ArrayList<>(Arrays.asList(Direction.values())); - if (col == FIRST_COL || 0 != grid.cells[row][col - 1].count) { + if (cell.col == FIRST_COL || grid.isPrevColSet(cell)) { possibleDirs.remove(Direction.GO_LEFT); } - if (row == FIRST_ROW || 0 != grid.cells[row - 1][col].count) { + if (cell.row == FIRST_ROW || grid.isPrevRowSet(cell)) { possibleDirs.remove(Direction.GO_UP); } - if (col == grid.lastCol || 0 != grid.cells[row][col + 1].count) { + if (cell.col == grid.lastCol || grid.isNextColSet(cell)) { possibleDirs.remove(Direction.GO_RIGHT); } - if (row == grid.lastRow || 0 != grid.cells[row + 1][col].count) { + if (cell.row == grid.lastRow || grid.isNextRowSet(cell)) { possibleDirs.remove(Direction.GO_DOWN); } return possibleDirs; @@ -177,6 +176,14 @@ public class Amazing { public class Cell { int exitType = EXIT_UNSET; int count = 0; + + int col; + int row; + + public Cell(int row, int col) { + this.row = row; + this.col = col; + } } public class Grid { @@ -193,10 +200,27 @@ public class Amazing { for (int i=0; i < length; i++) { cells[i] = new Cell[width]; for (int j = 0; j < width; j++) { - cells[i][j] = new Cell(); + cells[i][j] = new Cell(i, j); } } } + + public boolean isPrevColSet(Cell cell) { + return 0 != cells[cell.row][cell.col - 1].count; + } + + public boolean isPrevRowSet(Cell cell) { + return 0 != cells[cell.row - 1][cell.col].count; + } + + public boolean isNextColSet(Cell cell) { + return 0 != cells[cell.row][cell.col + 1].count; + } + + public boolean isNextRowSet(Cell cell) { + return 0 != cells[cell.row + 1][cell.col].count; + } + } } From 37681a40b66fb54590a20c6718a1a8e52e44cc26 Mon Sep 17 00:00:00 2001 From: Topher Lamey Date: Sat, 27 Feb 2021 22:33:03 -0700 Subject: [PATCH 06/10] Add setup to grid --- 02 Amazing/java/Amazing.java | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/02 Amazing/java/Amazing.java b/02 Amazing/java/Amazing.java index cd7c9cdd..9bb8b4ae 100644 --- a/02 Amazing/java/Amazing.java +++ b/02 Amazing/java/Amazing.java @@ -45,15 +45,13 @@ public class Amazing { Grid grid = new Grid(length, width); - int enterCol = random(0, width); - int col = enterCol; + int row = 0; - int count = 1; int totalWalls = width * length + 1; - // set up entrance - grid.cells[row][col].count = count; - count++; + int enterCol = grid.setup(); + int col = enterCol; + int count = 2; while (count != totalWalls) { ArrayList possibleDirs = getPossibleDirs(grid, new Cell(row, col)); @@ -192,19 +190,28 @@ public class Amazing { int lastCol; int lastRow; - public Grid(int length, int width) { - lastCol = width - 1; - lastRow = length - 1; + int width; - cells = new Cell[length][width]; + public Grid(int length, int width) { + this.lastCol = width - 1; + this.lastRow = length - 1; + this.width = width; + + this.cells = new Cell[length][width]; for (int i=0; i < length; i++) { - cells[i] = new Cell[width]; + this.cells[i] = new Cell[width]; for (int j = 0; j < width; j++) { - cells[i][j] = new Cell(i, j); + this.cells[i][j] = new Cell(i, j); } } } + public int setup() { + int enterCol = random(0, this.width); + cells[0][enterCol].count = 1; + return enterCol; + } + public boolean isPrevColSet(Cell cell) { return 0 != cells[cell.row][cell.col - 1].count; } From d188128a7c1126cd3de52d3f8114621cab08511d Mon Sep 17 00:00:00 2001 From: Topher Lamey Date: Sat, 27 Feb 2021 23:37:13 -0700 Subject: [PATCH 07/10] Clean up additions --- 02 Amazing/java/Amazing.java | 49 ++++++++++++++++++++++++++++++------ 1 file changed, 41 insertions(+), 8 deletions(-) diff --git a/02 Amazing/java/Amazing.java b/02 Amazing/java/Amazing.java index 9bb8b4ae..fd9edd54 100644 --- a/02 Amazing/java/Amazing.java +++ b/02 Amazing/java/Amazing.java @@ -44,15 +44,14 @@ public class Amazing { } while (width < 1 || length < 1); Grid grid = new Grid(length, width); - - - int row = 0; - int totalWalls = width * length + 1; - int enterCol = grid.setup(); + + int totalWalls = width * length + 1; int col = enterCol; + int row = 0; int count = 2; + Cell cell = grid.cells[row][col]; while (count != totalWalls) { ArrayList possibleDirs = getPossibleDirs(grid, new Cell(row, col)); @@ -90,7 +89,7 @@ public class Amazing { col = random(0, width - 1); row = length - 1; - grid.cells[row][col].exitType = grid.cells[row][col].exitType + 1; + grid.cells[row][col].exitType += 1; // top line for (int i=0; i < width; i++) { @@ -171,7 +170,7 @@ public class Amazing { return random.nextInt(max - min) + min; } - public class Cell { + public static class Cell { int exitType = EXIT_UNSET; int count = 0; @@ -182,9 +181,19 @@ public class Amazing { this.row = row; this.col = col; } + + @Override + public String toString() { + return "Cell{" + + "exitType=" + exitType + + ", count=" + count + + ", col=" + col + + ", row=" + row + + '}'; + } } - public class Grid { + public static class Grid { Cell[][] cells; int lastCol; @@ -228,6 +237,30 @@ public class Amazing { return 0 != cells[cell.row + 1][cell.col].count; } + public Cell getPrevCol(Cell cell) { return cells[cell.row][--cell.col]; } + public Cell getPrevRow(Cell cell) { return cells[--cell.row][cell.col]; } + public Cell getNextCol(Cell cell) { return cells[cell.row][++cell.col]; } + public Cell getNextRow(Cell cell) { return cells[++cell.row][cell.col]; } + + public Cell get(int row, int col) { return cells[row][col]; } + + public Cell getFirstUnset(Cell cell) { + int col = cell.col; + int row = cell.row; + Cell newCell; + do { + if (col != this.lastCol) { + col++; + } else if (row != this.lastRow) { + row++; + col = 0; + } else { + row = 0; + col = 0; + } + } while ((newCell = cells[row][col]).count == 0); + return newCell; + } } } From 5b2368fb9cf346d47bad02e77b040ebaecf5fc1d Mon Sep 17 00:00:00 2001 From: Topher Lamey Date: Sat, 27 Feb 2021 23:45:58 -0700 Subject: [PATCH 08/10] More cell --- 02 Amazing/java/Amazing.java | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/02 Amazing/java/Amazing.java b/02 Amazing/java/Amazing.java index fd9edd54..de262ca8 100644 --- a/02 Amazing/java/Amazing.java +++ b/02 Amazing/java/Amazing.java @@ -51,27 +51,26 @@ public class Amazing { int row = 0; int count = 2; - Cell cell = grid.cells[row][col]; while (count != totalWalls) { - ArrayList possibleDirs = getPossibleDirs(grid, new Cell(row, col)); + Cell cell = grid.cells[row][col]; + ArrayList possibleDirs = getPossibleDirs(grid, cell); if (possibleDirs.size() != 0) { Direction direction = possibleDirs.get(random(0, possibleDirs.size())); if (direction == Direction.GO_LEFT) { - col--; - grid.cells[row][col].exitType = EXIT_RIGHT; + cell = grid.cells[row][--col]; + cell.exitType = EXIT_RIGHT; } else if (direction == Direction.GO_UP) { - row--; - grid.cells[row][col].exitType = EXIT_DOWN; + cell = grid.cells[--row][col]; + cell.exitType = EXIT_DOWN; } else if (direction == Direction.GO_RIGHT) { - grid.cells[row][col].exitType = grid.cells[row][col].exitType + EXIT_RIGHT; - col++; + cell.exitType = cell.exitType + EXIT_RIGHT; + cell = grid.cells[row][++col]; } else if (direction == Direction.GO_DOWN) { - grid.cells[row][col].exitType = grid.cells[row][col].exitType + EXIT_DOWN; - row++; + cell.exitType = cell.exitType + EXIT_DOWN; + cell = grid.cells[++row][col]; } - grid.cells[row][col].count = count; - count++; + cell.count = count++; } else { do { if (col != grid.lastCol) { From 90e2adde76eb6f8837883af5a3c2fbc134a0de8c Mon Sep 17 00:00:00 2001 From: Topher Lamey Date: Sun, 28 Feb 2021 00:45:12 -0700 Subject: [PATCH 09/10] Code cleanup --- 02 Amazing/java/Amazing.java | 121 +++++++++++++++++------------------ 1 file changed, 58 insertions(+), 63 deletions(-) diff --git a/02 Amazing/java/Amazing.java b/02 Amazing/java/Amazing.java index de262ca8..2ff2169a 100644 --- a/02 Amazing/java/Amazing.java +++ b/02 Amazing/java/Amazing.java @@ -44,54 +44,48 @@ public class Amazing { } while (width < 1 || length < 1); Grid grid = new Grid(length, width); - int enterCol = grid.setup(); + int enterCol = grid.setupEntrance(); int totalWalls = width * length + 1; - int col = enterCol; - int row = 0; int count = 2; + Cell cell = grid.startingCell(); while (count != totalWalls) { - Cell cell = grid.cells[row][col]; ArrayList possibleDirs = getPossibleDirs(grid, cell); if (possibleDirs.size() != 0) { - Direction direction = possibleDirs.get(random(0, possibleDirs.size())); - if (direction == Direction.GO_LEFT) { - cell = grid.cells[row][--col]; - cell.exitType = EXIT_RIGHT; - } else if (direction == Direction.GO_UP) { - cell = grid.cells[--row][col]; - cell.exitType = EXIT_DOWN; - } else if (direction == Direction.GO_RIGHT) { - cell.exitType = cell.exitType + EXIT_RIGHT; - cell = grid.cells[row][++col]; - } else if (direction == Direction.GO_DOWN) { - cell.exitType = cell.exitType + EXIT_DOWN; - cell = grid.cells[++row][col]; - } + cell = setCellExit(grid, cell, possibleDirs); cell.count = count++; } else { - do { - if (col != grid.lastCol) { - col++; - } else if (row != grid.lastRow) { - row++; - col = 0; - } else { - row = 0; - col = 0; - } - } while (grid.cells[row][col].count == 0); + cell = grid.getFirstUnset(cell); } } + grid.setupExit(); - col = random(0, width - 1); - row = length - 1; - grid.cells[row][col].exitType += 1; + writeMaze(width, grid, enterCol); + } + private Cell setCellExit(Grid grid, Cell cell, ArrayList possibleDirs) { + Direction direction = possibleDirs.get(random(0, possibleDirs.size())); + if (direction == Direction.GO_LEFT) { + cell = grid.getPrevCol(cell); + cell.exitType = EXIT_RIGHT; + } else if (direction == Direction.GO_UP) { + cell = grid.getPrevRow(cell); + cell.exitType = EXIT_DOWN; + } else if (direction == Direction.GO_RIGHT) { + cell.exitType = cell.exitType + EXIT_RIGHT; + cell = grid.getNextCol(cell); + } else if (direction == Direction.GO_DOWN) { + cell.exitType = cell.exitType + EXIT_DOWN; + cell = grid.getNextRow(cell); + } + return cell; + } + + private void writeMaze(int width, Grid grid, int enterCol) { // top line - for (int i=0; i < width; i++) { + for (int i = 0; i < width; i++) { if (i == enterCol) { out.print(". "); } else { @@ -100,18 +94,18 @@ public class Amazing { } out.println('.'); - for (int i=0; i < length; i++) { + for (Cell[] rows : grid.cells) { out.print("I"); - for (int j = 0; j < width; j++) { - if (grid.cells[i][j].exitType == EXIT_UNSET || grid.cells[i][j].exitType == EXIT_DOWN) { + for (Cell cell : rows) { + if (cell.exitType == EXIT_UNSET || cell.exitType == EXIT_DOWN) { out.print(" I"); } else { out.print(" "); } } out.println(); - for (int j = 0; j < width; j++) { - if (grid.cells[i][j].exitType == EXIT_UNSET || grid.cells[i][j].exitType == EXIT_RIGHT) { + for (Cell cell : rows) { + if (cell.exitType == EXIT_UNSET || cell.exitType == EXIT_RIGHT) { out.print(":--"); } else { out.print(": "); @@ -159,11 +153,6 @@ public class Amazing { return new String(spacesTemp); } - public static boolean random() { - Random random = new Random(); - return random.nextBoolean(); - } - public static int random(int min, int max) { Random random = new Random(); return random.nextInt(max - min) + min; @@ -180,16 +169,6 @@ public class Amazing { this.row = row; this.col = col; } - - @Override - public String toString() { - return "Cell{" + - "exitType=" + exitType + - ", count=" + count + - ", col=" + col + - ", row=" + row + - '}'; - } } public static class Grid { @@ -199,6 +178,7 @@ public class Amazing { int lastRow; int width; + int enterCol; public Grid(int length, int width) { this.lastCol = width - 1; @@ -214,10 +194,19 @@ public class Amazing { } } - public int setup() { - int enterCol = random(0, this.width); - cells[0][enterCol].count = 1; - return enterCol; + public int setupEntrance() { + this.enterCol = random(0, this.width); + cells[0][this.enterCol].count = 1; + return this.enterCol; + } + + public void setupExit() { + int exit = random(0, width - 1); + cells[lastRow][exit].exitType += 1; + } + + public Cell startingCell() { + return cells[0][enterCol]; } public boolean isPrevColSet(Cell cell) { @@ -236,12 +225,18 @@ public class Amazing { return 0 != cells[cell.row + 1][cell.col].count; } - public Cell getPrevCol(Cell cell) { return cells[cell.row][--cell.col]; } - public Cell getPrevRow(Cell cell) { return cells[--cell.row][cell.col]; } - public Cell getNextCol(Cell cell) { return cells[cell.row][++cell.col]; } - public Cell getNextRow(Cell cell) { return cells[++cell.row][cell.col]; } - - public Cell get(int row, int col) { return cells[row][col]; } + public Cell getPrevCol(Cell cell) { + return cells[cell.row][cell.col - 1]; + } + public Cell getPrevRow(Cell cell) { + return cells[cell.row - 1][cell.col]; + } + public Cell getNextCol(Cell cell) { + return cells[cell.row][cell.col + 1]; + } + public Cell getNextRow(Cell cell) { + return cells[cell.row + 1][cell.col]; + } public Cell getFirstUnset(Cell cell) { int col = cell.col; From 57a5bad47d9ed47bc58fabf0ce8340531d236d89 Mon Sep 17 00:00:00 2001 From: Topher Lamey Date: Sun, 28 Feb 2021 00:47:50 -0700 Subject: [PATCH 10/10] Reformatted --- 02 Amazing/java/Amazing.java | 66 +++++++++++++++++++----------------- 1 file changed, 34 insertions(+), 32 deletions(-) diff --git a/02 Amazing/java/Amazing.java b/02 Amazing/java/Amazing.java index 2ff2169a..5188f14f 100644 --- a/02 Amazing/java/Amazing.java +++ b/02 Amazing/java/Amazing.java @@ -5,27 +5,40 @@ import java.util.Scanner; import static java.lang.System.in; import static java.lang.System.out; +/** + * Core algorithm copied from amazing.py + */ public class Amazing { + final static int FIRST_COL = 0; + final static int FIRST_ROW = 0; + final static int EXIT_UNSET = 0; + final static int EXIT_DOWN = 1; + final static int EXIT_RIGHT = 2; private final Scanner kbScanner; - public Amazing() { kbScanner = new Scanner(in); } - enum Direction { - GO_LEFT, - GO_UP, - GO_RIGHT, - GO_DOWN, + private static int getDelimitedValue(String text, int pos) { + String[] tokens = text.split(","); + try { + return Integer.parseInt(tokens[pos]); + } catch (Exception ex) { + return 0; + } } - final static int FIRST_COL = 0; - final static int FIRST_ROW = 0; + private static String tab(int spaces) { + char[] spacesTemp = new char[spaces]; + Arrays.fill(spacesTemp, ' '); + return new String(spacesTemp); + } - final static int EXIT_UNSET = 0; - final static int EXIT_DOWN = 1; - final static int EXIT_RIGHT = 2; + public static int random(int min, int max) { + Random random = new Random(); + return random.nextInt(max - min) + min; + } public void play() { out.println(tab(28) + "AMAZING PROGRAM"); @@ -138,24 +151,11 @@ public class Amazing { return kbScanner.next(); } - private static int getDelimitedValue(String text, int pos) { - String[] tokens = text.split(","); - try { - return Integer.parseInt(tokens[pos]); - } catch (Exception ex) { - return 0; - } - } - - private static String tab(int spaces) { - char[] spacesTemp = new char[spaces]; - Arrays.fill(spacesTemp, ' '); - return new String(spacesTemp); - } - - public static int random(int min, int max) { - Random random = new Random(); - return random.nextInt(max - min) + min; + enum Direction { + GO_LEFT, + GO_UP, + GO_RIGHT, + GO_DOWN, } public static class Cell { @@ -186,7 +186,7 @@ public class Amazing { this.width = width; this.cells = new Cell[length][width]; - for (int i=0; i < length; i++) { + for (int i = 0; i < length; i++) { this.cells[i] = new Cell[width]; for (int j = 0; j < width; j++) { this.cells[i][j] = new Cell(i, j); @@ -228,12 +228,15 @@ public class Amazing { public Cell getPrevCol(Cell cell) { return cells[cell.row][cell.col - 1]; } + public Cell getPrevRow(Cell cell) { return cells[cell.row - 1][cell.col]; } + public Cell getNextCol(Cell cell) { return cells[cell.row][cell.col + 1]; } + public Cell getNextRow(Cell cell) { return cells[cell.row + 1][cell.col]; } @@ -256,5 +259,4 @@ public class Amazing { return newCell; } } -} - +} \ No newline at end of file