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
82 commits
Select commit Hold shift + click to select a range
c81cfc4
new branch
rlrio Jul 5, 2021
e3fb801
update gitignore
rlrio Jul 5, 2021
f9b91e7
add readme
rlrio Jul 5, 2021
d833a1f
add homework_1
rlrio Jul 6, 2021
c542dbb
add refactoring to English to homework_1
rlrio Jul 6, 2021
fef0824
update readme, homework_1
rlrio Jul 7, 2021
8377630
update gitignore
rlrio Jul 11, 2021
9e2f25f
update homework_1
rlrio Jul 12, 2021
133b5b5
Merge branch 'master' into feature/SvetlanaEmelianchik
rlrio Jul 12, 2021
ada747e
update homework_1
rlrio Jul 12, 2021
a32e922
Merge remote-tracking branch 'origin/master' into feature/SvetlanaEme…
ArtemNikolaev1 Jul 12, 2021
4e2980b
Update
ArtemNikolaev1 Jul 12, 2021
9f16a90
update homework_1
rlrio Jul 12, 2021
f3c09be
update homework_1
rlrio Jul 13, 2021
af2a08f
add homework_2
rlrio Jul 14, 2021
ab87918
update readme with hw2
rlrio Jul 14, 2021
d1e07fd
update hw2 with Main classes
rlrio Jul 19, 2021
63db402
update hw2 with Main classes
rlrio Jul 19, 2021
4a79147
refactor homework_2
rlrio Jul 21, 2021
2c266b6
add homework_3
rlrio Jul 21, 2021
9f0773b
add homework_3
rlrio Jul 21, 2021
af9e990
add homework_3
rlrio Jul 21, 2021
72ec557
Merge branch 'master' into feature/SvetlanaEmelianchik
rlrio Jul 21, 2021
7fe8fa1
refactor homework_2
rlrio Jul 21, 2021
06bb33a
add unit tests for homework_2
rlrio Jul 21, 2021
912f78e
refactor homework_3
rlrio Jul 21, 2021
8d5f228
refactor homework_3
rlrio Jul 21, 2021
ba34642
update readme
rlrio Jul 22, 2021
e72c2f8
refactor homework_2, homework_3
rlrio Jul 22, 2021
e98f980
update readme
rlrio Jul 22, 2021
9be84ae
refactor hw2 and hw3
rlrio Jul 25, 2021
8e72a91
refactor hw2 and hw3
rlrio Jul 25, 2021
b714726
refactor hw2 and hw3
rlrio Jul 25, 2021
a7c4e40
update ImmutableWorker and ImmutableWorkerTest
rlrio Jul 25, 2021
e4e9e11
update RandomCharsTable and RandomCharsTableTest
rlrio Jul 25, 2021
95aa6ce
update hw2 and hw3
rlrio Jul 28, 2021
537f80a
update hw2 and hw3
rlrio Jul 28, 2021
6a93208
update hw2 and hw3
rlrio Aug 3, 2021
1ebba5f
add hw4
rlrio Aug 3, 2021
dab661b
add hw4
rlrio Aug 3, 2021
075565a
add hw4
rlrio Aug 3, 2021
7066727
add hw4
rlrio Aug 3, 2021
ef2d9e8
add hw4
rlrio Aug 3, 2021
82fd0db
add hw4
rlrio Aug 3, 2021
d5acd27
add hw4
rlrio Aug 3, 2021
c440a4d
add hw4
rlrio Aug 4, 2021
8f6ffa3
update hw4
rlrio Aug 6, 2021
080c768
update hw4
rlrio Aug 7, 2021
5adc35d
add hw5 math_power
rlrio Aug 18, 2021
38245c2
update README
rlrio Aug 18, 2021
dfa2920
update README
rlrio Aug 18, 2021
ddd9c88
update hw5
rlrio Aug 18, 2021
b4a2189
update hw5
rlrio Aug 19, 2021
5de585e
update readme
rlrio Aug 19, 2021
22cf66f
add hw5 custom_regex_matcher
rlrio Aug 19, 2021
6d6b1e0
update readme
rlrio Aug 19, 2021
dd2cc57
refactor hw2 and hw5
rlrio Aug 20, 2021
7a70ee1
refactor hw2 and hw5
rlrio Aug 20, 2021
aa15994
refactor hw5
rlrio Aug 26, 2021
2c194d3
add homework_6
rlrio Aug 31, 2021
45bbdbe
add homework_6
rlrio Sep 1, 2021
b5cd1be
add homework_7
rlrio Sep 9, 2021
be9b160
add raw course_project realization
rlrio Sep 10, 2021
4636cec
add raw course_project realization
rlrio Sep 10, 2021
c0c4f6b
add raw course_project realization
rlrio Sep 10, 2021
6fc2987
bug unit test
rlrio Sep 12, 2021
c7acea3
refactoring course_project
rlrio Sep 15, 2021
e159e8b
refactoring course_project
rlrio Sep 15, 2021
77ddf71
add Mockito dependency
rlrio Sep 15, 2021
7c3c540
refactor course_project
rlrio Sep 19, 2021
860a783
refactor course_project
rlrio Sep 19, 2021
a8810e3
refactor course_project
rlrio Sep 19, 2021
223083d
refactor course_project
rlrio Sep 19, 2021
b7a6098
refactor course_project
rlrio Sep 19, 2021
068e9fb
update README
rlrio Sep 19, 2021
98c3145
refactor course_project
rlrio Sep 19, 2021
34b0d61
hw5
rlrio Sep 20, 2021
03ea478
update hw6
rlrio Sep 20, 2021
9e732a7
update hw6
rlrio Sep 20, 2021
962a051
update hw3
rlrio Sep 21, 2021
750631e
update hw3
rlrio Sep 21, 2021
9af7017
update hw3
rlrio Sep 21, 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
21 changes: 18 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
# Java Core June 2021

## *Nikolaev Artem*
## *Emelianchik Svetlana*

| 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](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/SvetlanaEmelianchik/src/main/java/homework_1) | The app that reads input arguments and prints them, until "error" argument |
| HW2 | [TrafficLight](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/SvetlanaEmelianchik/src/main/java/homework_2/traffic_light) | The app that reads the input time of the day in seconds or in format hh:mm:ss and checks the traffic light to cross the road.|
| HW2 | [PyramidPrinter](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/SvetlanaEmelianchik/src/main/java/homework_2/pyramid_printer) | The app that reads the input number and prints the pyramid of symbol "x" with such a base. |
| HW2 | [RandomCharsTable](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/SvetlanaEmelianchik/src/main/java/homework_2/random_chars_table) | The app that reads two numbers and a string and prints a table with random chars from A to Z and also shows the even or odd chars that was in a table depending on a strategy that was chosen. |
| HW3 | [TrafficLightTest](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/SvetlanaEmelianchik/src/test/java/homework_2/traffic_light) | Add unit tests for Traffic Light app |
| HW3 | [PyramidPrinterTest](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/SvetlanaEmelianchik/src/test/java/homework_2/pyramid_printer) | Add unit tests for Print Pyramid app|
| HW3 | [RandomCharsTableTest](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/SvetlanaEmelianchik/src/test/java/homework_2/random_chars_table) | Add unit tests for Random Chars Table app |
| HW3 | [ImmutableClass](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/SvetlanaEmelianchik/src/main/java/homework_3) <br />[ImmutableClassTest](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/SvetlanaEmelianchik/src/test/java/homework_3) | Add Immutable class |
| HW4 | [CustomFileReader](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/SvetlanaEmelianchik/src/main/java/homework_4/custom_file_reader) [CustomFileReaderTest](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/SvetlanaEmelianchik/src/test/java/homework_4/custom_file_reader) | The app that reads data from the file and prints it to the console without dots and commas |
| HW4 | [Singleton](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/SvetlanaEmelianchik/src/main/java/homework_4/singleton) <br />[SingletonTest](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/SvetlanaEmelianchik/src/test/java/homework_4/singleton) | The class that can have only one instance |
| HW4 | [CustomAnnotation](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/SvetlanaEmelianchik/src/main/java/homework_4/custom_annotation) <br />[CustomAnnotationTest](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/SvetlanaEmelianchik/src/test/java/homework_4/custom_annotation) | The annotation Author is used to get a default value when creating an instance of Book |
| HW5 | [PowerOfNumber](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/SvetlanaEmelianchik/src/main/java/homework_5/math_power) [PowerOfNumberTest](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/SvetlanaEmelianchik/src/test/java/homework_5/math_power) | PowerOfNumber App with method pow implemented via recursion |
| HW5 | [CustomRegexMatcher](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/SvetlanaEmelianchik/src/main/java/homework_5/custom_regex_matcher) [CustomRegexMatcherTest](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/SvetlanaEmelianchik/src/test/java/homework_5/custom_regex_matcher) | The App checks whether the arguments match the hardcoded regex |
| HW6 | [MapProblemsGenerator](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/SvetlanaEmelianchik/src/main/java/homework_6/map_problems_generator) | 1)CollidingMapProblemsGeneratorKey is the class that always creates collisions in the HashMap. <br /> 2)MutableMapProblemsGeneratorKey is the class with HashCode method that returns random value. |
| HW7 | [KittenToCatFunction](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/SvetlanaEmelianchik/src/main/java/homework_7) | Custom Functional Interface KittenToCatFunction with abstract method grow(). |
| Course Project | [BattleShipGame](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/SvetlanaEmelianchik/src/main/java/course_project/battleship_game) <br /> [UnitTests](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/SvetlanaEmelianchik/src/test/java/course_project/battleship_game)| BattleShip game with three modes (CVC, PVC, PVP).

[Link to markdown giude](https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet)
[Link to CodingBat profile](https://codingbat.com/done?user=sveta881@mail.ru&tag=8234249112)
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'
testCompile 'org.junit.jupiter:junit-jupiter-params:5.7.0'
testImplementation group: 'org.mockito', name: 'mockito-all', version: '1.10.19'
}

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

import course_project.battleship_game.controller.GameController;

public class Main {

public static void main(String[] args) {
Copy link
Owner

Choose a reason for hiding this comment

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

Opt: it seems indentation is a little bit shifted
image

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

image
it looks like this on my computer

Copy link
Owner

Choose a reason for hiding this comment

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

Really nice game, well-done! Excellent architecture, model/service separation, easy to read code, pretty interface and a lot of options how to play. Also, you're showing good knowledge of Java Core and Java libraries, as well as following Code Conventions and creating proper application architecture. Special thanks for automatic ship placement :) Testing is also fine, as I understand testing of console application can be tough.

Good job, approved!

GameController controller = new GameController();
controller.run();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package course_project.battleship_game.controller;

import course_project.battleship_game.exception.GameException;
import course_project.battleship_game.model.GameMode;
import course_project.battleship_game.model.Player;
import course_project.battleship_game.service.GameService;

import static course_project.battleship_game.controller.PrintController.printMessage;
import static course_project.battleship_game.utils.Constants.EXCEPTION_MESSAGE;
import static course_project.battleship_game.utils.Constants.FLEETS_CREATED_MESSAGE;
import static course_project.battleship_game.utils.Constants.WELCOME_MESSAGE;

public class GameController {
private final GameService service = new GameService();
private final InputController inputController = new InputController();

public void run() {
printMessage(WELCOME_MESSAGE);
GameMode gameMode = inputController.getGameMode();
Player[] players = service.createPlayers(gameMode);
service.rollingDiceToChooseWhoStarts(players);
try {
service.createFleet(players);
} catch (GameException e) {
printMessage(EXCEPTION_MESSAGE);
}
printMessage(FLEETS_CREATED_MESSAGE);
Player player1 = players[0];
Player player2 = players[1];
boolean isGameOver = false;
while (!isGameOver) {
isGameOver = service.isGameOver(player1, player2, gameMode);
Copy link
Owner

Choose a reason for hiding this comment

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

Opt: service.isGameOver has really unexpected nature - most of game logic is there, but actually, by name, it should only check isGameOver or not, nothing else. Better separate

if (!isGameOver) {
isGameOver = service.isGameOver(player2, player1, gameMode);
}
}
service.determineWinner(players);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package course_project.battleship_game.controller;

import course_project.battleship_game.model.Cell;
import course_project.battleship_game.model.GameMode;
import course_project.battleship_game.utils.CoordinateValidator;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import static course_project.battleship_game.controller.PrintController.printMessage;
import static course_project.battleship_game.utils.Constants.CHOOSE_GAME_MODE_MESSAGE;
import static course_project.battleship_game.utils.Constants.CHOOSE_MODE_TO_CREATE_FLEET_MESSAGE;
import static course_project.battleship_game.utils.Constants.CHOOSE_SHIP_DIRECTION_MESSAGE;
import static course_project.battleship_game.utils.Constants.DEFAULT_PLAYER_NAME;
import static course_project.battleship_game.utils.Constants.ERROR_INPUT_MESSAGE;
import static course_project.battleship_game.utils.Constants.GET_CELL_COORDINATE_MESSAGE;
import static course_project.battleship_game.utils.Constants.GET_PLAYER_NAME_MESSAGE;

public class InputController {
private final BufferedReader READER = new BufferedReader(new InputStreamReader(System.in));

public String getPlayerName() {
printMessage(GET_PLAYER_NAME_MESSAGE);
String line = readLine();
return line == null || line.isEmpty() ? DEFAULT_PLAYER_NAME : line;
}

public GameMode getGameMode() {
printMessage(CHOOSE_GAME_MODE_MESSAGE);
String mode = readLine();
while (true) {
if (mode.equals("0")) {
return GameMode.CVC;
} else if (mode.equals("1")) {
return GameMode.PVC;
} else if (mode.equals("2")) {
return GameMode.PVP;
} else {
printMessage(ERROR_INPUT_MESSAGE);
mode = readLine();
}
}
}

public int getModeToCreateFleet() {
printMessage(CHOOSE_MODE_TO_CREATE_FLEET_MESSAGE);
return getZeroOrOne();
}

public int getDirection() {
printMessage(CHOOSE_SHIP_DIRECTION_MESSAGE);
return getZeroOrOne();
}

public Cell getCell() {
printMessage(GET_CELL_COORDINATE_MESSAGE);
String coordinate = readLine();
while (!CoordinateValidator.isCoordinate(coordinate)) {
printMessage(ERROR_INPUT_MESSAGE);
coordinate = readLine();
}
int x = Integer.parseInt(coordinate.substring(1)) - 1;
int y = ((int) coordinate.toUpperCase().charAt(0)) - 65;
return new Cell(x, y);
}

private String readLine() {
try {
return READER.readLine();
} catch (IOException e) {
return "";
}
}

private int getZeroOrOne() {
String mode = readLine();
while (true) {
if (mode.equals("0") || mode.equals("1")) {
return Integer.parseInt(mode);
}
printMessage(ERROR_INPUT_MESSAGE);
mode = readLine();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package course_project.battleship_game.controller;

import course_project.battleship_game.model.Cell;
import course_project.battleship_game.model.CellStatus;
import course_project.battleship_game.model.GameMode;
import course_project.battleship_game.model.Player;

import static course_project.battleship_game.utils.Constants.DEFAULT_COMPUTER_NAME;
import static course_project.battleship_game.utils.Constants.PLAYER_BOARD_MESSAGE_FORMAT;
import static java.lang.System.lineSeparator;

public class PrintController {

public static void printMessage(String message) {
System.out.print(message);
}

public void printBoards(GameMode mode, Player player1, Player player2) {
if (mode.equals(GameMode.CVC) || !player1.getName().contains(DEFAULT_COMPUTER_NAME)) {
printBoardForPlayer(player1, false);
printBoardForPlayer(player2, true);
}
}

public void printBoardForPlayer(Player player, boolean isEnemy) {
printMessage(String.format(PLAYER_BOARD_MESSAGE_FORMAT, player.getName()));
printHeader();
Cell[][] boardMatrix = player.getBoard().getBoardMatrix();
for (int y = 0; y < boardMatrix.length; y++) {
printMessage((char) (65 + y) + " ");
for (int x = 0; x < boardMatrix.length; x++) {
printCell(isEnemy, boardMatrix[y][x]);
}
printMessage(lineSeparator());
}
}

private void printCell(boolean isEnemy, Cell cell) {
if (isEnemy && cell.getCellStatus().equals(CellStatus.SHIP)) {
printMessage(CellStatus.EMPTY.getCharacter());
} else {
printMessage(cell.getCellStatus().getCharacter());
}
}

private void printHeader() {
printMessage(" ");
for (int i = 1; i < 11; i++) {
printMessage(i + " ");
}
printMessage(lineSeparator());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package course_project.battleship_game.exception;

public class GameException extends RuntimeException {
public GameException() {
}
}
35 changes: 35 additions & 0 deletions src/main/java/course_project/battleship_game/model/Board.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package course_project.battleship_game.model;

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

import static course_project.battleship_game.utils.Constants.BOARD_SIZE;

public class Board {
private final Cell[][] boardMatrix;
private final List<Ship> shipList;

public Board() {
this.boardMatrix = createBoard();
this.shipList = new ArrayList<>();
}

public Cell[][] getBoardMatrix() {
return boardMatrix;
}

public List<Ship> getShipList() {
return shipList;
}

private Cell[][] createBoard() {
Cell[][] boardMatrix = new Cell[BOARD_SIZE][BOARD_SIZE];
for (int y = 0; y < BOARD_SIZE; y++) {
for (int x = 0; x < BOARD_SIZE; x++) {
boardMatrix[y][x] = new Cell(x, y, CellStatus.EMPTY);
}
}
return boardMatrix;
}

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

import java.util.Objects;

public class Cell {
private final int x;
private final int y;
private CellStatus status;

public Cell(int x, int y, CellStatus status) { //change in controller
this.x = x;
this.y = y;
this.status = status;
}

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

public int getX() {
return x;
}

public int getY() {
return y;
}

public CellStatus getCellStatus() {
return status;
}

public void setCellStatus(CellStatus status) {
this.status = status;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Cell cell = (Cell) o;
return getX() == cell.getX() &&
getY() == cell.getY();
}

@Override
public int hashCode() {
return Objects.hash(getX(), getY());
}

@Override
public String toString() {
return String.valueOf((char) (y + 65)) + (x + 1);
}
}
19 changes: 19 additions & 0 deletions src/main/java/course_project/battleship_game/model/CellStatus.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package course_project.battleship_game.model;

public enum CellStatus {
EMPTY("\uD83C\uDF0A"),
SHIP("\uD83D\uDEA2"),
HIT("\u274C"),
SHIP_BORDER("\uD83C\uDF0A"),
MISSED("\uD83D\uDD18");

private final String character;

CellStatus(String character) {
this.character = character;
}

public String getCharacter() {
return character;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package course_project.battleship_game.model;

public enum GameMode {
CVC,
PVC,
PVP
}
35 changes: 35 additions & 0 deletions src/main/java/course_project/battleship_game/model/Player.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package course_project.battleship_game.model;

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

public class Player {
private final String name;
private final Board board;
private final int modeToCreateFleet;
private final List<Cell> logOfMoves;

public Player(String name, int modeToCreateFleet) {
this.name = name;
this.modeToCreateFleet = modeToCreateFleet;
this.board = new Board();
this.logOfMoves = new ArrayList<>();
}

public Board getBoard() {
return board;
}

public String getName() {
return name;
}

public List<Cell> getLogOfMoves() {
return logOfMoves;
}

public int getModeToCreateFleet() {
return modeToCreateFleet;
}

}
Loading