Commit 5054f9ed authored by Markus Klinik's avatar Markus Klinik
Browse files

add testcase for parameterized view

parent b059cffa
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="src" path="src"/>
<classpathentry kind="output" path="bin"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>assignment02-parameterized-view</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package ooassignment2;
/**
*
* @author Max Jongmans (s1026392)
* @author Mario Tsatsev (s1028415)
*/
public class Gallows {
private String word;
private int mistakes;
private StringBuilder guesses;
private StringBuilder displayedWord;
public Gallows (int mistakes, StringBuilder guesses, StringBuilder displayedWord) {
WordReader RndmWord = new WordReader ("words.txt");
this.word = RndmWord.getWord ();
this.mistakes = mistakes;
this.guesses = guesses;
this.displayedWord = displayedWord;
}
public Gallows (String word, int mistakes, StringBuilder guesses, StringBuilder displayedWord) {
this.word = word;
this.mistakes = mistakes;
this.guesses = guesses;
this.displayedWord = displayedWord;
}
public boolean guessRight (String guess) {
boolean right = false;
for (int i = 0; i < this.word.length (); i++) {
if (this.word.charAt (i) == guess.charAt (0)) {
right = true;
}
}
return right;
}
public int mistakesLeft (String guess) {
if (guessRight (guess) == false) {
this.mistakes = this.mistakes - 1;
}
return this.mistakes;
}
public StringBuilder addGuess (String guess) {
this.guesses.append(guess);
return this.guesses;
}
public int winOrLose () {
int counter = 0;
for (int i = 0; i < displayedWord.length(); i++) {
if (displayedWord.charAt (i) == '.') {
counter++;
}
}
return counter;
}
public boolean done () {
int counter = winOrLose ();
if (this.mistakes == 0 || counter == 0) {
return true;
}
return false;
}
public StringBuilder wordDisplay (StringBuilder displayedWord) {
for (int i = 0; i < this.word.length(); i++) {
displayedWord.append (".");
}
for (int i = 0; i < displayedWord.length(); i++) {
for (int j = 0; j < this.guesses.length(); j++) {
if (this.word.charAt (i) == this.guesses.charAt (j)) {
displayedWord.setCharAt (i, guesses.charAt (j));
}
}
}
this.displayedWord = displayedWord;
return displayedWord;
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package ooassignment2;
import java.util.Scanner;
/**
*
* @author Max Jongmans (s1026392)
* @author Mario Tsatsev (s1028415)
*/
public class OOAssignment2 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner input = new Scanner (System.in);
View View = new View (input);
View.pickWord();
View.guess();
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package ooassignment2;
import java.util.Scanner;
/**
*
* @author Max Jongmans (s1026392)
* @author Mario Tsatsev (s1028415)
*/
public class View {
private Scanner input;
private Gallows hangman;
public View (Scanner input) {
this.input = input;
}
public void pickWord () {
System.out.println ("Welcome to Hangman!\n"
+ "Please enter a word or press Enter to pick a random one: ");
String pickedWord = this.input.nextLine();
StringBuilder start = new StringBuilder ("");
if (pickedWord.length () == 0) {
this.hangman = new Gallows (10, start, start);
}
else {
this.hangman = new Gallows (pickedWord, 10, start, start);
}
}
public void guess () {
int counter = 0;
boolean done = false;
while (done == false) {
System.out.println ("Guess: ");
String guess = this.input.next ();
if (hangman.guessRight (guess)) {
System.out.println (guess + " is in the word.");
}
else {
System.out.println (guess + " is not in the word.");
}
int mistakes = hangman.mistakesLeft(guess);
System.out.println ("Remaining mistakes: " + mistakes);
StringBuilder guesses = new StringBuilder(hangman.addGuess(guess));
System.out.println ("Guessed letters: [" + guesses + "]");
StringBuilder displayedWord = new StringBuilder ("");
displayedWord = hangman.wordDisplay (displayedWord);
System.out.println ("Word: " + displayedWord);
done = hangman.done ();
}
if (hangman.winOrLose () == 0) {
System.out.println ("You won!");
}
else {
System.out.println ("You lost...");
}
}
}
package ooassignment2;
import java.util.Scanner;
import java.util.Random;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* Reads words from a given file and
* selects one of those words pseudo randomly
*
* @author Pieter Koopman
*/
public class WordReader {
private final List<String> words;
private final Random rand;
/**
* Saves words from the file in an arrayList
* IOexceptions while reading the file are caught and printed
*
* @param fileName: fileName of the file with the words
*/
public WordReader(String fileName) {
rand = new Random ();
words = new ArrayList<> ();
String pattern = "\\S+"; // consisting of at least one non-space
try {
FileReader fileReader = new FileReader(fileName);
Scanner file = new Scanner(fileReader);
while (file.hasNext(pattern)) {
words.add(file.next(pattern).toLowerCase());
}
fileReader.close();
} catch ( IOException ioe ) {
System.out.println("Error while reading file with name " + fileName +
": " + ioe.getMessage() );
}
}
/**
* @return the number of read words.
*/
public int getNumberOfWords() {
return words.size();
}
/**
* Gives a pseudo random word from the list of words
* or returns the empty word if the list is empty
*
* @return a pseudo random word
*/
public String getWord() {
if (! words.isEmpty()) {
return words.get(rand.nextInt(getNumberOfWords()));
} else {
return "";
}
}
}
computer interface extend artificial intelligence science bachelor master method class attribute teachingassistant lecture tutorial practical object factory pattern debugger testing abstract collections comparable trees binarytrees quadtrees concurrency multithreading hashing linkedlist array arraylist deadlock synchronize thread input output compile program javadoc project constructor recursion java
......@@ -28,4 +28,12 @@ test bool noUseStringBuilder()
{
errors = allAssignment02Rules(loadTestProject("assignment02-no-use-stringbuilder"));
return "Gallows should use StringBuilder" in { message | error(message, _) <- errors };
}
// Main class creates a Scanner and passes it on to the View class. This should be allowed.
test bool parameterizedView()
{
errors = allAssignment02Rules(loadTestProject("assignment02-parameterized-view"));
print(errors);
return isEmpty(errors);
}
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment