Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .idea/description.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions .idea/project-template.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

79 changes: 79 additions & 0 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions TwentyMatchsticksGame.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Используй .gitignore чтобы не засорять пул реквест

<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>

Binary file added out/production/TwentyMatchsticksGame/Game.class
Binary file not shown.
Binary file added out/production/TwentyMatchsticksGame/Main.class
Binary file not shown.
Binary file not shown.
71 changes: 71 additions & 0 deletions src/Game.java
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();
}
}

7 changes: 7 additions & 0 deletions src/Main.java
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
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Не стоит так делать, так как это нарушает принципы ООП. Лучше создать какой-нибудь независимый класс-стартер для приложения

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I got it

}
}
39 changes: 39 additions & 0 deletions src/MessageOutput.java
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("Некорректное количество спичек! Попробуйте ещё раз.");
}
}