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
39 commits
Select commit Hold shift + click to select a range
bba4c50
AS commit
Jul 5, 2021
39cd05f
AS commit git ignore
Jul 5, 2021
26433ce
05.07 Home Work 1
Jul 5, 2021
c21fa4e
06.07.21 Home Work 1.1 with Red Color
Jul 6, 2021
12b4c21
AS commit
Jul 5, 2021
5c04832
05.07 Home Work 1
Jul 5, 2021
95720c0
homework_1
Jul 9, 2021
83dd7ba
Merge branch 'master' into feature/AntonSegodnik
Jul 9, 2021
ed9c6b0
homework_1 with red "Alarm" + read.me: added link to main
Jul 11, 2021
4df4686
homework_1 with red "Alarm" + read.me: added link to main and Name ch…
Jul 11, 2021
0788e6c
homework_2 with BufferReaders methods
Jul 14, 2021
df79a67
homework_2 with BufferReaders methods + Read me
Jul 14, 2021
3968d87
homework_2 with BufferReaders methods + Read me again
Jul 14, 2021
85299bd
homework_2 with hierarchy and run()
Jul 21, 2021
6a71478
homework_2 with hierarchy and run() + read.me
Jul 21, 2021
2ce4e3e
homework_3
Jul 21, 2021
07dfad9
Merge branch 'master' into feature/AntonSegodnik
Jul 22, 2021
2e7c00c
homework_3 + tests
Jul 22, 2021
b4a2b84
homework_3 + tests finish version
Jul 25, 2021
c092c42
homework_3 + fixed version
Jul 25, 2021
7667c80
homework_4 without annotation tests
Aug 7, 2021
2d78839
homework_4 without annotation tests
Aug 10, 2021
a1aff4b
homework_4 whithout read me
Aug 10, 2021
10b9e62
homework_4 all
Aug 10, 2021
1b68701
homework_5 all
Aug 23, 2021
5f57dd9
homework_5 with read me and correct packaging
Aug 23, 2021
f1f5ecc
homework_5 with read me and correct packaging + private methods
Aug 23, 2021
4cff6d5
homework_2 fixed, Random Chars Table wasn't fixed (not enough details)
Sep 2, 2021
53086af
homework_2 fix tests for new messages
Sep 2, 2021
4d3a9cc
homework_6 start
Sep 3, 2021
97eca5e
homework_6 finished
Sep 3, 2021
90a99eb
homework_6 finished + read me
Sep 6, 2021
de1a0bd
Fix 2 from Kirill
Sep 7, 2021
c11b05a
Final project w/o tests
Sep 19, 2021
04aa763
Final project with some tests
Sep 20, 2021
ac78bf6
Homework_7 added
Sep 21, 2021
a43f2d5
Fix hm 3, and "txts"
Sep 22, 2021
75f3deb
Fix hm 3, and "txts"
Sep 22, 2021
4ee8e31
fix 3 just public
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
19 changes: 16 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
# Java Core June 2021

## *Nikolaev Artem*
## *Anton Segodnik*

| 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/AntonSegodnik/src/main/java/homework_1) | The app that reads input arguments and prints them, until "error" argument |
|HW2 | [pyramid_printer](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/AntonSegodnik/src/main/java/homework_2/pyramid_printer) | The app that reads input arguments and prints pyramid |
|HW2 | [random_chars_table](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/AntonSegodnik/src/main/java/homework_2/random_chars_table) | The app that reads input arguments and creates random table |
|HW2 | [traffic_light](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/AntonSegodnik/src/main/java/homework_2/traffic_light) | The app that reads input arguments and prints traffic light |
|HW3 | [immutable class](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/AntonSegodnik/src/main/java/homework_3) | The app that reads input arguments and prints traffic light |
|HW4 | [customAnnotation](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/AntonSegodnik/src/main/java/homework_4/customAnnotation) | The app create object with default value from constructor using annotation |
|HW4 | [customFileReader](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/AntonSegodnik/src/main/java/homework_4/customFileReader) | The app that reads from file |
|HW4 | [singleton](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/AntonSegodnik/src/main/java/homework_4/singleton) | The singleton app |
|HW5 | [customRegexMatcher](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/AntonSegodnik/src/main/java/homework_5/customRegexMatcher) | The app |
|HW5 | [PowerOfNumber](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/AntonSegodnik/src/main/java/homework_5/PowerOfNumber) | The app |
|HW6 | [MapProblemsCollisionGenerator](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/AntonSegodnik/src/main/java/homework_6/MapProblemsCollisionGenerator) | The app |
|HW6 | [MapProblemsMutableGenerator](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/AntonSegodnik/src/main/java/homework_6/MapProblemsMutableGenerator) | The app |
|HW7 | [KittenToCatFunction](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/AntonSegodnik/src/main/java/homework_7) | The app |
Comment on lines +14 to +19
Copy link
Owner

Choose a reason for hiding this comment

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

Nice description...


[Link to markdown giude](https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet)
[Link to markdown giude](https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet) |
[Link to CodingBat](https://codingbat.com/done?user=segodnik@gmail.com&tag=8130222420)
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@ repositories {
}

dependencies {
implementation 'org.jetbrains:annotations:20.1.0'
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 {
useJUnitPlatform()
}
Empty file modified gradlew
100644 → 100755
Empty file.
92 changes: 92 additions & 0 deletions src/main/java/SeaBattle/GenerateShips.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package SeaBattle;

import java.util.Random;

public class GenerateShips {

static int FIELD_SIZE = 10;

public char[][] createBoardWithShips() {
GenerateShips generateShips = new GenerateShips();
int[][] field = new int[FIELD_SIZE][FIELD_SIZE];
generateShips.fillField(field, 4);
generateShips.fillField(field, 3);
generateShips.fillField(field, 3);
generateShips.fillField(field, 2);
generateShips.fillField(field, 2);
generateShips.fillField(field, 2);
generateShips.fillField(field, 1);
generateShips.fillField(field, 1);
generateShips.fillField(field, 1);
generateShips.fillField(field, 1);

char map[][] = new char[FIELD_SIZE][FIELD_SIZE];
for(int i = 0; i < FIELD_SIZE; i++) {
for(int j = 0; j < FIELD_SIZE; j++) {
map[i][j] = field[i][j] == 0 || field[i][j] == 9 ? '_' : '0';
}
}
return map;
}

void fillField(int [][] fields, int shipLength) {

Random random = new Random();
int[][] field = fields;
while(true) {
int x = random.nextInt(field.length);
int y = random.nextInt(field.length);
boolean vertical = random.nextBoolean();

if(vertical) {
if(y + shipLength > FIELD_SIZE) {
y -= shipLength;
}
} else if(x + shipLength > FIELD_SIZE) {
x -= shipLength;
}
boolean isFree = true;
if(vertical) {
for(int m = y; m < y + shipLength; m++) {
if(field[m][x] != 0) {
isFree = false;
break;
}
}
} else {
for(int n = x; n < x + shipLength; n++) {
if(field[y][n] != 0) {
isFree = false;
break;
}
}
}
if(!isFree) {
continue;
}

if(vertical) {
for(int m = Math.max(0, x - 1); m < Math.min(FIELD_SIZE, x + 2); m++) {
for(int n = Math.max(0, y - 1); n < Math.min(FIELD_SIZE, y + shipLength + 1); n++) {
field[n][m] = 9;
}
}
} else {
for(int m = Math.max(0, y - 1); m < Math.min(FIELD_SIZE, y + 2); m++) {
for(int n = Math.max(0, x - 1); n < Math.min(FIELD_SIZE, x + shipLength + 1); n++) {
field[m][n] = 9;
}
}
}
for(int j = 0; j < shipLength; j++) {
field[y][x] = shipLength;
if(vertical) {
y++;
} else {
x++;
}
}
break;
}
}
}
23 changes: 23 additions & 0 deletions src/main/java/SeaBattle/InputValidator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package SeaBattle;

public interface InputValidator {

static boolean isInvalidInputData(int col, int row, char[][] board) {
if(row < 0 || row > 9) {
System.out.println("Invalid input, allowed values like 'a5'");
return true;
} else if(col == -1) {
System.out.println("Invalid input, please repeat input ");
return true;
} else if(board[row][col] == 'x' || board[row][col] == 'X') {
System.out.println("Already bombed, please repeat input ");
return true;
} else if(board[row][col] == '0') {
System.out.println("HIT!");
board[row][col] = 'X';
} else {
board[row][col] = 'x';
}
return false;
}
}
10 changes: 10 additions & 0 deletions src/main/java/SeaBattle/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package SeaBattle;

public class Main {

public static void main(String[] args) {

StartGame startGame = new StartGame();
Copy link
Owner

@NikolaevArtem NikolaevArtem Sep 22, 2021

Choose a reason for hiding this comment

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

Looks like a fresh view on the course project, fits the requirement, but doesn't look good, not from the interface and not from the code. You show knowledge in Java Core, I don't see much architecture or right abstractions in the app. Playing is nice though, but x for miss and X for hit looks awful...

Approved!

startGame.startGame();
}
}
16 changes: 16 additions & 0 deletions src/main/java/SeaBattle/PrintArray.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package SeaBattle;

import java.util.Arrays;
import java.util.concurrent.atomic.AtomicInteger;

import static SeaBattle.StartGame.currentCountOfShots;

interface PrintArray {

static void printBoard(char[][] map) {
AtomicInteger i = new AtomicInteger(1);
System.out.println("Current board: x - miss, X - HIT!, Current Round = " + currentCountOfShots + "\n A B C D E F G H I J ");
Arrays.stream(map)
.forEach(m -> System.out.println( i.getAndIncrement() + " " + Arrays.toString(m).replace(",", "").replace("0", "_")));
}
}
44 changes: 44 additions & 0 deletions src/main/java/SeaBattle/StartGame.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package SeaBattle;

import java.util.Scanner;

import static SeaBattle.InputValidator.isInvalidInputData;
import static SeaBattle.WinnerChecker.isWinnerExist;

public class StartGame {

private static final String LETTERS = "ABCDEFGHIJ";
static final int COUNT = 70;
static int currentCountOfShots = 0;

public void startGame() {
System.out.println("Game on! \n You should shoot all ships in " + COUNT + " rounds");
GenerateShips test = new GenerateShips();
char[][] board = test.createBoardWithShips();

while(true) {
PrintArray.printBoard(board);

Scanner in = new Scanner(System.in);
try {
String playerChoice = in.next();

int col = LETTERS.indexOf(playerChoice.toUpperCase().charAt(0));
int row = Integer.parseInt(playerChoice.substring(1)) - 1;

if(isInvalidInputData(col, row, board)) {
continue;
}
currentCountOfShots++;

if(isWinnerExist(board, currentCountOfShots)) {
return;
}

} catch(NumberFormatException e) {
System.out.println("Invalid input, please enter in format e.g. 'A5'");
in.next();
}
}
}
}
20 changes: 20 additions & 0 deletions src/main/java/SeaBattle/WinnerChecker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package SeaBattle;

import java.util.Arrays;

import static SeaBattle.StartGame.COUNT;

public interface WinnerChecker {

static boolean isWinnerExist(char[][] board, int currentShot) {
if(Arrays.stream(board).flatMap((item)->new String(item).chars().mapToObj(a->(char)a)).noneMatch(e -> e == '0')){
System.out.println("Game Over, you are winner !!!");
return true;
}
if(COUNT <= currentShot){
System.out.println("Your shells are out, try again");
return true;
}
return false;
}
}
11 changes: 10 additions & 1 deletion src/main/java/homework_1/Main.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
package homework_1;

public class Main {
public static final String ANSI_RESET = "\u001B[0m";
public static final String ANSI_RED = "\u001B[31m";

public static void main(String[] args) {
System.out.println("Hello homework!");

for (String i: args) {
if(i.equals("error")){
System.out.println(ANSI_RED + "Alarm!" + ANSI_RESET);
break;
}
System.out.println(i + ": " + i.length());
}
}

}
24 changes: 24 additions & 0 deletions src/main/java/homework_2/pyramid_printer/InputPyramidReader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package homework_2.pyramid_printer;

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

public class InputPyramidReader {

public int bufferReaderForPyramid() {
try(BufferedReader reader = new BufferedReader(new InputStreamReader(System.in))) {
int i = Integer.parseInt(reader.readLine());
if(i < 0) {
System.out.println("Error, negative numbers are not valid");
} else {
return i;
}
} catch(NumberFormatException ex) {
System.out.println("Error, only numbers are allowed");
} catch(IOException e) {
System.out.println("System Error");
}
return 0;
}
}
11 changes: 11 additions & 0 deletions src/main/java/homework_2/pyramid_printer/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package homework_2.pyramid_printer;

import java.io.IOException;

public class Main {

public static void main(String[] args) throws IOException {
PyramidPrinter pyramidPrinter = new PyramidPrinter();
pyramidPrinter.run();
}
}
16 changes: 16 additions & 0 deletions src/main/java/homework_2/pyramid_printer/PyramidPrinter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package homework_2.pyramid_printer;

public class PyramidPrinter {

public void run() {
InputPyramidReader IOHelperForPyramid = new InputPyramidReader();
int pyramidLength = IOHelperForPyramid.bufferReaderForPyramid();

for(int i = 0; i < pyramidLength; i++) {
for(int j = 0; j <= i; j++) {
System.out.print("x");
}
System.out.println();
}
}
}
14 changes: 14 additions & 0 deletions src/main/java/homework_2/random_chars_table/InputData.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package homework_2.random_chars_table;

public class InputData {
Copy link
Owner

Choose a reason for hiding this comment

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

Looks good


public int row;
public int column;
public String type;

public InputData(int row, int column, String type) {
this.row = row;
this.column = column;
this.type = type;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package homework_2.random_chars_table;

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

public class InputRandomCharsReader {

public String bufferReaderConsoleForRandom() {
try(BufferedReader reader = new BufferedReader(new InputStreamReader(System.in))) {
return reader.readLine();
} catch(IOException ex) {
System.out.println("System Error");
return "";
}
}
}
11 changes: 11 additions & 0 deletions src/main/java/homework_2/random_chars_table/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package homework_2.random_chars_table;

import java.io.IOException;

public class Main {

public static void main(String[] args) throws IOException {
RandomCharsTable randomCharsTable = new RandomCharsTable();
randomCharsTable.run();
}
}
9 changes: 9 additions & 0 deletions src/main/java/homework_2/random_chars_table/ParserError.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package homework_2.random_chars_table;

public class ParserError extends Exception {

public ParserError(String errorMessage) {
super(errorMessage);
}
}

Loading