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
72 commits
Select commit Hold shift + click to select a range
f44f031
Change "Hello World" to "Hello Russia"
blowushka Jul 5, 2021
c82e811
Change "Hello, Russia" to "Hello, Epam"
blowushka Jul 5, 2021
67e53ab
.gitignore update
blowushka Jul 5, 2021
58734b1
Add new folder called homework_1 and complete homework
blowushka Jul 5, 2021
0fcb854
Edit args output a bit
blowushka Jul 5, 2021
e4d75d5
Change russian words to english
blowushka Jul 6, 2021
56a06cf
Update .gitignore
blowushka Jul 6, 2021
f5f49ac
Add if(args.length == 0) validation
blowushka Jul 7, 2021
8201f13
Update .gitignore
blowushka Jul 7, 2021
3b14785
Delete unnecessary files
blowushka Jul 7, 2021
c6cc772
Merge remote-tracking branch 'origin/master' into feature/IliaProkofev
blowushka Jul 7, 2021
7e09d62
Add README.md and update .gitignore
blowushka Jul 7, 2021
6898d5c
Update README.md
blowushka Jul 7, 2021
5d50400
Delete build.gradle
blowushka Jul 7, 2021
5477b92
Delete Main.java from wrong folder
blowushka Jul 7, 2021
6af1c99
Ignore .gitignore for push
blowushka Jul 9, 2021
ee5f09c
Delete .gitignore from remote repo
blowushka Jul 9, 2021
3f56714
Restore .gitignore to remote repo
blowushka Jul 9, 2021
795a130
Merge remote-tracking branch 'origin/feature/IliaProkofev' into featu…
blowushka Jul 9, 2021
13ff790
Restore .gitignore to remote repo
blowushka Jul 9, 2021
c998ab1
Fix indentation and RED-text output
blowushka Jul 9, 2021
265a86f
Restore deleted files
blowushka Jul 9, 2021
121d8e2
Fix indentation
blowushka Jul 9, 2021
0421d59
Refactoring
blowushka Jul 11, 2021
d1f46cb
Refactoring
blowushka Jul 12, 2021
2fc8a83
Final refactoring
blowushka Jul 12, 2021
03eb76f
Complete Homework_2
blowushka Jul 14, 2021
e29f38b
Edit README.md
blowushka Jul 14, 2021
73b485c
Delete unnecessary import
blowushka Jul 14, 2021
0d89e98
Change output for PyramidPrinter
blowushka Jul 16, 2021
7d43fbc
Change program exit style
blowushka Jul 17, 2021
94cb687
Fix minor problems, close Scanner in RandomCharsTable.java, fix Engli…
blowushka Jul 18, 2021
a2d4b50
Changed the entry point to applications, major refactoring
blowushka Jul 20, 2021
b6b380a
Merge branch 'master' into feature/IliaProkofev
blowushka Jul 22, 2021
a6ebdae
Global refactoring:
blowushka Jul 22, 2021
cc1ee4a
Complete all tests for homework, create Immutable Class, minor improv…
blowushka Jul 23, 2021
86d5f3c
Update Readme.md
blowushka Jul 24, 2021
26f642c
Edit ImmutableClass and add test
blowushka Jul 24, 2021
5ccef57
--Changed the class names as specified in the condition
blowushka Jul 25, 2021
a2b062f
Fixed typo in README.md
blowushka Jul 25, 2021
3750150
Change the type of abstraction
blowushka Jul 26, 2021
3575493
Changed the names of the tests, added some blank lines, deleted tests…
blowushka Aug 8, 2021
e81d405
Refactoring + README edit
blowushka Aug 8, 2021
61b1bfa
README edit
blowushka Aug 8, 2021
7da109c
Change .txt input
blowushka Aug 12, 2021
f850b76
Refactoring
blowushka Aug 13, 2021
795027a
Add CustomAnnotation, edit README.md
blowushka Aug 13, 2021
7f805ed
Added test for CustomAnnotation
blowushka Aug 16, 2021
6e7d1e5
Edit README.md
blowushka Aug 16, 2021
f0833d6
Add couple of tests for CustomAnnotation
blowushka Aug 16, 2021
d2a9b33
Complete homework 5
blowushka Aug 16, 2021
631db28
HW5 refactoring and add tests
blowushka Aug 16, 2021
69e5170
Refactoring
blowushka Aug 16, 2021
e7a84a0
Edit README.md
blowushka Aug 16, 2021
80ad1a2
Add one case for unit-test
blowushka Aug 18, 2021
1d4b053
Major refactoring, add tests, complete CustomRegexMatcher
blowushka Aug 23, 2021
6a354d9
Edit README.md
blowushka Aug 23, 2021
f15163d
Complete Homework 6
blowushka Aug 30, 2021
bc43c5b
Edit README.md
blowushka Aug 30, 2021
1eac62d
Major refactoring
blowushka Sep 2, 2021
86ff890
Edited method names
blowushka Sep 3, 2021
759f5ea
Complete homework_7
blowushka Sep 20, 2021
fb8ef30
Edit README.md
blowushka Sep 20, 2021
f15468d
Fix wrong naming
blowushka Sep 20, 2021
01fb814
Edit README.md
blowushka Sep 20, 2021
641ed85
Course project complete
blowushka Sep 21, 2021
655d904
Edit README.md
blowushka Sep 21, 2021
3d5a14d
Edit README.md
blowushka Sep 21, 2021
0b4857b
Edit MapProblemsMutableGenerator
blowushka Sep 21, 2021
0d77640
Minor refactoring
blowushka Sep 22, 2021
775070c
Delete seriesUp method
blowushka Sep 22, 2021
77a16b2
FIX HW_3
blowushka 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: 17 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
# Java Core June 2021

## *Nikolaev Artem*
## *Prokofev Ilia*

| 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 |
| Index | Solution | Short description|
| :---: | --- | --- |
|**Course Project**|[Sea Battle](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/IliaProkofev/src/main/java/course_project)|Console version of the classic Sea Battle game with graphical representation|
| HW1 | [Console printer](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/IliaProkofev/src/main/java/homework_1) <br> [Test](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/IliaProkofev/src/test/java/homework_1/char_count) | The app that reads input arguments and prints them, until "error" argument |
|-----| [Traffic Light](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/IliaProkofev/src/main/java/homework_2/traffic_light) <br> [Test](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/IliaProkofev/src/test/java/homework_2/traffic_light) | The app that reads console and calculate current traffic light's color |
| HW2 | [Pyramid Printer](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/IliaProkofev/src/main/java/homework_2/pyramid_printer) <br> [Test](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/IliaProkofev/src/test/java/homework_2/pyramid_printer) | The app that reads console and prints a pyramid based on this |
|-----| [Random Chars Table](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/IliaProkofev/src/main/java/homework_2/random_chars_table) <br> [Test](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/IliaProkofev/src/test/java/homework_2/random_chars_table) | The app that reads console for length, width and strategy and prints table based on this|
| HW3 | [Immutable Class](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/IliaProkofev/src/main/java/homework_3) | The class that give opportunity for create Immutable object|
|-----| [Custom Annotation](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/IliaProkofev/src/main/java/homework_4/custom_annotation) <br> [Test](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/IliaProkofev/src/test/java/homework_4/custom_annotation) | A form of syntactic metadata that can be added to Java source code|
| HW4 | [Custom File Reader](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/IliaProkofev/src/main/java/homework_4/custom_file_reader) <br> [Test](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/IliaProkofev/src/test/java/homework_4/custom_file_reader) | The app that provides 3 ways to read and edit a file|
|-----| [Singleton](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/IliaProkofev/src/main/java/homework_4/singleton) <br> [Test](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/IliaProkofev/src/test/java/homework_4/singleton) | Creational design pattern, which ensures that only one object of its kind exists |
| HW5 | [PowerOfNumber](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/IliaProkofev/src/main/java/homework_5/power_of_number) <br> [Test](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/IliaProkofev/src/test/java/homework_5/power_of_number) | The app that raises a number to a power |
|-----| [CustomRegexMatcher](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/IliaProkofev/src/main/java/homework_5/custom_regex_matcher) <br> [Test](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/IliaProkofev/src/test/java/homework_5/custom_regex_matcher) | The app that check input string by hardcoded regex and return boolean answer |
| HW6 | [MapProblemsGenerator](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/IliaProkofev/src/main/java/homework_6/map_problems_generator)|Two classes that allow you to check the work of the collision and create a situation where it is impossible to get the object just put|
| HW7 | [KittenToCatFunction](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/IliaProkofev/src/main/java/homework_7)|Functional interface that allows you to transform a Kitten into a Cat|

[Link to markdown giude](https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet)
[CodingBat link](https://codingbat.com/done?user=slowly@live.ru&tag=8165639202)
6 changes: 6 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ repositories {
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.0'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.0'

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
8 changes: 8 additions & 0 deletions src/main/java/course_project/Main.java
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) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good job! Nice interface, a lot of features (such as automatic ship placement, different game modes), comfortable to play, informative messages. I like the architecture, the code is readable and understandable. You show good knowledge of Java Core (missing Java 8 and some concepts of OOP).

To make it better: add automatic miss marks placement around a destroyed ship. Also, your code has a lot of static. It seems logical that if you don't care about having multiple objects of a class in your app (like Ship), then mark it static, but actually in this case it should be a singleton, and still dynamic. Static is rarely used in Java, mostly for constants and utils. And mostly your methods are void, which is also rare.

Approved!

SeaBattle.initializeGame();
}
}
12 changes: 12 additions & 0 deletions src/main/java/course_project/SeaBattle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package course_project;

import course_project.dialogue.Menu;

final class SeaBattle {

public static void initializeGame() {
Menu.showLogo();
Menu.showGreetings();
Menu.showMainMenu();
}
}
207 changes: 207 additions & 0 deletions src/main/java/course_project/board/Battlefield.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
package course_project.board;

import course_project.player.Player;
import course_project.ships.*;

import java.util.*;

import static course_project.dialogue.Menu.*;
import static course_project.utils.RandomPositionGenerator.generateRandomForCell;
import static course_project.utils.RandomDirectionGenerator.generateRandomDirection;

public class Battlefield extends Board {

private final List<Ship> availableShips = new ArrayList<>();

public Battlefield() {
super.cells = new Cell[FIELD_SIZE][FIELD_SIZE];
fillCells();
addShipsToList();
}

public List<Ship> getAvailableShips() {
return this.availableShips;
}

public void addShipsToList() {
availableShips.add(new Battleship());
availableShips.add(new Destroyer());
availableShips.add(new Destroyer());
availableShips.add(new Cruiser());
availableShips.add(new Cruiser());
availableShips.add(new Cruiser());
availableShips.add(new PatrolBoat());
availableShips.add(new PatrolBoat());
availableShips.add(new PatrolBoat());
availableShips.add(new PatrolBoat());
}

public boolean addShipOnBattlefield(Ship ship, Cell cell, boolean isHorizontal) {
if (checkSpaceForPlaceShip(cell, ship.getSize(), isHorizontal)) {
if (isHorizontal) {
return !addRowOfCells(cell, ship);
} else {
return !addColumnOfCells(cell, ship);
}
}
return true;
}

public void availableShipsDecrease(Ship ship) {
this.availableShips.remove(ship);
}

private boolean addColumnOfCells(Cell entryCell, Ship ship) {
if (checkPossibilityForAddColumn(entryCell, ship.getSize())) {
int y = entryCell.getY();
for (int i = entryCell.getX(); i < entryCell.getX() + ship.getSize(); i++) {
getCellByPosition(i, y).setShipAtCell(ship);
}
return true;
}
return false;
}

private boolean addRowOfCells(Cell entryCell, Ship ship) {
if (checkPossibilityForAddRow(entryCell, ship.getSize())) {
int x = entryCell.getX();
for (int j = entryCell.getY(); j < entryCell.getY() + ship.getSize(); j++) {
getCellByPosition(x, j).setShipAtCell(ship);
}
return true;
}
return false;
}

private boolean checkPossibilityForAddColumn(Cell entryCell, int shipSize) {
if (!(checkCellAbove(entryCell) &&
checkCellAboveLeftDiagonal(entryCell) &&
checkCellAboveRightDiagonal(entryCell))) {
return false;
}
int y = entryCell.getY();
for (int i = entryCell.getX(); i < entryCell.getX() + shipSize; i++) {
Cell currentCell = cells[i][y];
if (!(isCellEmpty(currentCell) &&
checkCellBelow(currentCell) &&
checkCellOnTheLeft(currentCell) &&
checkCellOnTheRight(currentCell))) {
return false;
}
if (i == entryCell.getX() + shipSize - 1 &&
!(checkCellBelowLeftDiagonal(currentCell) &&
checkCellBelowRightDiagonal(currentCell))
) {
return false;
}
}
return true;
}

private boolean checkPossibilityForAddRow(Cell entryCell, int shipSize) {
if (!(checkCellOnTheLeft(entryCell) &&
checkCellAboveLeftDiagonal(entryCell) &&
checkCellBelowLeftDiagonal(entryCell) &&
checkCellAboveRightDiagonal(entryCell))
) {
return false;
}
int x = entryCell.getX();
for (int j = entryCell.getY(); j < entryCell.getY() + shipSize; j++) {
Cell currentCell = cells[x][j];
if (!(isCellEmpty(currentCell) &&
checkCellOnTheRight(currentCell) &&
checkCellAbove(currentCell) &&
checkCellBelow(currentCell))
) {
System.out.println("Cannot set the ship to coordinates " + Character.toUpperCase((char) (entryCell.getY() + 97)) + "" + (entryCell.getX() + 1) + ", check if the ship is in contact with another.\n");
return false;
}
if (j == entryCell.getY() + shipSize - 1 &&
!(checkCellAboveRightDiagonal(currentCell) &&
checkCellBelowRightDiagonal(currentCell))
) {
return false;
}
}
return true;
}

private boolean checkSpaceForPlaceShip(Cell fromCell, int shipSize, boolean isHorizontal) {
return isHorizontal ?
fromCell.getY() >= 0 && fromCell.getY() + shipSize <= FIELD_SIZE :
fromCell.getX() >= 0 && fromCell.getX() + shipSize <= FIELD_SIZE;
}

private boolean checkCellAbove(Cell currentCell) {
return currentCell.getX() == 0 || isCellEmpty(cells[currentCell.getX() - 1][currentCell.getY()]);
}

private boolean checkCellBelow(Cell currentCell) {
return currentCell.getX() == 9 || isCellEmpty(cells[currentCell.getX() + 1][currentCell.getY()]);
}

private boolean checkCellOnTheRight(Cell currentCell) {
return currentCell.getY() == 9 || isCellEmpty(cells[currentCell.getX()][currentCell.getY() + 1]);
}

private boolean checkCellOnTheLeft(Cell currentCell) {
return currentCell.getY() == 0 || isCellEmpty(cells[currentCell.getX()][currentCell.getY() - 1]);
}

private boolean checkCellAboveLeftDiagonal(Cell currentCell) {
return currentCell.getX() == 0 || currentCell.getY() == 0 ||
isCellEmpty(cells[currentCell.getX() - 1][currentCell.getY() - 1]);
}

private boolean checkCellBelowLeftDiagonal(Cell currentCell) {
return currentCell.getX() == 9 || currentCell.getY() == 0 ||
isCellEmpty(cells[currentCell.getX() + 1][currentCell.getY() - 1]);
}

private boolean checkCellAboveRightDiagonal(Cell currentCell) {
return currentCell.getX() == 0 || currentCell.getY() == 9 ||
isCellEmpty(cells[currentCell.getX() - 1][currentCell.getY() + 1]);
}

private boolean checkCellBelowRightDiagonal(Cell currentCell) {
return currentCell.getX() == 9 || currentCell.getY() == 9 ||
isCellEmpty(cells[currentCell.getX() + 1][currentCell.getY() + 1]);
}


public boolean hasAvailableShips() {
return !this.availableShips.isEmpty();
}

public static void addShipsOneByOne(Player currentPlayer) {
showHelp();
for (Ship ship : currentPlayer.getField().getAvailableShips()) {
currentPlayer.getField().printField();
Cell position;
boolean isHorizontal = true;
do {
System.out.print("Ship's type: " + ship.getName() + ". Size: " + ship.getSize() + ". Specify coordinates: ");
String[] cellPossition = getInputPosition().split(" ");
int x = Integer.parseInt(cellPossition[0].substring(1)) - 1;
int y = Integer.parseInt(String.valueOf(cellPossition[0].charAt(0) - 97));
position = currentPlayer.getField().getCellByPosition(x, y);
if (cellPossition.length == 2) {
isHorizontal = !"v".equals(cellPossition[1]);
}
} while (currentPlayer.getField().addShipOnBattlefield(ship, position, isHorizontal));
}
}

public static void generateRandomPlacesForShips(Player currentPlayer) {
for (Ship ship : currentPlayer.getField().getAvailableShips()) {
Cell toPos;
boolean isHorizontal;
do {
toPos = currentPlayer.getField().getCellByPosition(generateRandomForCell(),
generateRandomForCell());
isHorizontal = generateRandomDirection();
} while (currentPlayer.getField().addShipOnBattlefield(ship, toPos, isHorizontal));
}
}
}
113 changes: 113 additions & 0 deletions src/main/java/course_project/board/Board.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package course_project.board;

import course_project.player.Player;

import static course_project.utils.Colors.*;

public class Board {

public static final int FIELD_SIZE = 10;
private static final String SHIP = ANSI_GREEN + "■" + ANSI_RESET;
private static final String WATER = "~";
private static final String MISS = ANSI_MAGENTA + "▓" + ANSI_RESET;
private static final String HIT = ANSI_YELLOW + "×" + ANSI_RESET;
private static final String DESTROY = ANSI_RED + "*" + ANSI_RESET;

Cell[][] cells;

protected Board() {
this.cells = new Cell[FIELD_SIZE][FIELD_SIZE];
fillCells();
}

public void fillCells() {
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 field = new StringBuilder();
field.append("\t" + "A B C D E F G H I J").append("\n");

for (int i = 0; i < FIELD_SIZE; i++) {
field.append(String.format("|%-2d|", i + 1));
for (int j = 0; j < FIELD_SIZE; j++) {
Cell currentCell = getCellByPosition(i, j);
String symbol = WATER;
if (!isCellEmpty(currentCell)) {
symbol = SHIP;
}
field.append(String.format("%s ", symbol));
}
field.append("\n");
}
System.out.println(field);
}

public void showTwoFields(Player currentPlayer, Player enemy) {

StringBuilder twoFields = new StringBuilder();

twoFields.append("\t" + "A B C D E F G H I J").append("\t\t\t " + "A B C D E F G H I J").append("\n");

for (int i = 0; i < FIELD_SIZE; i++) {
twoFields.append(String.format("|%-2d|", i + 1));
for (int j = 0; j < FIELD_SIZE; j++) {
if (currentPlayer.getField().getCellByPosition(i, j).isHit()) {
checkCellStateAndMarkIt(currentPlayer, twoFields, i, j);
} else {
if (currentPlayer.getField().isCellEmpty(currentPlayer.getField().getCellByPosition(i, j))) {
twoFields.append(String.format("%s ", WATER));
} else {
twoFields.append(String.format("%s ", SHIP));
}
}
}
twoFields.append("\t\t").append(String.format("|%-2d|", i + 1));
for (int j = 0; j < FIELD_SIZE; j++) {
if (enemy.getField().getCellByPosition(i, j).isHit()) {
checkCellStateAndMarkIt(enemy, twoFields, i, j);
} else {
twoFields.append(String.format("%s ", WATER));
}
}
twoFields.append("\n");
}
System.out.println(twoFields);
}

private void checkCellStateAndMarkIt(Player currentPlayer, StringBuilder bothFields, int i, int j) {
if (currentPlayer.getField().isHit(currentPlayer.getField().getCellByPosition(i, j))) {
if (currentPlayer.getField().getCellByPosition(i, j).getReference().isDestroyed()) {
bothFields.append(String.format("%s ", DESTROY));
} else {
bothFields.append(String.format("%s ", HIT));
}
} else {
bothFields.append(String.format("%s ", MISS));
}
}

public Cell getCellByPosition(int x, int y) {
for (Cell[] cellRow : this.cells) {
for (Cell cellColumn : cellRow) {
if (cellColumn.getX() == x && cellColumn.getY() == y) {
return cellColumn;
}
}
}
return null;
}

public boolean isCellEmpty(Cell currentCell) {
return currentCell.getShipReference() == null;
}

public boolean isHit(Cell toCell) {
return !isCellEmpty(cells[toCell.getX()][toCell.getY()]);
}

}
Loading