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
57 commits
Select commit Hold shift + click to select a range
fbe96c1
Update Main
MariWrnk Jul 5, 2021
1397302
Gitignore update
MariWrnk Jul 5, 2021
e0cce8b
Gitignore update 2
MariWrnk Jul 5, 2021
43970c3
Add homework 1
MariWrnk Jul 6, 2021
abff6f7
Update
MariWrnk Jul 10, 2021
1a59785
Update README.md
MariWrnk Jul 10, 2021
b4298b6
Style fix
MariWrnk Jul 11, 2021
abb0216
Pyramid Printer
MariWrnk Jul 15, 2021
3a09f3f
Pyramid Printer done
MariWrnk Jul 15, 2021
aa7e294
Traffic light
MariWrnk Jul 15, 2021
b62a3a8
Random Chars Table
MariWrnk Jul 15, 2021
0dec122
README update
MariWrnk Jul 15, 2021
bdf3eff
Traffic Light add mode
MariWrnk Jul 16, 2021
ca9fc3e
README update
MariWrnk Jul 16, 2021
5730a8d
Merge remote-tracking branch 'origin/master' into Feature/MariaVoronko
MariWrnk Jul 24, 2021
e74728a
PyramidPrinter hw3
MariWrnk Jul 25, 2021
bcf657b
RandomCharsTable add classes
MariWrnk Jul 25, 2021
8e59c3a
PyramidPrinter error message fix
MariWrnk Jul 25, 2021
ea61e8e
PyramidPrinter tests fix
MariWrnk Jul 26, 2021
fc31c5c
RandomCharsTable tests
MariWrnk Jul 26, 2021
c582fe9
PyramidPrinter tests fix
MariWrnk Jul 26, 2021
38d7e09
TrafficLight tests
MariWrnk Jul 26, 2021
eb53c5e
Add link to codingbat
MariWrnk Jul 26, 2021
2137346
Immutable class
MariWrnk Jul 26, 2021
f671dbc
RandomCharsTableCreator update
MariWrnk Jul 26, 2021
ac5c12c
TrafficLight add ExtraMode
MariWrnk Jul 28, 2021
81e2f7c
README add HW3
MariWrnk Jul 28, 2021
d02a92b
HW2 style fix
MariWrnk Jul 28, 2021
71a7349
README add hw2 tests link
MariWrnk Jul 28, 2021
ff8a35c
HW2 tests update
MariWrnk Jul 28, 2021
b7edb8e
HW4 CustomFileReader done
MariWrnk Aug 12, 2021
23cfe1a
HW4 CustomFileReader tests
MariWrnk Aug 12, 2021
4e4bcb7
HW4 Singleton done
MariWrnk Aug 12, 2021
3de36b4
HW4 CustomAnnotation done
MariWrnk Aug 14, 2021
bf3d4c6
README add HW4
MariWrnk Aug 14, 2021
84e00e4
HW2 style update
MariWrnk Aug 14, 2021
7e5b2a3
HW5 PowerOfNumber done
MariWrnk Aug 22, 2021
123c6b4
HW5 PowerOfNumber fix
MariWrnk Aug 22, 2021
73191e1
HW5 CustomRegexMatcher done
MariWrnk Aug 22, 2021
36a3eee
HW5 PowerOfNumberTest fix
MariWrnk Aug 22, 2021
4f126e8
HW6 done
MariWrnk Sep 2, 2021
03f1278
README.md update
MariWrnk Sep 2, 2021
934190a
HW2 style fix
MariWrnk Sep 2, 2021
a7bcd75
HW5 PowerOfNumber validation fix
MariWrnk Sep 2, 2021
171ccfb
HW7 done
MariWrnk Sep 12, 2021
2297a5d
README.md update
MariWrnk Sep 12, 2021
59a2f5e
SeaBattle done
MariWrnk Sep 16, 2021
b4d6100
SeaBattle style fix
MariWrnk Sep 18, 2021
965218d
SeaBattle Tests done
MariWrnk Sep 18, 2021
0db95e0
README.md update
MariWrnk Sep 18, 2021
b1704c6
SeaBattle style fix
MariWrnk Sep 18, 2021
0bdfe50
SeaBattle tests add
MariWrnk Sep 18, 2021
4ef8478
HW3 bug fix
MariWrnk Sep 21, 2021
c5de362
SeaBattle interface fix
MariWrnk Sep 21, 2021
c3c7561
SeaBattle print field
MariWrnk Sep 22, 2021
bd25d14
SeaBattle bug fix
MariWrnk Sep 23, 2021
6b6e7a2
SeaBattle bug fix
MariWrnk 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
23 changes: 21 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,28 @@
# Java Core June 2021

## *Nikolaev Artem*
## *Maria Voronko*

| 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/MariaVoronko/src/main/java/homework_1/HomeWork1.java) | 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/MariaVoronko/src/main/java/homework_2/traffic_light) | The app that reads a number of seconds from the beginning of the day and prints traffic light color at that moment. |
| HW2 | [Pyramid Printer](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/MariaVoronko/src/main/java/homework_2/pyramid_printer/PyramidPrinter.java) | The app that reads pyramid size and prints pyramid of 'x' of given size. |
| HW2 | [Random Chars Table](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/MariaVoronko/src/main/java/homework_2/random_chars_table/RandomCharsTable.java) | The app that prints a table of random chars and finds odd or even letters in it. |
| HW2 | [Tests 2](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/MariaVoronko/src/test/java/homework_2) | Tests for homework 2. |
| HW3 | [Immutable Class](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/MariaVoronko/src/main/java/homework_3/ImmutableCat.java) | To create an immutable class and write its requirements in comments. |
| HW4 | [Custom File Reader](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/MariaVoronko/src/main/java/homework_4/custom_file_reader) | The app that reads data from file and prints it to the console. |
| HW4 | [Singleton](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/MariaVoronko/src/main/java/homework_4/singleton) | Singleton design pattern implementation. |
| HW4 | [Custom Annotation](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/MariaVoronko/src/main/java/homework_4/custom_annotation) | Annotation. |
| HW4 | [Tests 4](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/MariaVoronko/src/test/java/homework_4) | Tests for homework 4. |
| HW5 | [CustomRegexMatcher](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/MariaVoronko/src/main/java/homework_5/custom_regex_mathcer) | The app that checks if input string matches pattern. |
| HW5 | [PowerOfNumber](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/MariaVoronko/src/main/java/homework_5/power_of_number) | The app that calculates a number in a power using recursion. |
| HW5 | [Tests 5](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/MariaVoronko/src/test/java/homework_5) | Tests for homework 5. |
| HW6 | [MapProblemsCollisionGenerator & MapProblemMutableGenerator](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/MariaVoronko/src/main/java/homework_6) | Classes that create problems in HashMap. |
| HW7 | [KittenToCatFunction](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/MariaVoronko/src/main/java/homework_7) | The functional interface with an abstract method that turns a Kitten to a Cat. |
| HW7 | [Tests 7](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/MariaVoronko/src/test/java/homework_7) | Tests for homework 7. |
| Course Project | [Sea Battle](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/MariaVoronko/src/main/java/course_project) | The app tht plays Sea Battle game with user. |
| Course Project | [Tests](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/MariaVoronko/src/test/java/course_project) | Tests for Sea Battle. |

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

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

dependencies {
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'

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")

testImplementation "org.mockito:mockito-core:3.+"
}

test {
Expand Down
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.GameService;

public class Main {

public static void main(String[] args) {
new GameService().run();
Copy link
Owner

Choose a reason for hiding this comment

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

image

Copy link
Owner

Choose a reason for hiding this comment

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

I really don't understand what happened. It seems the interface needs refactoring. Also, it would be better if you add visual field representation

Copy link
Owner

Choose a reason for hiding this comment

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

There's much repetitive code, and it makes reading code harder

Copy link
Owner

Choose a reason for hiding this comment

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

image

Copy link
Owner

Choose a reason for hiding this comment

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

Here a bug again...
image

Copy link
Owner

Choose a reason for hiding this comment

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

But the interface was refactored, although I couldn't continue to play

Copy link
Owner

Choose a reason for hiding this comment

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

Good, I didn't find any bugs! As optional improvement, marking cells around a sunk ship as hit is a good idea. Approved!

}

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

public enum CellStatus {

FREE,
HAS_SHIP,
VISITED,
NOT_VISITED

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

public enum Response {

MISSED,
HIT,
KILLED,
VISITED

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

import course_project.enums.CellStatus;

import static course_project.enums.CellStatus.*;

public class Cell {

private final CellStatus status;
private final boolean gotShip;

public Cell(CellStatus status, boolean gotShip) {
this.status = status;
this.gotShip = gotShip;
}

public CellStatus getStatus() {
return status;
}

public boolean hadShip() {
return gotShip;
}

@Override
public String toString() {
if (status == VISITED) {
if(gotShip) {
return "X";
}
return "e";
}
return "-";
}
}
65 changes: 65 additions & 0 deletions src/main/java/course_project/objects/GameData.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package course_project.objects;

import course_project.enums.CellStatus;
import course_project.enums.Response;
import course_project.objects.Move;
import course_project.objects.fields.ComputerField;
import course_project.objects.fields.Field;
import course_project.objects.fields.UserField;
import course_project.services.ShipLocationGenerator;

import java.util.List;

public class GameData {

private final ComputerField computerField;
private final UserField userField;
private int computerShips = 10;
private int userShips = 10;

public GameData(ShipLocationGenerator generator) {
computerField = generator.generateShips();
userField = new UserField();
}

public ComputerField getComputerField() {
return computerField;
}

public Field getUserField() {
return userField;
}

public int getComputerShips() {
return computerShips;
}

public int getUserShips() {
return userShips;
}

public Response getResponseFromComputer(Move move) {
Response response = computerField.getResponse(move);
if (response == Response.KILLED) {
computerShips--;
}
return response;
}

public void saveComputerMoveResult(Response response, Move move, List<Move> lastHit) {
if (response == Response.KILLED) {
userField.setVisitedAroundShip(lastHit);
userShips--;
}
userField.setCellStatus(move.getX(), move.getY(), CellStatus.VISITED);
}

public boolean isUserFleetEmpty() {
return userShips == 0;
}

public boolean isComputerFleetEmpty() {
return computerShips == 0;
}

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

public class Move {

private final int x;
private final int y;

public Move(int x, int y) {
this.x = x;
this.y = y;
}

public Move(String move) {
x = move.charAt(0) - 65;
y = Integer.parseInt(move.substring(1)) - 1;
}

public int getX() {
return x;
}

public int getY() {
return y;
}

@Override
public String toString() {
return (char) (x + 65) + "" + (y + 1);
}
}
85 changes: 85 additions & 0 deletions src/main/java/course_project/objects/fields/ComputerField.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package course_project.objects.fields;

import course_project.enums.CellStatus;
import course_project.enums.Response;
import course_project.objects.Cell;
import course_project.objects.Move;
import course_project.objects.fields.Field;

import java.util.Arrays;

public class ComputerField extends Field {

public ComputerField() {
for (Cell[] row : table) {
Arrays.fill(row, new Cell(CellStatus.FREE, false));
}
}

public Response getResponse(Move move) {
if (table[move.getX()][move.getY()].getStatus() == CellStatus.FREE) {
table[move.getX()][move.getY()] = new Cell(CellStatus.VISITED, false);
return Response.MISSED;
}
if (table[move.getX()][move.getY()].getStatus() == CellStatus.VISITED) {
return Response.VISITED;
}
if (table[move.getX()][move.getY()].getStatus() == CellStatus.HAS_SHIP) {
table[move.getX()][move.getY()] = new Cell(CellStatus.VISITED, true);
int x = move.getX();
int y = move.getY();
if (hasShipAround(x, y)) {
return Response.HIT;
}
int prevX = -1;
int prevY = -1;
while (true) {
if (x != 0 && x - 1 != prevX && table[x - 1][y].getStatus() == CellStatus.VISITED && table[x - 1][y].hadShip()) {
prevX = x;
x--;
continue;
}
if (x + 1 < 10 && x + 1 != prevX && table[x + 1][y].getStatus() == CellStatus.VISITED && table[x + 1][y].hadShip()) {
prevX = x;
x++;
continue;
}
if (y != 0 && y - 1 != prevY && table[x][y - 1].getStatus() == CellStatus.VISITED && table[x][y - 1].hadShip()) {
prevY = y;
y--;
continue;
}
if (y + 1 < 10 && y + 1 != prevY && table[x][y + 1].getStatus() == CellStatus.VISITED && table[x][y + 1].hadShip()) {
prevY = y;
y++;
continue;
}
if (hasShipAround(x, y)) {
return Response.HIT;
}
break;
}
}
return Response.KILLED;
}

private boolean hasShipAround(int x, int y) {
return (x != 0 && table[x - 1][y].getStatus() == CellStatus.HAS_SHIP)
|| (x + 1 < table.length && table[x + 1][y].getStatus() == CellStatus.HAS_SHIP)
|| (y != 0 && table[x][y - 1].getStatus() == CellStatus.HAS_SHIP)
|| (y + 1 < table[0].length && table[x][y + 1].getStatus() == CellStatus.HAS_SHIP);
}

public void printField() {
System.out.println("Computer's field:");
System.out.println(" 1 2 3 4 5 6 7 8 9 10");
for (int i = 0; i < 10; i++) {
System.out.print((char) (i + 65));
System.out.println(Arrays.toString(table[i]));
}
System.out.println("\tX - computer's ship you've destroyed.");
System.out.println("\te - empty cell you've checked.");
System.out.println("\t- - a cell you've not checked.");
}

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

import course_project.enums.CellStatus;
import course_project.objects.Cell;

import java.util.Arrays;

public abstract class Field {

protected final Cell[][] table = new Cell[10][10];

public CellStatus getCellStatus(int x, int y) {
return table[x][y].getStatus();
}

public void setCellStatus(int x, int y, CellStatus newStatus) {
if (newStatus == CellStatus.HAS_SHIP) {
table[x][y] = new Cell(newStatus, true);
} else {
table[x][y] = new Cell(newStatus, false);
}
}

public void printField() {
for (Cell[] row : table) {
System.out.println(Arrays.toString(row));
}
}
}
65 changes: 65 additions & 0 deletions src/main/java/course_project/objects/fields/UserField.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package course_project.objects.fields;

import course_project.enums.CellStatus;
import course_project.objects.Cell;
import course_project.objects.Move;
import course_project.objects.fields.Field;

import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class UserField extends Field {

public UserField() {
for (Cell[] row : table) {
Arrays.fill(row, new Cell(CellStatus.NOT_VISITED, false));
}
}

public void setVisitedAroundShip(List<Move> list) {
boolean isSameX = false;
if (list.size() == 1 || list.get(0).getX() == list.get(1).getX()) {
isSameX = true;
}
if (isSameX) {
Comparator<Move> byY = Comparator.comparing(Move::getY);
list.sort(byY);
int x = list.get(0).getX();
int y1 = list.get(0).getY();
int y2 = list.get(list.size() - 1).getY();
for (int y = y1 - 1; y <= y2 + 1; y++) {
if (!(0 <= y && y < 10)) {
continue;
}
setCellStatus(x, y, CellStatus.VISITED);
if (x - 1 >= 0) {
setCellStatus(x - 1, y, CellStatus.VISITED);
}
if (x + 1 < 10) {
setCellStatus(x + 1, y, CellStatus.VISITED);
}
}
} else {
Comparator<Move> byX = Comparator.comparing(Move::getX);
list.sort(byX);
int y = list.get(0).getY();
int x1 = list.get(0).getX();
int x2 = list.get(list.size() - 1).getX();
for (int x = x1 - 1; x <= x2 + 1; x++) {
if (!(0 <= x && x < 10)) {
continue;
}
setCellStatus(x, y, CellStatus.VISITED);
if (y - 1 >= 0) {
setCellStatus(x, y - 1, CellStatus.VISITED);
}
if (y + 1 < 10) {
setCellStatus(x, y + 1, CellStatus.VISITED);
}
}
}
}

}
Loading