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
35 commits
Select commit Hold shift + click to select a range
4a301c4
Test commit
GamzatAsadulaev Jul 5, 2021
e415e29
Test commit 2
GamzatAsadulaev Jul 5, 2021
a10dd3a
Home work 1
GamzatAsadulaev Jul 5, 2021
9c504e1
Merge branch 'master' into feature/GamzatAsadulaev
GamzatAsadulaev Jul 7, 2021
bfb5444
Added README.md
GamzatAsadulaev Jul 7, 2021
ba3855b
Homework 2
GamzatAsadulaev Jul 15, 2021
05f80cc
Update README.md
GamzatAsadulaev Jul 16, 2021
41cc0af
Update Homework 2
GamzatAsadulaev Jul 18, 2021
a070d28
Merge branch 'master' into feature/GamzatAsadulaev
GamzatAsadulaev Jul 23, 2021
abaa02f
Update TrafficLight
GamzatAsadulaev Jul 24, 2021
a3c38f4
Update PyramidPrinter
GamzatAsadulaev Jul 24, 2021
1a4c1fe
Update RandomCharsTable
GamzatAsadulaev Jul 24, 2021
fe7492b
Add tests for Homework 2
GamzatAsadulaev Jul 24, 2021
33b8843
Homework 3
GamzatAsadulaev Jul 24, 2021
1600ec2
add test for homework 3
GamzatAsadulaev Jul 24, 2021
26bcf10
Update README
GamzatAsadulaev Jul 24, 2021
3cadaf8
homework 4
GamzatAsadulaev Aug 14, 2021
ebb46c1
update README
GamzatAsadulaev Aug 15, 2021
a6e2803
homework 5
GamzatAsadulaev Aug 27, 2021
9798739
add tests for homework 5
GamzatAsadulaev Aug 27, 2021
a8e54e2
fix homework 5
GamzatAsadulaev Aug 27, 2021
0362a55
add homework 5 to README.md
GamzatAsadulaev Aug 27, 2021
bbcf762
add lombok in dependencies
GamzatAsadulaev Sep 3, 2021
ab8c985
add homework_6
GamzatAsadulaev Sep 3, 2021
ebf65a5
add test for homework_6
GamzatAsadulaev Sep 3, 2021
7dc71cc
add homework 6 to README.md
GamzatAsadulaev Sep 3, 2021
72a8095
homework 7
GamzatAsadulaev Sep 20, 2021
dbeb65e
update README.md
GamzatAsadulaev Sep 20, 2021
e70afe3
course_project
GamzatAsadulaev Sep 21, 2021
62cffc2
fixed homework 3
GamzatAsadulaev Sep 21, 2021
2f7a81e
fixed homework 3
GamzatAsadulaev Sep 21, 2021
9715ccd
fixed homework3
GamzatAsadulaev Sep 22, 2021
8d55a6b
fixed homework_3
GamzatAsadulaev Sep 22, 2021
cda54b2
Merge branch 'feature/GamzatAsadulaev' of https://github.com/Nikolaev…
GamzatAsadulaev Sep 22, 2021
29d37c2
fixed constructors and getter in homework 3
GamzatAsadulaev 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
20 changes: 17 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
# Java Core June 2021

## *Nikolaev Artem*
## *Asadulaev Gamzat*

| 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/GamzatAsadulaev/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/GamzatAsadulaev/src/main/java/homework_2/traffic_light) | The app that reads the time from the console (seconds/hh:mm:ss) and calculates the output color of the traffic light |
| | [Pyramid printer](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/GamzatAsadulaev/src/main/java/homework_2/pyramid_printer) | The app that prints a pyramid based on user input |
| | [Random Chars Table](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/GamzatAsadulaev/src/main/java/homework_2/random_chars_table) | The app that reads length, width, strategy from the console generates the table and prints even/odd letters |
| HW3 | [ImmutableClass](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/GamzatAsadulaev/src/main/java/homework_3) | Immutable Class "Book" |
| HW4 | [CustomFileReader](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/GamzatAsadulaev/src/main/java/homework_4/custom_file_reader) |The app that reads the file in different ways and prints the text without(, .) |
| | [CustomSingleton](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/GamzatAsadulaev/src/main/java/homework_4/singleton) | Custom Singleton |
| | [CustomAnnotation](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/GamzatAsadulaev/src/main/java/homework_4/custom_annotation) | Custom Annotation |
| HW5 | [PowerOfNumber](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/GamzatAsadulaev/src/main/java/homework_5/power_of_number) | The app that reads the number from the console and raises it to the power |
| | [CustomRegexMatcher](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/GamzatAsadulaev/src/main/java/homework_5/custom_regex_matcher) | The app that reads the number from the console and checks it for validity |
| HW6 | [MapProblemsGenerator](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/GamzatAsadulaev/src/main/java/homework_6/map_problems_generator) | |
| HW7 | [KittenToCatFunction](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/GamzatAsadulaev/src/main/java/homework_7) | Custom functional interface that transform Kitten to Cat |
| CP | [Battleship](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/GamzatAsadulaev/src/main/java/homework_7/course_project_battleship) | Course project "Battleship" |

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

[CodingBat](https://codingbat.com/done?user=asadulaevgamzat@gmail.com&tag=2887423202)
6 changes: 6 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ repositories {
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.0'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.0'
testImplementation 'org.junit.jupiter:junit-jupiter-params:5.7.0'
testImplementation 'org.hamcrest:hamcrest:2.2'
testImplementation 'org.hamcrest:hamcrest-library:2.2'
Comment on lines +16 to +17
Copy link
Owner

Choose a reason for hiding this comment

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

Hamcrest matchers are nice!


compileOnly 'org.projectlombok:lombok:1.18.20'
annotationProcessor 'org.projectlombok:lombok:1.18.20'
}

test {
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/course_project/battleship/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package course_project.battleship;


import course_project.battleship.game.Game;

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.

On player's field hits/misses are not seen
image

Copy link
Owner

Choose a reason for hiding this comment

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

Input is not quite simple... Automatic ship placement is nice!

Copy link
Owner

Choose a reason for hiding this comment

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

Opt: Your utils folder is oversized, some classes should be moved to other places

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 show good knowledge of Java Core and Java libraries, code is mostly clean and easy to read. Abstractions are chosen correctly, also good architecture. I didn't see any bugs in the game, but it would look better if you refactor the interface.

Approved!

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


import course_project.battleship.player.Player;

public class AIController implements PlayerController {

@Override
public void placeShips(Player player) {
throw new UnsupportedOperationException();
}

@Override
public void makeTurns(Player player1, Player player2) {
throw new UnsupportedOperationException();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package course_project.battleship.controller;

import course_project.battleship.game.GameRule;
import course_project.battleship.player.Player;
import course_project.battleship.utils.*;


public class HumanController implements PlayerController {

@Override
public void placeShips(Player player) {
System.out.println(player.getName() + " choose the ship placement mode:\n1.Manually\n2.Randomly");
int setupMode = Menu.getInt(2);
if (setupMode == 1) {
new ManualShipPlacement().placeShips(player);
} else {
new RandomShipPlacement().placeShips(player);
}

System.out.println("Ships are placed!\n");
}

public void makeTurns(Player player1, Player player2) {
System.out.println("Your field: ");
Display.drawField(player1.getBattlefield());

while (true) {
System.out.println();
System.out.println("" + player1.getName() + ", please, make your turn.");
Display.drawField(player1.getMonitorField());

Coordinate coordinate = Menu.createCoordinate();

if (coordinate == null || !Validator.isValidCoordinate(coordinate)) {
continue;
}

if (!player1.makeTurn(player2,
coordinate)) {
System.out.println("\nMiss! Your opponents turn!\n");
break;
}

if (GameRule.isWinCondition(player1)) {
break;
}
// display.clearConsole();
}
}


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

import course_project.battleship.player.Player;

public interface PlayerController {

void placeShips(Player player);

void makeTurns(Player player1, Player player2);
}
73 changes: 73 additions & 0 deletions src/main/java/course_project/battleship/game/Game.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package course_project.battleship.game;


import course_project.battleship.controller.HumanController;
import course_project.battleship.controller.PlayerController;
import course_project.battleship.player.Human;
import course_project.battleship.player.Player;
import course_project.battleship.utils.Menu;

import java.util.Scanner;

public class Game {
private final GameMode gameMode = new GameMode();
private static final Scanner scanner = new Scanner(System.in);

public void run() {
System.out.println("----------SeaBattle---------");
System.out.println("Choose Mode\n1. PvP\n2. PvE");

int mode = Menu.getInt(2);
if (mode == 1) {
gameMode.setMode(GameMode.Mode.PVP);
} else {
gameMode.setMode(GameMode.Mode.PVE);
}
startGame();
}

public void startGame() {
if (gameMode.getMode() == GameMode.Mode.PVP) {
playVsHuman();
} else {
playVsComputer();
}
}

private void playVsHuman() {
System.out.println("Player 1, please, enter your name:");
String playerName1 = scanner.nextLine();
Player player1 = new Human(playerName1);
System.out.println("Player 2, please, enter your name:");
String playerName2 = scanner.nextLine();
Player player2 = new Human(playerName2);

System.out.println("-----------------------------");

PlayerController playerController = new HumanController();
playerController.placeShips(player1);
playerController.placeShips(player2);

String winner;

while (true) {
playerController.makeTurns(player1, player2);
if (GameRule.isWinCondition(player1)) {
winner = player1.getName();
break;
}
playerController.makeTurns(player2, player1);
if (GameRule.isWinCondition(player2)) {
winner = player1.getName();
break;
}
}
System.out.println(winner + " win this game!");
}

public void playVsComputer() {
throw new UnsupportedOperationException();
}
}


21 changes: 21 additions & 0 deletions src/main/java/course_project/battleship/game/GameMode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package course_project.battleship.game;

public class GameMode {
private Mode mode;

public GameMode() {
}

public enum Mode {
PVP,
PVE
}

public Mode getMode() {
return mode;
}

public void setMode(Mode mode) {
this.mode = mode;
}
}
21 changes: 21 additions & 0 deletions src/main/java/course_project/battleship/game/GameRule.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package course_project.battleship.game;


import course_project.battleship.player.Player;
import course_project.battleship.utils.Position;

public class GameRule {
private final static int WIN_STREAK = 20;

public static boolean isWinCondition(Player player) {
int counter = 0;
for (int i = 0; i < player.getMonitorField().getField().length; i++) {
for (int j = 0; j < player.getMonitorField().getField().length; j++) {
if (player.getMonitorField().getField()[i][j].getState() == Position.State.HIT) {
counter++;
}
}
}
Comment on lines +11 to +18
Copy link
Owner

Choose a reason for hiding this comment

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

Opt: that's not really efficient... you iterate through all field each turn. There are much easier ways to achieve the same result

return counter >= WIN_STREAK;
}
}
29 changes: 29 additions & 0 deletions src/main/java/course_project/battleship/model/Field.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package course_project.battleship.model;

import course_project.battleship.utils.Position;

public class Field {
public static final int FILED_SIZE = 10;
private Position[][] field;

public Field() {
this.field = new Position[FILED_SIZE][FILED_SIZE];
initTable();
}

private void initTable() {
for (int i = 0; i < field.length; i++) {
for (int j = 0; j < field[i].length; j++) {
field[i][j] = new Position();
}
}
}

public Position[][] getField() {
return field;
}

public void setField(Position[][] field) {
this.field = field;
}
}
46 changes: 46 additions & 0 deletions src/main/java/course_project/battleship/model/Ship.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package course_project.battleship.model;

public class Ship {
private int deck;
private boolean isAlive;

private int countHits = 1;

public Ship(int deck) {
this.isAlive = true;
this.deck = deck;
}

public boolean getDamage() {
if (countHits != deck) {
countHits++;
return true;
}
isAlive = false;
return false;
}

public int getDeck() {
return deck;
}

public void setDeck(int deck) {
this.deck = deck;
}

public boolean isAlive() {
return isAlive;
}

public void setAlive(boolean alive) {
isAlive = alive;
}

public int getCountHits() {
return countHits;
}

public void setCountHits(int countHits) {
this.countHits = countHits;
}
}
22 changes: 22 additions & 0 deletions src/main/java/course_project/battleship/player/Computer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package course_project.battleship.player;


import course_project.battleship.model.Ship;
import course_project.battleship.utils.Coordinate;

public class Computer extends Player {

public Computer() {
super("Computer");
}

@Override
public boolean makeTurn(Player enemy, Coordinate coordinate) {
throw new UnsupportedOperationException();
}

@Override
public void placeShip(Coordinate coordinate, Ship ship, int direction) {
throw new UnsupportedOperationException();
}
}
Loading