diff --git a/text-quest/README-G3.md b/text-quest/README-G3.md index afd2719..e1d8c9b 100644 --- a/text-quest/README-G3.md +++ b/text-quest/README-G3.md @@ -21,7 +21,7 @@ which the randomness can be obtained. ## Fast & cheap randomness In Java the `Math.random()` method is a **low-quality** and fast method for generating random -**double** values. It returns a pseudorandom number in the range **>= 0.0 and <1.0**. It turns out +**double** values. It returns a pseudorandom number in the range **>= 0.0 and <1.0**. It turns out having a random value beween 0 and 1 is quite useful, as you can think of this as being a **random fraction**. @@ -66,7 +66,7 @@ public class CoinToss { } else if (rand >= 0.5 && !guessTails) { System.out.println("Correct, it was heads (>= 0.5)!"); } else { - System.out.println("Oops, it was " + System.out.println("Oops, it was " + (rand < 0.5 ? "tails" : "heads") + ". Try again!"); } @@ -120,12 +120,12 @@ summoning your math teacher for assistance. Either way, bravo! You figured out y number between 0 and `chestCoinMaximum`. The result would be a decimal, but we can round the value to a nice integer to get whole integers between 0 and `chestCoinMaximum`. For example: -| Random Double | Chest Coin Maximum | Scary Math | Result | Rounded | -|:--------------|:-------------------|:-----------|:-------|:--------| -| 0.0 | 10 | `0.0 × 10` | 0.0 | 0 | -| 0.2 | 10 | `0.2 × 10` | 2.0 | 2 | -| 0.8 | 10 | `0.8 × 10` | 8.0 | 8 | -| 0.99999999 | 10 | `0.99999999 × 10` | 9.99999999 | 10 | +| Random Double | Chest Coin Maximum | Scary Math | Result | Rounded | +| :------------ | :----------------- | :---------------- | :--------- | :------ | +| 0.0 | 10 | `0.0 × 10` | 0.0 | 0 | +| 0.2 | 10 | `0.2 × 10` | 2.0 | 2 | +| 0.8 | 10 | `0.8 × 10` | 8.0 | 8 | +| 0.99999999 | 10 | `0.99999999 × 10` | 9.99999999 | 10 | In Java the `Math.random()` method can round a `double` value into a `long` value. When you only want an `int` you can **cast** a `long` into an `int` by adding `(int)` in front of the value you @@ -147,7 +147,6 @@ public class DynamicRandomRange { public static int randomIntFromZeroTo(int max) { // TODO: implement this method by generating a random integer between 0 and max - return 0; } public static void main(String[] args) throws IOException { diff --git a/text-quest/src/main/java/coding101/tq/TextQuest.java b/text-quest/src/main/java/coding101/tq/TextQuest.java index ffbf01a..bbfb5d0 100644 --- a/text-quest/src/main/java/coding101/tq/TextQuest.java +++ b/text-quest/src/main/java/coding101/tq/TextQuest.java @@ -315,11 +315,27 @@ private void interactWithChest() throws IOException { final GameConfiguration config = player.config(); int coinsFound = 0; int damageTaken = 0; + boolean trap; - // TODO: open chest and deal with outcome: damage vs coins; decide first if - // the chest provides coins or deducts health. Then decide either how many - // coins to reward with, or health to deduct from, the player, updating the - // coinsFound or damageTaken variables appropriately. + double outPut = (Math.random() * 100); + int roundedOutPut = (int) Math.round(outPut); + + // Decide wheater to give coins or pain + if (roundedOutPut >= config.chestRewardFactor()) { + trap = true; + } else { + trap = false; + } + + // givineing coins + if (trap) { + outPut = (Math.random() * config.chestCoinsMaximum()); + coinsFound = (int) Math.round(outPut); + } else { + // gieving damagef + outPut = (Math.random() * config.chestHealthDamageMaximum()); + damageTaken = (int) Math.round(outPut); + } if (coinsFound > 0) { message = MessageFormat.format(bundle.getString("chest.coinsAcquired"), coinsFound); @@ -658,6 +674,7 @@ public static void main(String[] args) { cl = null; try (Terminal terminal = createTerminal(config)) { + TerminalSize screenSize = terminal.getTerminalSize(); if (screenSize.getColumns() < 30 || screenSize.getRows() < 10) { printErrorAndExit("Terminal must be at least 30x10."); diff --git a/text-quest/src/main/java/coding101/tq/domain/Player.java b/text-quest/src/main/java/coding101/tq/domain/Player.java index 7bac465..038f099 100644 --- a/text-quest/src/main/java/coding101/tq/domain/Player.java +++ b/text-quest/src/main/java/coding101/tq/domain/Player.java @@ -405,6 +405,10 @@ public PlayerItems getItems() { */ public boolean visited(TerrainMap map, int x, int y) { assert map != null; + // DUNNIIT: walking on lava should decrease player's health + if (map.terrainAt(x, y) == TerrainType.Lava) { + this.health -= config.lavaHealthDamage(); + } // TODO: walking on lava should decrease player's health // update the visited state of this coordinate @@ -579,6 +583,15 @@ public boolean canMoveTo(TerrainMap map, int x, int y) { return (newTerrain == TerrainType.Water || newTerrain == TerrainType.Ship) && !vehicleLocatedAt(map, x, y); } // TODO: finish validation that player can move to specified coordinate - return true; + if ((newTerrain == TerrainType.Water + || newTerrain == TerrainType.Mountain + || newTerrain == TerrainType.WallHorizontal + || newTerrain == TerrainType.WallVertical + || newTerrain == TerrainType.WallCorner) + && !vehicleLocatedAt(map, x, y)) { + return false; + } else { + return true; + } } } diff --git a/text-quest/src/main/resources/META-INF/tqmaps/local/0000,0000.tqmap b/text-quest/src/main/resources/META-INF/tqmaps/local/0000,0000.tqmap new file mode 100644 index 0000000..64997b8 --- /dev/null +++ b/text-quest/src/main/resources/META-INF/tqmaps/local/0000,0000.tqmap @@ -0,0 +1,70 @@ +# Size: 100x100 +# +# A mountain O cave +# m hill & ship +# ~ water * town +# , sand +# . grass +# ^ forest +# = lava +# " lava rock +# +#- start: 26,44 +# +# 1 2 3 4 5 6 7 8 9 C +AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA +AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA +AAAAAAAAAAAA^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^AAAAAAAAAAAAAAAAAAA +AAAAAA^^^^^^^^^^^^^^^^^^^^^^^^^^^^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A................OAAAAAAAAAAAAAAAAAAAAAA~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^~~^^^^^^^^^~~~~~~~~~~~~~~~~~~~~~~~~~~ +AAAAAAAAAAAA........................AAAAAAAAAAAAAAAAAAAA...~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^~~^^^^^^^^^^^^~~~~~~~~~~~~~~~~~~~~~ +AAAAAAAAAA..........................AAAAAAAAAAAAAAAAAAA....~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^~~^^^^^^^^^^^~~~~~~~~~~~~~~~~~~~~~ +AAAAAAAAAA...........................AAAAAAAAAAAAAAAAA.....~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^~~^^^^^^^^^^^ +AAAAAAAA................................AAAAAAAAAAAA.........~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^~~ +AAAAAAAA................................AAAAAAA..............~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^~~~~~~~ +AAAAAAAA..................................AA.................~~~^^^^^^^~~~~~~~~~~~~~~~~~~~~~~~~~~ +AAAAAAA.......................................................~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +AAAAAAA.......................................................~~~~~~~ +AAAAAA........................................................~~ +AAAAAA..................................===...................~~ +AAAAAA..................................===...................~~ +AAAAA..........................................................~~ +AAAA............................................................~~~ +AAAA..............................................................~~ +AAA...............................................................~~~ +AA.................................................................~~~ +AA.................................................................&~~~ +AA^^^^^..............................................................~~ +AA^^^^^^^^^..........................................................~~ +AAA^^^^^^^^^^............*...........................................~~~ +AAAA^^^^^^^^^^^^^^^...................................................~~~ +AAAAA^^^^^^^^^^^^^^^^..................................................~~~ +AAAAA^^^^^^^^^^^^^^^^^^^^^.............................................~~~~~ +AAAAAA^^^^^^^^^^^^^^^^^^^^^^^..............................................~~~~~ +AAAAAAA^^^^^^^^^^^^^^^^^^^^^^^^...........................^^^^...............~~~~~~ +AAAAAAAA^^^^^^^^^^^^^^^^^^^^^^^^^^.......................^^^^^^..................~~~~~ +AAAAAAAAAAAA^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^.............^^^^^^^^^^....................~~ +AAAAAAAAAAAAAAAA^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^...................~~ +AAAAAAAAAAAAAAAAAAA^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^...................~~~ +AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA +AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA diff --git a/text-quest/src/main/resources/META-INF/tqmaps/main/0027,0001/0000,0000.tqmap b/text-quest/src/main/resources/META-INF/tqmaps/local/0034,0024/0000,0000.tqmap similarity index 100% rename from text-quest/src/main/resources/META-INF/tqmaps/main/0027,0001/0000,0000.tqmap rename to text-quest/src/main/resources/META-INF/tqmaps/local/0034,0024/0000,0000.tqmap diff --git a/text-quest/src/main/resources/META-INF/tqmaps/local/0078,0015/0000,0000.tqmap b/text-quest/src/main/resources/META-INF/tqmaps/local/0078,0015/0000,0000.tqmap new file mode 100644 index 0000000..3e88bc7 --- /dev/null +++ b/text-quest/src/main/resources/META-INF/tqmaps/local/0078,0015/0000,0000.tqmap @@ -0,0 +1,29 @@ +# A mountain O cave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^^^^^^^^^^^^^^^^^^^^^^~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^AAAAAAAAAA +AA^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^AAAAAAAAAA +AA^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^~^^^^^^^^^^^^^^^^OAAAAAAAA +AA^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^~~^^^^^^^^^^^^^AAAAAAAAAAA +AAA^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^AA~~AAAAAAAAAAAAAAAAAAAAAAA +AAAAA^^^^^^^^AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA~~AAAAAAAAAAAAAAAAAAAAAAA +AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA diff --git a/text-quest/src/test/java/coding101/tq/example/DynamicRandomRange.java b/text-quest/src/test/java/coding101/tq/example/DynamicRandomRange.java index dfee527..e1394a8 100644 --- a/text-quest/src/test/java/coding101/tq/example/DynamicRandomRange.java +++ b/text-quest/src/test/java/coding101/tq/example/DynamicRandomRange.java @@ -8,8 +8,10 @@ public class DynamicRandomRange { public static int randomIntFromZeroTo(int max) { - // TODO: implement this method by generating a random integer between 0 and max - return 0; + double outPut = (Math.random() * max); + int roundedOutPut = (int) Math.round(outPut); + + return roundedOutPut; } public static void main(String[] args) throws IOException { diff --git a/text-quest/src/test/java/coding101/tq/example/LinkedLists.java b/text-quest/src/test/java/coding101/tq/example/LinkedLists.java index 88040e2..39c81e1 100644 --- a/text-quest/src/test/java/coding101/tq/example/LinkedLists.java +++ b/text-quest/src/test/java/coding101/tq/example/LinkedLists.java @@ -90,7 +90,17 @@ public void clear() { * @param item the item to add */ public void add(T item) { - // TODO: add item to the end of the list + var newElement = new LinkedListElement(item); + if (head != null) { + for (var element = this.head; element != null; element = element.next) { + if (element.next == null) { + element.next = newElement; + return; + } + } + } else { + head = newElement; + } } /** @@ -100,7 +110,9 @@ public void add(T item) { * @param fn the callback function to call for every item in the list */ public void forEach(Consumer fn) { - // TODO: iterate over all items in the list and invoke fn.accept() on each + for (var element = this.head; element != null; element = element.next) { + fn.accept(element.item()); + } } /** @@ -111,8 +123,9 @@ public void forEach(Consumer fn) { public int size() { int size = 0; - // TODO: count and return the number of elements in the list - + for (var element = this.head; element != null; element = element.next) { + size++; + } return size; } @@ -125,6 +138,17 @@ public int size() { */ public void insertAfter(T item, T after) { // TODO insert item behind after, or the end of the list if not found + var newElement = new LinkedListElement(item); + for (var element = this.head; element != null; element = element.next) { + if (element.item.equals(after)) { + newElement.next = element.next; + element.next = newElement; + return; + } else if (element.next == null) { + element.next = newElement; + return; + } + } } /** diff --git a/tic-tac-toe/src/main/java/coding101/ttt/TicTacToe.java b/tic-tac-toe/src/main/java/coding101/ttt/TicTacToe.java index c57b98f..b9f77f5 100644 --- a/tic-tac-toe/src/main/java/coding101/ttt/TicTacToe.java +++ b/tic-tac-toe/src/main/java/coding101/ttt/TicTacToe.java @@ -105,7 +105,14 @@ public void go(BufferedReader in, BufferedWriter out) throws IOException { * @return true if the given coordinate can be moved on */ private boolean isMoveValid(Coordinate coord) { - return true; + if (coord.x() >= size + || coord.y() >= size + || board[coord.y()][coord.x()] == Status.X + || board[coord.y()][coord.x()] == Status.O) { + return false; + } else { + return true; + } } /** @@ -114,6 +121,69 @@ private boolean isMoveValid(Coordinate coord) { * @return true if the game has been won */ private boolean isWon(Coordinate lastMove) { + var player = board[lastMove.y()][lastMove.x()]; + var row = lastMove.y(); + var column = lastMove.x(); + + // keeps track how many squaers have been checked if is equal to size someone won + var counter = 0; + + // look for a row win + for (int i = 0; i < size; i++) { + if (board[row][i] == player) { + counter++; + } else { + break; + } + } + if (counter == size) { + return true; + } + + // reset the counter + counter = 0; + + // the same as previous loop but for columns + for (int i = 0; i < size; i++) { + if (board[i][column] == player) { + counter++; + } else { + break; + } + } + if (counter == size) { + return true; + } + + // reset the counter + counter = 0; + + // diagonal wining left top to rigth bottom + for (int i = 0; i < size; i++) { + if (board[i][i] == player) { + counter++; + } else { + break; + } + } + if (counter == size) { + return true; + } + + // reset the counter + counter = 0; + + // diagonal wining right top to left bottom + for (int i = 0, j = size - 1; i < size; i++, j--) { + if (board[i][j] == player) { + counter++; + } else { + break; + } + } + if (counter == size) { + return true; + } return false; } @@ -122,11 +192,24 @@ private boolean isWon(Coordinate lastMove) { * @return true if the game is a draw */ private boolean isDraw() { - return false; + for (int row = 0; row < size; row++) { + for (int col = 0; col < size; col++) { + if (board[row][col] == null) { + return false; + } + } + } + return true; } public static void main(String[] args) { - var game = new TicTacToe(3); + // accept an integer size for the game board, defaulting to 3 if none provided + var size = (args.length > 0 ? Integer.parseInt(args[0]) : 3); + if (size < 2 || size > 26) { + System.err.println("Invalid board size, please enter a size between 2 and 26."); + System.exit(1); + } + var game = new TicTacToe(size); try (BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out))) { game.go(in, out);