This repository was archived by the owner on Oct 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Regina_Gabdrakhimova #21
Open
GabdrakhimovaRR
wants to merge
30
commits into
master
Choose a base branch
from
feature/ReginaGabdrakhimova
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
d4cf36e
111
GabdrakhimovaRR 0418c02
Completed homework_1
GabdrakhimovaRR a510721
Update README.md
GabdrakhimovaRR 468f48e
homework_1(2)
GabdrakhimovaRR 3f41d39
Merge remote-tracking branch 'origin/feature/ReginaGabdrakhimova' int…
GabdrakhimovaRR ca5976b
homework_1(fixed indentation)
GabdrakhimovaRR 766336a
homework_2
GabdrakhimovaRR 76b4e64
Update README.md
GabdrakhimovaRR 01931b1
Merge branch 'master' into feature/ReginaGabdrakhimova
GabdrakhimovaRR 45b468c
Merge remote-tracking branch 'origin/feature/ReginaGabdrakhimova' int…
GabdrakhimovaRR 4d0669d
homework_2(2)
GabdrakhimovaRR 0198d99
RandomCharsTableTest has been added
GabdrakhimovaRR 4a643a2
Scanner was closed
GabdrakhimovaRR 8a71d94
homework_3
GabdrakhimovaRR 1fed33d
Update README.md
GabdrakhimovaRR cf983dc
Update README.md
GabdrakhimovaRR ee0af2b
homework_4: Singleton
GabdrakhimovaRR 657ba0f
homework_5: CastomRegexMatcher
GabdrakhimovaRR 68a1def
Update README.md
GabdrakhimovaRR ed66aa6
homework_4. CustomFileReader
GabdrakhimovaRR 5430f07
homework_4. CustomAnnotation
GabdrakhimovaRR 61294c2
homework_5. PowerOfNumber
GabdrakhimovaRR 73c93ca
homework_6. MapProblemsGenerator
GabdrakhimovaRR 33cf811
homework_3,4,5 Tests
GabdrakhimovaRR 8c3063d
Test of homework_4.CustomFileReader
GabdrakhimovaRR 232e3f7
Course Project "Sea Battle"
GabdrakhimovaRR 75d466e
homework_7. KittenToCatFunction
GabdrakhimovaRR 314ceff
Fixed bugs in homework 3, 6 and course project.
GabdrakhimovaRR 395aa28
Fixed bug in course project.
GabdrakhimovaRR 1d70974
Fixed bug in course project(2).
GabdrakhimovaRR File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package course_project; | ||
|
|
||
| public class Main { | ||
| public static void main(String[] args) { | ||
| new Game().run(); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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

There was a problem hiding this comment.
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?)
There was a problem hiding this comment.
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?)

There was a problem hiding this comment.
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...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Approved!