- COMP.CS.140
- 5. Inheritance
- 5.6 ⌛⌛ Word game
⌛⌛ Word game¶
There is material for this question in the remote material repository:
The material is located at the
round5/wordgame
directory.WordGameTest.java
: a program to test your implementation.words.txt
: words known by the program.input1.txt
jaoutput1.txt
: a test input and matching output.input2.txt
jaoutput2.txt
: a test input and matching output.
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.
Implementation of the game¶
Implement the word guessing game as a class WordGame
that has the following public
members:
An static nested 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 ‘_’). There are differences between the inner classes (non-static nested class) and the static nested classes. For example, a static nested class can access only the static features of its outer class. However, you do not need to worry now about the qualities of a this type of nested class, becauseWordGameState
does not need to know anything of its outer classWordGame
. Just be sure to include thestatic
modifier in the header of the class.Public member functions
String getWord()
,int getMistakes()
,int getMistakeLimit()
andint getMissingChars()
that return the respective values.Define in
WordGame
a private attribute of theWordGameState
type, if you want to utilise theWordGameState
class as a storage of the data needed to implement the game logic. In this case, return the possibly updated value of the attribute fromWordGame
functions that return the state of the game. The other approach is to define additional private attributes inWordGame
to store data ofWordGameState
and to create aWordGameState
object based on the values of these attributes, when a game state needs to be returned. In the latter case, theWordGameState
class is just as a vessel to return attribute values and has no part in the game logic.No public constructors! To prevent Java from automatically creating a public default constructor, write a private constructor.
A constructor
WordGame(String wordFilename)
that reads a list of words from the file specified by the parameter.The file is expected to contain one word per line without extra white space.
Define in
WordGame
a private container attribute for storing the words. TheArrayList<String>
container might best suited for this task. In the constructor, create a container object and assign it as a value to the attribute. The container should store the words in the same order as they appear in the file.When we later talk about the index of a word, we mean a zero-based index. The first word has index 0, the second word index 1, and so on. We denote the overall number of words by
N
.Define in
WordGame
a private attribute that indicates whether the game is running (active) or not (passive). Theboolean
type is recommended for this attribute. Initialise the attribute in the constructor with a value that symbolises a passive game.
A member function
void initGame(int wordIndex, int mistakeLimit)
that initialises a new game. The gamemaster’s word is the word stored by the container attribute at indexwordIndex % N
. Define a private attribute inWordGame
and set the selected word as the value of the attribute. The maximum mistake limit ismistakeLimit
. This value can be stored either to theWordGameState
object created and set as an attribute value here or to another attribute. Initialise also other relevant attributes, if you implementWordGame
without aWordGameState
attribute. A new game starts immediately regardless of whether a previous game was still active or not. Therefore, initialise the relevant attribute with a value that symbolises an active game.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 the game is passive.Return either the value of the
WordGameState
attribute or aWordGameState
object created using the values stored in the attributes ofWordGame
.
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.First, check that the game is active. Throw an
GameStateException("There is currently no active word game!")
exception, if the game is passive.Compare the guessed character to the characters of the gamemaster’s word in a case-insensitive manner. For example, the uppercase A and lowercase a are considered to be the same character.
The game becomes passive, if the whole word is now revealed or the maximum number of wrong guesses has been exceeded. Update the value of the relevant attribute.
Return either the updated value of the
WordGameState
attribute or aWordGameState
object created using the values stored in the attributes ofWordGame
.
A member function
WordGameState guess(String word)
that updates the game state in accordance to the player guessing the wordword
and returns aWordGameState
object that describes the new state. This function behaves as the previous one except for the comparison. Instead of comparing individual characters, whole words are compared.
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, for example 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.