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
30 commits
Select commit Hold shift + click to select a range
d4cf36e
111
GabdrakhimovaRR Jul 7, 2021
0418c02
Completed homework_1
GabdrakhimovaRR Jul 8, 2021
a510721
Update README.md
GabdrakhimovaRR Jul 8, 2021
468f48e
homework_1(2)
GabdrakhimovaRR Jul 9, 2021
3f41d39
Merge remote-tracking branch 'origin/feature/ReginaGabdrakhimova' int…
GabdrakhimovaRR Jul 10, 2021
ca5976b
homework_1(fixed indentation)
GabdrakhimovaRR Jul 26, 2021
766336a
homework_2
GabdrakhimovaRR Jul 26, 2021
76b4e64
Update README.md
GabdrakhimovaRR Jul 26, 2021
01931b1
Merge branch 'master' into feature/ReginaGabdrakhimova
GabdrakhimovaRR Aug 23, 2021
45b468c
Merge remote-tracking branch 'origin/feature/ReginaGabdrakhimova' int…
GabdrakhimovaRR Sep 2, 2021
4d0669d
homework_2(2)
GabdrakhimovaRR Sep 2, 2021
0198d99
RandomCharsTableTest has been added
GabdrakhimovaRR Sep 3, 2021
4a643a2
Scanner was closed
GabdrakhimovaRR Sep 3, 2021
8a71d94
homework_3
GabdrakhimovaRR Sep 4, 2021
1fed33d
Update README.md
GabdrakhimovaRR Sep 4, 2021
cf983dc
Update README.md
GabdrakhimovaRR Sep 4, 2021
ee0af2b
homework_4: Singleton
GabdrakhimovaRR Sep 5, 2021
657ba0f
homework_5: CastomRegexMatcher
GabdrakhimovaRR Sep 9, 2021
68a1def
Update README.md
GabdrakhimovaRR Sep 9, 2021
ed66aa6
homework_4. CustomFileReader
GabdrakhimovaRR Sep 13, 2021
5430f07
homework_4. CustomAnnotation
GabdrakhimovaRR Sep 14, 2021
61294c2
homework_5. PowerOfNumber
GabdrakhimovaRR Sep 14, 2021
73c93ca
homework_6. MapProblemsGenerator
GabdrakhimovaRR Sep 18, 2021
33cf811
homework_3,4,5 Tests
GabdrakhimovaRR Sep 18, 2021
8c3063d
Test of homework_4.CustomFileReader
GabdrakhimovaRR Sep 18, 2021
232e3f7
Course Project "Sea Battle"
GabdrakhimovaRR Sep 22, 2021
75d466e
homework_7. KittenToCatFunction
GabdrakhimovaRR Sep 22, 2021
314ceff
Fixed bugs in homework 3, 6 and course project.
GabdrakhimovaRR Sep 23, 2021
395aa28
Fixed bug in course project.
GabdrakhimovaRR Sep 23, 2021
1d70974
Fixed bug in course project(2).
GabdrakhimovaRR 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*
## *Gabdrakhimova Regina*

| 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/ReginaGabdrakhimova/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/ReginaGabdrakhimova/src/main/java/homework_2/traffic_light ) | The app reads input argument of seconds from the beginning of the day and shows the traffic light |
| | [Pyramid Printer](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/ReginaGabdrakhimova/src/main/java/homework_2/pyramid_printer ) | The app reads input argument and builds the required pyramid of letters |
| | [Random Chars Table](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/ReginaGabdrakhimova/src/main/java/homework_2/random_chars_table ) | The app reads input arguments and prints a table of random letters based on the data |
| | [Tests](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/ReginaGabdrakhimova/src/test/java/homework_2_tests ) | Tests to homework_2 |
| HW3 | [Immutable class](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/ReginaGabdrakhimova/src/main/java/homework_3/immutable_class ) | Immutable class describing the required contents of my bag |
| | [Tests](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/ReginaGabdrakhimova/src/test/java/homework_3_tests ) | Tests to homework_3 |
| HW4 | [Singleton](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/ReginaGabdrakhimova/src/main/java/homework_4/singleton ) | Singleton app |
| | [Custom File Reader](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/ReginaGabdrakhimova/src/main/java/homework_4/custom_file_reader ) | The app reads a file in different ways and outputs text excluding the characters "," and ".". |
| | [Custom Annotation](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/ReginaGabdrakhimova/src/main/java/homework_4/custom_annotation ) | Student class uses custom annotation StudentInformation to fill fields in class constructor. |
| | [Tests](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/ReginaGabdrakhimova/src/test/java/homework_4_tests ) | Tests to homework_4 |
| HW5 | [Custom Regex Matcher](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/ReginaGabdrakhimova/src/main/java/homework_5/custom_regex_matcher ) | The app reads input string and checks password strength |
| | [Power Of Number](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/ReginaGabdrakhimova/src/main/java/homework_5/power_of_number ) | The app reads input arguments(number and power) and prints number raised to the power |
| | [Tests](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/ReginaGabdrakhimova/src/test/java/homework_5_tests ) | Tests to homework_5 |
| HW6 | [Map Problems Generator](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/ReginaGabdrakhimova/src/main/java/homework_6/map_problems_generator ) | The app shows case with collision in hashmap and case when it is impossible to get object by key from hashmap |
| HW7 | [Kitten To Cat Function](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/ReginaGabdrakhimova/src/main/java/homework_7 ) | KittenToCatFunction interface, whose abstract grow() method can only accept Kitten and inheritors and return only Cat and inheritors |
| Course Project | [Sea Battle](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/ReginaGabdrakhimova/src/main/java/course_project ) | Sea Battle Game |
| | [Tests](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/ReginaGabdrakhimova/src/test/java/course_project_test ) | Course project tests |

[Link to markdown giude](https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet)
[Link to CodingBat](https://codingbat.com/done?user=gabdrakhimovarr@gmail.com&tag=707251349)
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ 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.6.2")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-params:5.6.2")
}

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

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

public class Game {
private static boolean isFinish = false;
private static GameField additionalFieldForComp;
private static GameField additionalFieldForHuman;
private static final String ERROR_MESSAGE = "Input must contain letter from A to J and number from 1 to 10 without a space, please try again:";

public void run() {
System.out.println("Hey! Mr computer invites you to play sea battle! The ships are already positioned across the field.\n" +
"Shoot and hit enemy ships. If you shoot successfully, you can shoot again, and the enemy misses a turn. \n" +
"The game continues until one of the players (you or computer) shoot all the enemy ships. \n");

Player human = new Player();
Player comp = new Player();

human.printField(comp);

battle(human, comp);
}

public void battle(Player human, Player comp) {
Scanner sc = new Scanner(System.in);
additionalFieldForComp = new GameField(false);
additionalFieldForHuman = new GameField(false);

while (!isFinish) {
boolean humanShotOnTarget = true;
boolean compShotOnTarget = true;

while (humanShotOnTarget) {
System.out.println("Enter the coordinate of the opponent's field (for example A1):");
int[] input = readInput(sc);
int x = input[0];
int y = input[1];

humanShotOnTarget = changeCellStatus(x, y, comp);
if (humanShotOnTarget) {
human.printField(comp);
System.out.println("You were lucky! Shoot again, computer skips.");
}

if (checkFinishOfGame(comp.getNumHitCells(), human.getNumHitCells())) {
isFinish = true;
return;
}
}

comp.setNumOfLastShots(0);

while (compShotOnTarget) {
int[] enemyMove = enemyMove();
int enemyX = enemyMove[0];
int enemyY = enemyMove[1];

compShotOnTarget = changeCellStatus(enemyX, enemyY, human);

if (compShotOnTarget)
comp.setNumOfLastShots(comp.getNumOfLastShots()+1);

if (checkFinishOfGame(comp.getNumHitCells(), human.getNumHitCells())) {
isFinish = true;
return;
}
}

human.printField(comp);

if (comp.getNumOfLastShots() > 0)
System.out.println("\nComputer hit well, it fired " + comp.getNumOfLastShots() + " additional shots, you missed a move.");
}
}

private int[] enemyMove() {
boolean shot = false;
int x = 0, y = 0;
while (!shot) {
x = new Random().nextInt(9)+1;
y = new Random().nextInt(9)+1;

// Moment was provided when random coordinate of computer shot could fall into filled cell.
// The cycle will continue until computer enters empty cell.
if (additionalFieldForComp.cells[x][y] == GameField.CellStatus.FREE) {
additionalFieldForComp.cells[x][y] = GameField.CellStatus.FILLED;
shot = true;
}
}

return new int[]{x, y};
}

private boolean checkFinishOfGame(int compNumHitCells, int humanNumHitCells) {
if (humanNumHitCells == 20) {
System.out.println("Computer is winner! Don't be upset, you are great too!");
return true;
} else if (compNumHitCells == 20) {
System.out.println("You are winner! Congratulations!");
return true;
}
return false;
}

private boolean changeCellStatus(int x, int y, Player player) {
boolean shotOnTarget = false;
if (player.getField().cells[x][y] == GameField.CellStatus.FREE) {
player.getField().cells[x][y] = GameField.CellStatus.FAULT;
shotOnTarget = false;
} else if (player.getField().cells[x][y] == GameField.CellStatus.SHIP) {
player.getField().cells[x][y] = GameField.CellStatus.HIT;
shotOnTarget = true;
player.increaseNumHitCells();
}

return shotOnTarget;
}

public static int[] readInput(Scanner sc) {

int[] input = new int[2];
boolean correctInput = false;

boolean shot = false;
while (!shot) {
String str = sc.nextLine();
correctInput = checkingCorrectInput(str);

if (correctInput) {
input[0] = convertX(str.substring(0,1));
input[1] = Integer.parseInt(str.substring(1));

if (additionalFieldForHuman.cells[input[0]][input[1]] == GameField.CellStatus.FREE) {
additionalFieldForHuman.cells[input[0]][input[1]] = GameField.CellStatus.FILLED;
shot = true;
} else {
System.out.println("You've already shot this cage. Enter coordinate of free cell:");
shot = false;
}
} else System.out.println(ERROR_MESSAGE);
}

return input;
}

public static boolean checkingCorrectInput(String str) {
int[] input = new int[2];
boolean correctInput = false;
if (str.length() < 2 || str.length() > 3) {
return false;
}

if (str.substring(0,1).matches("[A-J]") && str.substring(1).matches("[0-9]{1,2}")) {
try {
input[1] = Integer.parseInt(str.substring(1));
if (input[1] > 0 && input[1] < 11)
correctInput = true;
} catch (NumberFormatException ex) {
correctInput = false;
}
} else {
correctInput = false;
}

return correctInput;
}

public static int convertX(String s) {
switch (s) {
case ("A"):
return 1;
case ("B"):
return 2;
case ("C"):
return 3;
case ("D"):
return 4;
case ("E"):
return 5;
case ("F"):
return 6;
case ("G"):
return 7;
case ("H"):
return 8;
case ("I"):
return 9;
case ("J"):
return 10;
}

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

import java.util.Random;

public class GameField {

public CellStatus[][] cells;

public GameField(boolean isMainField) {
cells = new CellStatus[12][12];
for (int i = 0; i < 12; i++) {
for (int j = 0; j < 12; j++) {
cells[i][j] = CellStatus.FREE;
}
}

if (isMainField) {
createShip(4);
createShip(3);
createShip(3);
createShip(2);
createShip(2);
createShip(2);
createShip(1);
createShip(1);
createShip(1);
createShip(1);
}
}

public enum CellStatus {
FREE,
FAULT,
HIT,
SHIP,
FILLED
}

public void createShip(int deckNum) {
boolean enoughSpaceAround = false;
int[] coordinates = new int[]{0,0};
boolean horizontal = new Random().nextBoolean();

while (!enoughSpaceAround) {
coordinates = setRandomCoordinates(deckNum, horizontal);
enoughSpaceAround = checkSpaceAround(coordinates[0], coordinates[1], deckNum, horizontal);
}

setStatusShipInCell(deckNum, horizontal, coordinates);
}

public boolean checkSpaceAround(int x, int y, int num, boolean horizontal) {
if (horizontal) {
for (int i = x-1; i < x+num+1; i++) {
for (int j = y-1; j < y+2; j++) {
if (cells[i][j] == CellStatus.SHIP) return false;
}
}
} else {
for (int i = x-1; i < x+2; i++) {
for (int j = y-1; j < y+num+1; j++) {
if (cells[i][j] == CellStatus.SHIP) return false;
}
}
}

return true;
}

private int[] setRandomCoordinates(int deckNum, boolean horizontal) {
int x = new Random().nextInt(10-deckNum)+1;
int y = new Random().nextInt(9)+1;

if (horizontal) return new int[]{x, y};
else return new int[]{y, x};
}

public void setStatusShipInCell(int deckNum, boolean horizontal, int[] coordinates) {
if (horizontal) {
for (int i = 0; i < deckNum; i++) {
cells[coordinates[0]+i][coordinates[1]] = CellStatus.SHIP;
}
} else {
for (int i = 0; i < deckNum; i++) {
cells[coordinates[0]][coordinates[1]+i] = CellStatus.SHIP;
}
}
}
}
7 changes: 7 additions & 0 deletions src/main/java/course_project/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package course_project;

public class Main {
Copy link
Owner

Choose a reason for hiding this comment

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

Quite a strange ship here
image

Copy link
Owner

Choose a reason for hiding this comment

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

Good job! The game is playable and has a nice description, the code is pretty readable. It seems there are some bugs, good that you have automatic ship placement, the interface is cool. You show knowledge of Java Core and Java libraries. Abstractions and architecture is TBD

To make it better: add some features (like automatically mark cells around destroyed ship as miss), introduce the architecture, and more abstractions.

Ping me when you resolved this bug and tested your app manually for other bugs (like, what happens if the computer hits a cell already marked as a miss?)

Copy link
Owner

Choose a reason for hiding this comment

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

The game shows me the opponent's field in the beginning? Have you played your game yourself?)
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: Also, there are no messages what happened - did computer (or me) hit or miss, how many times computer shot and etc...

Copy link
Owner

Choose a reason for hiding this comment

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

Approved!

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