This repository was archived by the owner on Oct 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Andropov_Vsevolod #29
Open
heptaber
wants to merge
68
commits into
master
Choose a base branch
from
feature/AndropovVsevolod
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
68 commits
Select commit
Hold shift + click to select a range
3b3c1fa
upd
heptaber 3155bb3
upd ReadMe.md
heptaber 066e239
readme fix.
heptaber 4671e18
upd
heptaber f98562a
upd
heptaber 6e8ac3c
upd
heptaber 8155c0e
upd
heptaber 4677505
homework_2
heptaber fe59928
PyramidPrinter optimised:
heptaber ee41b0c
codingbat profile added
heptaber a71dbfa
_
heptaber 989a7fb
Merge remote-tracking branch 'origin/master' into feature/AndropovVse…
heptaber 790f9b1
hw3 partly done
heptaber a2c06db
builder.gradle lombok added
heptaber 16a3ac9
changed tests directories; fixed gradle testing.
heptaber 6df5a3c
refactored a little bit
heptaber 7ed4d2b
upd
heptaber a7acc1f
HW3 added, changed paths to HW2's.
heptaber 34046f2
parametrized test implemented
heptaber fcd59e3
valid tests added
heptaber 09a26be
structure changed
heptaber b8d7ab6
101
heptaber 42f59e3
deleted extra import ^_^
heptaber 1ac795a
small fixes
heptaber 50d82f9
_
heptaber f505079
_
heptaber c07a1b4
refactored: added parametrized test for not valid data;
heptaber 515ed77
_
heptaber 52a7a0c
refactored tests to parametrized
heptaber 871be72
text file add
heptaber 05f709e
hw 4
heptaber 28f0c99
small fix
heptaber 140faf5
add annotation test
heptaber 75853dd
upd
heptaber efa3446
upd
heptaber 450f636
global hw4 refactoring;
heptaber e978957
upd
heptaber f4c3f5d
mockito dependency added
heptaber db62369
empty file: processing removed, test removed;
heptaber 1102432
recursion power added;
heptaber 64b5e12
negative power test added;
heptaber aa04eef
armstrong number realization;
heptaber 9ea4849
prints method name fix;
heptaber fa218cb
armstrong number computation app removed;
heptaber a258b85
org.jetbrains:annotations updated to 22.0.0;
heptaber 0f7e68c
custom regex matcher added;
heptaber f6fe947
PowerOfNumber small fixes;
heptaber 71172c1
hw5 added;
heptaber cd78f32
hw6 added;
heptaber ba36011
hw6 tests added;
heptaber 2cbf012
interface renamed;
heptaber 8da9436
hw6, hw6 tests added;
heptaber 9851efb
hw6 upd;
heptaber d44b458
methods name refactored;
heptaber c0dd12e
hw7 added
heptaber b5f2dff
hw7 added
heptaber 7c492c9
hw7 refactored;
heptaber 1d95d1a
hw7 upd;
heptaber f36af95
little folders reshuffle ;)
heptaber 7813c80
little fix
heptaber 239bd82
course project: SeaBattle add;
heptaber 0fdfad3
changed field size to 10 ;)
heptaber f1dfc08
upd for more comfortable PVP game
heptaber d0e0a9f
bug fixed :)
heptaber 207e3e5
homeworks fixes
heptaber 8470929
course project optimizations
heptaber 127a57f
hw3 upd
heptaber 4f262da
hw3 upd
heptaber File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,25 @@ | ||
| # Java Core June 2021 | ||
|
|
||
| ## *Nikolaev Artem* | ||
| ### Andropov Vsevolod | ||
|
|
||
| | 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](./src/main/java/homework_1/Main.java) <br/> [Tests](./src/test/java/homework_1/MainTest.java) | The app that reads input arguments and prints them, until "error" argument | | ||
| HW2.1 | [Traffic Light](./src/main/java/homework_2/traffic_light/) <br/> [Tests](./src/test/java/homework_2/traffic_light/TrafficLightTest.java) | The app that switching colors on different number value (input from keyboard) | | ||
| HW2.2 | [Pyramid Printer](./src/main/java/homework_2/pyramid_printer/) <br/> [Tests](./src/test/java/homework_2/pyramid_printer/PyramidPrinterTest.java) | Printing 'x' Pyramid with the input parameter as Pyramid height | | ||
| HW2.3 | [Random Chars Table](./src/main/java/homework_2/random_chars_table/) <br/> [Tests](./src/test/java/homework_2/random_chars_table/RandomCharsTableTest.java) | The app that creates 2-dimensional array/matrix made of random Character values (from A=65 to Z=90), dimensions set by input from keyboard. Then generating resulting string with even/odd parameter depends on input | | ||
| HW3 | [Immutable Class](./src/main/java/homework_3/ImmutableClass.java) | Create Immutable class: several fields, including reference type; several constructors; method returning a new object. | | ||
| HW4.1 | [Custom File Reader](./src/main/java/homework_4/custom_file_reader/) <br/> [Tests](./src/test/java/homework_4/custom_file_reader/CustomFileReaderTest.java) | App reading data from file and prints to console. Text file path: main/resources/custom_file_reader/. Reading file in 4 different ways (one of them using NIO) executable with methods: run1(), run2(), run3(), run4() | | ||
| HW4.2 | [Singleton](./src/main/java/homework_4/singleton/) <br/> [Tests](./src/test/java/homework_4/singleton/SingletonMenuTest.java) | Singleton App: private constructor and public method getInstance() | | ||
| HW4.3 | [Custom Annotation](./src/main/java/homework_4/custom_annotation/) <br/> [Tests](./src/test/java/homework_4/custom_annotation/ResourcesPathTest.java) | Custom annotation class. | | ||
| HW5.1 | [Power Of Number](./src/main/java/homework_5/power_of_number/) <br/> [Tests](./src/test/java/homework_5/power_of_number/PowerOfNumberTest.java) | Power of number recursion using App. Reads 2 non-negative integer numbers from console and prints out the result of exponentiation. Only for positive integer numbers. On any problem output message will be gotten: "Only 2 non-negative integers are allowed" | | ||
| HW5.2 | [Custom Regex Matcher](./src/main/java/homework_5/custom_regex_matcher/) <br/> [Tests](./src/test/java/homework_5/custom_regex_matcher/CustomRegexMatcherTest.java) | App reads input from keyboard and print true or false for the input matching to the hardcoded regex format. | | ||
| HW6 | [MapProblemsGenerator](./src/main/java/homework_6/map_problems_collision_generator/) | App which has: <br/> 1) Class creating 100% HashMap collisions <br/> 2) Mutable class with overridden HashCode/Equals. When put MutableClass object into HashMap as key - can't retrieve it (equals works correctly) | | ||
| HW7 | [Kitten to Cat](./src/main/java/homework_7/kitten_to_cat/) <br/> [Tests](./src/test/java/homework_7/kitten_to_cat/KittenToCatTest.java) | App using Functional Interface KittenToCatFunction with abstract method grow(). Realization in Main class through lambda function | | ||
| Course project | [Sea Battle](./src/main/java/course_project/) | Sea Battle console game | | ||
|
|
||
| [Link to markdown giude](https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet) | ||
| <br> | ||
|
|
||
| ___ | ||
|
|
||
| [codingbat profile](https://codingbat.com/done?user=devngrow@gmail.com&tag=1205090974) `done` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| package course_project; | ||
|
|
||
| import course_project.services.SeaBattle; | ||
|
|
||
| public class Main { | ||
|
|
||
| public static void main(String[] args) { | ||
| SeaBattle.getInstance().run(); | ||
| } | ||
|
|
||
| } | ||
112 changes: 112 additions & 0 deletions
112
src/main/java/course_project/gamestuff/field/Battlefield.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| package course_project.gamestuff.field; | ||
|
|
||
| import course_project.gamestuff.player.Player; | ||
| import course_project.gamestuff.ships.*; | ||
|
|
||
| import java.util.Collection; | ||
| import java.util.LinkedList; | ||
|
|
||
| import static course_project.services.conversation.DialogueMenu.*; | ||
| import static course_project.utils.AroundDestroyedShipMarker.*; | ||
| import static course_project.utils.RandomCellPositionGenerator.generateRandomForCell; | ||
| import static course_project.utils.RandomShipDirectionGenerator.generateRandomDirection; | ||
| import static course_project.utils.ShipOnBattlefieldPlacer.*; | ||
|
|
||
| /** | ||
| * Battlefield is Field for the Player(owner) of this object. | ||
| */ | ||
| public class Battlefield extends Field { | ||
|
|
||
| private final Collection<Ship> availableShips; | ||
|
|
||
| public Battlefield() { | ||
| super(); | ||
| this.availableShips = new LinkedList<>(); | ||
| initAvailableShips(); | ||
| } | ||
|
|
||
| private void initAvailableShips() { | ||
| availableShips.add(new ExtraLargeShip()); | ||
| availableShips.add(new LargeShip()); | ||
| availableShips.add(new LargeShip()); | ||
| availableShips.add(new MiddleShip()); | ||
| availableShips.add(new MiddleShip()); | ||
| availableShips.add(new MiddleShip()); | ||
| availableShips.add(new SmallShip()); | ||
| availableShips.add(new SmallShip()); | ||
| availableShips.add(new SmallShip()); | ||
| availableShips.add(new SmallShip()); | ||
| } | ||
|
|
||
| public Collection<Ship> getAvailableShips() { | ||
| return this.availableShips; | ||
| } | ||
|
|
||
| public boolean addShipOnBattlefield(Ship ship, Cell toCell, boolean isHorizontal) { | ||
| if (isEnoughSpaceToPlaceShip(toCell, ship.getSize(), isHorizontal)) { | ||
| return isHorizontal ? | ||
| addRowOfCells(this, toCell, ship) : | ||
| addColumnOfCells(this, toCell, ship); | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| // onDestroy | ||
| public void availableShipsDecrease(Ship ship) { | ||
| this.availableShips.remove(ship); | ||
| } | ||
|
|
||
| public boolean hasAvailableShips() { | ||
| return countOfAvailableShips() > 0; | ||
| } | ||
|
|
||
| public int countOfAvailableShips() { | ||
| return this.availableShips.size(); | ||
| } | ||
|
|
||
| public static void addShipsManually(Player currentPlayer) { | ||
| printPlayerShipPlacingHelp(); | ||
| currentPlayer.getField().getAvailableShips().forEach(ship -> { | ||
| currentPlayer.getField().printField(); | ||
| Cell toPos; | ||
| boolean isHorizontal = true; | ||
| do { | ||
| printSetPositionMessage(ship.getName(), ship.getSize()); | ||
| String[] pos = getShipPlacingInput().split(" "); | ||
| int x = Integer.parseInt(pos[0].substring(1)) - 1; | ||
| int y = Integer.parseInt(String.valueOf(pos[0].charAt(0) - LOWERCASE_A)); | ||
| toPos = currentPlayer.getField().getCellByPosition(x, y); | ||
| if (pos.length == 2) { | ||
| isHorizontal = "h".equals(pos[1]); | ||
| } | ||
| } while (!currentPlayer.getField().addShipOnBattlefield(ship, toPos, isHorizontal)); | ||
| } | ||
| ); | ||
| } | ||
|
|
||
| public static void addShipsAuto(Player currentPlayer) { | ||
| currentPlayer.getField().getAvailableShips().forEach(ship -> { | ||
| Cell toPos; | ||
| boolean isHorizontal; | ||
| do { | ||
| toPos = currentPlayer.getField().getCellByPosition(generateRandomForCell(), | ||
| generateRandomForCell()); | ||
| isHorizontal = generateRandomDirection(); | ||
| } while (!currentPlayer.getField().addShipOnBattlefield(ship, toPos, isHorizontal)); | ||
| } | ||
| ); | ||
| } | ||
|
|
||
| public void setAroundShipCellsAsHit(Player currentPlayer, Ship ship) { | ||
| if (ship.getSize() == 1) { | ||
| fillAroundSmallShip(currentPlayer, ship); | ||
| } else { | ||
| if (ship.isHorizontal()) { | ||
| fillAroundHorizontalShip(currentPlayer, ship); | ||
| } else { | ||
| fillAroundVerticalShip(currentPlayer, ship); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| package course_project.gamestuff.field; | ||
|
|
||
| import course_project.gamestuff.ships.Ship; | ||
|
|
||
| public class Cell { | ||
|
|
||
| private final int x; | ||
| private final int y; | ||
| private Ship ref; | ||
| private boolean hit; // stores state of cell: if motion to this cell was made | ||
|
|
||
| public Cell(int x, int y) { | ||
| this.x = x; | ||
| this.y = y; | ||
| this.ref = null; | ||
| this.hit = false; | ||
| } | ||
|
|
||
| public boolean isHit() { | ||
| return this.hit; | ||
| } | ||
|
|
||
| public void setHit(boolean hit) { | ||
| this.hit = hit; | ||
| } | ||
|
|
||
| public Ship isShipPlacedAtCell() { | ||
| return this.ref == null ? null : ref; | ||
| } | ||
|
|
||
| public void placeShipOnCell(Ship ship) { | ||
| this.ref = ship; | ||
| } | ||
|
|
||
| public int getX() { | ||
| return this.x; | ||
| } | ||
|
|
||
| public int getY() { | ||
| return this.y; | ||
| } | ||
|
|
||
| public Ship getRef() { | ||
| return this.ref; | ||
| } | ||
|
|
||
| } |
140 changes: 140 additions & 0 deletions
140
src/main/java/course_project/gamestuff/field/Field.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,140 @@ | ||
| package course_project.gamestuff.field; | ||
|
|
||
| import course_project.gamestuff.player.Player; | ||
|
|
||
| import static homework_2.Colors.*; | ||
|
|
||
| public abstract class Field { | ||
|
|
||
| public static final int FIELD_SIZE = 10; | ||
| public static final int LOWERCASE_A = 'a'; | ||
| private static final String WATER = BLUE_BG + " " + ANSI_RESET; | ||
| private static final String DESTROYED = ANSI_RED + "†" + ANSI_RESET; | ||
| private static final String HIT = ANSI_RED + "X" + ANSI_RESET; | ||
| private static final String MISS = ANSI_YELLOW + "O" + ANSI_RESET; | ||
| private static final String SHIP = ANSI_GREEN + "¤" + ANSI_RESET; | ||
| private static final char FIRST_SYMBOL_TOP_LINE = 'A'; | ||
| private static final String TOP_LINE; | ||
| private static final String CELL_FORMAT = "%s "; | ||
| private static final String ROW_NUMBER_FORMAT = "[%-2d]"; | ||
|
|
||
| private final Cell[][] cells; | ||
|
|
||
| protected Field() { | ||
| this.cells = new Cell[FIELD_SIZE][FIELD_SIZE]; | ||
| fillCellsWithIndexes(); | ||
| } | ||
|
|
||
| static { | ||
| StringBuilder stringBuilder = new StringBuilder(); | ||
| for (int i = FIRST_SYMBOL_TOP_LINE; i < FIRST_SYMBOL_TOP_LINE + FIELD_SIZE; i++) { | ||
| stringBuilder | ||
| .append((char) i) | ||
| .append(" "); | ||
| } | ||
| TOP_LINE = stringBuilder.toString().trim(); | ||
| } | ||
|
|
||
| private void fillCellsWithIndexes() { | ||
| for (int i = 0; i < FIELD_SIZE; i++) { | ||
| for (int j = 0; j < FIELD_SIZE; j++) { | ||
| cells[i][j] = new Cell(i, j); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public void printField() { | ||
| StringBuilder oneField = new StringBuilder(); | ||
| oneField | ||
| .append("\t") | ||
| .append(TOP_LINE) | ||
| .append("\n"); | ||
| for (int i = 0; i < FIELD_SIZE; i++) { | ||
| oneField.append(String.format(ROW_NUMBER_FORMAT, i + 1)); | ||
| for (int j = 0; j < FIELD_SIZE; j++) { | ||
| Cell currentCell = getCellByPosition(i, j); | ||
| String toPrint = WATER; | ||
| if (!isCellEmpty(currentCell)) { | ||
| toPrint = SHIP; | ||
| } | ||
| oneField.append(String.format(CELL_FORMAT, toPrint)); | ||
| } | ||
| oneField.append("\n"); | ||
| } | ||
| System.out.println(oneField); | ||
| } | ||
|
|
||
| public void printBothFields(Player currentPlayer, Player enemy) { | ||
| StringBuilder bothFields = new StringBuilder(); | ||
| bothFields | ||
| .append("\t") | ||
| .append(TOP_LINE) | ||
| .append("\t\t\t\t") | ||
| .append(TOP_LINE) | ||
| .append("\n"); | ||
| for (int i = 0; i < FIELD_SIZE; i++) { | ||
| bothFields.append(String.format(ROW_NUMBER_FORMAT, i + 1)); | ||
| for (int j = 0; j < FIELD_SIZE; j++) { | ||
| Cell currentPlayerCell = currentPlayer.getField().getCellByPosition(i, j); | ||
| if (currentPlayerCell.isHit()) { | ||
| bothFields | ||
| .append(getCellStateAsString(currentPlayer, currentPlayerCell)); | ||
| } else { | ||
| if (currentPlayer.getField().isCellEmpty(currentPlayerCell)) { | ||
| bothFields | ||
| .append(String.format(CELL_FORMAT, WATER)); | ||
| } else { | ||
| bothFields | ||
| .append(String.format(CELL_FORMAT, SHIP)); | ||
| } | ||
| } | ||
| } | ||
| bothFields | ||
| .append("\t\t") | ||
| .append(String.format(ROW_NUMBER_FORMAT, i + 1)); | ||
| for (int j = 0; j < FIELD_SIZE; j++) { | ||
| Cell currentEnemyCell = enemy.getField().getCellByPosition(i, j); | ||
| if (currentEnemyCell.isHit()) { | ||
| bothFields | ||
| .append(getCellStateAsString(enemy, currentEnemyCell)); | ||
| } else { | ||
| bothFields | ||
| .append(String.format(CELL_FORMAT, WATER)); | ||
| } | ||
| } | ||
| bothFields.append("\n"); | ||
| } | ||
| System.out.println(bothFields); | ||
| } | ||
|
|
||
| private String getCellStateAsString(Player currentPlayer, Cell playersCell) { | ||
| if (currentPlayer.getField().getHit(playersCell)) { | ||
| if (playersCell.getRef().isShipDestroyed()) { | ||
| return String.format(CELL_FORMAT, DESTROYED); | ||
| } else { | ||
| return String.format(CELL_FORMAT, HIT); | ||
| } | ||
| } | ||
| return String.format(CELL_FORMAT, MISS); | ||
| } | ||
|
|
||
| public Cell getCellByPosition(int x, int y) { | ||
| for (Cell[] cellRow : this.cells) { | ||
| for (Cell cellCol : cellRow) { | ||
| if (cellCol.getX() == x && cellCol.getY() == y) { | ||
| return cellCol; | ||
| } | ||
| } | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| public boolean isCellEmpty(Cell currentCell) { | ||
| return currentCell.isShipPlacedAtCell() == null; | ||
| } | ||
|
|
||
| public boolean getHit(Cell toCell) { | ||
| return !isCellEmpty(cells[toCell.getX()][toCell.getY()]); | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| package course_project.gamestuff.player; | ||
|
|
||
| import course_project.gamestuff.field.Battlefield; | ||
|
|
||
| public class Player { | ||
|
|
||
| private final boolean computer; | ||
| private final String name; | ||
| private final Battlefield battlefield; | ||
|
|
||
| public Player(String name, boolean isComputer) { | ||
| this.computer = isComputer; | ||
| this.name = name; | ||
| this.battlefield = new Battlefield(); | ||
| } | ||
|
|
||
| public boolean isComputer() { | ||
| return this.computer; | ||
| } | ||
|
|
||
| public String getName() { | ||
| return this.name; | ||
| } | ||
|
|
||
| public Battlefield getField() { | ||
| return this.battlefield; | ||
| } | ||
|
|
||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
It seems indentation is shifted

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.
can you describe this case in detail?

my view:
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.
Well done! The game interface looks pretty nice, even though it lacks row delimiters. Playing is comfortable, it shows pretty informative messages. Also, a lot of features are introduced, as well as exit code and description. You show good knowledge of Java Core and Java libraries, abstractions are mostly well-picked, architecture is good, but it can be improved. Also, you have lots of static, and it's not recommended.
Approved!
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.
I understood you. Thanks for the review!