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

try do it in oop style #9

Open
wants to merge 2 commits 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
19 changes: 19 additions & 0 deletions src/main/java/hangman/IHangman.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package hangman;

import hangman.exceptions.HitNotAllowedException;

/**
* Created by teSGreat on 24.05.2017.
*/
public interface IHangman {

void trySurvive();

void hitMe(char letter) throws HitNotAllowedException;

boolean isAlive();

boolean winner();

int attemptsRemained();
}
13 changes: 13 additions & 0 deletions src/main/java/hangman/IWordKeeper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package hangman;

import hangman.exceptions.WordAlreadyOpenException;

/**
* Created by teSGreat on 24.05.2017.
*/
public interface IWordKeeper {

boolean findLetter(char letter) throws WordAlreadyOpenException;

boolean isOpen();
}
84 changes: 9 additions & 75 deletions src/main/java/hangman/Main.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/**
* The MIT License (MIT)
*
* <p>
* Copyright (c) 2017 Yegor Bugayenko
*
* <p>
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
Expand All @@ -14,85 +14,19 @@
*/
package hangman;

import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.Iterator;
import java.util.Random;
import java.util.Scanner;

public class Main {

private final InputStream input;
private final OutputStream output;
private final int max;
private static final String[] WORDS = {
"simplicity", "equality", "grandmother",
"neighborhood", "relationship", "mathematics",
"university", "explanation"
"simplicity", "equality", "grandmother",
"neighborhood", "relationship", "mathematics",
"university", "explanation"
};

public Main(final InputStream in, final OutputStream out, final int m) {
this.input = in;
this.output = out;
this.max = m;
public static void main(String[] args) {
SuperHangman johnny = new SuperHangman(
new WordKeeper(WORDS[new Random().nextInt(WORDS.length)]), 5);
johnny.trySurvive();
}

public static void main(final String... args) {
new Main(System.in, System.out, 5).exec();
}

public void exec() {
String word = WORDS[new Random().nextInt(WORDS.length)];
boolean[] visible = new boolean[word.length()];
int mistakes = 0;
try (final PrintStream out = new PrintStream(this.output)) {
final Iterator<String> scanner = new Scanner(this.input);
boolean done = true;
while (mistakes < this.max) {
done = true;
for (int i = 0; i < word.length(); ++i) {
if (!visible[i]) {
done = false;
}
}
if (done) {
break;
}
out.print("Guess a letter: ");
char chr = scanner.next().charAt(0);
boolean hit = false;
for (int i = 0; i < word.length(); ++i) {
if (word.charAt(i) == chr && !visible[i]) {
visible[i] = true;
hit = true;
}
}
if (hit) {
out.print("Hit!\n");
} else {
out.printf(
"Missed, mistake #%d out of %d\n",
mistakes + 1, this.max
);
++mistakes;
}
out.append("The word: ");
for (int i = 0; i < word.length(); ++i) {
if (visible[i]) {
out.print(word.charAt(i));
} else {
out.print("?");
}
}
out.append("\n\n");
}
if (done) {
out.print("You won!\n");
} else {
out.print("You lost.\n");
}
}
}

}
110 changes: 110 additions & 0 deletions src/main/java/hangman/SuperHangman.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package hangman;

import hangman.exceptions.HangmanAlreadyWinnerException;
import hangman.exceptions.HangmanIsDeadException;
import hangman.exceptions.HitNotAllowedException;
import hangman.exceptions.WordAlreadyOpenException;

import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.Iterator;
import java.util.Scanner;

/**
* Created by teSGreat on 24.05.2017.
*/
public class SuperHangman implements IHangman {

private final IWordKeeper wordKeeper;
private final Iterator<String> input;
private final PrintStream output;
private final int maxErrors;
private int errors;

public SuperHangman(IWordKeeper wordKeeper, int maxErrors, InputStream input, OutputStream output) {

this.wordKeeper = wordKeeper;
this.maxErrors = maxErrors;
this.input = new Scanner(input);
this.output = new PrintStream(output);
}

public SuperHangman(IWordKeeper wordKeeper, int maxErrors, InputStream input) {
this(wordKeeper, maxErrors, input, System.out);
}

public SuperHangman(IWordKeeper wordKeeper, int maxErrors) {
this(wordKeeper, maxErrors, System.in, System.out);
}

public SuperHangman(IWordKeeper wordKeeper) {
this(wordKeeper, 0);
}

@Override
public void trySurvive() {

do {
output.print("Guess a letter: ");
if (!input.hasNext()) {
break;
}
char letter = input.next().charAt(0);
try {
hitMe(letter);
} catch (HitNotAllowedException ex) {
output.print(ex.getMessage());
break;
}
output.println(this);
if (!isAlive()) {
output.println("You lost.");
break;
} else if (winner()) {
output.println("You won!");
break;
}
} while (true);
}

@Override
public void hitMe(char letter) throws HitNotAllowedException {

if (!isAlive()) {
throw new HangmanIsDeadException();
}
try {
if (wordKeeper.findLetter(letter)) {
output.println("Hit!");
} else {
errors++;
output.println("Missed, mistake #" + errors + " out of " + maxErrors);
}
} catch (WordAlreadyOpenException e) {
throw new HangmanAlreadyWinnerException();
}

}

@Override
public boolean isAlive() {
return errors < maxErrors || (errors == 0 && maxErrors == 0);
}

@Override
public boolean winner() {
return wordKeeper.isOpen();
}

@Override
public int attemptsRemained() {
return maxErrors - errors;
}

@Override
public String toString() {
return wordKeeper.toString();
}
}

55 changes: 55 additions & 0 deletions src/main/java/hangman/WordKeeper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package hangman;

import hangman.exceptions.WordAlreadyOpenException;

/**
* Created by teSGreat on 24.05.2017.
*/
public class WordKeeper implements IWordKeeper {

private final char[] word;
private final boolean[] visible;

public WordKeeper(String word) {

this.visible = new boolean[word.length()];
this.word = word.toCharArray();
}

@Override
public boolean findLetter(char letter) throws WordAlreadyOpenException {

if (isOpen()) {
throw new WordAlreadyOpenException();
}
boolean find = false;
for (int i = 0; i < word.length; ++i) {
if (word[i] == letter && !visible[i]) {
visible[i] = true;
find = true;
}
}
return find;
}

@Override
public boolean isOpen() {

for (boolean chv : visible) {
if (!chv) {
return false;
}
}
return true;
}

@Override
public String toString() {

String strWord = "";
for (int i = 0; i < word.length; i++) {
strWord += visible[i] ? word[i] : '?';
}
return "The word: " + strWord;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package hangman.exceptions;

/**
* Created by teSGreat on 24.5.17.
*/
public class HangmanAlreadyWinnerException extends HitNotAllowedException {
static final long serialVersionUID = 1L;

public HangmanAlreadyWinnerException() {
super("Dont hit it! It already win!");
}
}
12 changes: 12 additions & 0 deletions src/main/java/hangman/exceptions/HangmanIsDeadException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package hangman.exceptions;

/**
* Created by teSGreat on 24.5.17.
*/
public class HangmanIsDeadException extends HitNotAllowedException {
static final long serialVersionUID = 1L;

public HangmanIsDeadException() {
super("Dont hit it! It already dead!");
}
}
12 changes: 12 additions & 0 deletions src/main/java/hangman/exceptions/HitNotAllowedException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package hangman.exceptions;

/**
* Created by teSGreat on 24.5.17.
*/
public class HitNotAllowedException extends Exception {
static final long serialVersionUID = 1L;

public HitNotAllowedException(String message) {
super(message);
}
}
12 changes: 12 additions & 0 deletions src/main/java/hangman/exceptions/WordAlreadyOpenException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package hangman.exceptions;

/**
* Created by teSGreat on 24.05.2017.
*/
public class WordAlreadyOpenException extends Exception {
static final long serialVersionUID = 1L;

public WordAlreadyOpenException() {
super("You alreary open the word!");
}
}
Loading