-
Notifications
You must be signed in to change notification settings - Fork 2
Dima_Troshkin #14
base: master
Are you sure you want to change the base?
Dima_Troshkin #14
Changes from all commits
bc5b417
c9a91ef
9b2c53a
edb888f
0995d15
64d8f00
b2e3bc6
266c2b8
8b478c0
6823875
67f119c
565446c
4fe6027
c63ac42
bda20a4
26b29eb
a496419
a656a09
ac206da
6e99a49
cd5f59a
283294f
d2a67d8
419d58f
b6b2447
8355428
c6b08c4
42342a7
31540dc
a85f7af
2edf810
69dbdf9
a89343d
95c1290
06f4d21
4377bb9
3c0bae2
2a79bf1
23bf1af
23dfa52
c9ce529
a5b4ccf
6ca1c87
629406f
104f822
ae464c6
4811bf6
db302d7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,22 @@ | ||
| # Java Core June 2021 | ||
|
|
||
| ## *Nikolaev Artem* | ||
| ## *Dima Troshkin* | ||
|
|
||
| [CodingBat Profile](https://codingbat.com/done?user=allzza4279@gmail.com&tag=1424372731) | ||
|
|
||
| | Number | Solution | Short description | ||
| | --- | --- | --- | | ||
| | HW1 | [Console printer](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/master/src/main/java/homework_1) | The app that reads input arguments and prints them, until "error" argument | | ||
|
|
||
| [Link to markdown giude](https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet) | ||
| | HW1 | [Console printer](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/DimaTroshkin/src/main/java/homework_1/) | The app that reads input arguments and prints them, until "error" argument | | ||
| | HW2 | [Traffic light](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/DimaTroshkin/src/main/java/homework_2/traffic_light/) | The app that reads time from keyboard and says which color the traffic light is| | ||
| | | [Pyramid printer](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/DimaTroshkin/src/main/java/homework_2/homework_2.pyramid_printer/) | The app that makes pyramid of "x" using int input| | ||
| | | [Random chars table](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/DimaTroshkin/src/main/java/homework_2/random_chars_table/) | The app which prints table of random letters using int numbers of rows and columns and string strategy - "even" or "odd". Also prints out all even or odd letters according to strategy chosen| | ||
| | HW3 | [ImmutableClass](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/DimaTroshkin/src/main/java/homework_3/)| An example of Immutable Class realization | | ||
| | HW4 | [CustomAnnotation](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/DimaTroshkin/src/main/java/homework_4/custom_annotation/)| An example of custom annotation realization | | ||
| | | [CustomFileReader](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/DimaTroshkin/src/main/java/homework_4/custom_file_reader/)| An app which reads String data from file.txt and removes commas and fullstops | | ||
| | | [Singleton](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/DimaTroshkin/src/main/java/homework_4/singleton/)| An example of singleton class | | ||
| | HW5 | [CustomRegexMatcher](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/DimaTroshkin/src/main/java/homework_5/custom_regex_matcher/)| An app which takes String from terminal and checks whether given String matches hardcoded regex| | ||
| | | [PowerOfNumber](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/DimaTroshkin/src/main/java/homework_5/power_of_number/)| An app which calculates a^b for two non-negative integers a and b | | ||
| | HW6 | [MapPloblemGenerators](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/DimaTroshkin/src/main/java/homework_6/)| The realisation of two classes which generate problems using hashmap| | ||
| | HW7 | [KittenToCatFunction](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/DimaTroshkin/src/main/java/homework_7/)| The realisation of @FunctionalInterface KittenToCatFunction with abstract method grow(). From now on we know how does kitten become a cat!| | ||
| | HW7 | [SeaBattle](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/DimaTroshkin/src/main/java/course_project/)| The Sea Battle game 1 by 1 (No AI) | | ||
| [Link to markdown guide](https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| package course_project; | ||
|
|
||
| import course_project.models.Player; | ||
| import course_project.models.Ship; | ||
| import course_project.engine.ShipPlacer; | ||
| import course_project.engine.Shooter; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.ArrayList; | ||
|
|
||
| public class Battle { | ||
|
|
||
| public static void run() throws IOException { | ||
| Player player1 = new Player(); | ||
| Player player2 = new Player(); | ||
|
|
||
| ArrayList<Player> players = new ArrayList<>(); | ||
| players.add(player1); | ||
| players.add(player2); | ||
|
|
||
| Player currentPlayer; | ||
| for (Player player : players) { // preparing both players battlefields | ||
| currentPlayer = player; | ||
|
|
||
| System.out.println("\n \n" + currentPlayer.name() + ", welcome to the Sea Battle. \n \n" + | ||
| "It`s time to place your ships. Remember the main rule: ships must not touch each other.\n" + "To place a ship you basically need to type coordinate and then choose mode. \n" + | ||
| "There are two ways to place your ships from starting coordinate: from left to right (key \"h\") & from up to down (key \"v\")\n" + | ||
| "If there is only one possible way, it will be chosen automatically, if it`s impossible to place ship from this coordinate, you will have to choose another one.\n"); | ||
|
|
||
| System.out.println("Let`s begin with Carrier - huge battleship with size 4. \n"); | ||
| ShipPlacer.placement(new Ship("Carrier", 4), currentPlayer); | ||
|
|
||
| System.out.println("Now place 2 Cruisers of size 3"); | ||
| for (int i = 0; i < 2; i++) { | ||
| ShipPlacer.placement(new Ship("Cruiser", 3), currentPlayer); | ||
| } | ||
| System.out.println("Now place 3 Destroyers of size 2"); | ||
| for (int i = 0; i < 3; i++) { | ||
| ShipPlacer.placement(new Ship("Destroyer", 2), currentPlayer); | ||
| } | ||
| System.out.println("And finally place 4 Scout ships of size 1"); | ||
| for (int i = 0; i < 4; i++) { | ||
| ShipPlacer.placement(new Ship("Scout", 1), currentPlayer); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| System.out.println("\n\n\n\n\n\n\n\n\n\n\n\n\n"); | ||
|
|
||
| while (player1.hp() > 0 && player2.hp() > 0) { | ||
| Shooter.Shoot(player1, player2); | ||
| if (player2.hp() == 0) { | ||
| break; | ||
| } | ||
| Shooter.Shoot(player2, player1); | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| package course_project; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| public class Main { | ||
|
|
||
|
|
||
| public static void main(String[] args) throws IOException { | ||
| Battle.run(); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Quite good! You did PvP seabattle. Looks nice, interface is comfortable. Opt: add delays/enter between players, so they will not see each other fields. Also if ship is sunk, near cells can be marked as hit. But the game is good as-is.
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice abstractions, model/service segregation, the code is easy to read in general, I didn't find any bugs. Approved!
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it was planned, but had no time left. Thank you for checking. Will work on it later and have a whale of a time with my mates, encouraging them go programming) |
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| package course_project.engine; | ||
|
|
||
| import course_project.models.Player; | ||
| import course_project.models.Board; | ||
|
|
||
| import java.util.Locale; | ||
|
|
||
| public class BoardPrinter { | ||
|
|
||
| public static void printMainBoard(Player player) { | ||
| Board board = player.mainBoard(); | ||
| System.out.println(player.name() + ": MAIN BOARD"); | ||
| int rowNum = 1; | ||
| String rowOffset; | ||
| System.out.println(" " + board.getColNames().stream().reduce(((s, s2) -> s + " " + s2)).orElse("").toUpperCase(Locale.ROOT)); | ||
| for (int row : board.getRowNames()) { | ||
| rowOffset = (rowNum < 10) ? rowNum + " |" : rowNum + " |"; | ||
| System.out.print(rowOffset); | ||
|
|
||
| for (String cell : board.getBoard().get(row)) { | ||
| System.out.print(" " + cell + " " + "|"); | ||
| } | ||
| System.out.print("\n"); | ||
| rowNum++; | ||
| } | ||
| } | ||
|
|
||
| public static void printBothBoards(Player player) { | ||
| Board mainBoard = player.mainBoard(); | ||
| Board scanBoard = player.scanBoard(); | ||
| int rowNum = 1; | ||
| String spaceBetween = " "; | ||
| String rowOffset; | ||
| System.out.println(spaceBetween + player.name() + ": MAIN BOARD" + spaceBetween + spaceBetween + " ENEMY TRACKING"); | ||
| // top | ||
| System.out.print(" " + mainBoard.getColNames().stream().reduce(((s, s2) -> s + " " + s2)).orElse("").toUpperCase(Locale.ROOT)); | ||
| System.out.print(spaceBetween); | ||
| System.out.println(" " + scanBoard.getColNames().stream().reduce(((s, s2) -> s + " " + s2)).orElse("").toUpperCase(Locale.ROOT)); | ||
|
|
||
| for (int row : mainBoard.getRowNames()) { | ||
| rowOffset = (rowNum < 10) ? rowNum + " |" : rowNum + " |"; | ||
| System.out.print(rowOffset); | ||
|
|
||
| for (String cell : mainBoard.getBoard().get(row)) { | ||
| System.out.print(" " + cell + " " + "|"); | ||
| } | ||
| System.out.print(spaceBetween); | ||
| System.out.print(rowOffset); | ||
| for (String cell : scanBoard.getBoard().get(row)) { | ||
| System.out.print(" " + cell + " " + "|"); | ||
| } | ||
|
|
||
| System.out.print("\n"); | ||
| rowNum++; | ||
| } | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| package course_project.engine; | ||
|
|
||
| import java.io.BufferedReader; | ||
| import java.io.IOException; | ||
| import java.io.InputStreamReader; | ||
| import java.util.Locale; | ||
|
|
||
| public class Input { | ||
|
|
||
| public static String getRightCoordinate() throws IOException { | ||
| System.out.println("Enter coordinate if format: \"a1\""); | ||
| String s; | ||
| BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); | ||
| while (true) { | ||
| s = reader.readLine(); | ||
| if (Input.checkCoordinate(s)) { | ||
| System.out.println("Coordinate: " + s); | ||
| return s; | ||
| } else { | ||
| System.out.println("Wrong Input! use letters from a to j and numbers from 1 to 10, like \"d4\""); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public static String getRightMode() throws IOException { | ||
| System.out.println("Choose mode: use letter \"v\" to place it vertically or \"h\" to place it horizontally"); | ||
| String s; | ||
| BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); | ||
| while (true) { | ||
| s = reader.readLine(); | ||
| if (Input.checkMode(s)) { | ||
| System.out.println("Mode: " + s); | ||
| return s; | ||
| } else { | ||
| System.out.println("Wrong mode! use letter \"v\" to place it vertically or \"h\" to place it horizontally."); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public static boolean checkCoordinate(String s) { | ||
| if (s.length() > 3 || s.length() < 2) { | ||
| return false; | ||
| } | ||
|
|
||
| if (!s.toLowerCase(Locale.ROOT).substring(0, 1).matches("[a-j]")) { | ||
| return false; | ||
| } | ||
|
|
||
| if (!s.toLowerCase(Locale.ROOT).substring(1, 2).matches("[1-9]")) { | ||
| return false; | ||
| } | ||
|
|
||
| if (s.length() == 3 && !s.toLowerCase(Locale.ROOT).substring(2).matches("0")) { | ||
| return false; | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| public static boolean checkMode(String s) { | ||
| if (s.toLowerCase(Locale.ROOT).matches("[vh]")) { | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| package course_project.engine; | ||
|
|
||
| import course_project.models.Board; | ||
| import course_project.models.Coordinate; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public class PositionChecker { | ||
|
|
||
| public static boolean possibleToPlace(Board b, Coordinate c, int size, String mode) { | ||
| // mode h: left -> right , mode v: up - > down from coordinate given | ||
| int horizSize; | ||
| int vertSize; | ||
|
|
||
| if (mode.equalsIgnoreCase("h")) { // space needed for ship in horizontal mode | ||
| horizSize = size; | ||
| vertSize = 1; | ||
| } else { // space needed for ship in vertical mode | ||
| horizSize = 1; | ||
| vertSize = size; | ||
| } | ||
|
|
||
| List<String> currentRow; | ||
| List<String> upperRow; | ||
| List<String> lowerRow; | ||
|
|
||
| // Checking upper row | ||
| if (b.getBoard().containsKey(c.row() - 1)) { // if it exist | ||
| upperRow = b.getBoard().get(c.row() - 1); | ||
| for (int col = c.col() - 1; col <= c.col() + horizSize; col++) { // check all cols from current -1 to current + size | ||
| if (col >= 0 && col < 10) { // if mainBoard contains such col | ||
| if (!upperRow.get(col).equals(" ")) { // cell must be empty | ||
| return false; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Checking hull rows | ||
| for (int i = 0; i < vertSize; i++) { | ||
| if (!b.getBoard().containsKey(c.row() + i)) { // if doesn`t exist -> not enough vertical space | ||
| return false; | ||
| } | ||
| currentRow = b.getBoard().get(c.row() + i); | ||
| for (int col = c.col() - 1; col <= c.col() + horizSize; col++) { // check all cols from current -1 to current + horiz size | ||
| if (col >= 0 && col < 10) { // if mainBoard contains such col | ||
| if (!currentRow.get(col).equals(" ")) { // cell must be empty | ||
| return false; | ||
| } | ||
| } else { | ||
| if ((col - 1 > 9) && mode.equalsIgnoreCase("h")) { // if not enough space horizontally (last hull coord comes out of the map) | ||
| return false; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| } | ||
|
|
||
|
|
||
| // Checking lower row | ||
| if (b.getBoard().containsKey(c.row() + vertSize)) { // if it exist | ||
| lowerRow = b.getBoard().get(c.row() + vertSize); | ||
| for (int col = c.col() - 1; col <= c.col() + horizSize; col++) { // check all cols from current -1 to current + size | ||
| if (col >= 0 && col < 10) { // if mainBoard contains such col | ||
| if (!lowerRow.get(col).equals(" ")) { // cell must be empty | ||
| return false; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| package course_project.engine; | ||
|
|
||
| import course_project.models.Player; | ||
| import course_project.models.Board; | ||
| import course_project.models.Coordinate; | ||
| import course_project.models.Ship; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.ArrayList; | ||
|
|
||
| public class ShipPlacer { | ||
|
|
||
| public static void placement(Ship ship, Player p) throws IOException { | ||
|
|
||
| Board b = p.mainBoard(); | ||
| while (true) { | ||
| BoardPrinter.printMainBoard(p); | ||
| System.out.println("It`s time to place a ship."); | ||
| Coordinate c = new Coordinate(Input.getRightCoordinate()); | ||
| String mode; | ||
| int size = ship.getSize(); | ||
|
|
||
| if (PositionChecker.possibleToPlace(b, c, size, "v") && PositionChecker.possibleToPlace(b, c, size, "h")) { | ||
| if (size == 1) { //scout does not need direction | ||
| placeShip(p, c, ship, "h"); | ||
| break; | ||
| } | ||
| mode = Input.getRightMode(); | ||
| placeShip(p, c, ship, mode); | ||
| break; | ||
|
|
||
| } else if (PositionChecker.possibleToPlace(b, c, size, "v")) { | ||
| placeShip(p, c, ship, "v"); | ||
| break; | ||
| } else if (PositionChecker.possibleToPlace(b, c, size, "h")) { | ||
| placeShip(p, c, ship, "h"); | ||
| break; | ||
| } else { | ||
| System.out.println("Impossible to place it here!"); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public static void placeShip(Player p, Coordinate c, Ship ship, String mode) { | ||
| Board b = p.mainBoard(); | ||
| int size = ship.getSize(); | ||
| int cCol = c.col(); // current col & row | ||
| int cRow = c.row(); | ||
| ArrayList<Coordinate> shipCoords = new ArrayList<>(); | ||
| for (int i = 0; i < size; i++) { | ||
| if (mode.equalsIgnoreCase("h")) { | ||
| shipCoords.add(new Coordinate(cRow, cCol + i)); | ||
| } else if (mode.equalsIgnoreCase("v")) { | ||
| shipCoords.add(new Coordinate(cRow + i, cCol)); | ||
| } | ||
| } | ||
| for (Coordinate coordinate : shipCoords) { | ||
| b.changeCell(coordinate, "H"); | ||
| } | ||
| ship.setCoords(shipCoords); | ||
| p.ships().add(ship); | ||
| BoardPrinter.printMainBoard(p); | ||
| } | ||
| } | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Opt: remove static