Skip to content
This repository was archived by the owner on Oct 25, 2021. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
68 commits
Select commit Hold shift + click to select a range
3b3c1fa
upd
heptaber Jul 9, 2021
3155bb3
upd ReadMe.md
heptaber Jul 9, 2021
066e239
readme fix.
heptaber Jul 12, 2021
4671e18
upd
heptaber Jul 12, 2021
f98562a
upd
heptaber Jul 13, 2021
6e8ac3c
upd
heptaber Jul 13, 2021
8155c0e
upd
heptaber Jul 13, 2021
4677505
homework_2
heptaber Jul 13, 2021
fe59928
PyramidPrinter optimised:
heptaber Jul 19, 2021
ee41b0c
codingbat profile added
heptaber Jul 19, 2021
a71dbfa
_
heptaber Jul 21, 2021
989a7fb
Merge remote-tracking branch 'origin/master' into feature/AndropovVse…
heptaber Jul 21, 2021
790f9b1
hw3 partly done
heptaber Jul 22, 2021
a2c06db
builder.gradle lombok added
heptaber Jul 22, 2021
16a3ac9
changed tests directories; fixed gradle testing.
heptaber Jul 22, 2021
6df5a3c
refactored a little bit
heptaber Jul 23, 2021
7ed4d2b
upd
heptaber Jul 24, 2021
a7acc1f
HW3 added, changed paths to HW2's.
heptaber Jul 24, 2021
34046f2
parametrized test implemented
heptaber Jul 24, 2021
fcd59e3
valid tests added
heptaber Jul 24, 2021
09a26be
structure changed
heptaber Jul 24, 2021
b8d7ab6
101
heptaber Jul 24, 2021
42f59e3
deleted extra import ^_^
heptaber Jul 24, 2021
1ac795a
small fixes
heptaber Jul 24, 2021
50d82f9
_
heptaber Jul 24, 2021
f505079
_
heptaber Jul 24, 2021
c07a1b4
refactored: added parametrized test for not valid data;
heptaber Jul 25, 2021
515ed77
_
heptaber Jul 25, 2021
52a7a0c
refactored tests to parametrized
heptaber Jul 25, 2021
871be72
text file add
heptaber Aug 2, 2021
05f709e
hw 4
heptaber Aug 6, 2021
28f0c99
small fix
heptaber Aug 7, 2021
140faf5
add annotation test
heptaber Aug 8, 2021
75853dd
upd
heptaber Aug 9, 2021
efa3446
upd
heptaber Aug 9, 2021
450f636
global hw4 refactoring;
heptaber Aug 11, 2021
e978957
upd
heptaber Aug 11, 2021
f4c3f5d
mockito dependency added
heptaber Aug 11, 2021
db62369
empty file: processing removed, test removed;
heptaber Aug 16, 2021
1102432
recursion power added;
heptaber Aug 16, 2021
64b5e12
negative power test added;
heptaber Aug 18, 2021
aa04eef
armstrong number realization;
heptaber Aug 18, 2021
9ea4849
prints method name fix;
heptaber Aug 18, 2021
fa218cb
armstrong number computation app removed;
heptaber Aug 21, 2021
a258b85
org.jetbrains:annotations updated to 22.0.0;
heptaber Aug 21, 2021
0f7e68c
custom regex matcher added;
heptaber Aug 21, 2021
f6fe947
PowerOfNumber small fixes;
heptaber Aug 21, 2021
71172c1
hw5 added;
heptaber Aug 21, 2021
cd78f32
hw6 added;
heptaber Aug 29, 2021
ba36011
hw6 tests added;
heptaber Aug 29, 2021
2cbf012
interface renamed;
heptaber Aug 29, 2021
8da9436
hw6, hw6 tests added;
heptaber Aug 29, 2021
9851efb
hw6 upd;
heptaber Aug 30, 2021
d44b458
methods name refactored;
heptaber Aug 31, 2021
c0dd12e
hw7 added
heptaber Sep 12, 2021
b5f2dff
hw7 added
heptaber Sep 12, 2021
7c492c9
hw7 refactored;
heptaber Sep 14, 2021
1d95d1a
hw7 upd;
heptaber Sep 14, 2021
f36af95
little folders reshuffle ;)
heptaber Sep 15, 2021
7813c80
little fix
heptaber Sep 21, 2021
239bd82
course project: SeaBattle add;
heptaber Sep 21, 2021
0fdfad3
changed field size to 10 ;)
heptaber Sep 21, 2021
f1dfc08
upd for more comfortable PVP game
heptaber Sep 21, 2021
d0e0a9f
bug fixed :)
heptaber Sep 21, 2021
207e3e5
homeworks fixes
heptaber Sep 22, 2021
8470929
course project optimizations
heptaber Sep 22, 2021
127a57f
hw3 upd
heptaber Sep 22, 2021
4f262da
hw3 upd
heptaber Sep 22, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions README.md
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`
12 changes: 10 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,16 @@ repositories {
}

dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.0'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.0'
implementation 'org.junit.jupiter:junit-jupiter:5.7.2'
implementation 'org.jetbrains:annotations:22.0.0'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.2'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.2'

// lombok
compileOnly 'org.projectlombok:lombok:1.18.20'
annotationProcessor 'org.projectlombok:lombok:1.18.20'
testCompileOnly 'org.projectlombok:lombok:1.18.20'
testAnnotationProcessor 'org.projectlombok:lombok:1.18.20'
}

test {
Expand Down
Empty file modified gradlew
100644 → 100755
Empty file.
11 changes: 11 additions & 0 deletions src/main/java/course_project/Main.java
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) {
Copy link
Owner

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
image

Copy link
Collaborator Author

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:
Screen Shot 2021-09-21 at 10 28 39 PM

Copy link
Owner

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!

Copy link
Collaborator Author

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!

SeaBattle.getInstance().run();
}

}
112 changes: 112 additions & 0 deletions src/main/java/course_project/gamestuff/field/Battlefield.java
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);
}
}
}

}
47 changes: 47 additions & 0 deletions src/main/java/course_project/gamestuff/field/Cell.java
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 src/main/java/course_project/gamestuff/field/Field.java
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()]);
}

}
29 changes: 29 additions & 0 deletions src/main/java/course_project/gamestuff/player/Player.java
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;
}

}
Loading