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
36 commits
Select commit Hold shift + click to select a range
08cee92
aaaa
Sotheres Jul 5, 2021
35505cc
v1
Sotheres Jul 5, 2021
212b306
Homework 1
Sotheres Jul 6, 2021
0417ac5
Merge remote-tracking branch 'origin/master' into Feature/NikitaChertov
Sotheres Jul 8, 2021
d4423dc
Merge remote-tracking branch 'origin/master' into feature/ChertovNikita
Sotheres Jul 8, 2021
bc145db
Homework 1
Sotheres Jul 8, 2021
43ca781
Homework 1
Sotheres Jul 8, 2021
a4d8a15
PyramidPrinter
Sotheres Jul 13, 2021
e1022da
RandomCharsTable
Sotheres Jul 14, 2021
7cf7d50
TrafficLight
Sotheres Jul 14, 2021
5c5c468
update
Sotheres Jul 14, 2021
e41d953
LocalTime version
Sotheres Jul 17, 2021
6f2619b
HW2 refactoring update
Sotheres Jul 23, 2021
1050bb6
Merge remote-tracking branch 'origin/master' into feature/ChertovNikita
Sotheres Jul 23, 2021
f0faba8
HW2 unit test coverage
Sotheres Jul 24, 2021
7bcda0b
Readme update
Sotheres Jul 25, 2021
97c5046
RandomCharsTable whitespaces removed
Sotheres Jul 25, 2021
bc748fc
HW3 added
Sotheres Jul 26, 2021
b65fd74
HW4 singleton added
Sotheres Aug 12, 2021
11bab80
HW4 custom annotation added
Sotheres Aug 12, 2021
0e3bb71
HW4 cfl
Sotheres Aug 13, 2021
fe8e815
HW4 CustomFileReader added
Sotheres Aug 15, 2021
8fdd9e8
HW4 CustomFileReader tests update
Sotheres Aug 16, 2021
f5c1ae7
Delete defaultInput.txt
Sotheres Aug 16, 2021
e2771a5
HW4 CustomFileReader update
Sotheres Aug 17, 2021
e4e129a
Merge remote-tracking branch 'origin/Feature/ChertovNikita' into Feat…
Sotheres Aug 17, 2021
3d2a834
HW5 PowerOfNumber added
Sotheres Aug 20, 2021
f117f48
HW5 PowerOfNumber update
Sotheres Aug 20, 2021
b007ce9
HW5 CustomRegexMatcher added
Sotheres Aug 23, 2021
beb9349
HW6 added.
Sotheres Aug 30, 2021
66684a2
Readme update.
Sotheres Aug 30, 2021
62d5f8b
HW7 added
Sotheres Sep 15, 2021
a168937
Course project added
Sotheres Sep 16, 2021
561da00
Course project update
Sotheres Sep 17, 2021
726df11
Course project tests update
Sotheres Sep 18, 2021
abdbf4b
Course project tests update2
Sotheres Sep 18, 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
22 changes: 19 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,25 @@
# Java Core June 2021

## *Nikolaev Artem*
## *Chertov Nikita*

| 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/ChertovNikita/src/main/java/homework_1) | The app that reads input arguments and prints them, until "error" argument |
| HW2.1 | [Pyramid Printer](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/ChertovNikita/src/main/java/homework_2/pyramid_printer) | The app prints pyramid of entered height with "x" symbols|
| HW2.2 | [Traffic Light](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/ChertovNikita/src/main/java/homework_2/traffic_light) | The app shows traffic light corresponding to entered time of the day|
| HW2.3 | [Random Chars Table](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/ChertovNikita/src/main/java/homework_2/random_chars_table) | The app prints table of randomized letters and outputs list of odd/even letters in it|
| HW2 Tests | [Tests](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/ChertovNikita/src/test/java/homework_2_test/) | Tests for homework 2|
| HW3 | [Immutable Class](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/ChertovNikita/src/main/java/homework_3) | Cafe class that shows immutable class' key features |
| HW4.1 | [Custom Annotation](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/ChertovNikita/src/main/java/homework_4/custom_annotation) | Default User Info declares custom annotation and Profile class uses it|
| HW4.2 | [Custom File Reader](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/ChertovNikita/src/main/java/homework_4/custom_file_reader) | The app reads text from file and outputs it without dots and commas|
| HW4.3 | [Singleton](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/ChertovNikita/src/main/java/homework_4/singleton) | Theater class as an example of singleton pattern|
| HW4 Tests | [Tests](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/ChertovNikita/src/test/java/homework_4_test/) | Tests for homework 4|
| HW5.1 | [Power of number](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/ChertovNikita/src/main/java/homework_5/power_of_number) | The app that raises a number to a power |
| HW5.2 | [Custom regex matcher](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/ChertovNikita/src/main/java/homework_5/custom_regex_matcher) | The app that checks input text for IP |
| HW5 Tests | [Tests](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/ChertovNikita/src/test/java/homework_5_test/) | Tests for homework 5|
| HW6 | [Map problems generator](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/ChertovNikita/src/main/java/homework_6/) | Class with subclasses that create collisions and retrieval problems in hashmap |
| HW6 Tests | [Tests](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/ChertovNikita/src/test/java/homework_6_test/) | Tests for homework 6|
| HW7 | [Functional Interface](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/ChertovNikita/src/main/java/homework_7/) | Functional interface with method to transform Kitten and subclasses to Cat and subclasses |
| CP | [Course project](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/ChertovNikita/src/main/java/course_project/) | Sea battle game |

[Link to markdown giude](https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet)
[Link to my CodingBat](https://codingbat.com/done?user=sotheres@gmail.com&tag=6008519258)
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
8 changes: 8 additions & 0 deletions src/main/java/course_project/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package course_project;

public class Main {

public static void main(String[] args) {
new SeaBattle().playVsComputer();
Copy link
Contributor

@ArtemNikolaev1 ArtemNikolaev1 Sep 20, 2021

Choose a reason for hiding this comment

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

Good use of abstractions, thorough knowledge of Java Core aspects and libraries. Well done!

To make better: Quite difficult to play, during ship placement the field does not show my placed ships. Also, you could Introduce random ship placement. And void methods are not thread-safe and easy to test, better use return values

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

import course_project.components.Coordinate;
import course_project.components.PlayingField;
import course_project.util.PlayingFieldPrinter;
import course_project.util.user_input_reader.UserInputReader;
import course_project.util.user_input_reader.UserShotInputReader;
import course_project.util.ship_placer.ComputerShipPlacer;
import course_project.util.ship_placer.UserShipPlacer;

import java.util.Random;
import java.util.Scanner;

public class SeaBattle {

final PlayingField playersField = new PlayingField();
final PlayingField enemyField = new PlayingField();
final PlayingFieldPrinter printer = new PlayingFieldPrinter();
private final Scanner scanner = new Scanner(System.in);
private final Random randomGenerator = new Random();
private boolean isHit;

public void playVsComputer() {
placePlayersShips();
placeComputerShips();
makeMoves();
defineWinner();
closeInput();
Comment on lines +24 to +28
Copy link
Contributor

Choose a reason for hiding this comment

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

Opt: these methods should have return values. It helps to test them separately. Better not to use void methods.

Copy link
Contributor

Choose a reason for hiding this comment

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

Opt: ship placement is difficult to understand (h1 hor on empty field does not work)

}

void placePlayersShips() {
new UserShipPlacer(scanner).placeShips(playersField);
}
Comment on lines +31 to +33
Copy link
Contributor

Choose a reason for hiding this comment

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

Opt: It does not make sense to put oneliner into a separate method


void placeComputerShips() {
new ComputerShipPlacer().placeShips(enemyField);
}

void makeMoves() {
UserInputReader inputReader = new UserShotInputReader(scanner);
while (!playersField.getPlayersShips().isEmpty() && !enemyField.getPlayersShips().isEmpty()) {
playersMove(inputReader);
computersMove();
}
}

private void playersMove(UserInputReader inputReader) {
do {
System.out.println("Enemy field:");
printer.printShotCells(enemyField);
Coordinate shotCoordinate = inputReader.getPointFromInput();
playersShot(shotCoordinate);
delayPrint();
} while (isHit && !enemyField.getPlayersShips().isEmpty());
}

private void playersShot(Coordinate point) {
isHit = enemyField.placeShot(point);
}

private void delayPrint() {
try {
Thread.sleep(700);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}

private void computersMove() {
if (!enemyField.getPlayersShips().isEmpty()) {
do {
enemyShot();
delayPrint();
System.out.println("Your field:");
printer.printField(playersField);
} while (isHit && !playersField.getPlayersShips().isEmpty());
}
}

private void enemyShot() {
Coordinate cell = createCellToShoot();
System.out.println("Computer shoot at : " + cell);
isHit = playersField.placeShot(cell);
}

private Coordinate createCellToShoot() {
Coordinate cellToShoot;
do {
int x = randomGenerator.nextInt(10);
int y = randomGenerator.nextInt(10);
cellToShoot = new Coordinate(x, y);
} while (playersField.isCellShot(cellToShoot));

return cellToShoot;
}

private void closeInput() {
scanner.close();
}

void defineWinner() {
if (enemyField.getPlayersShips().isEmpty()) {
System.out.println("Player won!");
} else {
System.out.println("Computer won!");
}
}
}
45 changes: 45 additions & 0 deletions src/main/java/course_project/components/Coordinate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package course_project.components;

public final class Coordinate {

private final int x;
private final int y;

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

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof Coordinate)) {
return false;
}
Coordinate other = (Coordinate) obj;

return (x == other.x) && (y == other.y);
}

@Override
public int hashCode() {
int result = Integer.hashCode(x);
result = 31 * result + Integer.hashCode(y);
return result;
Comment on lines +28 to +30
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
int result = Integer.hashCode(x);
result = 31 * result + Integer.hashCode(y);
return result;
return 31 * Integer.hashCode(x)+ Integer.hashCode(y);;

}

@Override
public String toString() {
return "" + (char) (y + 65) + (x + 1);
}

public int getX() {
return x;
}

public int getY() {
return y;
}
}
87 changes: 87 additions & 0 deletions src/main/java/course_project/components/PlayingField.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package course_project.components;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PlayingField {

private final int[][] fleet = new int[10][10];
private final boolean[][] cellsShot = new boolean[10][10];
private final Map<Coordinate, Ship> coordinateToShip = new HashMap<>(32); // size for 10 ships and no rehashing
private final List<Ship> playersShips = new ArrayList<>();

public Map<Coordinate, Ship> getCoordinateMapping() {
return coordinateToShip;
}

public List<Ship> getPlayersShips() {
return playersShips;
}

public int[][] getFleet() {
return fleet;
}

public boolean[][] getCellsShot() {
return cellsShot;
}

public boolean isCellShot(Coordinate cell) {
return cellsShot[cell.getX()][cell.getY()];
}

public void placeShip(Ship ship) {
int size = ship.getSize();
for (Coordinate cell : ship.getCoordinates()) {
fleet[cell.getX()][cell.getY()] = size;
coordinateToShip.put(cell, ship);
}
playersShips.add(ship);
}

public boolean placeShot(Coordinate cell) {
if (!cellsShot[cell.getX()][cell.getY()]) {
cellsShot[cell.getX()][cell.getY()] = true;
if (coordinateToShip.containsKey(cell)) {
System.out.println("Hit");
shootAtShip(coordinateToShip.get(cell));
return true;
}
}
System.out.println("Miss");
return false;
}

private void shootAtShip(Ship hitShip) {
hitShip.gotShot();
if (hitShip.isSunk()) {
sinkShip(hitShip);
}
}

private void sinkShip(Ship hitShip) {
playersShips.remove(hitShip);
markAdjacentCells(hitShip);
System.out.println(hitShip.getSize() + "-deck ship is sunk");
}

private void markAdjacentCells(Ship hitShip) {
for (Coordinate c : hitShip.getCoordinates()) {
if (c.getX() > 0) {
cellsShot[c.getX() - 1][c.getY()] = true;
}
if (c.getX() < 9) {
cellsShot[c.getX() + 1][c.getY()] = true;
}
if (c.getY() > 0) {
cellsShot[c.getX()][c.getY() - 1] = true;
}
if (c.getY() < 9) {
cellsShot[c.getX()][c.getY() + 1] = true;
}
}
}

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

import java.util.ArrayList;
import java.util.List;

public class Ship {

private final int size;
private final List<Coordinate> coordinates;
private int health;
private boolean sunk;

public Ship(int size, List<Coordinate> coordinates) {
health = size;
this.size = size;
this.coordinates = new ArrayList<>(coordinates);
}

public List<Coordinate> getCoordinates() {
return new ArrayList<>(coordinates);
}

public void gotShot() {
health--;
if (health == 0) {
sunk = true;
}
}

public int getSize() {
return size;
}

public boolean isSunk() {
return sunk;
}
}
Loading