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
Rog_Elena #35
Open
RogElenaYu
wants to merge
65
commits into
master
Choose a base branch
from
feature/RogElena
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
Rog_Elena #35
Changes from all commits
Commits
Show all changes
65 commits
Select commit
Hold shift + click to select a range
968ecdd
homework 1
RogElenaYu c87c2cf
homework 1
RogElenaYu b28db62
homework 1
RogElenaYu eca987a
Revert "homework 1"
RogElenaYu 185a73d
homework 1
RogElenaYu fd89188
homework 1
RogElenaYu 4f1ae88
homework 2
RogElenaYu eb7c43e
homework 2
RogElenaYu e5f7d55
homework 2
RogElenaYu 725e7c9
homework 2
RogElenaYu 0505471
homework 2
RogElenaYu 2bc879f
Merge remote-tracking branch 'origin/master'
RogElenaYu 9278544
Merge branch 'master' into feature/RogElena
RogElenaYu c328b7f
homework 2
RogElenaYu 0670db5
homework 2
RogElenaYu 3a8540c
homework 2
RogElenaYu 12441b5
homework 2
RogElenaYu 4dd951a
homework 2
RogElenaYu e3c3e2e
homework 2
RogElenaYu a52d19f
homework 2
RogElenaYu f005ea6
homework 2
RogElenaYu 84c1232
homework 2
RogElenaYu 7deb319
homework 2
RogElenaYu 9bd9911
homework 3
RogElenaYu e398fd1
homework 3
RogElenaYu c1d3ad1
homework 3
RogElenaYu d104c17
homework 3
RogElenaYu c6323ee
homework 3
RogElenaYu 197fb99
homework 3
RogElenaYu ab8da27
homework 3
RogElenaYu aa81195
homework 4
RogElenaYu 2d4c494
homework 4
RogElenaYu bbc435c
homework 4
RogElenaYu e3683a6
homework 4
RogElenaYu 6c4adab
homework 5
RogElenaYu 3f9bde2
homework 5
RogElenaYu 8b63633
homework 5
RogElenaYu 2010e03
homework 5
RogElenaYu f7f635e
homework 6
RogElenaYu 1df8534
homework 7
RogElenaYu 4e1749f
homework 7
RogElenaYu 11182b5
homework 7
RogElenaYu 0f833ad
homework 7
RogElenaYu 41d2cc1
homework 7
RogElenaYu 583fa73
homework 7
RogElenaYu 0b42b57
homework 7
RogElenaYu b875eaa
homework 7
RogElenaYu 6ac20cf
course_project
RogElenaYu 26e2f7d
course_project
RogElenaYu b78d768
course_project
RogElenaYu f70eaba
course_project
RogElenaYu 4fa9769
course_project
RogElenaYu 4471afc
course_project
RogElenaYu e0bf43e
course_project
RogElenaYu 13797bd
course_project
RogElenaYu 4ea5f9b
course_project
RogElenaYu e4b4700
CollisionClass
RogElenaYu 80f0e76
SingletonTest
RogElenaYu 391f684
MutableClass
RogElenaYu 312d1a0
SeaBattle
RogElenaYu cad8cde
SeaBattle
RogElenaYu 4665d44
SeaBattle
RogElenaYu ead5e91
ImmutableClass
RogElenaYu 5c7fcf1
ImmutableClass
RogElenaYu 1aa4774
ImmutableClass
RogElenaYu 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,24 @@ | ||
| # Java Core June 2021 | ||
|
|
||
| ## *Nikolaev Artem* | ||
| ## *Rog Elena* | ||
|
|
||
| | 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. ConsolePrinter | [Console printer](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/RogElena/src/main/java/homework_1) | The app that reads input arguments and prints them, until "error" argument | | ||
| | HW2.1. TrafficLight | [Traffic light](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/RogElena/src/main/java/homework_2/traffic_light) | traffic_light | | ||
| | HW2.2. PyramidPrinter | [Pyramid printer](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/RogElena/src/main/java/homework_2/pyramid_printer) | pyramid_printer | | ||
| | HW2.3. RandomCharsTable | [Random chars table](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/RogElena/src/main/java/homework_2/random_chars_table) | random_chars_table | | ||
| | HW3. ImmutableClass | [Immutable class](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/RogElena/src/main/java/homework_3) | immutable_class | | ||
| | HW4.1. CustomFileReader | [Custom file reader](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/RogElena/src/main/java/homework_4/custom_file_reader) | custom_file_reader | | ||
| | HW4.2. Singleton | [Singleton](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/RogElena/src/main/java/homework_4/singleton) | singleton | | ||
| | HW4.3. CustomAnnotation | [Custom annotation](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/RogElena/src/main/java/homework_4/custom_annotation) | custom_annotation | | ||
| | HW5.1. PowerOfNumber | [Power of number](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/RogElena/src/main/java/homework_5/power_of_number) | power_of_number | ||
| | HW5.2. CustomRegexMatcher | [Custom regex matcher](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/RogElena/src/main/java/homework_5/custom_regex_matcher) | custom_regex_matcher | ||
| | HW6. MapProblemsGenerator | [Map problems generator](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/RogElena/src/main/java/homework_6/map_problems_generator) | map_problems_generator | ||
| | HW7. KittenToCatFunction | [Kitten to cat function](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/RogElena/src/main/java/homework_7/kitten_to_cat_function) | kitten_to_cat_function | ||
| | CourseProject | [Course project](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/RogElena/src/main/java/course_project) | course_project | ||
|
|
||
| | tests | [Test classes](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/RogElena/src/test/java) | Test classes | | ||
| [Link to markdown giude](https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet) | ||
|
|
||
| [Link to codingbat profile](https://codingbat.com/done?user=rog.elena.yu@gmail.com&tag=9660623200) |
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,299 @@ | ||
| package course_project; | ||
|
|
||
| import lombok.Data; | ||
| import lombok.Value; | ||
|
|
||
| import java.util.*; | ||
|
|
||
| @Data | ||
| public class Field { | ||
| enum cellStatus { | ||
| CLEAN, | ||
| TOUCHING, | ||
| SHIP, | ||
| WOUNDED, | ||
| SUNK, | ||
| MISS | ||
| } | ||
| private final static int fieldLength = 10; | ||
| private static final HashMap<cellStatus, Character> cellStatusSign = new HashMap<cellStatus, Character>(){{ | ||
| put(cellStatus.CLEAN, '~'); | ||
| put(cellStatus.TOUCHING, '~'); | ||
| put(cellStatus.SHIP, 'U'); | ||
| put(cellStatus.WOUNDED, 'x'); | ||
| put(cellStatus.SUNK, 'x'); | ||
| put(cellStatus.MISS, 'o'); | ||
| }}; | ||
| public static final String ANSI_RED = "\u001B[31m"; | ||
| public static final String ANSI_RESET = "\u001B[0m"; | ||
| private cellStatus[][] cells = new cellStatus[fieldLength][fieldLength]; | ||
| private List<Ship> listOfShips = new ArrayList<>(); | ||
|
|
||
| public static int[] stringToCell(String str) { | ||
| char columnNumberChar = str.substring(0, 1).charAt(0); | ||
| int columnNumber; | ||
|
|
||
| if ((int) columnNumberChar > 74) { | ||
| return null; | ||
| } else { | ||
| columnNumber = (int) columnNumberChar - 64; | ||
| } | ||
| String lineNumberStr = str.substring(1); | ||
|
|
||
| try { | ||
| int lineNumber = Integer.parseInt(lineNumberStr); | ||
| if (lineNumber < 1 || lineNumber > 10) { | ||
| return null; | ||
| } | ||
| return new int[]{lineNumber - 1, columnNumber - 1}; | ||
| } catch (NumberFormatException e) { | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| public Field() { | ||
| for (int i = 0; i < fieldLength; i++) { | ||
| for (int j = 0; j < fieldLength; j++) { | ||
| cells[j][i] = cellStatus.CLEAN; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public boolean createShip(int shipLength, String[] shipCoordinates) { | ||
| int[] beginningCell; | ||
| int[] endCell; | ||
|
|
||
| if (shipLength == 1 && (shipCoordinates.length != 1 || shipCoordinates[0] == null)) { | ||
| System.out.println("Error! Wrong length of the ship! Try again.\n"); | ||
| return false; | ||
| } else if (shipLength > 1 && (shipCoordinates.length != 2 | ||
| || shipCoordinates[0] == null | ||
| || shipCoordinates[1] == null)) { | ||
| System.out.println("Error! Wrong length of the ship! Try again.\n"); | ||
| return false; | ||
| } | ||
|
|
||
| beginningCell = stringToCell(shipCoordinates[0]); | ||
| endCell = shipLength == 1 ? beginningCell : stringToCell(shipCoordinates[1]); | ||
|
|
||
| if (beginningCell == null || endCell == null) { | ||
| System.out.println("Error! Wrong enter! Try again.\n"); | ||
| return false; | ||
| } | ||
|
|
||
| if (checkShip(shipLength, beginningCell, endCell)) { | ||
| drawShip(shipLength, beginningCell, endCell); | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| private boolean checkShip(int shipLength, int[] beginningCell, int[] endCell) { | ||
| if (!cellWithinFieldBoundaries(beginningCell) || !cellWithinFieldBoundaries(endCell)) { | ||
| System.out.println("Error! Out of field bounds! Try again.\n"); | ||
| return false; | ||
| } else if (!Ship.shipIsInAStraightLine(shipLength, beginningCell, endCell)) { | ||
| System.out.println("Error! The ship is not in a straight line! Try again.\n"); | ||
| return false; | ||
| } else if (!Ship.rightShipLength(shipLength, beginningCell, endCell)) { | ||
| System.out.println("Error! Wrong length of the ship! Try again.\n"); | ||
| return false; | ||
| } else if (!cellsAbleToFill(beginningCell, endCell)) { | ||
| System.out.println("Error! This cell is not able to be filled with a ship. Try again.\n"); | ||
| return false; | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| private boolean cellWithinFieldBoundaries(int[] cell) { | ||
| return cell[0] <= 9 && cell[1] <= 9 && cell[0] >= 0 && cell[1] >= 0; | ||
| } | ||
|
|
||
| private boolean cellsAbleToFill(int[] beginningCell, int[] endCell) { | ||
| int[][] shipCells = getShipCells(beginningCell, endCell); | ||
| if (shipCells != null) { | ||
| for (int[] cell : shipCells) { | ||
| if (!cellCleanAndNotTouching(cell)) { | ||
| return false; | ||
| } | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| private int[][] getShipCells(int[] beginningCell, int[] endCell) { | ||
| int[][] shipCells; | ||
|
|
||
| if (beginningCell[1] == endCell[1]) { //horizontal | ||
| shipCells = new int[Math.abs(beginningCell[0] - endCell[0]) + 1][3]; | ||
| int lineMin = Math.min(beginningCell[0], endCell[0]); | ||
| int lineMax = Math.max(beginningCell[0], endCell[0]); | ||
| for (int i = 0; i <= lineMax - lineMin; i++) { | ||
| shipCells[i][0] = lineMin + i; | ||
| shipCells[i][1] = beginningCell[1]; | ||
| shipCells[i][2] = 0; //status | ||
| } | ||
| return shipCells; | ||
| } else if (beginningCell[0] == endCell[0]) { //vertical | ||
| shipCells = new int[Math.abs(beginningCell[1] - endCell[1]) + 1][3]; | ||
| int columnMin = Math.min(beginningCell[1], endCell[1]); | ||
| int columnMax = Math.max(beginningCell[1], endCell[1]); | ||
| for (int i = 0; i <= columnMax - columnMin; i++) { | ||
| shipCells[i][0] = beginningCell[0]; | ||
| shipCells[i][1] = columnMin + i; | ||
| shipCells[i][2] = 0; //status | ||
| } | ||
| return shipCells; | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| private boolean cellCleanAndNotTouching(int[] cell) { | ||
| int line = cell[0]; | ||
| int column = cell[1]; | ||
|
|
||
| if (cells[line][column] != cellStatus.CLEAN) { | ||
| return false; | ||
| } | ||
|
|
||
| ArrayList<int[]> adjacentCells = getAdjacentCells(cell); | ||
| for (int[] adjacentCell : adjacentCells) { | ||
| int adjacentLine = adjacentCell[0]; | ||
| int adjacentColumn = adjacentCell[1]; | ||
|
|
||
| if (cells[adjacentLine][adjacentColumn] == cellStatus.SHIP) { | ||
| return false; | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| private ArrayList<int[]> getAdjacentCells(int[] cell) { | ||
| int column = cell[0]; | ||
| int line = cell[1]; | ||
|
|
||
| ArrayList<int[]> adjacentCells = new ArrayList<>(); | ||
| for (int i = line - 1; i <= line + 1; i++) { | ||
| for (int j = column - 1; j <= column + 1; j++) { | ||
| int[] checkingCell = new int[]{j, i}; | ||
| if (cellWithinFieldBoundaries(checkingCell) && !Arrays.equals(cell, checkingCell)) { | ||
| adjacentCells.add(checkingCell); | ||
| } | ||
| } | ||
| } | ||
| return adjacentCells; | ||
| } | ||
|
|
||
| public void drawTestShips(int[][][] data) { | ||
| for (int[][] arr : data) { | ||
| drawShip(arr[0][0], arr[1], arr[2]); | ||
| } | ||
| } | ||
|
|
||
| private void drawShip(int shipLength, int[] beginningCell, int[] endCell) { | ||
| int[][] shipCells = getShipCells(beginningCell, endCell); | ||
| listOfShips.add(new Ship(shipLength, shipCells)); | ||
|
|
||
| if (shipCells != null) { | ||
| for (int[] cell : shipCells) { | ||
| int line = cell[0]; | ||
| int column = cell[1]; | ||
| cells[line][column] = cellStatus.SHIP; | ||
|
|
||
| ArrayList<int[]> adjacentCells = getAdjacentCells(cell); | ||
| for (int[] adjacentCell : adjacentCells) { | ||
| int adjacentColumn = adjacentCell[1]; | ||
| int adjacentLine = adjacentCell[0]; | ||
|
|
||
| if (cells[adjacentLine][adjacentColumn] != cellStatus.SHIP) { | ||
| cells[adjacentLine][adjacentColumn] = cellStatus.TOUCHING; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| boolean allShipsSunk() { | ||
| for (Ship ship : listOfShips) { | ||
| if (ship.getShipStatus() != Ship.status.SUNK) { | ||
| return false; | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| String shotResult(int[] shotCell) { | ||
| int line = shotCell[0]; | ||
| int column = shotCell[1]; | ||
| cellStatus status = getCellStatus(line, column); | ||
|
|
||
| if (status == cellStatus.SHIP) { | ||
| cells[line][column] = cellStatus.WOUNDED; | ||
| Ship hitShip = getHitShip(shotCell); | ||
| if (hitShip != null && hitShip.getShipStatus() == Ship.status.SUNK) { | ||
| setSunkStatusToCells(hitShip); | ||
| return "sunk"; | ||
| } | ||
| return "hit"; | ||
| } else if (status == cellStatus.MISS || status == cellStatus.WOUNDED || status == cellStatus.SUNK) { | ||
| return "repeat"; | ||
| } else { | ||
| cells[line][column] = cellStatus.MISS; | ||
| } | ||
| return "miss"; | ||
| } | ||
|
|
||
| private cellStatus getCellStatus(int line, int column) { | ||
| return cells[line][column]; | ||
| } | ||
|
|
||
| private Ship getHitShip(int[] shotCell) { | ||
| for (Ship ship : listOfShips) { | ||
| if (ship.cellBelongsToShip(shotCell)) { | ||
| ship.setShipStatus(Ship.status.HIT); | ||
| ship.setCellHit(shotCell); | ||
| return ship; | ||
| } | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| private void setSunkStatusToCells(Ship ship) { | ||
| int[][] shipCells = ship.getShipCells(); | ||
|
|
||
| for (int[] shipCell : shipCells) { | ||
| int line = shipCell[0]; | ||
| int column = shipCell[1]; | ||
| cells[line][column] = cellStatus.SUNK; | ||
| } | ||
| } | ||
|
|
||
| void printField(boolean showShips) { | ||
| System.out.print(" "); | ||
| char c = 0x0041; | ||
|
|
||
| for (int i = 1; i <= fieldLength; i++) { | ||
| System.out.print(c + " "); | ||
| c += 1; | ||
| } | ||
| System.out.print("\n"); | ||
|
|
||
| for (int i = 0; i < fieldLength; i++) { | ||
| System.out.print(i < 9 ? " " + (i + 1) : " " + (i + 1)); | ||
| for (int j = 0; j < fieldLength; j++) { | ||
| System.out.print(" "); | ||
| if (!showShips && (cells[i][j] == cellStatus.SHIP || cells[i][j] == cellStatus.TOUCHING)) { | ||
| System.out.print(cellStatusSign.get(cellStatus.CLEAN)); | ||
| } else if (cells[i][j] == cellStatus.SUNK) { | ||
| System.out.print(ANSI_RED + cellStatusSign.get(cells[i][j]) + ANSI_RESET); | ||
| } else { | ||
| System.out.print(cellStatusSign.get(cells[i][j])); | ||
| } | ||
| } | ||
| System.out.println(); | ||
| } | ||
| System.out.print("\n"); | ||
| } | ||
|
|
||
| } | ||
|
|
||
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.
Good! The game is nice to play, the interface is good, playing is easy. Approved!
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.
Thanks for the science and advices!