- COMP.CS.140
- 5. Inheritance
- 5.7 ⌛⌛ Word game
⌛⌛ Word game¶
The files needed in this task can be pulled from version control.
They are located in the repository student_template_project.
You must set student_template_project
as a remote in your local repository.
The codes needed in this exercise are in the directory Round4/sudoku
.
Note that you do not have rights to update the repository.
Place your code into files named WordGame.java and GameStateException.java in Round5/wordgame.
Introduction¶
One classic word guessing game proceeds as described below. The game involves a gamemaster and a player.
The gamemaster selects a word without showing it to the player. In addition a maximum limit for mistakes (wrong guesses) is defined. The game then proceeds by alternating steps 2 and 3 until either the player wins or the mistake limit is exceeded.
The gamemaster displays the current status of the gamemaster’s word in such manner, that all still unknown letters are hidden by showing the character ‘_’ in their places. Note that initially all letters are unknown and thus hidden.
The player’s turn. The player may either try to guess a single letter or the whole word.
If the player tries to guess the whole word, then:
If the player’s guess differs even slightly from the gamemaster’s word, the guess is a mistake. The player’s mistake count is incremented by one and no new information is revealed to the player (unless the game ends; see below).
If the guess was correct, the game ends in the player winning.
If the player tries to guess a single character, then:
If the character occurs in the gamemaster’s word and was still unknown, the gamemaster reveals all occurrences of the character in the gamemaster’s word (and the character thus becomes known). If as a result all characters of the gamemaster’s word became known, the game ends in the player winning.
If the character does not occur in the gamemaster’s word or was already known, the guess is a mistake. The player’s mistake count is incremented by one and no new information is revealed to the player (unless the game ends; see below).
If the player’s mistake count exceeds the mistake limit, the games ends in the player losing and the gamemaster’s word is revealed completely to the player.
The actual task¶
Implement the word guessing game as a class WordGame
that has the following public
members:
An inner static class
WordGameState
whose objects can store the current game state: the word, mistake count, mistake limit and the number of still missing characters (the number of ‘_’).No public constructors!
Public member functions
String getWord()
,int getMistakes()
,int getMistakeLimit()
andint getMissingChars()
that return the respective values.
A constructor
WordGame(String wordFilename)
that reads a list of words from the file specified by the parameter.The file is expected to cotain one word per line without extra white space.
The constructor should store the words (e.g into a
String
array) into theWordGame
object in the same order as they appear in the file (e.g.ArrayList<String>
might be a suitable container type).When we later talk about the index of a word, we mean a 0-based index. The first word has index 0, the second word index 1, and so on. We furthermore denote the overall number of words by
N
.
A member function
void initGame(int wordIndex, int mistakeLimit)
that initializes a new game where the gamemaster’s word is the word stored by the constructor at indexwordIndex % N
and the maximum mistake limit ismistakeLimit
. A new game starts immediately regardless of whether a previous game was still active.A member function
boolean isGameActive()
that returnstrue
if a game is currently active and otherwisefalse
.A member function
WordGameState getGameState()
that returns aWordGameState
object that describes the current game state.Throws an exception
GameStateException("There is currently no active word game!")
if a game is not currently active.
A member function
WordGameState guess(char c)
that updates the game state in accordance to the player guessing the characterc
and returns aWordGameState
object that describes the new state.Throws an exception
GameStateException("There is currently no active word game!")
if a game is not currently active.
A member function
WordGameState guess(String word)``that updates the game state in accordance to the player guessing the word ``word
and returns aWordGameState
object that describes the new state.Throws an exception
GameStateException("There is currently no active word game!")
if a game is not currently active.
The description above mentioned exceptions of type GameStateException
. You need to implement
also this class in such manner that it inherits the Java library class Exception
and has only
one member: a public constructor GameStateException(String msg)
that only proceeds msg
to
the constructor of its superclass. This is a very simple class.
The testing material provided below should clarify how the WordGame
class is expected to work.
Testing¶
You may test your implementation by using the test program given in the file WordGameTest.java
,
the test data given in the files words.txt
, input1.txt
and input2.txt
, and the example
output given in the files output1.txt
and output2.txt
. Place these files to the same
directory with your code, compile the test program e.g. as javac *.java
, and run the first test
as java WordGameTest words.txt input1.txt
and the second test as
java WordGameTest words.txt input2.txt
. The expected outputs of these two tests are given in
the files output1.txt
and output2.txt
. Note that all output is produced by the test
program. The functions of the WordGame
class do not print anything.
A+ presents the exercise submission form here.