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
65 commits
Select commit Hold shift + click to select a range
968ecdd
homework 1
RogElenaYu Jul 10, 2021
c87c2cf
homework 1
RogElenaYu Jul 10, 2021
b28db62
homework 1
RogElenaYu Jul 10, 2021
eca987a
Revert "homework 1"
RogElenaYu Jul 10, 2021
185a73d
homework 1
RogElenaYu Jul 10, 2021
fd89188
homework 1
RogElenaYu Jul 10, 2021
4f1ae88
homework 2
RogElenaYu Jul 19, 2021
eb7c43e
homework 2
RogElenaYu Jul 21, 2021
e5f7d55
homework 2
RogElenaYu Jul 21, 2021
725e7c9
homework 2
RogElenaYu Jul 21, 2021
0505471
homework 2
RogElenaYu Jul 23, 2021
2bc879f
Merge remote-tracking branch 'origin/master'
RogElenaYu Jul 23, 2021
9278544
Merge branch 'master' into feature/RogElena
RogElenaYu Jul 23, 2021
c328b7f
homework 2
RogElenaYu Jul 23, 2021
0670db5
homework 2
RogElenaYu Jul 23, 2021
3a8540c
homework 2
RogElenaYu Jul 24, 2021
12441b5
homework 2
RogElenaYu Jul 24, 2021
4dd951a
homework 2
RogElenaYu Jul 24, 2021
e3c3e2e
homework 2
RogElenaYu Jul 24, 2021
a52d19f
homework 2
RogElenaYu Jul 26, 2021
f005ea6
homework 2
RogElenaYu Jul 26, 2021
84c1232
homework 2
RogElenaYu Jul 26, 2021
7deb319
homework 2
RogElenaYu Jul 26, 2021
9bd9911
homework 3
RogElenaYu Jul 26, 2021
e398fd1
homework 3
RogElenaYu Jul 28, 2021
c1d3ad1
homework 3
RogElenaYu Jul 28, 2021
d104c17
homework 3
RogElenaYu Jul 28, 2021
c6323ee
homework 3
RogElenaYu Aug 4, 2021
197fb99
homework 3
RogElenaYu Aug 4, 2021
ab8da27
homework 3
RogElenaYu Aug 4, 2021
aa81195
homework 4
RogElenaYu Aug 13, 2021
2d4c494
homework 4
RogElenaYu Aug 13, 2021
bbc435c
homework 4
RogElenaYu Aug 13, 2021
e3683a6
homework 4
RogElenaYu Aug 13, 2021
6c4adab
homework 5
RogElenaYu Sep 1, 2021
3f9bde2
homework 5
RogElenaYu Sep 1, 2021
8b63633
homework 5
RogElenaYu Sep 1, 2021
2010e03
homework 5
RogElenaYu Sep 1, 2021
f7f635e
homework 6
RogElenaYu Sep 10, 2021
1df8534
homework 7
RogElenaYu Sep 10, 2021
4e1749f
homework 7
RogElenaYu Sep 10, 2021
11182b5
homework 7
RogElenaYu Sep 11, 2021
0f833ad
homework 7
RogElenaYu Sep 11, 2021
41d2cc1
homework 7
RogElenaYu Sep 11, 2021
583fa73
homework 7
RogElenaYu Sep 12, 2021
0b42b57
homework 7
RogElenaYu Sep 12, 2021
b875eaa
homework 7
RogElenaYu Sep 12, 2021
6ac20cf
course_project
RogElenaYu Sep 19, 2021
26e2f7d
course_project
RogElenaYu Sep 19, 2021
b78d768
course_project
RogElenaYu Sep 20, 2021
f70eaba
course_project
RogElenaYu Sep 20, 2021
4fa9769
course_project
RogElenaYu Sep 20, 2021
4471afc
course_project
RogElenaYu Sep 20, 2021
e0bf43e
course_project
RogElenaYu Sep 20, 2021
13797bd
course_project
RogElenaYu Sep 20, 2021
4ea5f9b
course_project
RogElenaYu Sep 20, 2021
e4b4700
CollisionClass
RogElenaYu Sep 22, 2021
80f0e76
SingletonTest
RogElenaYu Sep 22, 2021
391f684
MutableClass
RogElenaYu Sep 23, 2021
312d1a0
SeaBattle
RogElenaYu Sep 23, 2021
cad8cde
SeaBattle
RogElenaYu Sep 23, 2021
4665d44
SeaBattle
RogElenaYu Sep 23, 2021
ead5e91
ImmutableClass
RogElenaYu Sep 23, 2021
5c7fcf1
ImmutableClass
RogElenaYu Sep 23, 2021
1aa4774
ImmutableClass
RogElenaYu Sep 24, 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
19 changes: 17 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
# Java Core June 2021

## *Nikolaev Artem*
## *Rog Elena*

| 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. ConsolePrinter | [Console printer](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/RogElena/src/main/java/homework_1) | The app that reads input arguments and prints them, until "error" argument |
| HW2.1. TrafficLight | [Traffic light](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/RogElena/src/main/java/homework_2/traffic_light) | traffic_light |
| HW2.2. PyramidPrinter | [Pyramid printer](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/RogElena/src/main/java/homework_2/pyramid_printer) | pyramid_printer |
| HW2.3. RandomCharsTable | [Random chars table](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/RogElena/src/main/java/homework_2/random_chars_table) | random_chars_table |
| HW3. ImmutableClass | [Immutable class](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/RogElena/src/main/java/homework_3) | immutable_class |
| HW4.1. CustomFileReader | [Custom file reader](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/RogElena/src/main/java/homework_4/custom_file_reader) | custom_file_reader |
| HW4.2. Singleton | [Singleton](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/RogElena/src/main/java/homework_4/singleton) | singleton |
| HW4.3. CustomAnnotation | [Custom annotation](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/RogElena/src/main/java/homework_4/custom_annotation) | custom_annotation |
| HW5.1. PowerOfNumber | [Power of number](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/RogElena/src/main/java/homework_5/power_of_number) | power_of_number
| HW5.2. CustomRegexMatcher | [Custom regex matcher](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/RogElena/src/main/java/homework_5/custom_regex_matcher) | custom_regex_matcher
| HW6. MapProblemsGenerator | [Map problems generator](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/RogElena/src/main/java/homework_6/map_problems_generator) | map_problems_generator
| HW7. KittenToCatFunction | [Kitten to cat function](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/RogElena/src/main/java/homework_7/kitten_to_cat_function) | kitten_to_cat_function
| CourseProject | [Course project](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/RogElena/src/main/java/course_project) | course_project

| tests | [Test classes](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/RogElena/src/test/java) | Test classes |
[Link to markdown giude](https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet)

[Link to codingbat profile](https://codingbat.com/done?user=rog.elena.yu@gmail.com&tag=9660623200)
7 changes: 6 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,13 @@ 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 {
useJUnitPlatform()
}
}
299 changes: 299 additions & 0 deletions src/main/java/course_project/Field.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,299 @@
package course_project;

import lombok.Data;
import lombok.Value;

import java.util.*;

@Data
public class Field {
Copy link
Owner

Choose a reason for hiding this comment

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

Good! The game is nice to play, the interface is good, playing is easy. Approved!

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Thanks for the science and advices!

enum cellStatus {
CLEAN,
TOUCHING,
SHIP,
WOUNDED,
SUNK,
MISS
}
private final static int fieldLength = 10;
private static final HashMap<cellStatus, Character> cellStatusSign = new HashMap<cellStatus, Character>(){{
put(cellStatus.CLEAN, '~');
put(cellStatus.TOUCHING, '~');
put(cellStatus.SHIP, 'U');
put(cellStatus.WOUNDED, 'x');
put(cellStatus.SUNK, 'x');
put(cellStatus.MISS, 'o');
}};
public static final String ANSI_RED = "\u001B[31m";
public static final String ANSI_RESET = "\u001B[0m";
private cellStatus[][] cells = new cellStatus[fieldLength][fieldLength];
private List<Ship> listOfShips = new ArrayList<>();

public static int[] stringToCell(String str) {
char columnNumberChar = str.substring(0, 1).charAt(0);
int columnNumber;

if ((int) columnNumberChar > 74) {
return null;
} else {
columnNumber = (int) columnNumberChar - 64;
}
String lineNumberStr = str.substring(1);

try {
int lineNumber = Integer.parseInt(lineNumberStr);
if (lineNumber < 1 || lineNumber > 10) {
return null;
}
return new int[]{lineNumber - 1, columnNumber - 1};
} catch (NumberFormatException e) {
return null;
}
}

public Field() {
for (int i = 0; i < fieldLength; i++) {
for (int j = 0; j < fieldLength; j++) {
cells[j][i] = cellStatus.CLEAN;
}
}
}

public boolean createShip(int shipLength, String[] shipCoordinates) {
int[] beginningCell;
int[] endCell;

if (shipLength == 1 && (shipCoordinates.length != 1 || shipCoordinates[0] == null)) {
System.out.println("Error! Wrong length of the ship! Try again.\n");
return false;
} else if (shipLength > 1 && (shipCoordinates.length != 2
|| shipCoordinates[0] == null
|| shipCoordinates[1] == null)) {
System.out.println("Error! Wrong length of the ship! Try again.\n");
return false;
}

beginningCell = stringToCell(shipCoordinates[0]);
endCell = shipLength == 1 ? beginningCell : stringToCell(shipCoordinates[1]);

if (beginningCell == null || endCell == null) {
System.out.println("Error! Wrong enter! Try again.\n");
return false;
}

if (checkShip(shipLength, beginningCell, endCell)) {
drawShip(shipLength, beginningCell, endCell);
return true;
}
return false;
}

private boolean checkShip(int shipLength, int[] beginningCell, int[] endCell) {
if (!cellWithinFieldBoundaries(beginningCell) || !cellWithinFieldBoundaries(endCell)) {
System.out.println("Error! Out of field bounds! Try again.\n");
return false;
} else if (!Ship.shipIsInAStraightLine(shipLength, beginningCell, endCell)) {
System.out.println("Error! The ship is not in a straight line! Try again.\n");
return false;
} else if (!Ship.rightShipLength(shipLength, beginningCell, endCell)) {
System.out.println("Error! Wrong length of the ship! Try again.\n");
return false;
} else if (!cellsAbleToFill(beginningCell, endCell)) {
System.out.println("Error! This cell is not able to be filled with a ship. Try again.\n");
return false;
}
return true;
}

private boolean cellWithinFieldBoundaries(int[] cell) {
return cell[0] <= 9 && cell[1] <= 9 && cell[0] >= 0 && cell[1] >= 0;
}

private boolean cellsAbleToFill(int[] beginningCell, int[] endCell) {
int[][] shipCells = getShipCells(beginningCell, endCell);
if (shipCells != null) {
for (int[] cell : shipCells) {
if (!cellCleanAndNotTouching(cell)) {
return false;
}
}
}
return true;
}

private int[][] getShipCells(int[] beginningCell, int[] endCell) {
int[][] shipCells;

if (beginningCell[1] == endCell[1]) { //horizontal
shipCells = new int[Math.abs(beginningCell[0] - endCell[0]) + 1][3];
int lineMin = Math.min(beginningCell[0], endCell[0]);
int lineMax = Math.max(beginningCell[0], endCell[0]);
for (int i = 0; i <= lineMax - lineMin; i++) {
shipCells[i][0] = lineMin + i;
shipCells[i][1] = beginningCell[1];
shipCells[i][2] = 0; //status
}
return shipCells;
} else if (beginningCell[0] == endCell[0]) { //vertical
shipCells = new int[Math.abs(beginningCell[1] - endCell[1]) + 1][3];
int columnMin = Math.min(beginningCell[1], endCell[1]);
int columnMax = Math.max(beginningCell[1], endCell[1]);
for (int i = 0; i <= columnMax - columnMin; i++) {
shipCells[i][0] = beginningCell[0];
shipCells[i][1] = columnMin + i;
shipCells[i][2] = 0; //status
}
return shipCells;
}
return null;
}

private boolean cellCleanAndNotTouching(int[] cell) {
int line = cell[0];
int column = cell[1];

if (cells[line][column] != cellStatus.CLEAN) {
return false;
}

ArrayList<int[]> adjacentCells = getAdjacentCells(cell);
for (int[] adjacentCell : adjacentCells) {
int adjacentLine = adjacentCell[0];
int adjacentColumn = adjacentCell[1];

if (cells[adjacentLine][adjacentColumn] == cellStatus.SHIP) {
return false;
}
}
return true;
}

private ArrayList<int[]> getAdjacentCells(int[] cell) {
int column = cell[0];
int line = cell[1];

ArrayList<int[]> adjacentCells = new ArrayList<>();
for (int i = line - 1; i <= line + 1; i++) {
for (int j = column - 1; j <= column + 1; j++) {
int[] checkingCell = new int[]{j, i};
if (cellWithinFieldBoundaries(checkingCell) && !Arrays.equals(cell, checkingCell)) {
adjacentCells.add(checkingCell);
}
}
}
return adjacentCells;
}

public void drawTestShips(int[][][] data) {
for (int[][] arr : data) {
drawShip(arr[0][0], arr[1], arr[2]);
}
}

private void drawShip(int shipLength, int[] beginningCell, int[] endCell) {
int[][] shipCells = getShipCells(beginningCell, endCell);
listOfShips.add(new Ship(shipLength, shipCells));

if (shipCells != null) {
for (int[] cell : shipCells) {
int line = cell[0];
int column = cell[1];
cells[line][column] = cellStatus.SHIP;

ArrayList<int[]> adjacentCells = getAdjacentCells(cell);
for (int[] adjacentCell : adjacentCells) {
int adjacentColumn = adjacentCell[1];
int adjacentLine = adjacentCell[0];

if (cells[adjacentLine][adjacentColumn] != cellStatus.SHIP) {
cells[adjacentLine][adjacentColumn] = cellStatus.TOUCHING;
}
}
}
}
}

boolean allShipsSunk() {
for (Ship ship : listOfShips) {
if (ship.getShipStatus() != Ship.status.SUNK) {
return false;
}
}
return true;
}

String shotResult(int[] shotCell) {
int line = shotCell[0];
int column = shotCell[1];
cellStatus status = getCellStatus(line, column);

if (status == cellStatus.SHIP) {
cells[line][column] = cellStatus.WOUNDED;
Ship hitShip = getHitShip(shotCell);
if (hitShip != null && hitShip.getShipStatus() == Ship.status.SUNK) {
setSunkStatusToCells(hitShip);
return "sunk";
}
return "hit";
} else if (status == cellStatus.MISS || status == cellStatus.WOUNDED || status == cellStatus.SUNK) {
return "repeat";
} else {
cells[line][column] = cellStatus.MISS;
}
return "miss";
}

private cellStatus getCellStatus(int line, int column) {
return cells[line][column];
}

private Ship getHitShip(int[] shotCell) {
for (Ship ship : listOfShips) {
if (ship.cellBelongsToShip(shotCell)) {
ship.setShipStatus(Ship.status.HIT);
ship.setCellHit(shotCell);
return ship;
}
}
return null;
}

private void setSunkStatusToCells(Ship ship) {
int[][] shipCells = ship.getShipCells();

for (int[] shipCell : shipCells) {
int line = shipCell[0];
int column = shipCell[1];
cells[line][column] = cellStatus.SUNK;
}
}

void printField(boolean showShips) {
System.out.print(" ");
char c = 0x0041;

for (int i = 1; i <= fieldLength; i++) {
System.out.print(c + " ");
c += 1;
}
System.out.print("\n");

for (int i = 0; i < fieldLength; i++) {
System.out.print(i < 9 ? " " + (i + 1) : " " + (i + 1));
for (int j = 0; j < fieldLength; j++) {
System.out.print(" ");
if (!showShips && (cells[i][j] == cellStatus.SHIP || cells[i][j] == cellStatus.TOUCHING)) {
System.out.print(cellStatusSign.get(cellStatus.CLEAN));
} else if (cells[i][j] == cellStatus.SUNK) {
System.out.print(ANSI_RED + cellStatusSign.get(cells[i][j]) + ANSI_RESET);
} else {
System.out.print(cellStatusSign.get(cells[i][j]));
}
}
System.out.println();
}
System.out.print("\n");
}

}

Loading