diff --git a/.idea/description.html b/.idea/description.html new file mode 100644 index 0000000..db5f129 --- /dev/null +++ b/.idea/description.html @@ -0,0 +1 @@ +Simple Java application that includes a class with main() method \ No newline at end of file diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..97626ba --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..89e42c1 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,9 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..66171d7 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/project-template.xml b/.idea/project-template.xml new file mode 100644 index 0000000..1f08b88 --- /dev/null +++ b/.idea/project-template.xml @@ -0,0 +1,3 @@ + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 0000000..163933b --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,79 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1576998552975 + + + + + + + \ No newline at end of file diff --git a/TwentyMatchsticksGame.iml b/TwentyMatchsticksGame.iml new file mode 100644 index 0000000..d5c0743 --- /dev/null +++ b/TwentyMatchsticksGame.iml @@ -0,0 +1,12 @@ + + + + + + + + + + + + diff --git a/out/production/TwentyMatchsticksGame/Game.class b/out/production/TwentyMatchsticksGame/Game.class new file mode 100644 index 0000000..3d2c6d0 Binary files /dev/null and b/out/production/TwentyMatchsticksGame/Game.class differ diff --git a/out/production/TwentyMatchsticksGame/Main.class b/out/production/TwentyMatchsticksGame/Main.class new file mode 100644 index 0000000..a5f241e Binary files /dev/null and b/out/production/TwentyMatchsticksGame/Main.class differ diff --git a/out/production/TwentyMatchsticksGame/MessageOutput.class b/out/production/TwentyMatchsticksGame/MessageOutput.class new file mode 100644 index 0000000..cfae9f1 Binary files /dev/null and b/out/production/TwentyMatchsticksGame/MessageOutput.class differ diff --git a/src/Game.java b/src/Game.java new file mode 100644 index 0000000..19ea307 --- /dev/null +++ b/src/Game.java @@ -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(); + } +} + diff --git a/src/Main.java b/src/Main.java new file mode 100644 index 0000000..70e792f --- /dev/null +++ b/src/Main.java @@ -0,0 +1,7 @@ +public class Main { + + public static void main(String[] args) { + Game game = new Game(); + game.gameLogic(game);// write your code here + } +} diff --git a/src/MessageOutput.java b/src/MessageOutput.java new file mode 100644 index 0000000..853d777 --- /dev/null +++ b/src/MessageOutput.java @@ -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("Некорректное количество спичек! Попробуйте ещё раз."); + } +}