Skip to content
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

Refactor 2 by char16t #19

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
52 changes: 52 additions & 0 deletions src/main/java/hangman/AbstractGame.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package hangman;

import java.io.PrintStream;
import java.util.List;
import java.util.Scanner;

/**
* Hangman game.
*/
public interface AbstractGame {
/**
* The hidden word.
*
* @return The hidden word
*/
AbstractWord word();

/**
* Maximum number of mistakes.
*
* @return Maximum number of mistakes
*/
Integer mistakes();

/**
* Input stream scanner.
*
* @return Input stream scanner
*/
Scanner input();

/**
* Output stream.
*
* @return Output stream
*/
PrintStream output();

/**
* Attempts to guess the word.
*
* @return List of guesses
*/
List<AbstractGuess> guesses();

/**
* Run game.
*
* @throws Exception When something is wrong
*/
void run() throws Exception;
}
28 changes: 28 additions & 0 deletions src/main/java/hangman/AbstractGuess.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package hangman;

/**
* AbstractGuess.
*/
public interface AbstractGuess {
/**
* AbstractWord.
*
* @return AbstractWord
*/
AbstractWord word();

/**
* Letter in the word.
*
* @return Letter in the word
*/
Character character();

/**
* Is letter guessed?
*
* @return true if the guessed letter
* @throws Exception When something is wrong
*/
boolean guessed() throws Exception;
}
20 changes: 20 additions & 0 deletions src/main/java/hangman/AbstractRandomWord.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package hangman;

/**
* Random word.
*/
public interface AbstractRandomWord {
/**
* Available words.
*
* @return Available words
*/
AbstractWord[] words();

/**
* Choose one of available words.
*
* @throws Exception When something is wrong
*/
AbstractWord random() throws Exception;
}
20 changes: 20 additions & 0 deletions src/main/java/hangman/AbstractStringsAsWords.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package hangman;

/**
* Strings as words.
*/
public interface AbstractStringsAsWords {
/**
* Original strings.
*
* @return Original strings
*/
String[] strings();

/**
* Convert original strings to words.
*
* @return Original strings as words
*/
AbstractWord[] toWords();
}
59 changes: 59 additions & 0 deletions src/main/java/hangman/AbstractWord.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package hangman;

/**
* AbstractWord.
*/
public interface AbstractWord {
/**
* Original word.
*
* @return Original word
*/
String original();

/**
* Mask symbol that displayed instead of unguessed letters
*
* @return Mask symbol
*/
Character symbol();

/**
* The word with replaced unguessed letters to mask symbol.
*
* @return Masked word
*/
String word();

/**
* Check that word contains letter.
*
* @param character Letter
* @return True if word contains letter
* @throws Exception When something is wrong
*/
boolean contains(Character character) throws Exception;

/**
* Check that word is guessed
* @return true if all letters of word is guessed
* @throws Exception When something is wrong
*/
boolean guessed() throws Exception;

/**
* Open letter by value.
*
* @param character Letter
* @throws Exception When something is wrong
*/
void open(Character character) throws Exception;

/**
* Check that letter is opened.
*
* @param character Letter
* @return tue if letter is opened
*/
boolean opened(Character character);
}
34 changes: 34 additions & 0 deletions src/main/java/hangman/Application.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package hangman;

import java.io.InputStream;
import java.io.OutputStream;

public final class Application {

private final InputStream input;
private final OutputStream output;
private final Integer mistakes;

public Application(final InputStream input, final OutputStream output, final Integer mistakes) {
this.input = input;
this.output = output;
this.mistakes = mistakes;
}

public static void main(final String[] args) throws Exception {
new Application(System.in, System.out, 5).execute();
}

public void execute() throws Exception {
new Game(
new RandomWord(
"simplicity", "equality", "grandmother",
"neighborhood", "relationship", "mathematics",
"university", "explanation"
).random(),
this.mistakes,
this.input,
this.output
).run();
}
}
74 changes: 74 additions & 0 deletions src/main/java/hangman/Game.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package hangman;

import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;

public final class Game implements AbstractGame {

private final AbstractWord word;
private final Integer mistakes;
private final Scanner input;
private final PrintStream output;
private final List<AbstractGuess> guesses;

public Game(final AbstractWord word, final Integer mistakes, final InputStream input, final OutputStream output) {
this(word, mistakes, input, new PrintStream(output), new LinkedList<AbstractGuess>());
}

public Game(final AbstractWord word, final Integer mistakes, final InputStream input, final OutputStream output, final List<AbstractGuess> guesses) {
this(word, mistakes, new Scanner(input), new PrintStream(output), guesses);
}

public Game(final AbstractWord word, final Integer mistakes, final Scanner input, final PrintStream output, final List<AbstractGuess> guesses) {
this.word = word;
this.mistakes = mistakes;
this.input = input;
this.output = output;
this.guesses = guesses;
}

public AbstractWord word() {
return this.word;
}

public Integer mistakes() {
return this.mistakes;
}

public Scanner input() {
return this.input;
}

public PrintStream output() {
return this.output;
}

public List<AbstractGuess> guesses() {
return this.guesses;
}

public void run() throws Exception {
while (this.guesses.size() < this.mistakes && !this.word.guessed()) {
this.output.print("AbstractGuess a letter: ");
final AbstractGuess guess = new Guess(this.word, this.input.next().charAt(0));
if (guess.guessed()) {
this.word.open(guess.character());
this.output.println("Hit!");
} else {
this.guesses.add(guess);
this.output.printf("Missed, mistake #%d out of %d\n", this.guesses.size(), this.mistakes);
}
this.output.println(this.word.word());
}
if (this.word.guessed()) {
this.output.println("You won!");
} else {
this.output.println("You lost.");
}
this.output.append("The word: " + this.word.original());
}
}
24 changes: 24 additions & 0 deletions src/main/java/hangman/Guess.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package hangman;

public final class Guess implements AbstractGuess {

private final AbstractWord word;
private final Character character;

public Guess(final AbstractWord word, final Character character) {
this.word = word;
this.character = character;
}

public AbstractWord word() {
return this.word;
}

public Character character() {
return this.character;
}

public boolean guessed() throws Exception {
return this.word.contains(this.character) && !this.word.opened(this.character());
}
}
Loading