-
Notifications
You must be signed in to change notification settings - Fork 2
Lera_Rodina #45
base: master
Are you sure you want to change the base?
Lera_Rodina #45
Changes from all commits
35e0d54
a2500da
b52fdb3
ff1a0c0
78b2f08
103b908
463cebd
4407534
8be4005
6105e3a
87d92db
aa82415
412219d
a41ea11
f94a73a
dc44900
efc7f9b
071afe2
d598b6f
9c06053
859c9fb
b0003c3
0d2dadf
8d1cf77
5d96e60
e8d13e3
4b8140c
6f6accb
a0fcc65
e86428c
d1fd579
eac4166
eeedfbb
dd81725
faade02
7091eed
fa0e484
ad3d16a
06821ce
d3852d8
2d1f91b
3194d81
a493b9f
c82aee5
d877e43
158c8f2
f8f2fad
4e9dece
4166be0
b49043a
e548bdf
4551b57
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -34,4 +34,7 @@ hs_err_pid* | |
| out | ||
| target | ||
| *.iml | ||
| .idea | ||
| .idea | ||
| /.idea/ | ||
| /gradle/ | ||
| /.DS_Store/ | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,22 @@ | ||
| # Java Core June 2021 | ||
|
|
||
| ## *Nikolaev Artem* | ||
| ## *Rodina Lera* | ||
|
|
||
| | 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/Lera_Rodina/src/main/java/homework_1) | The app that reads input arguments and prints them, until "error" argument | | ||
| | HW2_1 | [Traffic light](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/Lera_Rodina/src/main/java/homework_2/traffic_light) | The app that reads the number of seconds from the beginning of the day from the command line displays the color of the traffic light (green, yellow or red) | | ||
| | HW2_2 | [Pyramid printer](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/Lera_Rodina/src/main/java/homework_2/pyramid_printer) | The app that reads the number from the command line and print a pyramid with levels according to this number, this pyramid consists only of 'x' symbols | | ||
| | HW2_3 | [Random chars table](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/Lera_Rodina/src/main/java/homework_2/random_chars_table) | The app that reads 2 numbers as size of a table and a strategy: even or odd, print this table consisting of random symbols from A to Z and then print all symbols in one line which are odd or even according to the strategy (their ASCII codes) | | ||
| | HW_3 | [Immutable class](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/Lera_Rodina/src/main/java/homework_3) | Immutable class with fields (including reference type), several constructors, method which return new object and requirements for Immutable class in comments | | ||
| | HW_4_1 | [Custom File Reader](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/Lera_Rodina/src/main/java/homework_4/custom_file_reader) | The app which gets data from file and then print it without any commas and dots (there are different ways for reading from a file) | | ||
| | HW_4_2 | [Singleton](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/Lera_Rodina/src/main/java/homework_4/singleton) | The app - singleton | | ||
| | HW_4_3 | [Custom annotation](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/Lera_Rodina/src/main/java/homework_4/custom_annotation) | A custom annotation | | ||
| | HW_5_1 | [Power Of Number](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/Lera_Rodina/src/main/java/homework_5/power_of_number) | The app which read number and its power and calculate the result. Recursion should be used here | | ||
| | HW_5_2 | [Custom Regex Matcher](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/Lera_Rodina/src/main/java/homework_5/custom_regex_matcher) | The app which read string and and check if this string matches with hardcoded regex in the app and get result: true or false | | ||
| | HW_6 | [Map Problems Generator](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/Lera_Rodina/src/main/java/homework_6/map_problems_generator) | The app which create collisions in hashmap | | ||
| | HW_7 | [Kitten To Cat Function Interface](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/Lera_Rodina/src/main/java/homework_7/kitten_to_cat_function) | The app which create an interface for turning a kitten into a cat | | ||
| | Course project | [Sea Battle](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/Lera_Rodina/src/main/java/course_project) | A console Sea Battle Game (Player vs Computer version only)| | ||
|
|
||
| [Link to Coding Bat](https://codingbat.com/done?user=lerarodina24@gmail.com&tag=9318549243) | ||
| [Link to markdown giude](https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
|
|
||
| Course project SEA BATTLE | ||
|
|
||
| --------------------------------------- | ||
|
|
||
| Here you can try to play Sea Battle game! | ||
|
|
||
|
|
||
| I developed a version of a game "Player vs Computer" | ||
|
|
||
| Just to make a game a bit faster - all ships automatically are created randomly before starting this game. | ||
| And you might not spend time for creating it by yourself. | ||
|
|
||
| There are: | ||
| 4-deck ships - 1 | ||
| 3-deck ships - 2 | ||
| 2-deck ships - 3 | ||
| 1-deck ships - 4 | ||
|
|
||
| (Just because I ve remembered these rules since my childhood:)) | ||
|
|
||
| After starting a game you see your seaMap (with blue color) and you should make your first move! | ||
|
|
||
| If your/computer's ship is found, cell will have a yellow color. | ||
| If a ship is destroyed fully, a color is red. | ||
| If you/computer makes a mistake - you wil see just a white "star". | ||
|
|
||
| I decided to make a console interface of game in Russian as our traditional game:) | ||
|
|
||
| Hope this game will be convenient for you! | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| package couse_project; | ||
|
|
||
| public class Color { | ||
|
|
||
| static String blue = "\033[1;34m"; | ||
| static String red = "\033[1;31m"; | ||
| static String yellow = "\033[1;33m"; | ||
| static String white = "\033[1;37m"; | ||
| static String reset = "\033[0m"; | ||
|
|
||
| public static String setColor(String text, String color) { | ||
| return color + text + reset; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| package couse_project; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.HashMap; | ||
|
|
||
| public class Displayer { | ||
|
|
||
| private final String columns = " А Б В Г Д Е Ж З И К\n"; | ||
| private final String row = "%2d %s %s %s %s %s %s %s %s %s %s\n"; | ||
| private final HashMap<Integer, String> mapSymbols = new HashMap<Integer, String>() {{ | ||
| put(0, " "); // пустая клетка | ||
| put(1, Color.setColor("■", Color.blue)); // корабль | ||
| put(2, Color.setColor("□", Color.yellow)); // подбитый корабль | ||
| put(3, Color.setColor("X", Color.red)); // разрушенный корабль | ||
| put(4, Color.setColor("*", Color.white)); // промах | ||
| }}; | ||
| private final HashMap<Integer, String> radarSymbols = new HashMap<Integer, String>() {{ | ||
| put(0, " "); // пустая клетка | ||
| put(1, " "); // корабль | ||
| put(2, Color.setColor("□", Color.yellow)); // подбитый корабль | ||
| put(3, Color.setColor("X", Color.red)); // разрушенный корабль | ||
| put(4, Color.setColor("*", Color.white)); // промах | ||
| }}; | ||
|
|
||
| public void clearScreen() throws IOException, InterruptedException { | ||
| String os = System.getProperty("os.name").toLowerCase(); | ||
| if (os.contains("win")) { | ||
| new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor(); | ||
| } else if (os.contains("nix") || os.contains("nux")) { | ||
| new ProcessBuilder("terminal", "/c", "clear").inheritIO().start().waitFor(); | ||
| } | ||
| System.out.println(); | ||
| } | ||
|
|
||
| public void displayMap(SeaMap map) { | ||
| StringBuilder mapImage = new StringBuilder(); | ||
| mapImage.append(columns); | ||
| for (int i = 0; i < map.size()[0]; i++) { | ||
| mapImage.append(String.format(row, i + 1, | ||
| mapSymbols.get(map.getCell(i, 0)), mapSymbols.get(map.getCell(i, 1)), | ||
| mapSymbols.get(map.getCell(i, 2)), mapSymbols.get(map.getCell(i, 3)), | ||
| mapSymbols.get(map.getCell(i, 4)), mapSymbols.get(map.getCell(i, 5)), | ||
| mapSymbols.get(map.getCell(i, 6)), mapSymbols.get(map.getCell(i, 7)), | ||
| mapSymbols.get(map.getCell(i, 8)), mapSymbols.get(map.getCell(i, 9)))); | ||
| } | ||
| mapImage.append("\n"); | ||
| System.out.print(mapImage); | ||
| } | ||
|
|
||
| public void displayRadar(SeaMap radar) { | ||
| StringBuilder radarImage = new StringBuilder(); | ||
| radarImage.append(columns); | ||
| for (int i = 0; i < radar.size()[0]; i++) { | ||
| radarImage.append(String.format(row, i + 1, | ||
| radarSymbols.get(radar.getCell(i, 0)), radarSymbols.get(radar.getCell(i, 1)), | ||
| radarSymbols.get(radar.getCell(i, 2)), radarSymbols.get(radar.getCell(i, 3)), | ||
| radarSymbols.get(radar.getCell(i, 4)), radarSymbols.get(radar.getCell(i, 5)), | ||
| radarSymbols.get(radar.getCell(i, 6)), radarSymbols.get(radar.getCell(i, 7)), | ||
| radarSymbols.get(radar.getCell(i, 8)), radarSymbols.get(radar.getCell(i, 9)))); | ||
| } | ||
| radarImage.append("\n"); | ||
| System.out.print(radarImage); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| package couse_project; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| public class Game { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good! You introduced some abstractions, showed knowledge of Java Core and Java libraries. The code in general looks good, but there are a few cases where I'm not sure about the correct behavior. Also, your project lacks abstract classes/interfaces, and some architecture (the simplest is service/model). The interface is cool, I like it. playing is quite comfortable) I also appreciate automatic ship placement and description, it really helps to test. Approved! |
||
| Player playerOne; | ||
| RandomIntelligence playerTwo; | ||
|
|
||
| public Game(Player playerOne, RandomIntelligence playerTwo) { | ||
| this.playerOne = playerOne; | ||
| this.playerTwo = playerTwo; | ||
| } | ||
|
|
||
| public void startGame() throws IOException, InterruptedException { | ||
| Displayer displayer = new Displayer(); | ||
| while (true) { | ||
| displayer.clearScreen(); | ||
| displayer.displayMap(playerOne.getMap()); | ||
| displayer.displayRadar(playerOne.getRadar()); | ||
| if (playerOne.shoot()) { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That doesn't look good... usually methods should follow Single Responsibility Prinsiple - it should do only one thing, the thing it promises. In this case it's unexpected it returns boolean. Have you tried finishing the game? I'm not sure it will not go to an infinite loop of recursion
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, you're absolutely right. Seems that I forgot to add a condition for loop after last changes for this part:( Let me check |
||
| System.out.println("Отлично! Сделайте еще один ход:"); | ||
| continue; | ||
| } | ||
|
|
||
| while (true) { | ||
| if (playerTwo.shoot()) { | ||
| System.out.println("Наш корабль попал под обстрел!"); | ||
| } else { | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| package couse_project; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| public class Main { | ||
|
|
||
| public static void main(String[] args) throws IOException, InterruptedException { | ||
| SeaMap mapOne = new SeaMap(); | ||
| SeaMap mapTwo = new SeaMap(); | ||
|
|
||
| Player playerOne = new Player("Lera", mapOne, mapTwo); | ||
| RandomIntelligence playerTwo = new RandomIntelligence(mapTwo, mapOne); | ||
|
|
||
| Game gameSession = new Game(playerOne, playerTwo); | ||
| gameSession.startGame(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| package couse_project; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.HashMap; | ||
| import java.util.Scanner; | ||
|
|
||
| public class Player { | ||
| final private String name; | ||
| private final SeaMap map; | ||
| private final SeaMap radar; | ||
| final private String[] commands = new String[]{ | ||
| "А1", "А2", "А3", "А4", "А5", "А6", "А7", "А8", "А9", "А10", | ||
| "Б1", "Б2", "Б3", "Б4", "Б5", "Б6", "Б7", "Б8", "Б9", "Б10", | ||
| "В1", "В2", "В3", "В4", "В5", "В6", "В7", "В8", "В9", "В10", | ||
| "Г1", "Г2", "Г3", "Г4", "Г5", "Г6", "Г7", "Г8", "Г9", "Г10", | ||
| "Д1", "Д2", "Д3", "Д4", "Д5", "Д6", "Д7", "Д8", "Д9", "Д10", | ||
| "Е1", "Е2", "Е3", "Е4", "Е5", "Е6", "Е7", "Е8", "Е9", "Е10", | ||
| "Ж1", "Ж2", "Ж3", "Ж4", "Ж5", "Ж6", "Ж7", "Ж8", "Ж9", "Ж10", | ||
| "З1", "З2", "З3", "З4", "З5", "З6", "З7", "З8", "З9", "З10", | ||
| "И1", "И2", "И3", "И4", "И5", "И6", "И7", "И8", "И9", "И10", | ||
| "К1", "К2", "К3", "К4", "К5", "К6", "К7", "К8", "К9", "К10", | ||
| }; | ||
| final private HashMap<String, Integer> commandsMap = new HashMap<String, Integer>() {{ | ||
| put("А", 0); | ||
| put("Б", 1); | ||
| put("В", 2); | ||
| put("Г", 3); | ||
| put("Д", 4); | ||
| put("Е", 5); | ||
| put("Ж", 6); | ||
| put("З", 7); | ||
| put("И", 8); | ||
| put("К", 9); | ||
| }}; | ||
|
|
||
| public Player(String name, SeaMap map, SeaMap radar) { | ||
| this.name = name; | ||
| this.map = map; | ||
| this.radar = radar; | ||
| } | ||
|
|
||
| public boolean shoot() { | ||
| Scanner in = new Scanner(System.in); | ||
| System.out.println("Ваш ход:"); | ||
| String command = in.nextLine(); | ||
|
|
||
| if (Arrays.asList(commands).contains(command)) { | ||
| System.out.println(command.toUpperCase().charAt(0)); | ||
|
|
||
| int x = commandsMap.get((Character.toString(command.toUpperCase().charAt(0)))); | ||
| int y = Integer.parseInt(Character.toString(command.charAt(1))) - 1; | ||
|
|
||
| if (radar.isFreeEnterCell(x, y)) { | ||
| return radar.registerShot(x, y); | ||
| } | ||
| } else { | ||
| System.out.println("Ошибка формата данных"); | ||
| System.out.println("Попробуйте, пожалуйста, ещё раз"); | ||
| } | ||
|
|
||
| shoot(); | ||
| return false; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's the case when it will return false? Seems impossible
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. let me check and fix if it is needed |
||
| } | ||
|
|
||
| public SeaMap getMap() { | ||
| return map; | ||
| } | ||
|
|
||
| public SeaMap getRadar() { | ||
| return radar; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| package couse_project; | ||
|
|
||
| import java.util.Random; | ||
|
|
||
| public class RandomIntelligence { | ||
| private SeaMap map; | ||
| private final SeaMap radar; | ||
|
|
||
| public RandomIntelligence(SeaMap map, SeaMap radar) { | ||
| this.map = map; | ||
| this.radar = radar; | ||
| } | ||
|
|
||
| public boolean shoot() { | ||
| Random random = new Random(); | ||
| int x = random.nextInt(10); | ||
| int y = random.nextInt(10); | ||
|
|
||
| if (radar.isFreeEnterCell(x, y)) { | ||
| return radar.registerShot(x, y); | ||
| } | ||
|
|
||
| shoot(); | ||
| return false; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here: What's the case when it will return false? Seems impossible
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. let me check and fix if it is needed |
||
| } | ||
| } | ||
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 description!