-
Notifications
You must be signed in to change notification settings - Fork 2
Chertov_Nikita #18
base: master
Are you sure you want to change the base?
Chertov_Nikita #18
Changes from all commits
08cee92
35505cc
212b306
0417ac5
d4423dc
bc145db
43ca781
a4d8a15
e1022da
7cf7d50
5c5c468
e41d953
6f2619b
1050bb6
f0faba8
7bcda0b
97c5046
bc748fc
b65fd74
11bab80
0e3bb71
fe8e815
8fdd9e8
f5c1ae7
e2771a5
e4e129a
3d2a834
f117f48
b007ce9
beb9349
66684a2
62d5f8b
a168937
561da00
726df11
abdbf4b
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,25 @@ | ||
| # Java Core June 2021 | ||
|
|
||
| ## *Nikolaev Artem* | ||
| ## *Chertov Nikita* | ||
|
|
||
| | 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 | | ||
| | HW1 | [Console printer](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/ChertovNikita/src/main/java/homework_1) | The app that reads input arguments and prints them, until "error" argument | | ||
| | HW2.1 | [Pyramid Printer](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/ChertovNikita/src/main/java/homework_2/pyramid_printer) | The app prints pyramid of entered height with "x" symbols| | ||
| | HW2.2 | [Traffic Light](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/ChertovNikita/src/main/java/homework_2/traffic_light) | The app shows traffic light corresponding to entered time of the day| | ||
| | HW2.3 | [Random Chars Table](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/ChertovNikita/src/main/java/homework_2/random_chars_table) | The app prints table of randomized letters and outputs list of odd/even letters in it| | ||
| | HW2 Tests | [Tests](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/ChertovNikita/src/test/java/homework_2_test/) | Tests for homework 2| | ||
| | HW3 | [Immutable Class](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/ChertovNikita/src/main/java/homework_3) | Cafe class that shows immutable class' key features | | ||
| | HW4.1 | [Custom Annotation](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/ChertovNikita/src/main/java/homework_4/custom_annotation) | Default User Info declares custom annotation and Profile class uses it| | ||
| | HW4.2 | [Custom File Reader](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/ChertovNikita/src/main/java/homework_4/custom_file_reader) | The app reads text from file and outputs it without dots and commas| | ||
| | HW4.3 | [Singleton](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/ChertovNikita/src/main/java/homework_4/singleton) | Theater class as an example of singleton pattern| | ||
| | HW4 Tests | [Tests](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/ChertovNikita/src/test/java/homework_4_test/) | Tests for homework 4| | ||
| | HW5.1 | [Power of number](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/ChertovNikita/src/main/java/homework_5/power_of_number) | The app that raises a number to a power | | ||
| | HW5.2 | [Custom regex matcher](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/ChertovNikita/src/main/java/homework_5/custom_regex_matcher) | The app that checks input text for IP | | ||
| | HW5 Tests | [Tests](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/ChertovNikita/src/test/java/homework_5_test/) | Tests for homework 5| | ||
| | HW6 | [Map problems generator](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/ChertovNikita/src/main/java/homework_6/) | Class with subclasses that create collisions and retrieval problems in hashmap | | ||
| | HW6 Tests | [Tests](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/ChertovNikita/src/test/java/homework_6_test/) | Tests for homework 6| | ||
| | HW7 | [Functional Interface](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/ChertovNikita/src/main/java/homework_7/) | Functional interface with method to transform Kitten and subclasses to Cat and subclasses | | ||
| | CP | [Course project](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/ChertovNikita/src/main/java/course_project/) | Sea battle game | | ||
|
|
||
| [Link to markdown giude](https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet) | ||
| [Link to my CodingBat](https://codingbat.com/done?user=sotheres@gmail.com&tag=6008519258) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| package course_project; | ||
|
|
||
| public class Main { | ||
|
|
||
| public static void main(String[] args) { | ||
| new SeaBattle().playVsComputer(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| package course_project; | ||
|
|
||
| import course_project.components.Coordinate; | ||
| import course_project.components.PlayingField; | ||
| import course_project.util.PlayingFieldPrinter; | ||
| import course_project.util.user_input_reader.UserInputReader; | ||
| import course_project.util.user_input_reader.UserShotInputReader; | ||
| import course_project.util.ship_placer.ComputerShipPlacer; | ||
| import course_project.util.ship_placer.UserShipPlacer; | ||
|
|
||
| import java.util.Random; | ||
| import java.util.Scanner; | ||
|
|
||
| public class SeaBattle { | ||
|
|
||
| final PlayingField playersField = new PlayingField(); | ||
| final PlayingField enemyField = new PlayingField(); | ||
| final PlayingFieldPrinter printer = new PlayingFieldPrinter(); | ||
| private final Scanner scanner = new Scanner(System.in); | ||
| private final Random randomGenerator = new Random(); | ||
| private boolean isHit; | ||
|
|
||
| public void playVsComputer() { | ||
| placePlayersShips(); | ||
| placeComputerShips(); | ||
| makeMoves(); | ||
| defineWinner(); | ||
| closeInput(); | ||
|
Comment on lines
+24
to
+28
Contributor
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. Opt: these methods should have return values. It helps to test them separately. Better not to use void methods.
Contributor
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. Opt: ship placement is difficult to understand (h1 hor on empty field does not work) |
||
| } | ||
|
|
||
| void placePlayersShips() { | ||
| new UserShipPlacer(scanner).placeShips(playersField); | ||
| } | ||
|
Comment on lines
+31
to
+33
Contributor
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. Opt: It does not make sense to put oneliner into a separate method |
||
|
|
||
| void placeComputerShips() { | ||
| new ComputerShipPlacer().placeShips(enemyField); | ||
| } | ||
|
|
||
| void makeMoves() { | ||
| UserInputReader inputReader = new UserShotInputReader(scanner); | ||
| while (!playersField.getPlayersShips().isEmpty() && !enemyField.getPlayersShips().isEmpty()) { | ||
| playersMove(inputReader); | ||
| computersMove(); | ||
| } | ||
| } | ||
|
|
||
| private void playersMove(UserInputReader inputReader) { | ||
| do { | ||
| System.out.println("Enemy field:"); | ||
| printer.printShotCells(enemyField); | ||
| Coordinate shotCoordinate = inputReader.getPointFromInput(); | ||
| playersShot(shotCoordinate); | ||
| delayPrint(); | ||
| } while (isHit && !enemyField.getPlayersShips().isEmpty()); | ||
| } | ||
|
|
||
| private void playersShot(Coordinate point) { | ||
| isHit = enemyField.placeShot(point); | ||
| } | ||
|
|
||
| private void delayPrint() { | ||
| try { | ||
| Thread.sleep(700); | ||
| } catch (InterruptedException e) { | ||
| Thread.currentThread().interrupt(); | ||
| } | ||
| } | ||
|
|
||
| private void computersMove() { | ||
| if (!enemyField.getPlayersShips().isEmpty()) { | ||
| do { | ||
| enemyShot(); | ||
| delayPrint(); | ||
| System.out.println("Your field:"); | ||
| printer.printField(playersField); | ||
| } while (isHit && !playersField.getPlayersShips().isEmpty()); | ||
| } | ||
| } | ||
|
|
||
| private void enemyShot() { | ||
| Coordinate cell = createCellToShoot(); | ||
| System.out.println("Computer shoot at : " + cell); | ||
| isHit = playersField.placeShot(cell); | ||
| } | ||
|
|
||
| private Coordinate createCellToShoot() { | ||
| Coordinate cellToShoot; | ||
| do { | ||
| int x = randomGenerator.nextInt(10); | ||
| int y = randomGenerator.nextInt(10); | ||
| cellToShoot = new Coordinate(x, y); | ||
| } while (playersField.isCellShot(cellToShoot)); | ||
|
|
||
| return cellToShoot; | ||
| } | ||
|
|
||
| private void closeInput() { | ||
| scanner.close(); | ||
| } | ||
|
|
||
| void defineWinner() { | ||
| if (enemyField.getPlayersShips().isEmpty()) { | ||
| System.out.println("Player won!"); | ||
| } else { | ||
| System.out.println("Computer won!"); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,45 @@ | ||||||||||
| package course_project.components; | ||||||||||
|
|
||||||||||
| public final class Coordinate { | ||||||||||
|
|
||||||||||
| private final int x; | ||||||||||
| private final int y; | ||||||||||
|
|
||||||||||
| public Coordinate(int x, int y) { | ||||||||||
| this.x = x; | ||||||||||
| this.y = y; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| @Override | ||||||||||
| public boolean equals(Object obj) { | ||||||||||
| if (this == obj) { | ||||||||||
| return true; | ||||||||||
| } | ||||||||||
| if (!(obj instanceof Coordinate)) { | ||||||||||
| return false; | ||||||||||
| } | ||||||||||
| Coordinate other = (Coordinate) obj; | ||||||||||
|
|
||||||||||
| return (x == other.x) && (y == other.y); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| @Override | ||||||||||
| public int hashCode() { | ||||||||||
| int result = Integer.hashCode(x); | ||||||||||
| result = 31 * result + Integer.hashCode(y); | ||||||||||
| return result; | ||||||||||
|
Comment on lines
+28
to
+30
Contributor
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.
Suggested change
|
||||||||||
| } | ||||||||||
|
|
||||||||||
| @Override | ||||||||||
| public String toString() { | ||||||||||
| return "" + (char) (y + 65) + (x + 1); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| public int getX() { | ||||||||||
| return x; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| public int getY() { | ||||||||||
| return y; | ||||||||||
| } | ||||||||||
| } | ||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| package course_project.components; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| public class PlayingField { | ||
|
|
||
| private final int[][] fleet = new int[10][10]; | ||
| private final boolean[][] cellsShot = new boolean[10][10]; | ||
| private final Map<Coordinate, Ship> coordinateToShip = new HashMap<>(32); // size for 10 ships and no rehashing | ||
| private final List<Ship> playersShips = new ArrayList<>(); | ||
|
|
||
| public Map<Coordinate, Ship> getCoordinateMapping() { | ||
| return coordinateToShip; | ||
| } | ||
|
|
||
| public List<Ship> getPlayersShips() { | ||
| return playersShips; | ||
| } | ||
|
|
||
| public int[][] getFleet() { | ||
| return fleet; | ||
| } | ||
|
|
||
| public boolean[][] getCellsShot() { | ||
| return cellsShot; | ||
| } | ||
|
|
||
| public boolean isCellShot(Coordinate cell) { | ||
| return cellsShot[cell.getX()][cell.getY()]; | ||
| } | ||
|
|
||
| public void placeShip(Ship ship) { | ||
| int size = ship.getSize(); | ||
| for (Coordinate cell : ship.getCoordinates()) { | ||
| fleet[cell.getX()][cell.getY()] = size; | ||
| coordinateToShip.put(cell, ship); | ||
| } | ||
| playersShips.add(ship); | ||
| } | ||
|
|
||
| public boolean placeShot(Coordinate cell) { | ||
| if (!cellsShot[cell.getX()][cell.getY()]) { | ||
| cellsShot[cell.getX()][cell.getY()] = true; | ||
| if (coordinateToShip.containsKey(cell)) { | ||
| System.out.println("Hit"); | ||
| shootAtShip(coordinateToShip.get(cell)); | ||
| return true; | ||
| } | ||
| } | ||
| System.out.println("Miss"); | ||
| return false; | ||
| } | ||
|
|
||
| private void shootAtShip(Ship hitShip) { | ||
| hitShip.gotShot(); | ||
| if (hitShip.isSunk()) { | ||
| sinkShip(hitShip); | ||
| } | ||
| } | ||
|
|
||
| private void sinkShip(Ship hitShip) { | ||
| playersShips.remove(hitShip); | ||
| markAdjacentCells(hitShip); | ||
| System.out.println(hitShip.getSize() + "-deck ship is sunk"); | ||
| } | ||
|
|
||
| private void markAdjacentCells(Ship hitShip) { | ||
| for (Coordinate c : hitShip.getCoordinates()) { | ||
| if (c.getX() > 0) { | ||
| cellsShot[c.getX() - 1][c.getY()] = true; | ||
| } | ||
| if (c.getX() < 9) { | ||
| cellsShot[c.getX() + 1][c.getY()] = true; | ||
| } | ||
| if (c.getY() > 0) { | ||
| cellsShot[c.getX()][c.getY() - 1] = true; | ||
| } | ||
| if (c.getY() < 9) { | ||
| cellsShot[c.getX()][c.getY() + 1] = true; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| package course_project.components; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| public class Ship { | ||
|
|
||
| private final int size; | ||
| private final List<Coordinate> coordinates; | ||
| private int health; | ||
| private boolean sunk; | ||
|
|
||
| public Ship(int size, List<Coordinate> coordinates) { | ||
| health = size; | ||
| this.size = size; | ||
| this.coordinates = new ArrayList<>(coordinates); | ||
| } | ||
|
|
||
| public List<Coordinate> getCoordinates() { | ||
| return new ArrayList<>(coordinates); | ||
| } | ||
|
|
||
| public void gotShot() { | ||
| health--; | ||
| if (health == 0) { | ||
| sunk = true; | ||
| } | ||
| } | ||
|
|
||
| public int getSize() { | ||
| return size; | ||
| } | ||
|
|
||
| public boolean isSunk() { | ||
| return sunk; | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.
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.
Good use of abstractions, thorough knowledge of Java Core aspects and libraries. Well done!
To make better: Quite difficult to play, during ship placement the field does not show my placed ships. Also, you could Introduce random ship placement. And void methods are not thread-safe and easy to test, better use return values