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
55 changes: 52 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,40 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.jcabi</groupId>
<artifactId>parent</artifactId>
<version>0.42</version>
<groupId>oo</groupId>
<artifactId>atom-starter</artifactId>
<version>0.0.11</version>
</parent>
<groupId>com.yegor256</groupId>
<artifactId>hangman</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>hangman</name>

<repositories>
<repository>
<snapshots>
<enabled>false</enabled>
</snapshots>
<id>bintray-releases</id>
<url>http://dl.bintray.com/skapral/oo-maven</url>
</repository>
</repositories>

<dependencies>
<dependency>
<groupId>io.vavr</groupId>
<artifactId>vavr</artifactId>
<version>0.9.0</version>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.8.0</version>
<scope>provided</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
Expand All @@ -37,6 +62,30 @@
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.pitest</groupId>
<artifactId>pitest-maven</artifactId>
<version>1.2.0</version>
<executions>
<execution>
<phase>integration-test</phase>
<goals>
<goal>mutationCoverage</goal>
</goals>
<configuration>
<skip>${skipTests}</skip>
<excludedMethods>
<param>equals</param>
<param>hashCode</param>
<param>toString</param>
</excludedMethods>
<timestampedReports>false</timestampedReports>
<failWhenNoMutations>false</failWhenNoMutations>
<verbose>false</verbose>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
82 changes: 14 additions & 68 deletions src/main/java/hangman/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,85 +14,31 @@
*/
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;
import hangman.game.GameImplementation;
import hangman.input.IFromStream;
import hangman.output.OToStream;
import hangman.vocabulary.VocStatic;
import oo.atom.anno.NotAtom;

@NotAtom
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"
};

public Main(final InputStream in, final OutputStream out, final int m) {
this.input = in;
this.output = out;
this.max = m;
}

public static void main(final String... args) {
new Main(System.in, System.out, 5).exec();
new Main().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");
}
}
new GameImplementation(
new OToStream(System.out),
new IFromStream(System.in),
new VocStatic(WORDS),
5,
System.currentTimeMillis()
).play();
}

}
60 changes: 60 additions & 0 deletions src/main/java/hangman/asserts/AssertScenarioProducesTheOutput.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* The MIT License
*
* Copyright 2018 Kapralov Sergey.
*
* 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
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package hangman.asserts;

import hangman.input.Input;
import hangman.output.OToStream;
import hangman.output.Output;
import oo.atom.tests.Assertion;
import org.assertj.core.api.Assertions;

import java.io.ByteArrayOutputStream;

public class AssertScenarioProducesTheOutput implements Assertion {
@FunctionalInterface
public interface Scenario {
void execute(Output output);
}

private final Scenario scenario;
private final String expectedOutput;

public AssertScenarioProducesTheOutput(final Scenario scenario, final String expectedOutput) {
this.scenario = scenario;
this.expectedOutput = expectedOutput;
}

@Override
public final void check() throws Exception {
final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
scenario.execute(
new OToStream(
byteArrayOutputStream
)
);
final String string = byteArrayOutputStream.toString("UTF-8");
Assertions.assertThat(string).isEqualTo(expectedOutput);
}
}
54 changes: 54 additions & 0 deletions src/main/java/hangman/game/AssertGameOutcome.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* The MIT License
*
* Copyright 2018 Kapralov Sergey.
*
* 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
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package hangman.game;

import hangman.input.Input;
import hangman.output.Output;
import hangman.output.OutputMock;
import oo.atom.tests.Assertion;

public class AssertGameOutcome implements Assertion {
public interface AssertionSubject {
Game game(Input input, Output output);
}

private final AssertionSubject subject;
private final Input input;
private final String expectedOutput;

public AssertGameOutcome(final AssertionSubject subject, final Input input, final String expectedOutput) {
this.subject = subject;
this.input = input;
this.expectedOutput = expectedOutput;
}

@Override
public final void check() throws Exception {
final OutputMock outputMock = new OutputMock(expectedOutput);
final Game game = subject.game(input, outputMock);
game.play();
outputMock.check();
}
}
32 changes: 32 additions & 0 deletions src/main/java/hangman/game/Game.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* The MIT License
*
* Copyright 2018 Kapralov Sergey.
*
* 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
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package hangman.game;

public interface Game {
interface Inference {
Game game();
}
void play();
}
Loading