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
48 commits
Select commit Hold shift + click to select a range
bc5b417
Test push
ArtemNikolaev1 Jul 5, 2021
c9a91ef
код домашки №1. С кириллицей не пашет(
Allzza Jul 5, 2021
9b2c53a
Merge remote-tracking branch 'origin/feature/DimaTroshkin'
Allzza Jul 7, 2021
edb888f
readme modified
Allzza Jul 7, 2021
0995d15
Delete src/main/java/Homework_1 directory
Allzza Jul 9, 2021
64d8f00
connecting on new pc
Allzza Jul 11, 2021
b2e3bc6
Merge remote-tracking branch 'origin/feature/DimaTroshkin'
Allzza Jul 11, 2021
266c2b8
connecting on new pc
Allzza Jul 11, 2021
8b478c0
Update src/main/java/homework_1/Main.java
Allzza Jul 14, 2021
6823875
made link to my branch
Allzza Jul 14, 2021
67f119c
changed functionality according to description
Allzza Jul 19, 2021
565446c
traffic light added
Allzza Jul 19, 2021
4fe6027
Update Main.java
Allzza Jul 19, 2021
c63ac42
traffic light added
Allzza Jul 19, 2021
bda20a4
traffic light name changed
Allzza Jul 19, 2021
26b29eb
traffic light exception added
Allzza Jul 19, 2021
a496419
while (true) deleted
Allzza Jul 20, 2021
a656a09
traffic light remade according to specification
Allzza Jul 25, 2021
ac206da
Merge remote-tracking branch 'origin/feature/DimaTroshkin' into featu…
Allzza Jul 25, 2021
6e99a49
2nd HW is ready for review
Allzza Jul 25, 2021
cd5f59a
2nd HW is ready for review_
Allzza Jul 25, 2021
283294f
2nd HW is ready for review. Added info to README.md
Allzza Jul 25, 2021
d2a67d8
2nd HW is ready for review. Added info to README.md
Allzza Jul 25, 2021
419d58f
2nd HW is ready for review. Added CodingBat Link to README.md
Allzza Jul 25, 2021
b6b2447
Merge remote-tracking branch 'origin/master' into feature/DimaTroshkin
Allzza Aug 7, 2021
8355428
Tests for HW2 added, +slight changes
Allzza Aug 8, 2021
c6b08c4
new methods are added to HW2 tests to make code clearer
Allzza Aug 8, 2021
42342a7
README info added
Allzza Aug 8, 2021
31540dc
packages and input for hw4
Allzza Aug 21, 2021
a85f7af
new file.txt
Allzza Aug 21, 2021
2edf810
HW 4 is done
Allzza Aug 21, 2021
69dbdf9
README.md modified
Allzza Aug 21, 2021
a89343d
HW 5: PowerOfNumber & tests are done
Allzza Aug 21, 2021
95c1290
README.md modified for HW5
Allzza Aug 21, 2021
06f4d21
CustomRegexMatcher is done
Allzza Aug 25, 2021
4377bb9
CustomRegexMatcher tests added
Allzza Aug 26, 2021
3c0bae2
hw 7 done
Allzza Sep 15, 2021
2a79bf1
course project alpha
Allzza Sep 18, 2021
23bf1af
README.md modified
Allzza Sep 18, 2021
23dfa52
slight changes, added important test section, checked style
Allzza Sep 19, 2021
c9ce529
removed commented part of Battle
Allzza Sep 19, 2021
a5b4ccf
HW 4 updated, remade Java 11 based solutions
Allzza Sep 20, 2021
6ca1c87
Hw 5 added examples and description for CustomRegexMatcher
Allzza Sep 20, 2021
629406f
Hw 5 added examples and description for CustomRegexMatcher
Allzza Sep 20, 2021
104f822
removed unnecessary variable
Allzza Sep 20, 2021
ae464c6
Fixed ImmutableClassTerminator. Now it`s truly immutable
Allzza Sep 20, 2021
4811bf6
hw 6
Allzza Sep 22, 2021
db302d7
hw 6 readme modified
Allzza Sep 22, 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: 17 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
# Java Core June 2021

## *Nikolaev Artem*
## *Dima Troshkin*

[CodingBat Profile](https://codingbat.com/done?user=allzza4279@gmail.com&tag=1424372731)

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

[Link to markdown giude](https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet)
| HW1 | [Console printer](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/DimaTroshkin/src/main/java/homework_1/) | 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/DimaTroshkin/src/main/java/homework_2/traffic_light/) | The app that reads time from keyboard and says which color the traffic light is|
| | [Pyramid printer](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/DimaTroshkin/src/main/java/homework_2/homework_2.pyramid_printer/) | The app that makes pyramid of "x" using int input|
| | [Random chars table](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/DimaTroshkin/src/main/java/homework_2/random_chars_table/) | The app which prints table of random letters using int numbers of rows and columns and string strategy - "even" or "odd". Also prints out all even or odd letters according to strategy chosen|
| HW3 | [ImmutableClass](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/DimaTroshkin/src/main/java/homework_3/)| An example of Immutable Class realization |
| HW4 | [CustomAnnotation](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/DimaTroshkin/src/main/java/homework_4/custom_annotation/)| An example of custom annotation realization |
| | [CustomFileReader](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/DimaTroshkin/src/main/java/homework_4/custom_file_reader/)| An app which reads String data from file.txt and removes commas and fullstops |
| | [Singleton](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/DimaTroshkin/src/main/java/homework_4/singleton/)| An example of singleton class |
| HW5 | [CustomRegexMatcher](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/DimaTroshkin/src/main/java/homework_5/custom_regex_matcher/)| An app which takes String from terminal and checks whether given String matches hardcoded regex|
| | [PowerOfNumber](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/DimaTroshkin/src/main/java/homework_5/power_of_number/)| An app which calculates a^b for two non-negative integers a and b |
| HW6 | [MapPloblemGenerators](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/DimaTroshkin/src/main/java/homework_6/)| The realisation of two classes which generate problems using hashmap|
| HW7 | [KittenToCatFunction](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/DimaTroshkin/src/main/java/homework_7/)| The realisation of @FunctionalInterface KittenToCatFunction with abstract method grow(). From now on we know how does kitten become a cat!|
| HW7 | [SeaBattle](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/DimaTroshkin/src/main/java/course_project/)| The Sea Battle game 1 by 1 (No AI) |
[Link to markdown guide](https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet)
58 changes: 58 additions & 0 deletions src/main/java/course_project/Battle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package course_project;

import course_project.models.Player;
import course_project.models.Ship;
import course_project.engine.ShipPlacer;
import course_project.engine.Shooter;

import java.io.IOException;
import java.util.ArrayList;

public class Battle {

public static void run() throws IOException {
Player player1 = new Player();
Player player2 = new Player();

ArrayList<Player> players = new ArrayList<>();
players.add(player1);
players.add(player2);

Player currentPlayer;
for (Player player : players) { // preparing both players battlefields
currentPlayer = player;

System.out.println("\n \n" + currentPlayer.name() + ", welcome to the Sea Battle. \n \n" +
"It`s time to place your ships. Remember the main rule: ships must not touch each other.\n" + "To place a ship you basically need to type coordinate and then choose mode. \n" +
"There are two ways to place your ships from starting coordinate: from left to right (key \"h\") & from up to down (key \"v\")\n" +
"If there is only one possible way, it will be chosen automatically, if it`s impossible to place ship from this coordinate, you will have to choose another one.\n");

System.out.println("Let`s begin with Carrier - huge battleship with size 4. \n");
ShipPlacer.placement(new Ship("Carrier", 4), currentPlayer);

System.out.println("Now place 2 Cruisers of size 3");
for (int i = 0; i < 2; i++) {
ShipPlacer.placement(new Ship("Cruiser", 3), currentPlayer);
}
System.out.println("Now place 3 Destroyers of size 2");
for (int i = 0; i < 3; i++) {
ShipPlacer.placement(new Ship("Destroyer", 2), currentPlayer);
}
System.out.println("And finally place 4 Scout ships of size 1");
for (int i = 0; i < 4; i++) {
ShipPlacer.placement(new Ship("Scout", 1), currentPlayer);
}

}

System.out.println("\n\n\n\n\n\n\n\n\n\n\n\n\n");

while (player1.hp() > 0 && player2.hp() > 0) {
Shooter.Shoot(player1, player2);
if (player2.hp() == 0) {
break;
}
Shooter.Shoot(player2, player1);
}
}
}
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 java.io.IOException;

public class Main {


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

Choose a reason for hiding this comment

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

Opt: remove static

Battle.run();
Copy link
Owner

Choose a reason for hiding this comment

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

Quite good! You did PvP seabattle. Looks nice, interface is comfortable. Opt: add delays/enter between players, so they will not see each other fields. Also if ship is sunk, near cells can be marked as hit. But the game is good as-is.

Copy link
Owner

Choose a reason for hiding this comment

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

Nice abstractions, model/service segregation, the code is easy to read in general, I didn't find any bugs. 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.

it was planned, but had no time left. Thank you for checking. Will work on it later and have a whale of a time with my mates, encouraging them go programming)

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

import course_project.models.Player;
import course_project.models.Board;

import java.util.Locale;

public class BoardPrinter {

public static void printMainBoard(Player player) {
Board board = player.mainBoard();
System.out.println(player.name() + ": MAIN BOARD");
int rowNum = 1;
String rowOffset;
System.out.println(" " + board.getColNames().stream().reduce(((s, s2) -> s + " " + s2)).orElse("").toUpperCase(Locale.ROOT));
for (int row : board.getRowNames()) {
rowOffset = (rowNum < 10) ? rowNum + " |" : rowNum + " |";
System.out.print(rowOffset);

for (String cell : board.getBoard().get(row)) {
System.out.print(" " + cell + " " + "|");
}
System.out.print("\n");
rowNum++;
}
}

public static void printBothBoards(Player player) {
Board mainBoard = player.mainBoard();
Board scanBoard = player.scanBoard();
int rowNum = 1;
String spaceBetween = " ";
String rowOffset;
System.out.println(spaceBetween + player.name() + ": MAIN BOARD" + spaceBetween + spaceBetween + " ENEMY TRACKING");
// top
System.out.print(" " + mainBoard.getColNames().stream().reduce(((s, s2) -> s + " " + s2)).orElse("").toUpperCase(Locale.ROOT));
System.out.print(spaceBetween);
System.out.println(" " + scanBoard.getColNames().stream().reduce(((s, s2) -> s + " " + s2)).orElse("").toUpperCase(Locale.ROOT));

for (int row : mainBoard.getRowNames()) {
rowOffset = (rowNum < 10) ? rowNum + " |" : rowNum + " |";
System.out.print(rowOffset);

for (String cell : mainBoard.getBoard().get(row)) {
System.out.print(" " + cell + " " + "|");
}
System.out.print(spaceBetween);
System.out.print(rowOffset);
for (String cell : scanBoard.getBoard().get(row)) {
System.out.print(" " + cell + " " + "|");
}

System.out.print("\n");
rowNum++;
}
}

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

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

public class Input {

public static String getRightCoordinate() throws IOException {
System.out.println("Enter coordinate if format: \"a1\"");
String s;
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
while (true) {
s = reader.readLine();
if (Input.checkCoordinate(s)) {
System.out.println("Coordinate: " + s);
return s;
} else {
System.out.println("Wrong Input! use letters from a to j and numbers from 1 to 10, like \"d4\"");
}
}
}

public static String getRightMode() throws IOException {
System.out.println("Choose mode: use letter \"v\" to place it vertically or \"h\" to place it horizontally");
String s;
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
while (true) {
s = reader.readLine();
if (Input.checkMode(s)) {
System.out.println("Mode: " + s);
return s;
} else {
System.out.println("Wrong mode! use letter \"v\" to place it vertically or \"h\" to place it horizontally.");
}
}
}

public static boolean checkCoordinate(String s) {
if (s.length() > 3 || s.length() < 2) {
return false;
}

if (!s.toLowerCase(Locale.ROOT).substring(0, 1).matches("[a-j]")) {
return false;
}

if (!s.toLowerCase(Locale.ROOT).substring(1, 2).matches("[1-9]")) {
return false;
}

if (s.length() == 3 && !s.toLowerCase(Locale.ROOT).substring(2).matches("0")) {
return false;
}
return true;
}

public static boolean checkMode(String s) {
if (s.toLowerCase(Locale.ROOT).matches("[vh]")) {
return true;
}
return false;
}

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

import course_project.models.Board;
import course_project.models.Coordinate;

import java.util.List;

public class PositionChecker {

public static boolean possibleToPlace(Board b, Coordinate c, int size, String mode) {
// mode h: left -> right , mode v: up - > down from coordinate given
int horizSize;
int vertSize;

if (mode.equalsIgnoreCase("h")) { // space needed for ship in horizontal mode
horizSize = size;
vertSize = 1;
} else { // space needed for ship in vertical mode
horizSize = 1;
vertSize = size;
}

List<String> currentRow;
List<String> upperRow;
List<String> lowerRow;

// Checking upper row
if (b.getBoard().containsKey(c.row() - 1)) { // if it exist
upperRow = b.getBoard().get(c.row() - 1);
for (int col = c.col() - 1; col <= c.col() + horizSize; col++) { // check all cols from current -1 to current + size
if (col >= 0 && col < 10) { // if mainBoard contains such col
if (!upperRow.get(col).equals(" ")) { // cell must be empty
return false;
}
}
}
}

// Checking hull rows
for (int i = 0; i < vertSize; i++) {
if (!b.getBoard().containsKey(c.row() + i)) { // if doesn`t exist -> not enough vertical space
return false;
}
currentRow = b.getBoard().get(c.row() + i);
for (int col = c.col() - 1; col <= c.col() + horizSize; col++) { // check all cols from current -1 to current + horiz size
if (col >= 0 && col < 10) { // if mainBoard contains such col
if (!currentRow.get(col).equals(" ")) { // cell must be empty
return false;
}
} else {
if ((col - 1 > 9) && mode.equalsIgnoreCase("h")) { // if not enough space horizontally (last hull coord comes out of the map)
return false;
}
}
}

}


// Checking lower row
if (b.getBoard().containsKey(c.row() + vertSize)) { // if it exist
lowerRow = b.getBoard().get(c.row() + vertSize);
for (int col = c.col() - 1; col <= c.col() + horizSize; col++) { // check all cols from current -1 to current + size
if (col >= 0 && col < 10) { // if mainBoard contains such col
if (!lowerRow.get(col).equals(" ")) { // cell must be empty
return false;
}
}
}
}

return true;
}


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

import course_project.models.Player;
import course_project.models.Board;
import course_project.models.Coordinate;
import course_project.models.Ship;

import java.io.IOException;
import java.util.ArrayList;

public class ShipPlacer {

public static void placement(Ship ship, Player p) throws IOException {

Board b = p.mainBoard();
while (true) {
BoardPrinter.printMainBoard(p);
System.out.println("It`s time to place a ship.");
Coordinate c = new Coordinate(Input.getRightCoordinate());
String mode;
int size = ship.getSize();

if (PositionChecker.possibleToPlace(b, c, size, "v") && PositionChecker.possibleToPlace(b, c, size, "h")) {
if (size == 1) { //scout does not need direction
placeShip(p, c, ship, "h");
break;
}
mode = Input.getRightMode();
placeShip(p, c, ship, mode);
break;

} else if (PositionChecker.possibleToPlace(b, c, size, "v")) {
placeShip(p, c, ship, "v");
break;
} else if (PositionChecker.possibleToPlace(b, c, size, "h")) {
placeShip(p, c, ship, "h");
break;
} else {
System.out.println("Impossible to place it here!");
}
}
}

public static void placeShip(Player p, Coordinate c, Ship ship, String mode) {
Board b = p.mainBoard();
int size = ship.getSize();
int cCol = c.col(); // current col & row
int cRow = c.row();
ArrayList<Coordinate> shipCoords = new ArrayList<>();
for (int i = 0; i < size; i++) {
if (mode.equalsIgnoreCase("h")) {
shipCoords.add(new Coordinate(cRow, cCol + i));
} else if (mode.equalsIgnoreCase("v")) {
shipCoords.add(new Coordinate(cRow + i, cCol));
}
}
for (Coordinate coordinate : shipCoords) {
b.changeCell(coordinate, "H");
}
ship.setCoords(shipCoords);
p.ships().add(ship);
BoardPrinter.printMainBoard(p);
}
}

Loading