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
52 commits
Select commit Hold shift + click to select a range
35e0d54
First commit
Jul 5, 2021
a2500da
Add new gitignore
Jul 5, 2021
b52fdb3
Add homework1 task
Jul 6, 2021
ff1a0c0
Add homework1 task
Jul 12, 2021
78b2f08
Change a structure of project
Jul 12, 2021
103b908
Update after merging
Jul 12, 2021
463cebd
Update after review
Jul 13, 2021
4407534
Add PyramidPrinter task for homework_2
Jul 14, 2021
8be4005
Add RandomCharsTable task for homework_2
Jul 15, 2021
6105e3a
Add RandomCharsTable task for homework_2
Jul 16, 2021
87d92db
Refactor PyramidPrinter and RandomCharsTable tasks, add TrafficLight …
Jul 16, 2021
aa82415
Update README
Jul 16, 2021
412219d
Fix all 3 tasks
Jul 17, 2021
a41ea11
Fix for RandomCharsTable task
Jul 17, 2021
f94a73a
the 2nd Fix for RandomCharsTable task
Jul 19, 2021
dc44900
Merge branch 'master' into feature/Lera_Rodina
Jul 21, 2021
efc7f9b
Add link to Coding bat for README.md
Jul 24, 2021
071afe2
Add empty ImmutableClass
Jul 24, 2021
d598b6f
Add requirements for ImmutableClass
Jul 25, 2021
9c06053
Add ImmutableClass - RGB (which defines objects that represent colors)
Jul 25, 2021
859c9fb
Update Immutable class
Jul 25, 2021
b0003c3
Add new run method for RandomCharsTable task
Jul 25, 2021
0d2dadf
Add new run method for TrafficLight task
Jul 25, 2021
8d1cf77
Fix text for errors for all 3 tasks
Jul 25, 2021
5d96e60
Move task to new ExtraTrafficLight class
Jul 25, 2021
e8d13e3
Add tests for TrafficLight task
Jul 25, 2021
4b8140c
Update README with homework 3 - Immutable class
Jul 25, 2021
6f6accb
1st iteration: Add tests for RandomCharsTable
Jul 25, 2021
a0fcc65
Fix RandomCharsTable and TrafficLight tasks after review
Jul 25, 2021
e86428c
Fix comment type in ImmutableClass
Aug 5, 2021
d1fd579
Add CustomFileReader carcass and new structure for hw4
Aug 6, 2021
eac4166
Add CustomFileReader task + the method for unit testing
Aug 7, 2021
eeedfbb
Add new package
Aug 7, 2021
dd81725
Fix CustomFileReader task,
Aug 8, 2021
faade02
Add Singleton task
Aug 8, 2021
7091eed
Finish Singleton task
Aug 8, 2021
fa0e484
Update README with hw4
Aug 8, 2021
ad3d16a
Add CustomAnnotation task
Aug 8, 2021
06821ce
Add power of number task for hw5 + tests and readme
Aug 31, 2021
d3852d8
Add CustomRegexMatcher task, update readme, create test directory
Aug 31, 2021
2d1f91b
Add tests for CustomRegexMatcher task
Aug 31, 2021
3194d81
Add MapProblemsGenerator task
Sep 6, 2021
a493b9f
Update README.md
Sep 19, 2021
c82aee5
Add Cat and Kitten classes
Sep 19, 2021
d877e43
Add Main
Sep 20, 2021
158c8f2
Finish with Main class
Sep 20, 2021
f8f2fad
Update README file
Sep 21, 2021
4e9dece
Add About for course project
Sep 21, 2021
4166be0
Update src/main/java/homework_7/Main.java
valeryrodina Sep 21, 2021
b49043a
Fix HW3
Sep 22, 2021
e548bdf
Fix HW3
Sep 22, 2021
4551b57
Fix HW6
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
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,7 @@ hs_err_pid*
out
target
*.iml
.idea
.idea
/.idea/
/gradle/
/.DS_Store/
19 changes: 16 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
# Java Core June 2021

## *Nikolaev Artem*
## *Rodina Lera*

| 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/Lera_Rodina/src/main/java/homework_1) | The app that reads input arguments and prints them, until "error" argument |
| HW2_1 | [Traffic light](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/Lera_Rodina/src/main/java/homework_2/traffic_light) | The app that reads the number of seconds from the beginning of the day from the command line displays the color of the traffic light (green, yellow or red) |
| HW2_2 | [Pyramid printer](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/Lera_Rodina/src/main/java/homework_2/pyramid_printer) | The app that reads the number from the command line and print a pyramid with levels according to this number, this pyramid consists only of 'x' symbols |
| HW2_3 | [Random chars table](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/Lera_Rodina/src/main/java/homework_2/random_chars_table) | The app that reads 2 numbers as size of a table and a strategy: even or odd, print this table consisting of random symbols from A to Z and then print all symbols in one line which are odd or even according to the strategy (their ASCII codes) |
| HW_3 | [Immutable class](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/Lera_Rodina/src/main/java/homework_3) | Immutable class with fields (including reference type), several constructors, method which return new object and requirements for Immutable class in comments |
| HW_4_1 | [Custom File Reader](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/Lera_Rodina/src/main/java/homework_4/custom_file_reader) | The app which gets data from file and then print it without any commas and dots (there are different ways for reading from a file) |
| HW_4_2 | [Singleton](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/Lera_Rodina/src/main/java/homework_4/singleton) | The app - singleton |
| HW_4_3 | [Custom annotation](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/Lera_Rodina/src/main/java/homework_4/custom_annotation) | A custom annotation |
| HW_5_1 | [Power Of Number](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/Lera_Rodina/src/main/java/homework_5/power_of_number) | The app which read number and its power and calculate the result. Recursion should be used here |
| HW_5_2 | [Custom Regex Matcher](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/Lera_Rodina/src/main/java/homework_5/custom_regex_matcher) | The app which read string and and check if this string matches with hardcoded regex in the app and get result: true or false |
| HW_6 | [Map Problems Generator](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/Lera_Rodina/src/main/java/homework_6/map_problems_generator) | The app which create collisions in hashmap |
| HW_7 | [Kitten To Cat Function Interface](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/Lera_Rodina/src/main/java/homework_7/kitten_to_cat_function) | The app which create an interface for turning a kitten into a cat |
| Course project | [Sea Battle](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/Lera_Rodina/src/main/java/course_project) | A console Sea Battle Game (Player vs Computer version only)|

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

}

test {
Expand Down
30 changes: 30 additions & 0 deletions src/main/java/couse_project/About
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@

Course project SEA BATTLE
Copy link
Owner

Choose a reason for hiding this comment

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

Good description!


---------------------------------------

Here you can try to play Sea Battle game!


I developed a version of a game "Player vs Computer"

Just to make a game a bit faster - all ships automatically are created randomly before starting this game.
And you might not spend time for creating it by yourself.

There are:
4-deck ships - 1
3-deck ships - 2
2-deck ships - 3
1-deck ships - 4

(Just because I ve remembered these rules since my childhood:))

After starting a game you see your seaMap (with blue color) and you should make your first move!

If your/computer's ship is found, cell will have a yellow color.
If a ship is destroyed fully, a color is red.
If you/computer makes a mistake - you wil see just a white "star".

I decided to make a console interface of game in Russian as our traditional game:)

Hope this game will be convenient for you!
14 changes: 14 additions & 0 deletions src/main/java/couse_project/Color.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package couse_project;

public class Color {

static String blue = "\033[1;34m";
static String red = "\033[1;31m";
static String yellow = "\033[1;33m";
static String white = "\033[1;37m";
static String reset = "\033[0m";

public static String setColor(String text, String color) {
return color + text + reset;
}
}
64 changes: 64 additions & 0 deletions src/main/java/couse_project/Displayer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package couse_project;

import java.io.IOException;
import java.util.HashMap;

public class Displayer {

private final String columns = " А Б В Г Д Е Ж З И К\n";
private final String row = "%2d %s %s %s %s %s %s %s %s %s %s\n";
private final HashMap<Integer, String> mapSymbols = new HashMap<Integer, String>() {{
put(0, " "); // пустая клетка
put(1, Color.setColor("■", Color.blue)); // корабль
put(2, Color.setColor("□", Color.yellow)); // подбитый корабль
put(3, Color.setColor("X", Color.red)); // разрушенный корабль
put(4, Color.setColor("*", Color.white)); // промах
}};
private final HashMap<Integer, String> radarSymbols = new HashMap<Integer, String>() {{
put(0, " "); // пустая клетка
put(1, " "); // корабль
put(2, Color.setColor("□", Color.yellow)); // подбитый корабль
put(3, Color.setColor("X", Color.red)); // разрушенный корабль
put(4, Color.setColor("*", Color.white)); // промах
}};

public void clearScreen() throws IOException, InterruptedException {
String os = System.getProperty("os.name").toLowerCase();
if (os.contains("win")) {
new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor();
} else if (os.contains("nix") || os.contains("nux")) {
new ProcessBuilder("terminal", "/c", "clear").inheritIO().start().waitFor();
}
System.out.println();
}

public void displayMap(SeaMap map) {
StringBuilder mapImage = new StringBuilder();
mapImage.append(columns);
for (int i = 0; i < map.size()[0]; i++) {
mapImage.append(String.format(row, i + 1,
mapSymbols.get(map.getCell(i, 0)), mapSymbols.get(map.getCell(i, 1)),
mapSymbols.get(map.getCell(i, 2)), mapSymbols.get(map.getCell(i, 3)),
mapSymbols.get(map.getCell(i, 4)), mapSymbols.get(map.getCell(i, 5)),
mapSymbols.get(map.getCell(i, 6)), mapSymbols.get(map.getCell(i, 7)),
mapSymbols.get(map.getCell(i, 8)), mapSymbols.get(map.getCell(i, 9))));
}
mapImage.append("\n");
System.out.print(mapImage);
}

public void displayRadar(SeaMap radar) {
StringBuilder radarImage = new StringBuilder();
radarImage.append(columns);
for (int i = 0; i < radar.size()[0]; i++) {
radarImage.append(String.format(row, i + 1,
radarSymbols.get(radar.getCell(i, 0)), radarSymbols.get(radar.getCell(i, 1)),
radarSymbols.get(radar.getCell(i, 2)), radarSymbols.get(radar.getCell(i, 3)),
radarSymbols.get(radar.getCell(i, 4)), radarSymbols.get(radar.getCell(i, 5)),
radarSymbols.get(radar.getCell(i, 6)), radarSymbols.get(radar.getCell(i, 7)),
radarSymbols.get(radar.getCell(i, 8)), radarSymbols.get(radar.getCell(i, 9))));
}
radarImage.append("\n");
System.out.print(radarImage);
}
}
34 changes: 34 additions & 0 deletions src/main/java/couse_project/Game.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package couse_project;

import java.io.IOException;

public class Game {
Copy link
Owner

Choose a reason for hiding this comment

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

Good! You introduced some abstractions, showed knowledge of Java Core and Java libraries. The code in general looks good, but there are a few cases where I'm not sure about the correct behavior. Also, your project lacks abstract classes/interfaces, and some architecture (the simplest is service/model). The interface is cool, I like it. playing is quite comfortable) I also appreciate automatic ship placement and description, it really helps to test.

Approved!

Player playerOne;
RandomIntelligence playerTwo;

public Game(Player playerOne, RandomIntelligence playerTwo) {
this.playerOne = playerOne;
this.playerTwo = playerTwo;
}

public void startGame() throws IOException, InterruptedException {
Displayer displayer = new Displayer();
while (true) {
displayer.clearScreen();
displayer.displayMap(playerOne.getMap());
displayer.displayRadar(playerOne.getRadar());
if (playerOne.shoot()) {
Copy link
Owner

Choose a reason for hiding this comment

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

That doesn't look good... usually methods should follow Single Responsibility Prinsiple - it should do only one thing, the thing it promises. In this case it's unexpected it returns boolean.

Have you tried finishing the game? I'm not sure it will not go to an infinite loop of recursion

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Oh, you're absolutely right. Seems that I forgot to add a condition for loop after last changes for this part:( Let me check

System.out.println("Отлично! Сделайте еще один ход:");
continue;
}

while (true) {
if (playerTwo.shoot()) {
System.out.println("Наш корабль попал под обстрел!");
} else {
break;
}
}
}
}
}
17 changes: 17 additions & 0 deletions src/main/java/couse_project/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package couse_project;

import java.io.IOException;

public class Main {

public static void main(String[] args) throws IOException, InterruptedException {
SeaMap mapOne = new SeaMap();
SeaMap mapTwo = new SeaMap();

Player playerOne = new Player("Lera", mapOne, mapTwo);
RandomIntelligence playerTwo = new RandomIntelligence(mapTwo, mapOne);

Game gameSession = new Game(playerOne, playerTwo);
gameSession.startGame();
}
}
72 changes: 72 additions & 0 deletions src/main/java/couse_project/Player.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package couse_project;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Scanner;

public class Player {
final private String name;
private final SeaMap map;
private final SeaMap radar;
final private String[] commands = new String[]{
"А1", "А2", "А3", "А4", "А5", "А6", "А7", "А8", "А9", "А10",
"Б1", "Б2", "Б3", "Б4", "Б5", "Б6", "Б7", "Б8", "Б9", "Б10",
"В1", "В2", "В3", "В4", "В5", "В6", "В7", "В8", "В9", "В10",
"Г1", "Г2", "Г3", "Г4", "Г5", "Г6", "Г7", "Г8", "Г9", "Г10",
"Д1", "Д2", "Д3", "Д4", "Д5", "Д6", "Д7", "Д8", "Д9", "Д10",
"Е1", "Е2", "Е3", "Е4", "Е5", "Е6", "Е7", "Е8", "Е9", "Е10",
"Ж1", "Ж2", "Ж3", "Ж4", "Ж5", "Ж6", "Ж7", "Ж8", "Ж9", "Ж10",
"З1", "З2", "З3", "З4", "З5", "З6", "З7", "З8", "З9", "З10",
"И1", "И2", "И3", "И4", "И5", "И6", "И7", "И8", "И9", "И10",
"К1", "К2", "К3", "К4", "К5", "К6", "К7", "К8", "К9", "К10",
};
final private HashMap<String, Integer> commandsMap = new HashMap<String, Integer>() {{
put("А", 0);
put("Б", 1);
put("В", 2);
put("Г", 3);
put("Д", 4);
put("Е", 5);
put("Ж", 6);
put("З", 7);
put("И", 8);
put("К", 9);
}};

public Player(String name, SeaMap map, SeaMap radar) {
this.name = name;
this.map = map;
this.radar = radar;
}

public boolean shoot() {
Scanner in = new Scanner(System.in);
System.out.println("Ваш ход:");
String command = in.nextLine();

if (Arrays.asList(commands).contains(command)) {
System.out.println(command.toUpperCase().charAt(0));

int x = commandsMap.get((Character.toString(command.toUpperCase().charAt(0))));
int y = Integer.parseInt(Character.toString(command.charAt(1))) - 1;

if (radar.isFreeEnterCell(x, y)) {
return radar.registerShot(x, y);
}
} else {
System.out.println("Ошибка формата данных");
System.out.println("Попробуйте, пожалуйста, ещё раз");
}

shoot();
return false;
Copy link
Owner

Choose a reason for hiding this comment

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

What's the case when it will return false? Seems impossible

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

let me check and fix if it is needed

}

public SeaMap getMap() {
return map;
}

public SeaMap getRadar() {
return radar;
}
}
26 changes: 26 additions & 0 deletions src/main/java/couse_project/RandomIntelligence.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package couse_project;

import java.util.Random;

public class RandomIntelligence {
private SeaMap map;
private final SeaMap radar;

public RandomIntelligence(SeaMap map, SeaMap radar) {
this.map = map;
this.radar = radar;
}

public boolean shoot() {
Random random = new Random();
int x = random.nextInt(10);
int y = random.nextInt(10);

if (radar.isFreeEnterCell(x, y)) {
return radar.registerShot(x, y);
}

shoot();
return false;
Copy link
Owner

Choose a reason for hiding this comment

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

Same here: What's the case when it will return false? Seems impossible

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

let me check and fix if it is needed

}
}
Loading