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
58 commits
Select commit Hold shift + click to select a range
14ecb8d
add homework_1
Jul 11, 2021
c36f3b8
refactor README.md
Jul 11, 2021
caa0d07
update homework_1
Jul 11, 2021
0939a72
del serr, add "\033[0;31m" in sout
Jul 12, 2021
5321ff5
homework_2, 1st task done
Jul 15, 2021
b1fda1f
homework_2, 2nd task done
Jul 15, 2021
95f480c
homework_2, 1st task refactor
Jul 16, 2021
7ce3d3c
homework_2, 3rd task done
Jul 16, 2021
f5158ab
update README.md
Jul 16, 2021
342868e
update README.md
Jul 19, 2021
e39ae9a
refactor TrafficLight
Jul 21, 2021
9bfa59f
refactor PyramidPrinter
Jul 21, 2021
248fc44
refactor RandomCharsTable
Jul 21, 2021
a24124e
add ImmutableClass
Jul 21, 2021
3f69a84
Merge branch 'master' into feature/DrozdovNikita
Jul 21, 2021
eab5371
update TrafficLight
Jul 22, 2021
2eabb92
update PyramidPrinter
Jul 22, 2021
49edb40
update RandomCharsTable
Jul 22, 2021
34779e9
update README.md
Jul 22, 2021
ceff88c
add UnitBaseMode.java
Jul 23, 2021
2ba110a
add junit tests for TrafficLight, TrafficLightExtraMode
Jul 23, 2021
a3548a9
add junit tests for 1st homework
Jul 23, 2021
f6e138f
add junit tests for PyramidPrinter
Jul 23, 2021
c6cb722
add junit tests for RandomCharsTable, RandomCharsTableCreator
Jul 23, 2021
a1de007
refactor hw2
pepya4ka Aug 1, 2021
f56c198
refactor hw3
pepya4ka Aug 1, 2021
db966e0
refactor hw3
pepya4ka Aug 1, 2021
88f9283
add hw4 CustomFileReader
pepya4ka Aug 3, 2021
4af3225
add hw4 Singleton
pepya4ka Aug 3, 2021
7796db2
add hw4 CustomAnnotation
pepya4ka Aug 3, 2021
e1c78c6
refactor hw4 CustomFileReader
pepya4ka Aug 3, 2021
b3ee00f
update README.md
pepya4ka Aug 3, 2021
b67f41f
update README.md
pepya4ka Aug 3, 2021
328e0e6
update README.md
pepya4ka Aug 6, 2021
b2257ec
refactor HW4
pepya4ka Aug 19, 2021
a8816e3
refactor HW4: remove apache and guava
pepya4ka Aug 20, 2021
0621fee
add hw5 CustomRegexMatcher
pepya4ka Aug 20, 2021
d4ef445
add hw5 PowerOfNumber
pepya4ka Aug 20, 2021
7e0bf34
add BaseClazz
pepya4ka Aug 20, 2021
a546438
refactor hw1
pepya4ka Aug 20, 2021
db58272
refactor hw2 PyramidPrinter
pepya4ka Aug 20, 2021
4e9cfe3
refactor hw2 TrafficLight
pepya4ka Aug 20, 2021
e6769f7
refactor hw2 RandomCharsTable
pepya4ka Aug 20, 2021
68efb7a
refactor hw3 MyImmutableClass
pepya4ka Aug 20, 2021
804c5ef
refactor hw4 CustomFileReader
pepya4ka Aug 20, 2021
070db1b
refactor hw5 CustomRegexMatcher
pepya4ka Aug 20, 2021
579fac1
refactor hw5 PowerOfNumber
pepya4ka Aug 20, 2021
1df79cb
update README.md
pepya4ka Aug 20, 2021
750a82d
add hw7
pepya4ka Sep 13, 2021
aee0d31
add hw6
pepya4ka Sep 13, 2021
4f7d50c
beta 1
pepya4ka Sep 19, 2021
d17e937
refactor hw6 MapProblemsGenerator
pepya4ka Sep 20, 2021
d8ee91a
refactor hw6 MapProblemsGenerator
pepya4ka Sep 20, 2021
f5fa480
beta 2
pepya4ka Sep 21, 2021
07b95a7
refactor hw5 PowerOfNumber
pepya4ka Sep 21, 2021
203ef1d
CP: add some message for players
pepya4ka Sep 21, 2021
dd2df0e
update README.md
pepya4ka Sep 22, 2021
e34eceb
CP: add autoset marks around sunken ship, mark damage on your field, …
pepya4ka Sep 23, 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
18 changes: 15 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@

# Java Core June 2021

## *Nikolaev Artem*
## *Drozdov Nikita* つ ◕_◕ ༽つ
Copy link
Owner

Choose a reason for hiding this comment

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

Good-looking readme 👍


| 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/DrozdovNikita/src/main/java/homework_1) | The app that reads input arguments and prints them, until "error" argument |
| HW2 | [Traffic light](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/DrozdovNikita/src/main/java/homework_2/traffic_light) </br> [PyramidPrinter](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/DrozdovNikita/src/main/java/homework_2/pyramid_printer) </br> [RandomCharsTable](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/DrozdovNikita/src/main/java/homework_2/random_chars_table)| The app that reads input args and prints current traffic light </br> The app that reads input integer and prints "x" pyramid </br> The app that reads input arg, generate chars table and prints odd/even letters |
| HW3 | [ImmutableClass](https://github.com/NikolaevArtem/Java_Core_June_2021/blob/feature/DrozdovNikita/src/main/java/homework_3/MyImmutableClass.java) | |
| HW4 | [CustomFileReader](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/DrozdovNikita/src/main/java/homework_4/custom_file_reader) <br> [Singleton](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/DrozdovNikita/src/main/java/homework_4/singleton) <br> [CustomAnnotation](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/DrozdovNikita/src/main/java/homework_4/custom_annotation) | The app has 6 implementation file reader: Apache, BufferedReader, FileInputStream, Guava, Scanner and NIO <br> The app-singleton <br> The app use custom annotations to create xml structure for your object: XmlSerializable and XmlElement |
| HW5 | [PowerOfNumber](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/DrozdovNikita/src/main/java/homework_5/power_of_number) <br> [CustomRegexMatcher](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/DrozdovNikita/src/main/java/homework_5/custom_regex_matcher) | The app that reads two input integers and prints a to the power of b <br> The app that reads input phone number and prints its validity |
| HW6 | [MapProblemsGenerator](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/DrozdovNikita/src/main/java/homework_6) | The app that generate map problems
| HW7 | [KittenToCatFunction](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/DrozdovNikita/src/main/java/homework_7) | The app that implementation KittenToCatFunction functional interface
| CP | [SeaBattle](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/DrozdovNikita/src/main/java/course_project) | Sea Battle
| | | |
| TEST | [Test dir](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/DrozdovNikita/src/test/java)


[Link to markdown giude](https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet)
[Link to markdown giude](https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet) </br>
[Link to сodingBat](https://codingbat.com/done?user=ndrozdov9@gmail.com&tag=8408048475)
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ repositories {
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.0'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.0'
testCompile 'org.junit.jupiter:junit-jupiter-params:5.7.0'
}

test {
Expand Down
Empty file modified gradlew
100644 → 100755
Empty file.
28 changes: 28 additions & 0 deletions src/main/java/base/BaseClazz.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package base;

import java.math.BigInteger;
import java.util.stream.IntStream;

public abstract class BaseClazz {

protected static final String ANSI_RESET = "\033[0m";
protected static final String ANSI_RED = "\u001B[31m";
protected static final String ANSI_RED_BACKGROUND = "\u001B[41m";
protected static final String ANSI_GREEN_BACKGROUND = "\u001B[42m";
protected static final String ANSI_YELLOW_BACKGROUND = "\u001B[43m";

public abstract void run();

protected boolean isValid(String... arg) {
if (arg[0].split("\\s").length > 1) {
return false;
}
if (!arg[0].chars().allMatch(Character::isDigit)) {
return false;
}

return new BigInteger(arg[0]).compareTo(BigInteger.valueOf(Integer.MAX_VALUE)) <= 0 &&
IntStream.of(Integer.parseInt(arg[0])).allMatch(elem -> elem + 1 > 0);
}

}
97 changes: 97 additions & 0 deletions src/main/java/course_project/Game.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package course_project;

import base.BaseClazz;
import course_project.services.FieldPrinter;
import course_project.services.GameService;
import course_project.services.PlayerController;
import course_project.ship.abstracts.SinglePartShip;
import course_project.ship.models.Player;
import course_project.ship.abstracts.ShipType;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Stream;

import static course_project.services.GameService.*;

public class Game extends BaseClazz {
Copy link
Owner

Choose a reason for hiding this comment

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

It's quite difficult to put ships manually, especially because the field does not refresh

Copy link
Owner

Choose a reason for hiding this comment

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

I got it, you refresh after the type of ships was placed. Smart move

Copy link
Owner

Choose a reason for hiding this comment

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

Some bug in field rendering
image

Copy link
Owner

Choose a reason for hiding this comment

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

Also time between turns is quite long, could you please reduce sleep time?


private static String MISS = "Промах. В следующий раз повезет";

private final Player player = new Player("player1");
private final Player computer = new Player("player2");

public static Scanner scanner = new Scanner(System.in);

private static final Function<Player, Stream<SinglePartShip>> convert =
elem -> elem
.getMapShips()
.values()
.stream()
.flatMap(e -> e
.stream()
.flatMap(l -> l.getList().stream()));

@Override
public void run() {
player.getMapShips().putAll(GameService.getNewPlayerWithRandomShips(player.getName()).getMapShips());
computer.getMapShips().putAll(GameService.getNewPlayerWithRandomShips(computer.getName()).getMapShips());
startGame();
}

private void startGame() {
Player aim = computer;
Player attacker = player;
while (!(player.isEmptyHealthShips() || computer.isEmptyHealthShips())) {
FieldPrinter.printFiled(convert.apply(attacker), attacker.getHitsMap(), attacker.getDamageList().stream());
String result = playersHit(aim, attacker);
if (!result.equals(MISS)) {
System.out.println(result);
} else {
aim = (aim == computer ? player : computer);
attacker = (attacker == computer ? player : computer);
clearConsole();
System.out.println(result);
try {
Thread.sleep(1500);
} catch (InterruptedException e) {
e.printStackTrace();
}
clearConsole();
}
}
FieldPrinter.printFiled(convert.apply(attacker), attacker.getHitsMap(), attacker.getDamageList().stream());
System.out.println("Победил " + attacker.getName());
}

private void initPlayer(Player gamer) {
FieldPrinter.printFieldForInit(convert.apply(gamer));
PlayerController.initShipLists(gamer, ShipType.SINGLE_DECK, 4);
FieldPrinter.printFieldForInit(convert.apply(gamer));
PlayerController.initShipLists(gamer, ShipType.DOUBLE_DECK, 3);
FieldPrinter.printFieldForInit(convert.apply(gamer));
PlayerController.initShipLists(gamer, ShipType.THREE_DECK, 2);
FieldPrinter.printFieldForInit(convert.apply(gamer));
PlayerController.initShipLists(gamer, ShipType.FOUR_DECK, 1);
}

private void initPlayer1(Player gamer) {
PlayerController.addToList(gamer, ShipType.SINGLE_DECK, "A10", "V", 1);
PlayerController.addToList(gamer, ShipType.SINGLE_DECK, "D9", "V", 1);
PlayerController.addToList(gamer, ShipType.SINGLE_DECK, "F9", "V", 1);
PlayerController.addToList(gamer, ShipType.SINGLE_DECK, "G6", "V", 1);
PlayerController.addToList(gamer, ShipType.DOUBLE_DECK, "A1", "H", 2);
PlayerController.addToList(gamer, ShipType.DOUBLE_DECK, "J3", "V", 2);
PlayerController.addToList(gamer, ShipType.DOUBLE_DECK, "I9", "V", 2);
PlayerController.addToList(gamer, ShipType.THREE_DECK, "D1", "V", 3);
PlayerController.addToList(gamer, ShipType.THREE_DECK, "B5", "V", 3);
PlayerController.addToList(gamer, ShipType.FOUR_DECK, "G1", "H", 4);
}

private void clearConsole() {
for (int i = 0; i < 20; i++) {
System.out.println();
}
}

}
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;

public class Main {

public static void main(String[] args) {

new Game().run();

}

}
99 changes: 99 additions & 0 deletions src/main/java/course_project/services/FieldPrinter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package course_project.services;

import course_project.ship.abstracts.SinglePartShip;

import java.util.List;
import java.util.Map;
import java.util.stream.IntStream;
import java.util.stream.Stream;

public class FieldPrinter {

private static final int FIELD_CAPACITY = 10;
protected static final String ANSI_RESET = "\033[0m";
protected static final String ANSI_GREEN = "\u001B[32m";
protected static final String ANSI_CYAN = "\u001B[36m";

private FieldPrinter() {
throw new IllegalStateException("Utility class");
}

public static void printFieldForInit(Stream<SinglePartShip> attacker) {
int[][] attackerField = new int[FIELD_CAPACITY][FIELD_CAPACITY];
attacker.forEach(elem -> add(attackerField, elem, 1));
printTableHeader();
System.out.println();
for (int i = 0; i < FIELD_CAPACITY; i++) {
printLineTable(i, attackerField);
System.out.println();
}
}

public static void printFiled(Stream<SinglePartShip> attacker,
Map<Boolean, List<SinglePartShip>> opponent,
Stream<SinglePartShip> damageList) {
int[][] attackerField = new int[FIELD_CAPACITY][FIELD_CAPACITY];
int[][] opponentField = new int[FIELD_CAPACITY][FIELD_CAPACITY];
attacker.forEach(elem -> add(attackerField, elem, 1));
damageList.forEach(elem -> add(attackerField, elem, -1));
opponent.entrySet().forEach(elem -> add(opponentField, elem));

System.out.printf("%-51s", "your field");
System.out.println("opponent field");

printTableHeader();
System.out.printf("%4s", " | ");
printTableHeader();
System.out.println();

for (int i = 0; i < FIELD_CAPACITY; i++) {
printLineTable(i, attackerField);
System.out.printf("%4s", " | ");
printLineOpponentTable(i, opponentField);
System.out.println();
}
}

private static void printTableHeader() {
System.out.printf("%4s", "");
IntStream.range(0, FIELD_CAPACITY).forEach(i -> System.out.printf("%4s", (char) (65 + i)));
}

private static void printLineTable(int i, int[][] field) {
System.out.printf("%4s", i + 1);
for (int j = 0; j < FIELD_CAPACITY; j++) {
if (field[i][j] == 1) {
System.out.printf("%13s", ANSI_GREEN + "x" + ANSI_RESET);
} else if (field[i][j] == -1) {
System.out.printf("%13s", ANSI_CYAN + "o" + ANSI_RESET);
} else {
System.out.printf("%4s", ".");
}
}
}

private static void printLineOpponentTable(int i, int[][] field) {
System.out.printf("%4s", i + 1);
for (int j = 0; j < FIELD_CAPACITY; j++) {
if (field[i][j] == 1) {
System.out.printf("%13s", ANSI_GREEN + "x" + ANSI_RESET);
} else if (field[i][j] == -1) {
System.out.printf("%13s", ANSI_CYAN + "o" + ANSI_RESET);
} else {
System.out.printf("%4s", "?");
}
}
}

private static void add(int[][] field, SinglePartShip partShip, int value) {
field[partShip.getY() - 1][partShip.getX() - 1] = value;
}

private static void add(int[][] field, Map.Entry<Boolean, List<SinglePartShip>> partShip) {
if (partShip.getKey()) {
partShip.getValue().forEach(elem -> add(field, elem, 1));
} else {
partShip.getValue().forEach(elem -> add(field, elem, -1));
}
}
}
Loading