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
84 commits
Select commit Hold shift + click to select a range
5d3db82
test from kirill
filipchikkirill Jul 5, 2021
cda7765
Gitignore update
ArtemNikolaev1 Jul 5, 2021
986bf6e
Initial commit v5
ArtemNikolaev1 Jul 5, 2021
1d0a10b
Moving and gitignore update
ArtemNikolaev1 Jul 5, 2021
a4c3c7b
add homework_lesson_2
filipchikkirill Jul 5, 2021
b7db6c6
add homework_lesson_2
filipchikkirill Jul 5, 2021
dbfe254
homework_lesson_2
filipchikkirill Jul 5, 2021
52ae014
rename Homework_lesson_2 -> homework
filipchikkirill Jul 9, 2021
59d2dd8
refactor README.md
filipchikkirill Jul 9, 2021
42cd073
Delete src/main/java/lesson_2 directory
filipchikkirill Jul 9, 2021
aed8cb2
fix bugs
filipchikkirill Jul 12, 2021
1d4356c
Merge remote-tracking branch 'origin/master' into feature/KirillFilip…
filipchikkirill Jul 12, 2021
ce8002f
fix bugs
filipchikkirill Jul 12, 2021
0775258
fix bugs
filipchikkirill Jul 12, 2021
c0cc291
fix style
filipchikkirill Jul 13, 2021
a57fb6d
homework 2 start
filipchikkirill Jul 15, 2021
a7344a0
homework 2 PyramidPrinter
filipchikkirill Jul 15, 2021
3dff0e8
homework 2 change README.md
filipchikkirill Jul 15, 2021
c1b7933
homework 2 add TrafficLight.
filipchikkirill Jul 15, 2021
4947867
homework 2 add RandomCharsTable.
filipchikkirill Jul 16, 2021
7c4b7a7
homework 2 ready for check.
filipchikkirill Jul 19, 2021
b21edbb
homework 2 ready for check.
filipchikkirill Jul 19, 2021
accdbb2
homework 2 ready for check.
filipchikkirill Jul 19, 2021
5fa1a3b
homework 2 refactoring the project structure
filipchikkirill Jul 20, 2021
0db5d6d
homework 2 RandomCharsTable refactor
filipchikkirill Jul 20, 2021
4ad9d12
homework 2 refactor TrafficLight
filipchikkirill Jul 20, 2021
eb09bf6
homework 2 refactor utils
filipchikkirill Jul 20, 2021
781e1e5
modify README.md
filipchikkirill Jul 20, 2021
76fa0ec
homework_3
filipchikkirill Jul 20, 2021
ce3ca96
homework_2 small refactor
filipchikkirill Jul 21, 2021
f333990
homework_2 small refactor
filipchikkirill Jul 21, 2021
97ee39c
homework_2 I don't know what result is needed, so now output the raw …
filipchikkirill Jul 21, 2021
bc6a30f
homework_2 method start() -> run()
filipchikkirill Jul 21, 2021
1bda9c7
Merge remote-tracking branch 'origin/master' into feature/KirillFilip…
filipchikkirill Jul 21, 2021
6a9110a
homework_3 add PyramidPrinterTest
filipchikkirill Jul 21, 2021
2355efd
homework_3 add TrafficLightTest
filipchikkirill Jul 21, 2021
92d5491
homework_3 add RandomCharsTableTest not full
filipchikkirill Jul 21, 2021
e44be42
homework_2 fix RandomCharsTable
filipchikkirill Jul 21, 2021
f9af1a8
homework_3 ready for check
filipchikkirill Jul 21, 2021
18b9a0e
homework_3 ready for check
filipchikkirill Jul 21, 2021
b003a8c
homework_3 ready for check
filipchikkirill Jul 21, 2021
ba88fc3
homework_3 ready for check
filipchikkirill Jul 21, 2021
1ce9200
homework_3 ready for check
filipchikkirill Jul 22, 2021
d210ece
homework_3 ready for check
filipchikkirill Jul 22, 2021
6478f77
homework_3 ready for check
filipchikkirill Jul 22, 2021
838ce8d
homework_2 fix Main.java
filipchikkirill Jul 24, 2021
95c50dc
homework_2 fix Main.java
filipchikkirill Jul 24, 2021
514a8c1
homework_4 start
filipchikkirill Jul 29, 2021
0cb7be3
homework_4 change README.md
filipchikkirill Aug 15, 2021
f0c4bda
homework_4 add annotation
filipchikkirill Aug 15, 2021
0a1c1b6
homework_4 add annotation
filipchikkirill Aug 15, 2021
5870a57
homework_4 add CustomFileReader
filipchikkirill Aug 15, 2021
f1cded3
homework_4 add fix README.md
filipchikkirill Aug 15, 2021
c603925
homework_4 add fix
filipchikkirill Aug 16, 2021
311e359
homework_5 add
filipchikkirill Aug 17, 2021
13b2021
homework_4 fix
filipchikkirill Aug 18, 2021
6117c71
homework_4 add test for CustomAnnotation
filipchikkirill Aug 18, 2021
4d24b63
homework_5 add
filipchikkirill Aug 22, 2021
c940b66
homework_5 README.md change
filipchikkirill Aug 22, 2021
3049b13
homework_6
filipchikkirill Aug 30, 2021
4ce308e
homework_6
filipchikkirill Sep 1, 2021
663d28a
homework_7
filipchikkirill Sep 14, 2021
9dcbbe4
homework_4
filipchikkirill Sep 14, 2021
35fb91b
course_project first iteration
filipchikkirill Sep 20, 2021
0b28c70
course_project fix 1
filipchikkirill Sep 20, 2021
9e2d0b2
course_project fix 2
filipchikkirill Sep 20, 2021
e71d04a
course_project fix 3
filipchikkirill Sep 20, 2021
628b0c3
course_project fix 4
filipchikkirill Sep 20, 2021
1c931dd
course_project fix 5
filipchikkirill Sep 20, 2021
72b5a3b
course_project fix 6
filipchikkirill Sep 20, 2021
8a66fec
course_project add Tests (not all)
filipchikkirill Sep 20, 2021
24408c3
Homework3 fix immutableclass
filipchikkirill Sep 21, 2021
d579c63
Homework3 fix immutableclass
filipchikkirill Sep 21, 2021
acb9a49
Homework 6 add mutable
filipchikkirill Sep 21, 2021
7a97598
Homework 3 add mutable
filipchikkirill Sep 21, 2021
71662a4
Homework fix SingletonTest
filipchikkirill Sep 21, 2021
193cfd9
course_project fix and add random fill field
filipchikkirill Sep 21, 2021
5d67208
course_project fix
filipchikkirill Sep 21, 2021
2be5bc1
course_project fix
filipchikkirill Sep 21, 2021
a2464ce
course_project test
filipchikkirill Sep 21, 2021
d56c17e
course_project test
filipchikkirill Sep 21, 2021
71f99ca
course_project test
filipchikkirill Sep 21, 2021
d6cb9f7
course_project test
filipchikkirill Sep 21, 2021
a7c5231
hw_3 fix
filipchikkirill 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
24 changes: 21 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,27 @@
# Java Core June 2021

## *Nikolaev Artem*
## *Filipchik Kirill*

| 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/KirillFilipchik/src/main/java/homework_1) | The app that reads input arguments and prints them, until "ошибка" argument |
| HW2 | [PyramidPrinter](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/KirillFilipchik/src/main/java/homework_2/pyramid_printer) | The app that reads input argument and builds pyramid base on input value |
| HW2 | [RandomCharsTable](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/KirillFilipchik/src/main/java/homework_2/random_chars_table) | App reads from the console width and length of the chart, strategy keyword (even or odd). Prints to the console the chart of random chars from A to Z |
| HW2 | [TrafficLight](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/KirillFilipchik/src/main/java/homework_2/traffic_light) | App reads current time in seconds from the console and prints the traffic light|
| HW3 | [ImmutableClass](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/KirillFilipchik/src/main/java/homework_3) | ImmutableClass |
| HW3 | [Tests](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/KirillFilipchik/src/test/java/homework_2/) | HW_2 Tests |
| HW4 | [Singleton](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/KirillFilipchik/src/main/java/homework_4/singleton) | Singleton|
| HW4 | [CustomAnnotation](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/KirillFilipchik/src/main/java/homework_4/сustom_annotation) | CustomAnnotation|
| HW4 | [CustomFileReader](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/KirillFilipchik/src/main/java/homework_4/custom_file_reader) | CustomFileReader|
| HW4 | [Tests](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/KirillFilipchik/src/test/java/homework_4) | HW_4 Tests|
| HW5 | [HW_5](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/KirillFilipchik/src/main/java/homework_5) | HW_5|
| HW5 | [PowerOfNumber](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/KirillFilipchik/src/main/java/homework_5/power_of_number) | PowerOfNumber|
| HW5 | [CustomRegexMatcher](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/KirillFilipchik/src/main/java/homework_5/custom_regex_matcher) | CustomRegexMatcher|
| HW5 | [Tests](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/KirillFilipchik/src/test/java/homework_5) | HW_5 Tests|
| HW6 | [MapProblemsGenerator](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/KirillFilipchik/src/main/java/homework_6/map_problems_generator) | MapProblemsGenerator|
| HW7 | [KittenToCatFunction](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/KirillFilipchik/src/main/java/homework_7/kitten_to_cat_function) | KittenToCatFunction|

[Link to markdown giude](https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet)

[Link to CodingBat](https://codingbat.com/done?user=parallaksik@gmail.com&tag=9517187513)

[Link to markdown guide](https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet)
6 changes: 6 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,10 @@ dependencies {

test {
useJUnitPlatform()
}

jar {
manifest {
attributes 'Main-Class': 'homework_4.custom_file_reader.Main'
}
}
16 changes: 16 additions & 0 deletions src/main/java/course_project/sea_battle/SeaBattleApp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package course_project.sea_battle;

import course_project.sea_battle.controller.impl.GameControllerImpl;
import course_project.sea_battle.controller.impl.IOControllerImpl;

public class SeaBattleApp {
Copy link
Owner

Choose a reason for hiding this comment

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

Opt: The field is quite narrow, i think it would look better with more space between cells by width
image

Copy link
Owner

Choose a reason for hiding this comment

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

The game seems not working. I see you implemented PvP, but I play only for 1 Player. I don't see field for 2nd, don't see his name anywhere. Don't know how to shoot for 2nd.
And please introduce automatic ship placement, testing will be much easier

Copy link
Owner

Choose a reason for hiding this comment

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

Good! Nice architecture, SOLID is followed. Code is pretty readable. Good Java Core and Java libraries usage.

To make better: interface could be refactored. Also check correct working of the game.

Copy link
Owner

Choose a reason for hiding this comment

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

Also nice approach to testing, test cases are readable

Copy link
Owner

Choose a reason for hiding this comment

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

Good, automatic ship placement! I found some minor issues with the interface:

  1. When miss, there's no message to press enter to finish the turn. Actually, I didn't see this message anywhere... Confusing
  2. In case i hit a ship, i need to press enter 2 times to be able to shoot again
  3. No error message in case I already shot this cell
    image
  4. No message in case of destroyed ship
    image
  5. And please make field representation wider :)

The app now works correctly, I didn't find any bugs. And from the code perspective, the app looks great!

To make better: improve interface:)
The homework is approved

public static void main(String[] args) {
try {
GameControllerImpl.getInstance().start();
} catch (Exception ex) {
ex.printStackTrace();
} finally {
IOControllerImpl.getInstance().close();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package course_project.sea_battle.controller;

import course_project.sea_battle.model.Field;

public interface FieldController {
Field instance();
void drawFields(Field... fields);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package course_project.sea_battle.controller;

import course_project.sea_battle.model.Field;

public interface FieldFillController {
void setShips(Field field);
FieldFillController setGameSetupHolder(GameSetupHolder gameSetupHolder);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package course_project.sea_battle.controller;

import course_project.sea_battle.model.CellStatus;
import course_project.sea_battle.model.Field;

public interface FireController {
CellStatus fire(Field targetField, Field enemyField, String coordinate);
boolean checkFire(Field field, String coordinate);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package course_project.sea_battle.controller;

import course_project.sea_battle.model.Game;

public interface GameController {
Game start();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package course_project.sea_battle.controller;

import java.util.Map;

public interface GameSetupHolder {
Map<Integer, Integer> getMapShips();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package course_project.sea_battle.controller;

public interface IOController {
void clr();
void close();
String dialog(String msg);
void print(String msg);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package course_project.sea_battle.controller;

public interface ParseController {
int[] parseCoordinate(String coordinate);
boolean isVerticalVector(String vector);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package course_project.sea_battle.controller;

import course_project.sea_battle.model.GamePlayer;

public interface PlayerController {
GamePlayer instance(String name);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package course_project.sea_battle.controller;

import course_project.sea_battle.model.Field;

public interface ShipController {
boolean placeShip(Field field, String coordinate, String vector, int size);
boolean placeShipByCoordinate(Field field, int x, int y, boolean isVertical, int size);
boolean isAnyShipAlive(Field field);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package course_project.sea_battle.controller.impl;

import course_project.sea_battle.controller.FieldController;
import course_project.sea_battle.controller.IOController;
import course_project.sea_battle.model.CellStatus;
import course_project.sea_battle.model.Field;

import java.util.Arrays;
import java.util.Optional;
import java.util.stream.IntStream;

public final class FieldControllerImpl implements FieldController {
private static final String TOP = " ╔══════════╗";
private static final String HUD = " ║ABCDEFGHIG║";
private static final String SPLIT_TOP_LINE = "╔══╬══════════╣";
private static final String SPLIT_BOT_LINE = "╚══╩══════════╝";
private static final String SPLIT_COL_LINE = "║";

private final IOController ioController = IOControllerImpl.getInstance();

private FieldControllerImpl() {}

@Override
public Field instance() {
Field field = new Field();
this.fillWater(field);
return field;
}

private void fillWater(Field field) {
if (field == null) return;
CellStatus[][] cells = field.getCells();
for (CellStatus[] row :
cells) {
Arrays.fill(row, CellStatus.WATER);
}
}

@Override
public void drawFields(Field... fields) {
Arrays.stream(fields)
.map(this::prepareForPrint)
.reduce((arr1, arr2) -> {
Arrays.setAll(arr1, i -> arr1[i] + "\t" + arr2[i]);
return arr1;
}).ifPresent(x -> Arrays.stream(x).forEach(ioController::print));
}

private String[] prepareForPrint(Field field) {
String[] arr = new String[field.getCells().length + 4];
arr[0] = TOP;
arr[1] = HUD;
arr[2] = SPLIT_TOP_LINE;
arr[arr.length - 1] = SPLIT_BOT_LINE;
StringBuilder sb = new StringBuilder();
int bound = Field.FIELD_SIZE;
IntStream.range(0, bound).forEachOrdered(i -> {
sb.append(String.format(SPLIT_COL_LINE + "%2s" + SPLIT_COL_LINE, i + 1));
Arrays.stream(field.getCells()[i]).forEachOrdered(sb::append);
sb.append(SPLIT_COL_LINE);
arr[i + 3] = sb.toString();
sb.setLength(0);
});
return arr;
}

public static FieldControllerImpl getInstance() {
return FieldControllerImpl.Holder.INSTANCE;
}

private static class Holder {
static final FieldControllerImpl INSTANCE = new FieldControllerImpl();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package course_project.sea_battle.controller.impl;

import course_project.sea_battle.controller.FireController;
import course_project.sea_battle.model.CellStatus;
import course_project.sea_battle.model.Field;

public final class FireControllerImpl implements FireController {
private final ValidateControllerImpl validateController = ValidateControllerImpl.getInstance();
private final ParserControllerImpl parserController = ParserControllerImpl.getInstance();

private FireControllerImpl() {}

@Override
public CellStatus fire(Field opponentField, Field radar, String coordinate) {
if (radar == null || opponentField == null) throw new RuntimeException("Поле не может быть null");

int[] cordXY = parserController.parseCoordinate(coordinate);
int x = cordXY[0], y = cordXY[1];

CellStatus cellBefore = opponentField.getCell(x, y);
CellStatus cellAfter;
if (cellBefore == CellStatus.SHIP || cellBefore == CellStatus.HIT) {
cellAfter = CellStatus.HIT;
}
else {
cellAfter = CellStatus.SHOT;
}
opponentField.setCell(x, y, cellAfter);
radar.setCell(x, y, cellAfter);
return cellAfter;
}

@Override
public boolean checkFire(Field field, String coordinate) {
if (field == null) return false;
if (!validateController.coordinate(coordinate)) return false;
int[] cordXY = parserController.parseCoordinate(coordinate);

return field.getCell(cordXY[0], cordXY[1]) != CellStatus.HIT
&& field.getCell(cordXY[0], cordXY[1]) != CellStatus.SHOT;
}

public static FireControllerImpl getInstance() {
return FireControllerImpl.Holder.INSTANCE;
}

private static class Holder {
static final FireControllerImpl INSTANCE = new FireControllerImpl();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package course_project.sea_battle.controller.impl;

import course_project.sea_battle.controller.*;
import course_project.sea_battle.model.CellStatus;
import course_project.sea_battle.model.Game;
import course_project.sea_battle.model.GamePlayer;

public final class GameControllerImpl implements GameController{
private final PlayerController playerController = PlayerControllerImpl.getInstance();
private final IOController ioController = IOControllerImpl.getInstance();
private final FieldFillController manualFieldFillService = ManualFieldFillController.getInstance();
private final FieldFillController randomFieldFillController = RandomFieldFillController.getInstance();
private final FieldController fieldController = FieldControllerImpl.getInstance();
private final FireController fireController = FireControllerImpl.getInstance();
private final ShipController shipController = ShipControllerImpl.getInstance();

private GameControllerImpl() {}

public Game start() {
Game game = new Game();
addPlayers(game);
setShips(game.getPlayer1());
ioController.dialog("Позовите Следующего");
ioController.clr();
setShips(game.getPlayer2());
ioController.dialog("Да начнется битва");
ioController.clr();

doGame(game);
ioController.close();
return game;
}

private void addPlayers(Game game) {
String name1;
String name2;
do {
name1 = ioController.dialog("Введите имя первого игрока:");
} while (name1.isEmpty());
game.setPlayer1(playerController.instance(name1));
do {
name2 = ioController.dialog("Введите имя второго игрока:");
} while (name2.isEmpty());
game.setPlayer2(playerController.instance(name2));
}

private void setShips(GamePlayer player) {
ioController.print(player.getName() + ", установите корабли!");
boolean nextStep = false;
while (!nextStep) {
String answerP1 = ioController.dialog("Заполнить рандомно?(y/n):");
if (answerP1.trim().toLowerCase().matches("y")) {
randomFieldFillController.setShips(player.getFieldPlayer());
nextStep = true;
} else if (answerP1.trim().toLowerCase().matches("n")) {
manualFieldFillService.setShips(player.getFieldPlayer());
nextStep = true;
} else {
ioController.print("Попробуйте еще раз");
}
}
}

private void doGame(Game game){
boolean isPlayer1Move = true;
boolean isGameOver = false;

GamePlayer playerAtk = game.getPlayer1();
GamePlayer playerDef = game.getPlayer2();

while (!isGameOver) {

if (isPlayer1Move) {
playerAtk = game.getPlayer1();
playerDef = game.getPlayer2();
} else {
playerAtk = game.getPlayer2();
playerDef = game.getPlayer1();
}

ioController.clr();
if (!turnResult(playerAtk, playerDef)) {
isPlayer1Move = !isPlayer1Move;
}
isGameOver = !shipController.isAnyShipAlive(playerDef.getFieldPlayer());
}
ioController.print(playerAtk.getName() + " Выйграл!");
ioController.print(playerDef.getName() + " Проиграл!");
fieldController.drawFields(playerAtk.getFieldPlayer(), playerAtk.getRadarPlayer(),
playerDef.getFieldPlayer(), playerDef.getRadarPlayer());
game.setPlayerWin(playerAtk);
}

private boolean turnResult(GamePlayer playerAtk, GamePlayer playerDef) {
ioController.dialog(playerAtk.getName() + ", Ваш ход!");
fieldController.drawFields(playerAtk.getFieldPlayer(), playerAtk.getRadarPlayer());
String coordinate;
do coordinate = ioController.dialog("Координаты:");
while (!fireController.checkFire(playerAtk.getRadarPlayer(), coordinate));
CellStatus cellStatus = fireController.fire(playerDef.getFieldPlayer(),
playerAtk.getRadarPlayer(),
coordinate);
fieldController.drawFields(playerAtk.getFieldPlayer(), playerAtk.getRadarPlayer());
if (cellStatus != CellStatus.HIT) {
ioController.dialog("Мимо!");
ioController.clr();
return false;
} else {
ioController.dialog("Попали!");
return true;
}
}

public static GameControllerImpl getInstance() {
return GameControllerImpl.Holder.INSTANCE;
}

private static class Holder {
static final GameControllerImpl INSTANCE = new GameControllerImpl();
}
}
Loading