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
e66525b
hometask_1 second commit
ashandrikov Jul 7, 2021
ef9907f
commit without gradlew.bat
ashandrikov Jul 9, 2021
6f86142
First homework has been fixed
ashandrikov Jul 20, 2021
79b46d5
Second homework. ReadMe added.
ashandrikov Jul 20, 2021
3877a6b
Immutable class.
ashandrikov Jul 21, 2021
7ef7cb3
Fixed secondHomework.
ashandrikov Jul 21, 2021
e4fd2d1
Merge branch 'master' into feature/TomShandrikov
ashandrikov Jul 21, 2021
af7bd1e
Merge branch 'master' into feature/TomShandrikov
ashandrikov Jul 22, 2021
68b209a
Full third Homework.
ashandrikov Jul 22, 2021
7f81616
Merge remote-tracking branch 'origin/feature/TomShandrikov' into feat…
ashandrikov Jul 22, 2021
f5f87bd
Added parameterized tests at hw3.
ashandrikov Aug 5, 2021
0d7db0d
hw4. Singleton
ashandrikov Aug 10, 2021
e7dab2f
hw4. CustomFileReader + basic tests
ashandrikov Aug 10, 2021
63b7ca4
hw4. Readme fixed
ashandrikov Aug 10, 2021
2161b45
hw4. +CustomAnnotations
ashandrikov Aug 11, 2021
07344b2
hw4. Bug Fixed
ashandrikov Aug 13, 2021
2ef335e
hw5. Tests & ReadMe update are included
ashandrikov Aug 20, 2021
eff024d
hw6. Tests are included.
ashandrikov Aug 29, 2021
2056e0c
hw6. Reduced the amount of code.
ashandrikov Aug 30, 2021
a538113
hw6. Renamed classes.
ashandrikov Aug 31, 2021
e8f7ba7
hw7. KittenToCat function
ashandrikov Sep 13, 2021
716d62b
hw7. Added Kitten to Cat' constructor
ashandrikov Sep 13, 2021
b238f2d
hw7. Added Kitten to Cat' constructor
ashandrikov Sep 18, 2021
6edc76c
Course_project added. No tests yet.
ashandrikov Sep 20, 2021
e84abaa
Course_project added. No tests yet. Small bug fixed.
ashandrikov Sep 20, 2021
021ab62
Few tests added.
ashandrikov Sep 20, 2021
36366f9
Finished.
ashandrikov Sep 21, 2021
9d3d835
Small bug fixed.
ashandrikov Sep 21, 2021
fc2f103
Two classes are deleted. Added computer ship placement mode with its …
ashandrikov Sep 22, 2021
f546077
Added game mode Player vs Computer(easy).
ashandrikov Sep 22, 2021
e9a861d
hw3 fixed.
ashandrikov Sep 22, 2021
d322425
hw7 fixed.
ashandrikov Sep 22, 2021
33e175b
course_project. Small bugs fixed.
ashandrikov Sep 22, 2021
37642d1
course_project. Small bugs fixed.
ashandrikov Sep 22, 2021
6b82beb
course_project. Rearranged some methods and added utils and controlle…
ashandrikov Sep 23, 2021
4e09aa0
course_project. Hope, last commit. Few changes more.
ashandrikov 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
23 changes: 20 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,26 @@
# Java Core June 2021

## *Nikolaev Artem*
## *Tom Shandrikov*

| 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](./src/main/java/homework_1) | This console app prints our words and number of letters until your input equals "error". |
| HW2.1 | [Traffic Light](./src/main/java/homework_2/traffic_light) | This console app reads an integer of seconds and prints a color of traffic light.|
| HW2.2 | [Pyramid Printer](./src/main/java/homework_2/pyramid_printer) | This console app prints a pyramid of "x". |
| HW2.3 | [Random Chars Table](./src/main/java/homework_2/random_chars_table) | This console app makes a table of chars from A to Z and shows evens/odds of them depending on the strategy we have chosen. |
| HW3.1 | [Immutable class](./src/main/java/homework_3) | This is an example of simple immutable class. |
| HW3.2 | [JUnit Tests](./src/test/java/homework_2) | Tests for homework2. |
| HW4.1 | [CustomFileReader](./src/main/java/homework_4/customFileReader) | Here you can see 4 different ways to read text from file.txt. |
| HW4.2 | [Singleton](./src/main/java/homework_4/singleton) | Here are Basic and Thread-safe examples of Singleton. |
| HW4.2 | [CustomAnnotation](./src/main/java/homework_4/customAnnotation) | This is an example of creating and using custom Annotations. |
| HW5.1 | [PowerOfNumber](./src/main/java/homework_5/powerOfNumber) | This console app realizes Math.pow(int base, int n) method using recursion. |
| HW5.2 | [CustomRegexMatcher](./src/main/java/homework_5/customRegexMatcher) | This console app verifies custom email with regex. |
| HW6 | [MapProblemsGenerator](./src/main/java/homework_6/mapProblemsGenerator) | Here you can find example of incorrect hash method overriding and a case where we can loose information in our map. |
| HW7 | [KittenToCatFunction](./src/main/java/homework_7/kittenToCatFunction) | This is an example of realization my functional interface that takes a Kitty as a parameter and returns a Cat. |
| course_project | [Sea Battle](./src/main/java/course_project/sea_battle) | Course project "Sea Battle Game" is here. You can find description and rules in file: rules.txt. |

[Link to my CodingBat](https://codingbat.com/done?user=ashandrikov@gmail.com&tag=8115696431)

[Link to markdown giude (article)](https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet) <br/>
[Link to markdown giude (video)](https://www.youtube.com/watch?v=FFBTGdEMrQ4)

[Link to markdown giude](https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet)
8 changes: 8 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,16 @@ repositories {
}

dependencies {
compileOnly 'org.projectlombok:lombok:1.18.20'
annotationProcessor 'org.projectlombok:lombok:1.18.20'

testCompileOnly 'org.projectlombok:lombok:1.18.20'
testAnnotationProcessor 'org.projectlombok:lombok:1.18.20'

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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions src/main/java/course_project/sea_battle/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package course_project.sea_battle;

import course_project.sea_battle.controller.GameController;

public class Main {
public static void main(String[] args) {
new GameController().start();
}
}
24 changes: 24 additions & 0 deletions src/main/java/course_project/sea_battle/boards/Board.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package course_project.sea_battle.boards;

public abstract class Board {

protected int[][] board;

public void setBasicGrids() {
board = new int[10][10];
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
board[i][j] = 0;
}
}
}

public int[][] getBoard() {
return board;
}

public void setBoard(int[][] board) {
this.board = board;
}

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

import course_project.sea_battle.model.Point;
import course_project.sea_battle.model.Ship;

import java.util.List;

public class MyBoard extends Board {

public void placeShip(Ship ship) {
List<Point> coordinates = ship.getCoordinates();
for (Point point : coordinates) {
board[point.getX()][point.getY()] = 1;
}
}

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

import course_project.sea_battle.boards.MyBoard;
import course_project.sea_battle.model.Player;
import course_project.sea_battle.model.Ship;
import course_project.sea_battle.service.inputs.InputShipReader;
import course_project.sea_battle.service.GameModeAndNameSetter;
import course_project.sea_battle.service.inputs.InputShooterReader;
import course_project.sea_battle.service.ShipPlacer;

import java.util.Scanner;

import static course_project.sea_battle.utils.Constants.*;

public class GameController extends Thread {

private final Scanner scanner = new Scanner(System.in);
private final InputShipReader inputShipReader = new InputShipReader(scanner);

private Player player1;
private Player player2;

public GameController() {
this.player1 = new Player(new MyBoard(), new MyBoard());
this.player2 = new Player(new MyBoard(), new MyBoard());
}

@Override
public void run() {
setUpNames();
setUpShips();
play();
defineWinner();
}

public void setUpNames() {
new GameModeAndNameSetter(inputShipReader).setNames(player1, player2);
}

public void setUpShips() {
new ShipPlacer(inputShipReader).placeShip(player1, player2);
new ShipPlacer(inputShipReader).placeShip(player2, player1);
}

public void play() {
while (bothPlayersHaveShips(player1, player2)) {
new InputShooterReader(scanner).checkAndValidatePlayerShot(player1, player2);
new InputShooterReader(scanner).checkAndValidatePlayerShot(player2, player1);
}
}

public boolean bothPlayersHaveShips(Player player1, Player player2) {
return countShips(player1) > 0 && countShips(player2) > 0;
}

public long countShips(Player player) {
return player.getMyShips().stream().filter(Ship::isAlive).count();
}

public void defineWinner() {
if (countShips(player1) == 0 && countShips(player2) == 0) {
System.out.println(TIE);
} else if (countShips(player1) == 0) {
System.out.println(GAMEOVER + player2.getName() + "!");
} else if (countShips(player2) == 0) {
System.out.println(GAMEOVER + player1.getName() + "!");
}
}

public Player getPlayer1() {
return player1;
}

public Player getPlayer2() {
return player2;
}

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

import course_project.sea_battle.boards.MyBoard;

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

public class Player {
private String name;
private MyBoard myBoard;
private MyBoard myShots;
private boolean isComputer;
private final List<Ship> myShips = new ArrayList<>();

public Player(MyBoard myBoard, MyBoard enemyBoard) {
this.myBoard = myBoard;
this.myShots = enemyBoard;
myBoard.setBasicGrids();
myShots.setBasicGrids();
}

public long countShips() {
return getMyShips().stream().filter(Ship::isAlive).count();
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public List<Ship> getMyShips() {
return myShips;
}

public MyBoard getMyBoard() {
return myBoard;
}

public MyBoard getMyShots() {
return myShots;
}

public boolean isComputer() {
return isComputer;
}

public void setComputer(boolean computer) {
isComputer = computer;
}
}
29 changes: 29 additions & 0 deletions src/main/java/course_project/sea_battle/model/Point.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package course_project.sea_battle.model;

import lombok.Data;

@Data
public class Point {
private final int x;
private final int y;

@Override
public String toString() {
return "[" + x +
"," + y +
"]";
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Point point = (Point) o;
return x == point.x && y == point.y;
}

@Override
public int hashCode() {
return Integer.hashCode(x) + Integer.hashCode(y);
}
}
30 changes: 30 additions & 0 deletions src/main/java/course_project/sea_battle/model/Ship.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package course_project.sea_battle.model;

import java.util.List;

public class Ship {
private final List<Point> coordinates;
private int lives;

public Ship(List<Point> coordinates) {
this.coordinates = coordinates;
this.lives = coordinates.size();
}

public int getLives() {
return lives;
}

public void setLives(int lives) {
this.lives = lives;
}

public boolean isAlive() {
return lives > 0;
}

public List<Point> getCoordinates() {
return coordinates;
}

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

public enum Shot {
MISS,
HIT,
KILLED
}
18 changes: 18 additions & 0 deletions src/main/java/course_project/sea_battle/rules
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
GENERAL
2 gameController modes are available: Player vs Player and Player vs Computer (very easy).
Each player has 10 ships: 1*4deck, 2*3, 3*2, 4*1.

SHIP PLACEMENT
You can choose how to place ships: manually or automatically.
It's not allowed to place ships close to each other (diagonal cells count as well).
You put a ship by pointing its topLeft point and with position ("v" - vertical, "h" - horizontal).
So your input should be like this: [A7 V] or [b1 h].

RULES
Shot format: [A7] or [b1].
You cannot shoot out of board or cells that you have shot before.
When you destroy a ship all cells around are filled automatically and you cannot shoot them anymore (cause there are no ships).
Each player has same count of moves.
Game over when one of player (or both) don't have "alive" ships.


Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package course_project.sea_battle.service;

import course_project.sea_battle.model.Player;
import course_project.sea_battle.service.inputs.InputReader;
import lombok.SneakyThrows;

import static course_project.sea_battle.utils.Constants.*;

public class GameModeAndNameSetter {
private final InputReader inputReader;

private int gameMode;

public GameModeAndNameSetter(InputReader inputReader) {
this.inputReader = inputReader;
}

@SneakyThrows
public void setNames(Player player1, Player player2) {

defineGameMode();

System.out.println(PL1);
String player1Name = inputReader.readLine();
player1.setName(player1Name);
System.out.println("Hello, " + player1Name + "!");

if (gameMode == 1) {
System.out.println(PL2);
String player2Name = inputReader.readLine();
player2.setName(player2Name);
System.out.println("Hello, " + player2Name + "!");
} else if (gameMode == 2) {
player2.setName("Computer");
player2.setComputer(true);
}
}

private void defineGameMode() {
while (true) {
System.out.println(SHOOSEGAMEMODE);
String gameModeInput = inputReader.readLine();
if (gameModeInput.matches("[1]|[2]")) {
gameMode = Integer.parseInt(gameModeInput);
break;
}
}
}

}
Loading