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
53 commits
Select commit Hold shift + click to select a range
076a20a
ds
kostya23azaza Jul 5, 2021
6bf9f8e
ds
kostya23azaza Jul 5, 2021
592a770
first_homework
kostya23azaza Jul 6, 2021
17484b9
first_homework
kostya23azaza Jul 6, 2021
fb055d7
Merge remote-tracking branch 'origin/master' into feature/Konstantin_…
kostya23azaza Jul 7, 2021
e5c1fbf
first_homework
kostya23azaza Jul 7, 2021
366079a
first_homework
kostya23azaza Jul 12, 2021
13419a1
first_homework
kostya23azaza Jul 12, 2021
ad6354c
second_homework
kostya23azaza Jul 13, 2021
3e4884f
first_homework_again i have no idea what do i do wrong
kostya23azaza Jul 13, 2021
cf48abe
Create README.md
kostya23azaza Jul 13, 2021
7b1def2
Update README.md
kostya23azaza Jul 13, 2021
9c67241
Update README.md
kostya23azaza Jul 13, 2021
cfdb99b
fixed RandomCharsTable task
kostya23azaza Jul 13, 2021
5bb35c2
delete Main
kostya23azaza Jul 13, 2021
98700eb
Merge remote-tracking branch 'origin/Feature/konstantinIvanov' into f…
kostya23azaza Jul 13, 2021
29a0bf5
Merge branch 'master' into feature/konstantinIvanov
ArtemNikolaev1 Jul 15, 2021
cb875be
add start method!
kostya23azaza Jul 20, 2021
4ed07ef
add start method!
kostya23azaza Jul 20, 2021
1276e48
Delete PyramidPrinter.java
kostya23azaza Jul 20, 2021
b2d1c27
Delete RandomCharsTable.java
kostya23azaza Jul 20, 2021
884d144
Update README.md
kostya23azaza Jul 20, 2021
d34e189
add start method!
kostya23azaza Jul 21, 2021
ca87723
Merge remote-tracking branch 'origin/master' into Feature/konstantinI…
kostya23azaza Jul 22, 2021
9c257ed
add start method!
kostya23azaza Jul 22, 2021
c834134
add junit test on homework
kostya23azaza Jul 22, 2021
9f89578
fixed little bugs
kostya23azaza Jul 26, 2021
6fb521e
add singleton class
kostya23azaza Aug 4, 2021
1644b84
add singleton class
kostya23azaza Aug 4, 2021
61d07ec
add customFileReader class
kostya23azaza Aug 9, 2021
4233fb2
add custom test annotation
kostya23azaza Aug 12, 2021
8082d30
Update README.md
kostya23azaza Aug 12, 2021
356fcff
fix small bug
kostya23azaza Aug 20, 2021
4f4eb47
add homework_5 with CustomRegexMatcher task plus PowerOfNumber task
kostya23azaza Aug 20, 2021
145e652
add homework_6
kostya23azaza Aug 31, 2021
b682108
fix bugs in homework 2 and 3
kostya23azaza Sep 3, 2021
c4dc14e
add homework_7
kostya23azaza Sep 14, 2021
eaba51c
Update README.md
kostya23azaza Sep 14, 2021
872a384
Update README.md
kostya23azaza Sep 14, 2021
2f9216a
Update README.md
kostya23azaza Sep 14, 2021
cc7d57e
Update README.md
kostya23azaza Sep 14, 2021
ac5a26f
Update README.md
kostya23azaza Sep 14, 2021
38c1376
Update README.md
kostya23azaza Sep 14, 2021
4272a70
Update README.md
kostya23azaza Sep 14, 2021
73d3be4
Create README.md
kostya23azaza Sep 14, 2021
a383a51
add course project seaBattle
kostya23azaza Sep 16, 2021
b179ea0
Merge remote-tracking branch 'origin/Feature/konstantinIvanov' into F…
kostya23azaza Sep 16, 2021
9e131ed
fix bugs in homework 3 and homework 6
kostya23azaza Sep 22, 2021
a3ed72d
fix bugs in homework 6 and in test for homework 2
kostya23azaza Sep 22, 2021
3b99362
add test for seaBattle
kostya23azaza Sep 22, 2021
9b54d78
fix homework 3
kostya23azaza Sep 24, 2021
f0bbc5f
fix homework 6
kostya23azaza Sep 24, 2021
ee58141
fix homework 7 and junit test in homework 2 PyramidPrinterTest
kostya23azaza 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
18 changes: 15 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
# Java Core June 2021

## *Nikolaev Artem*
## *Konstantin Ivanov*

| 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/blob/feature/konstantinIvanov/src/main/java/homework_1/Main.java) | The app that reads input arguments and prints them, until "error" argument |
| HW2 | [TrafficLight homework](https://github.com/NikolaevArtem/Java_Core_June_2021/blob/feature/konstantinIvanov/src/main/java/homework_2/traffic_light/Main.java) | App reads current time in seconds from the console and prints the traffic light |
| HW2 | [Pyramid homework](https://github.com/NikolaevArtem/Java_Core_June_2021/blob/feature/konstantinIvanov/src/main/java/homework_2/pyramid_printer/Main.java) | The app that reads input argument and builds pyramid base on input value |
| HW2 | [RandomCharsTable homework](https://github.com/NikolaevArtem/Java_Core_June_2021/blob/feature/konstantinIvanov/src/main/java/homework_2/random_chars_table/Main.java) | App reads from the console width and length of the chart, strategy keyword (even or odd). Prints to the console the chart of random chars from A to Z |
| HW3 | [ImmutableClass homework](https://github.com/NikolaevArtem/Java_Core_June_2021/blob/feature/konstantinIvanov/src/main/java/homework_3/ImmutableClass.java) | Immutable class |
| HW4 | [Singleton homework](https://github.com/NikolaevArtem/Java_Core_June_2021/blob/feature/konstantinIvanov/src/main/java/homework_4/Singleton/MySingleton.java) | Singleton |
| HW4 | [CustomFileReader homework](https://github.com/NikolaevArtem/Java_Core_June_2021/blob/feature/konstantinIvanov/src/main/java/homework_4/custom_file_reader/CustomFileReader.java) | The app that reads from a file using nio io |
| HW4 | [CustomAnnotation homework](https://github.com/NikolaevArtem/Java_Core_June_2021/blob/feature/konstantinIvanov/src/main/java/homework_4/custom_annotation/test_annotation/Main.java) | The app has three annotation, BeforeSuite, which runs once at the beginning, AfterSuite, which runs once at the end, and Test which runs methods by priority |
| HW5 | [CustomRegexMatcher homework](https://github.com/NikolaevArtem/Java_Core_June_2021/blob/feature/konstantinIvanov/src/main/java/homework_5/customRegexMatcher/CustomRegexMatcher.java) | The app with customRegexMatcher |
| HW5 | [PowerOfNumber homework](https://github.com/NikolaevArtem/Java_Core_June_2021/blob/feature/konstantinIvanov/src/main/java/homework_5/powerOfNumber/PowerOfNumber.java) | The app realizes PowerOfNumber method which using recursion |
| HW6 | [MapProblemsGenerator homework](https://github.com/NikolaevArtem/Java_Core_June_2021/blob/feature/konstantinIvanov/src/main/java/homework_6/MapProblemsCollisionGenerator.java) | The app has two classes, MapProblemsCollisionGenerator which creates 100% collision and MapProblemsMutableGenerator with hashcoreEquals |
| HW7 | [KittenToCatFunction homework](https://github.com/NikolaevArtem/Java_Core_June_2021/blob/feature/konstantinIvanov/src/main/java/homework_7/KittenToCat.java) | This app transform Kitten to Cat |

[Link to markdown giude](https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet)

[Link to CodingBat](https://codingbat.com/done?user=kostya-ivanov9623@mail.ru&tag=8599662828)
22 changes: 22 additions & 0 deletions src/main/java/course_project/seaBattle/Computer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package course_project.seaBattle;


import java.util.Arrays;

import static course_project.seaBattle.Field.*;

public class Computer {
private String name;

public String getName() {
return name;
}

public Computer(String name) {
this.name = name;
for (int i = 0; i < computer_field.length; i++) {
Arrays.fill(computer_field[i], '*');
Arrays.fill(fake_field[i], '*');
}
}
}
110 changes: 110 additions & 0 deletions src/main/java/course_project/seaBattle/Field.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package course_project.seaBattle;

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

public class Field {
public static final int SIZE = 10;
public static char[][] player_field = new char[SIZE][SIZE];
public static char[][] computer_field = new char[SIZE][SIZE];
public static char[][] fake_field = new char[SIZE][SIZE];
public static int playerShip = 20;
public static int computerShip = 20;

public static void printBattleField(char[][] field) {
for (int i = 0; i < SIZE; i++) {
if (i == 0) {
for (int k = 0; k < SIZE; k++) {
System.out.print("\t" + k);
}
System.out.println();
}
System.out.print(i);
for (int j = 0; j < SIZE; j++) {
System.out.print("\t" + field[i][j]);
}
System.out.println();
}
}

public static void printAllMaps(Player user, course_project.seaBattle.Computer computer) {
System.out.print("\t=========" + user.getName() + "========");
System.out.println("\t\t\t\t \t=======" + computer.getName() + "=======");
System.out.print(" ");
for (int i = 0; i < SIZE; i++) {
System.out.print(i + " ");
}
System.out.print("\t\t ");
for (int i = 0; i < SIZE; i++) {
System.out.print(i + " ");
}

System.out.print("\n");
for (int i = 0; i < 10; i++) {
System.out.print(i + " ");
for (int j = 0; j < 10; j++) {
System.out.print(player_field[i][j] + " ");
}
System.out.print("\t\t");
System.out.print(i + " ");
for (int k = 0; k < 10; k++) {
System.out.print(computer_field[i][k] + " ");
}
System.out.print("\n");
}
}
public static void playerShoot(Scanner scanner, char[][] field) {
while (true) {
try {
System.out.println("Print the coordinate x");
int x = scanner.nextInt();
System.out.println("Print the coordinate y");
int y = scanner.nextInt();
if (field[x][y] == 'X') {
System.out.println("GOAL");
System.out.println("Nice shot");
field[x][y] = '@';
fake_field[x][y] = '@';
computerShip--;
printBattleField(fake_field);
if (computerShip == 0) {
return;
}
} else {
System.out.println("You missed");
field[x][y] = '-';
fake_field[x][y] = '-';
printBattleField(fake_field);
return;
}
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("You can use number from 0 to 9");
break;
} catch (Exception e) {
throw new RuntimeException("Error, you must use only number");
}
}
}
public static void computerShoot(char[][] field, Random random) {
while (true) {
int x = random.nextInt(10);
int y = random.nextInt(10);
if (field[x][y] == 'X') {
System.out.println("Computer shot your ship");
field[x][y] = '@';
playerShip--;
printBattleField(player_field);
if (playerShip == 0) {
return;
}
} else if (field[x][y] == '@' || field[x][y] == '-') {

} else {
System.out.println("Computer missed");
field[x][y] = '-';
printBattleField(player_field);
return;
}
}
}
}
43 changes: 43 additions & 0 deletions src/main/java/course_project/seaBattle/Game.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package course_project.seaBattle;


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

import static course_project.seaBattle.Field.*;
import static course_project.seaBattle.Field.computerShip;
import static course_project.seaBattle.Ship.*;

public class Game {
Scanner scanner = new Scanner(System.in);
Random rand = new Random();

public static boolean checkWin(int playerShip, int computerShip) {
if (playerShip == 0) {
System.out.println("Computer win -_-");
return false;
} else if (computerShip == 0) {
System.out.println("You win!!");
return false;
} else {
return true;
}
}

public void run() {
System.out.println("Print your name");
Player User = new Player(scanner.nextLine());
course_project.seaBattle.Computer computer = new course_project.seaBattle.Computer("Computer");
randomGenerateShip(player_field, rand);
printAllMaps(User, computer);
randomGenerateShip(computer_field, rand);
System.out.println("Start game");
do {
playerShoot(scanner, computer_field);
computerShoot(player_field, rand);
}
while (checkWin(playerShip, computerShip));
System.out.println(" End ");
System.out.println("if you like this game you can buy it for three hundred bucks");
}
}
8 changes: 8 additions & 0 deletions src/main/java/course_project/seaBattle/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package course_project.seaBattle;

public class Main {

public static void main(String[] args) {
new Game().run();
Copy link
Owner

Choose a reason for hiding this comment

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

Strange behaviour in case of wrong input
image

Copy link
Owner

Choose a reason for hiding this comment

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

opt: No any message in case I fully destroyed opponent's ship

Copy link
Owner

Choose a reason for hiding this comment

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

Opt: when ship is destroyed, close cells are not marked as already hit

Copy link
Owner

Choose a reason for hiding this comment

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

Also strange, but okay
image

Copy link
Owner

Choose a reason for hiding this comment

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

But I do like simplicity of the approach, nice interface and abstractions.

Copy link
Owner

@NikolaevArtem NikolaevArtem Sep 20, 2021

Choose a reason for hiding this comment

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

Good! There are a few bugs to fix, except that looks good. You could also introduce some abstractions, like abstract classes/interfaces, but it's optional. As well as testing the app

Btw, why do you need fakeField?

Copy link
Owner

Choose a reason for hiding this comment

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

SeaBattle has the same bugs as before. Please fix

Copy link
Owner

Choose a reason for hiding this comment

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

Sorry, can't approve with current bugs

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


import java.util.Arrays;

import static course_project.seaBattle.Field.*;

public class Player {
private String name;
public String getName() {
return name;
}

public Player(String name) {
this.name = name;
for (int i = 0; i < player_field.length; i++) {
Arrays.fill(player_field[i], '*');
}
}
}
73 changes: 73 additions & 0 deletions src/main/java/course_project/seaBattle/Ship.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package course_project.seaBattle;


import java.util.Random;

import static course_project.seaBattle.Field.SIZE;

public class Ship {
int positionY;
int positionX;
int size;
boolean isVertical;
int health;
public static Ship[] ships = new Ship[10];

public Ship(int positionX, int positionY, int size, boolean isVertical) {
this.positionX = positionX;
this.positionY = positionY;
this.size = size;
this.isVertical = isVertical;
this.health = size;
}

public static void randomGenerateShip(char[][] field, Random rand) {
for (int i = 0; i < 10; i++) {
boolean isShipPlaced = false;
do {
if (i == 0) ships[i] = new Ship(rand.nextInt(10), rand.nextInt(10), 4, rand.nextBoolean());
if (i > 0 && i <= 2)
ships[i] = new Ship(rand.nextInt(10), rand.nextInt(10), 3, rand.nextBoolean());
if (i > 2 && i <= 5)
ships[i] = new Ship(rand.nextInt(10), rand.nextInt(10), 2, rand.nextBoolean());
if (i > 5) ships[i] = new Ship(rand.nextInt(10), rand.nextInt(10), 1, rand.nextBoolean());
if (canSetShip(ships[i], field)) {
setShipOnField(ships[i], field);
isShipPlaced = true;
}
} while (!isShipPlaced);
}
}
private static void setShipOnField(Ship ship, char[][] field) {
if (ship.isVertical) {
for (int i = 0; i < ship.size; i++) {
field[ship.positionX][ship.positionY + i] = 'X';
}
} else {
for (int i = 0; i < ship.size; i++) {
field[ship.positionX + i][ship.positionY] = 'X';
}
}
}

private static boolean canSetShip(Ship ship, char[][] field) {
if (ship.positionX < 0 || ship.positionY < 0 || SIZE <= ship.positionX || SIZE <= ship.positionY)
return false;
if (ship.isVertical && SIZE <= ship.positionY + ship.size) return false;
if (!ship.isVertical && SIZE <= ship.positionX + ship.size) return false;
int minX = Math.max(0, ship.positionX - 1);
int minY = Math.max(0, ship.positionY - 1);
int maxX = Math.min(SIZE - 1, ship.positionX + 1 + (ship.isVertical ? 0 : ship.size));
int maxY = Math.min(SIZE - 1, ship.positionY + 1 + (ship.isVertical ? ship.size : 0));
for (int x = minX; x <= maxX; x++) {
for (int y = minY; y <= maxY; y++) {
if (isShipPresent(x, y, field)) return false;
}
}
return true;
}

private static boolean isShipPresent(int x, int y, char[][] field) {
return field[x][y] == 'X';
}
}
16 changes: 13 additions & 3 deletions src/main/java/homework_1/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,18 @@

public class Main {

public static void main(String[] args) {
System.out.println("Hello homework!");
}
public static final String ANSI_RED = "\u001B[31m";

public static final String ANSI_RESET = "\u001B[0m";

public static void main(String[] args) {
for (String s : args) {
if (s.equals("error")) {
System.out.println(ANSI_RED + "Alarm!" + ANSI_RESET);
break;
} else {
System.out.println(s + ": " + s.length() + " letters");
}
}
}
}
10 changes: 10 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,10 @@
package homework_2.pyramid_printer;

import java.io.IOException;

public class Main {

public static void main(String[] args) {
new PyramidPrinter().run();
}
}
35 changes: 35 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,35 @@
package homework_2.pyramid_printer;


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

public class PyramidPrinter {

public void run() {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in))) {
int rows = Integer.parseInt(reader.readLine());
if (rows<0) {
System.out.println("Only 1 non-negative integer is allowed as passed parameter");
return;
}
printPyramid(rows);
} catch (IOException e) {
e.printStackTrace();
return;
} catch (NumberFormatException e){
System.out.println("Only 1 non-negative integer is allowed as passed parameter");
return;
}
}

static void printPyramid(int rows){
for (int i = 1; i <= rows; i++) {
for (int j = 0; j < i; j++) {
System.out.print("x");
}
System.out.println();
}
}
}
10 changes: 10 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,10 @@
package homework_2.random_chars_table;

import java.io.IOException;

public class Main {

public static void main(String[] args) {
new RandomCharsTable().run();
}
}
Loading