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
14 changes: 14 additions & 0 deletions src/main/java/edu/project1/Dictionary.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package edu.project1;

import java.util.Random;

public class Dictionary {

private final String[] allWords = {"hello", "apple", "mouse", "pizza", "horse", "beach", "steak", "glass", "actor"};
private final Random rn = new Random();

public String generateWord() {
return allWords[rn.nextInt(allWords.length)];

}
}
7 changes: 7 additions & 0 deletions src/main/java/edu/project1/GameStatus.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package edu.project1;

public enum GameStatus {
IN_PROCESS,
LOSE,
WIN
}
53 changes: 40 additions & 13 deletions src/main/java/edu/project1/Main.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,54 @@
package edu.project1;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.util.Scanner;


// Press Shift twice to open the Search Everywhere dialog and type `show whitespaces`,
// then press Enter. You can now see whitespace characters in your code.
public final class Main {
private final static Logger LOGGER = LogManager.getLogger();
private static final String GIVE_UP_SIGN = "-1";

private Main() {
}

public static Character parseString(String userString) {
if (userString.length() == 1) {
if (Character.isAlphabetic(userString.charAt(0))) {
return userString.toLowerCase().charAt(0);
}
}
return null;
}

public static void main(String[] args) {
// Press Alt+Enter with your caret at the highlighted text to see how
// IntelliJ IDEA suggests fixing it.
LOGGER.info("Hello and welcome!");
Scanner myObj = new Scanner(System.in);
Session session = new Session(new Dictionary().generateWord());

// Press Shift+F10 or click the green arrow button in the gutter to run the code.
for (int i = 0; i <= 2; i++) {
Printer.welcomeMessage();

while (true) {
String userInput = myObj.nextLine();

if (userInput.equals(GIVE_UP_SIGN)) {
Printer.gameOver();
break;
}
Character userChar = parseString(userInput);
if (userChar == null) {
Printer.badInput();
continue;
}
if (session.attemptToGuess(userChar)) {
Printer.hit();
} else {
Printer.missed(session.getAttempts(), session.getMaxAttempts());
}
Printer.printUserAnswer(session.getUserAnswer());
GameStatus gameStatus = session.gameStatusCheck();
if (gameStatus != GameStatus.IN_PROCESS) {
Printer.gameResult(gameStatus);
break;
}

// Press Shift+F9 to start debugging your code. We have set one breakpoint
// for you, but you can always add more by pressing Ctrl+F8.
LOGGER.info("i = {}", i);
}

}
}
58 changes: 58 additions & 0 deletions src/main/java/edu/project1/Printer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package edu.project1;

import org.jetbrains.annotations.NotNull;

public class Printer {

private Printer() {
}

@SuppressWarnings("RegexpSinglelineJava")
public static void printUserAnswer(char @NotNull [] userAnswer) {
System.out.print("The word: ");
for (char elem : userAnswer) {
System.out.print(elem);
}
System.out.println();
}

@SuppressWarnings("RegexpSinglelineJava")
public static void welcomeMessage() {
System.out.println("""
Welcome to the game!
Try to guess my word
Enter a letter to guess or -1 to stop the game""");

}

@SuppressWarnings("RegexpSinglelineJava")
public static void hit() {
System.out.println("Hit!");
}

@SuppressWarnings("RegexpSinglelineJava")
public static void missed(int currentAttempts, int maxAttempts) {
System.out.printf("Missed! Misake %d out of %d", currentAttempts, maxAttempts);
}

@SuppressWarnings("RegexpSinglelineJava")
public static void gameOver() {
System.out.println("Game is over!");
}

@SuppressWarnings("RegexpSinglelineJava")
public static void gameResult(GameStatus gameStatus) {
if (gameStatus == GameStatus.WIN) {
System.out.println("You win!");
} else if (gameStatus == GameStatus.LOSE) {
System.out.println("You lose!");
}
gameOver();
}

@SuppressWarnings("RegexpSinglelineJava")
public static void badInput() {
System.out.println("Bad input!");
}

}
69 changes: 69 additions & 0 deletions src/main/java/edu/project1/Session.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package edu.project1;

import java.util.Arrays;

public class Session {

private static final char UNKNOWN_LETTER_SYMBOL = '*';
private final String answer;
private final char[] userAnswer;
private final int maxAttempts;
private int attempts;

private GameStatus gameStatus;

public Session(String answer) {
this.answer = answer;
int answerLenght = answer.length();
this.userAnswer = new char[answerLenght];
Arrays.fill(this.userAnswer, UNKNOWN_LETTER_SYMBOL);
this.maxAttempts = answerLenght;
this.attempts = 0;
this.gameStatus = GameStatus.IN_PROCESS;
}

char[] getUserAnswer() {
return userAnswer;
}

int getAttempts() {
return attempts;
}

int getMaxAttempts() {
return maxAttempts;
}

public boolean attemptToGuess(char symbol) {
boolean guessed = false;
for (int i = 0; i < answer.length(); i++) {
if (userAnswer[i] == UNKNOWN_LETTER_SYMBOL && answer.charAt(i) == symbol) {
guessed = true;
userAnswer[i] = symbol;
}
}
if (!guessed) {
attempts++;
}
return guessed;
}

public GameStatus gameStatusCheck() {
if (isAllGuessed()) {
gameStatus = GameStatus.WIN;
} else if (!(attempts < maxAttempts)) {
gameStatus = GameStatus.LOSE;
}
return gameStatus;
}

protected boolean isAllGuessed() {
for (char elem : userAnswer) {
if (elem == UNKNOWN_LETTER_SYMBOL) {
return false;
}
}
return true;
}
}

22 changes: 0 additions & 22 deletions src/test/java/edu/hw1/SampleTest.java

This file was deleted.

4 changes: 4 additions & 0 deletions src/test/java/edu/hw1/Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package edu.hw1;

public class Test {
}
68 changes: 68 additions & 0 deletions src/test/java/edu/project1/GameTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package edu.project1;

import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class GameTest {

@Test
void attemptToGuessTest() {
Session session = new Session("word");
assertEquals(0, session.getAttempts());
assertEquals(4, session.getMaxAttempts());

assertTrue(session.attemptToGuess('o'));
assertEquals(0, session.getAttempts());

assertFalse(session.attemptToGuess('a'));
assertEquals(1, session.getAttempts());

assertTrue(session.attemptToGuess('d'));
assertEquals(1, session.getAttempts());

}

@Nested
class gameStatusCheckTest {
@Test
void winStatus() {
Session session = new Session("test");
assertEquals(GameStatus.IN_PROCESS, session.gameStatusCheck());
session.attemptToGuess('t');
session.attemptToGuess('e');
assertEquals(GameStatus.IN_PROCESS, session.gameStatusCheck());
session.attemptToGuess('s');
assertEquals(GameStatus.WIN, session.gameStatusCheck());
}

@Test
void loseStatus() {
Session session = new Session("word");
assertEquals(GameStatus.IN_PROCESS, session.gameStatusCheck());
session.attemptToGuess('a');
session.attemptToGuess('b');
assertEquals(GameStatus.IN_PROCESS, session.gameStatusCheck());
session.attemptToGuess('c');
session.attemptToGuess('k');
assertEquals(GameStatus.LOSE, session.gameStatusCheck());

}
}

@Test
void isAllGuessedTest() {
Session session = new Session("word");
assertFalse(session.isAllGuessed());

session.attemptToGuess('w');
session.attemptToGuess('o');
session.attemptToGuess('r');
session.attemptToGuess('d');

assertTrue(session.isAllGuessed());
}

}
28 changes: 28 additions & 0 deletions src/test/java/edu/project1/ParsingTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package edu.project1;

import org.junit.jupiter.api.Test;
import static edu.project1.Main.parseString;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

public class ParsingTest {
@Test
void parseValidInputTest() {
assertEquals('a', parseString("a"));
assertEquals('b', parseString("b"));

assertEquals('c', parseString("C"));
assertEquals('z', parseString("Z"));
}

@Test
void parseInvalidInputTest() {
assertNull(parseString("123"));
assertNull(parseString("qwerty"));
assertNull(parseString(""));
assertNull(parseString("12kol,=-"));
assertNull(parseString("oo"));
assertNull(parseString(" P "));
assertNull(parseString("]"));
}
}
23 changes: 0 additions & 23 deletions src/test/java/edu/project1/SampleTest.java

This file was deleted.