-
Notifications
You must be signed in to change notification settings - Fork 11
Valery Volodin task8 #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
warcrafter9
wants to merge
13
commits into
mak-42:master
Choose a base branch
from
warcrafter9:master
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
13 commits
Select commit
Hold shift + click to select a range
f8721c3
added task8
warcrafter9 4914f79
Добавлен вывод "ОК"
warcrafter9 19743d9
Исправлен (конкретизирован) выброс исключений
warcrafter9 eb79219
Подправлен вывод "конь так не ходит...".
warcrafter9 018d0dd
Добавлены информативные исключения.
warcrafter9 378fdfd
Исправлен выброс исключения на x и y.
warcrafter9 c55b270
Исправлен выброс исключения IllegalMoveException.
warcrafter9 f52882f
Переделан выброс исключений: добавлен builder.
warcrafter9 d23dc4d
+throws IllegalPositionException.
warcrafter9 ca06dc3
Передача проверки в конструктор создателя исключений.
warcrafter9 ba5f845
Удалено TODO.
warcrafter9 ed9189a
+ отдельные конструкторы для isValidLength и isValidLetter/Number/
warcrafter9 5c1e546
Реализовано исключение при вводе пустой строке.
warcrafter9 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
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
59 changes: 59 additions & 0 deletions
59
src/main/java/croc/education/ws2023spb/knightsmove/IllegalPositionException.java
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,59 @@ | ||
| package croc.education.ws2023spb.knightsmove; | ||
|
|
||
| public class IllegalPositionException extends RuntimeException { | ||
|
|
||
| public IllegalPositionException(String message) { | ||
| super(message); | ||
| } | ||
|
|
||
| public IllegalPositionException(int x, int y) { | ||
| super("Неверно заданные координаты: " + x + ", " + y + " - введите цифры от 0 до 7."); | ||
|
|
||
| } | ||
|
|
||
|
|
||
| public static class IllegalPositionExceptionBuilder extends RuntimeException { | ||
| private String position; | ||
| private char letter, number; | ||
| private boolean isValidLength, isValidLetter, isValidNumber; | ||
|
|
||
| public IllegalPositionExceptionBuilder( boolean isValidLetter, boolean isValidNumber) { | ||
| this.isValidLetter = isValidLetter; | ||
| this.isValidNumber = isValidNumber; | ||
| } | ||
| public IllegalPositionExceptionBuilder(boolean isValidLength) { | ||
| this.isValidLength = isValidLength; | ||
| } | ||
|
|
||
| public IllegalPositionExceptionBuilder setPosition(String position) { | ||
| this.position = position; | ||
| return this; | ||
| } | ||
|
|
||
| public IllegalPositionExceptionBuilder setLetter(char letter) { | ||
| this.letter = letter; | ||
| return this; | ||
| } | ||
|
|
||
| public IllegalPositionExceptionBuilder setNumber(char number) { | ||
| this.number = number; | ||
| return this; | ||
| } | ||
|
|
||
| public IllegalPositionException build() { | ||
| String errorMessage = "Неверно определена: " + position + " - введите "; | ||
| if (isValidLength) { | ||
| errorMessage += "позицию двумя литерами"; | ||
| return new IllegalPositionException(errorMessage); | ||
| } | ||
| if (isValidLetter) { | ||
| errorMessage += "вместо \"" + letter + "\" латинскую букву от a до h; "; | ||
| } | ||
| if (isValidNumber) { | ||
| errorMessage += "вместо \"" + number + "\" цифру от 1 до 8."; | ||
| } | ||
| return new IllegalPositionException(errorMessage); | ||
| } | ||
|
|
||
| } | ||
| } |
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
27 changes: 27 additions & 0 deletions
27
src/main/java/croc/education/ws2023spb/knightsmove/MyChessPosition.java
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,27 @@ | ||
| package croc.education.ws2023spb.knightsmove; | ||
|
|
||
| public class MyChessPosition implements ChessPosition{ | ||
| private int x; | ||
| private int y; | ||
| public MyChessPosition(int x,int y) throws IllegalPositionException { | ||
| if ((x >= 0 && x <= 7) && (y >= 0 && y <= 7)) { | ||
| this.x = x; | ||
| this.y = y; | ||
| }else{ | ||
| throw new IllegalPositionException(x,y); | ||
| } | ||
| } | ||
| @Override | ||
| public int x() { | ||
| return x; | ||
| } | ||
|
|
||
| @Override | ||
| public int y() { | ||
| return y; | ||
| } | ||
| @Override | ||
| public String toString(){ | ||
| return ((char) ('a' + x)) +Integer.toString(y+1); | ||
| } | ||
| } |
25 changes: 25 additions & 0 deletions
25
src/main/java/croc/education/ws2023spb/knightsmove/MyKnightsMoveChecker.java
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,25 @@ | ||
| package croc.education.ws2023spb.knightsmove; | ||
|
|
||
| public class MyKnightsMoveChecker implements KnightsMoveChecker { | ||
| @Override | ||
| public void check(String[] positions) throws IllegalMoveException { | ||
| String fromPosition, toPosition; | ||
| for (int i = 1; i < positions.length; i++) { | ||
| fromPosition = positions[i-1]; | ||
| toPosition= positions[i]; | ||
| if (!isKnightMove(fromPosition, toPosition)) { | ||
| throw new IllegalMoveException(fromPosition,toPosition); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public boolean isKnightMove(String from, String to) { | ||
| int x2 = ChessPositionParser.parse(to).x(); | ||
| int x1 = ChessPositionParser.parse(from).x(); | ||
| int y2 = ChessPositionParser.parse(to).y(); | ||
| int y1 = ChessPositionParser.parse(from).y(); | ||
| int dx = Math.abs(x2 - x1); | ||
| int dy = Math.abs(y2 - y1); | ||
| return ((dx == 2 && dy == 1) || (dx == 1 && dy == 2)); | ||
| } | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.