-
Notifications
You must be signed in to change notification settings - Fork 0
finish #1
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
base: master
Are you sure you want to change the base?
finish #1
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <module type="JAVA_MODULE" version="4"> | ||
| <component name="NewModuleRootManager" inherit-compiler-output="true"> | ||
| <exclude-output /> | ||
| <content url="file://$MODULE_DIR$"> | ||
| <sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" /> | ||
| </content> | ||
| <orderEntry type="inheritedJdk" /> | ||
| <orderEntry type="sourceFolder" forTests="false" /> | ||
| </component> | ||
| </module> | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| import java.util.Scanner; | ||
|
|
||
| public class Game { | ||
| private static final int INITIAL_MATCHSTICKS = 20; | ||
| private static final int THREE_MATCHSTICKS = 3; | ||
| private static final int TWO_MATCHSTICKS = 2; | ||
| private static final int ONE_MATCHSTICK = 1; | ||
| int amountOfMatchsticks; | ||
|
|
||
| Game() { | ||
| this.amountOfMatchsticks = INITIAL_MATCHSTICKS; | ||
| } | ||
|
|
||
| static void takeFirstStep(Game game) { | ||
| game.amountOfMatchsticks -= THREE_MATCHSTICKS; | ||
| MessageOutput.announceComputerSelectedMatchsticks(THREE_MATCHSTICKS); | ||
| } | ||
|
|
||
| static void takeNextStep(Game game) { | ||
| if (game.amountOfMatchsticks % 2 != 0) { | ||
| game.amountOfMatchsticks -= TWO_MATCHSTICKS; | ||
| MessageOutput.announceComputerSelectedMatchsticks(TWO_MATCHSTICKS); | ||
| } else if (game.amountOfMatchsticks % 4 == 0) { | ||
| game.amountOfMatchsticks -= THREE_MATCHSTICKS; | ||
| MessageOutput.announceComputerSelectedMatchsticks(THREE_MATCHSTICKS); | ||
| } else { | ||
| game.amountOfMatchsticks--; | ||
| MessageOutput.announceComputerSelectedMatchsticks(ONE_MATCHSTICK); | ||
| } | ||
| } | ||
|
|
||
| private Scanner scanner = new Scanner(System.in); | ||
|
|
||
| private int PlayerStepsAnalysis() { | ||
| if (scanner.hasNextInt()) { | ||
| int matchsticksToDelete = scanner.nextInt(); | ||
| if (matchsticksToDelete <= 3 && matchsticksToDelete >= 1) { | ||
| return matchsticksToDelete; | ||
| } else { | ||
| MessageOutput.printWrongAmount(); | ||
| } | ||
| } | ||
| return 0; | ||
| } | ||
|
|
||
| void gameLogic(Game game) { | ||
| int matchsticksToDelete; | ||
|
|
||
| MessageOutput.announceTheStartOfTheGame(); | ||
| MessageOutput.announceRemainingMatchsticks(amountOfMatchsticks); | ||
| game.takeFirstStep(game); | ||
| MessageOutput.announceRemainingMatchsticks(amountOfMatchsticks); | ||
|
|
||
| while (amountOfMatchsticks != 1) { | ||
| MessageOutput.announcePlayersStep(); | ||
| matchsticksToDelete = PlayerStepsAnalysis(); | ||
| while (matchsticksToDelete == 0) { | ||
| MessageOutput.announceRemainingMatchsticks(amountOfMatchsticks); | ||
| MessageOutput.announcePlayersStep(); | ||
| matchsticksToDelete = PlayerStepsAnalysis(); | ||
| } | ||
| amountOfMatchsticks -= matchsticksToDelete; | ||
| MessageOutput.announceRemainingMatchsticks(amountOfMatchsticks); | ||
| game.takeNextStep(game); | ||
| MessageOutput.announceRemainingMatchsticks(amountOfMatchsticks); | ||
| } | ||
| MessageOutput.announceLoser(); | ||
| scanner.close(); | ||
| } | ||
| } | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| public class Main { | ||
|
|
||
| public static void main(String[] args) { | ||
| Game game = new Game(); | ||
| game.gameLogic(game);// write your code here | ||
|
Comment on lines
+4
to
+5
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. Не стоит так делать, так как это нарушает принципы ООП. Лучше создать какой-нибудь независимый класс-стартер для приложения
Owner
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. I got it |
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| public class MessageOutput { | ||
| static void announceTheStartOfTheGame() { | ||
| System.out.println("Игра началась!"); | ||
| } | ||
|
|
||
| static void announceRemainingMatchsticks(int amount) { | ||
| switch (amount) { | ||
| case 4: | ||
| case 3: | ||
| case 2: { | ||
| System.out.printf("На столе осталось %d спички. \n", amount); | ||
| break; | ||
| } | ||
| case 1: { | ||
| System.out.print("Для игрока осталась последняя спичка. "); | ||
| break; | ||
| } | ||
| default: { | ||
| System.out.printf("На столе осталось %d спичек. \n", amount); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| static void announcePlayersStep() { | ||
| System.out.print("-Ход игрока. Введите количество спичек: "); | ||
| } | ||
|
|
||
| static void announceComputerSelectedMatchsticks(int matchsticks) { | ||
| System.out.println("-Ход комьпьютера. Количество выбранных компьютером спичек = " + matchsticks + "."); | ||
| } | ||
|
|
||
| static void announceLoser() { | ||
| System.out.println("Игрок проиграл!"); | ||
| } | ||
|
|
||
| public static void printWrongAmount() { | ||
| System.out.println("Некорректное количество спичек! Попробуйте ещё раз."); | ||
| } | ||
| } |
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.
Используй .gitignore чтобы не засорять пул реквест