A hangman style game library and implementation in javascript for both Web and command line for those who want that sort thing. The actual game takes the form of Compu-Guess, a battle of the minds between you, the user, and the machine, a super-intelligence that has emerged from cyberspace to make you guess words.
The Web version does not need to be installed anywhere. It can be run in your preferred browser by loading index.html
.
The CLI version of Compu-Guess requires node.js to run. Find and install it from their website. The index.js
file itself depends on the inquirer
, bootstrap
, and jquery
npm
packages. They can be installed with the included package.json
file, along with all their own requirements, by running npm i
from the command line.
The rest of the library: Letter.js
, Word.js
, and Hangman.js
do not depend on any external packages.
The Web version of Compu-Guess can be played here on almost any browser, or by loading index.html
from the repository directly.
The web game takes input directly from the keyboard. The game will display a series of underscores prominantly, representing the unguessed word.
Type letters on the keyboard to guess, the game will ignore non-letter keys, letters that have already been revealed, and letters that have already been guessed.
Run the CLI version from the command line by entering:
node index.js
This will display the title, COMPU-GUESS and then a number of dashes representing the unguessed word. Below that is a prompt.
Enter one letter at a time. Non-letters and already guessed letters will cause the game to re-prompt the user.
Both versions of Compu-Guess give the player 10 wrong guesses, and will prompt the user to play a new game when the current one is concluded.
Word-Guess is a web app and a small library of files that implements a word guessing game, commonly called Hangman here in the United States. The web version, contained in assets/javascript/gamelogic.js
and assets/javascript/word-guess.js
. The former reflects the code in index.js
which takes in an object and uses a Hangman
to run a game. The latter is a
The remaining files in assets/javascript/
each define objects that share their filenames: Letter.js
, Word.js
, and Hangman.js
. Each file depends the the preceeding one.
A Letter
object takes in and stores an a-z character and displays an underscore in its place until it has been guessed.
Letter
s are built so that they, and their methods will only take one character.
Non-alpha characters, notably spaces are initialized as already guessed. This way, Word
never counts them in .remainingLetters()
.
This property is used to access the actual letter the Letter
object represents. When created, .letter
will only take the first character in any string passed into Letter()
.
A boolean value when set to true causes the toString()
to return .letter
.
Returns false
when this.letter
is not an a-z character, otherwise returns true
. isAlpha()
is invoked when a new Letter
is created to automatically reveal spaces and symbols.
Returns '_'
unless the Letter
has been .guessed
. It is so named to take advantage of default JavaScript behavior that invokes a method named toString
whenever that object is used in a string-like fashion.
Takes in a letter and sets .guessed
to true if it matches .letter
. If the *`char`*
doesn't match, .guessLetter
will return false
. .guessLetter
will only check the first character in any string passed to it.
this.word[
i
]
this.toString()
this.guessLetter(
char
)
this.guessWord(
string
)
this.remainingLetters()
this.getAnswer()
this.revealWord()
this.includes(
char
)
A Word
is an array of Letter
objects, and as such depends on the presences of Letter.js
in the same directory. Word
is also responsible for displaying itself with its .toString
method.
The only property in a Word
is the .word
array, which contains only Letter
s.
Returns the Word
in a string, with each Letter
displaying according to its Letter.guessed
status. It is so named to take advantage of default JavaScript behavior that invokes a method named toString
whenever that object is used in a string-like fashion.
Takes a letter and checks each Letter
by using Letter.guessLetter(
char
)
.
Takes in a word guess in the form of a string. If it's not the word, .guessWord
returns false
, otherwise it returns the results of calling this.revealWord()
.
Returns the number of Letter
s that are not guessed
.
As you'd expect, this function returns the solution to the hidden word, in the form of a string.
Reveal word is intended to be called after a game has been determined, or a word has been guessed with guessWord()
. .revealWord()
sets each Letter
to guessed
and then returns a call to .toString()
Returns true
or false
if the currently revealed letters in a word include the passed character, thus behaving similarly to .toString()
.
this.word
this.guesses
this.answer
this.guessedLetters
this.toString()
this.isSolved()
this.noGuesses()
this.isGuessed()
this.guess(
char
)
this.revealAnswer()
A Hangman
object takes in a Word
and then provides a framework for accessing and guessing. A Hangman
not only handles guessing through the .guess
method which will report back true
/false
/guessed
whether the guess was successful or not, but also stores guessed letters, and has a way of accessing the answer.
The Word
object that makes the backbone of the Hangman
instance. Hangman.word
gets set to a new Word
when a new Hangman
gets created by the game.
Keeps track of the wrong tries left in the game. Initialized from a value passed into the new Hangman
when it is invoked. Hangman.guesses
is decremented after every wrong guess.
A convenient parameter initialized with the Word.getAnswer()
method. Provides a way to peek into the answer to the Hangman
. Someday, Hangman.guess()
will hopefully allow for guessing whole answers at once, and this parameter may be useful. Or it will get depracated.
The array in which all wrong guesses go. Currently this parameter is the only way to access the guessed letters.
Does exactly what the .toString()
function does for the Word
object because it works by calling it, and returning the resulting string. It is so named to take advantage of default JavaScript behavior that invokes a method named toString
whenever that object is used in a string-like fashion.
Returns true
or false
whether the Word
is fully solved or not. Used to register a win, but still abstracted from the concepts of victory and defeat.
Returns true
when there are no more guesses
. Used to register a loss, but not expressed in terms of winning and losing.
Returns true
or false
whether the passed character has already been guessed, but is not in the Word
.
Returns 'guessed'
, true
, or false
depending on the character passed to the method. If it has already been guessed or revealed in the Word
. If it is in the Word
then that and all matching Letter
s are revealed. Or if it hasn't been guessed and isn't in the Word
, then that guess is added to guessedLetters
.
Pretty much just calls Word.revealAnswer()
so there's a convenient way to do that through the Hangman
.
Compuguess.title
Compuguess.tagline
Compuguess.instructions
Compuguess.guesses
Compuguess.winnerMessage
Compuguess.losersMessage
Compuguess.pickWord
Compuguess.goodGuess
Compuguess.badGuess
Compuguess.tryAgain
Defines a gameObject
that provides necessary game rules, choices, and output. Most of the data in the Compuguess
object are user outputs to add flavor and a list of possible words to guess. All the following entries are called by code in index.js
and are therefore required.
A string that contains the title of the gameObject
.
A string that contains a fun tagline to add atmosphere.
A string that contains instructions. Incase the user has never heard of this game before.
Set to an integer number of wrong guesses before the user loses. Gets passed into Hangman
as the second parameter.
A required method that serves up a sweet congrats on being a winner.
A required method that tosses out some attitude if the user's a loser.
A required method that picks a word to instantiate the Hangman
object with.
A required method that gives the user positive reinforcement on good guesses.
A required method that gives out some sly negative feedback on bad guesses.
A required method that prompts the user to try again on duplicate or already discovered entries.
Implements code that:
- Loads a
gameObject
from a file - Instantiates a new
Hangman
- Executes the game loop
- Generates console output based loop results
- Records wins and losses for the session
- Gives an option to play again
index.js
is where the gameObject
, in this case Compu-Guess, combines with the Hangman.js
object constructor to make a functioning game. index.js
uses the inquirer
package to take in user input and pass it into the game.
Implements the same as index.js
but rewritten for the web. The latter file is a slightly condensed version of the library defined here, but without any node.js
specific functions. I couldn't find a satisfactory solution to this problem. I cannot expose and import each file without module.export
or require()
, and I can't load javascript that has those calls without a reference error in the browser! All the solutions I found were some automated variation on what I've done here.
Word-Guess was developed based on an assignment in the University of Minnesota Full-Stack Web Developer Boot Camp program. This version of the assignment was authored entirely by me, Thomas Christ.