We're going to implement a word guessing game that works a bit like Wheel of Fortune. There's a target word and users pick one letter at a time, until they've guessed every letter in a word.
- Fork this repository
- Clone your fork to your local machine using
git clone - Enter the project directory using
cd - Run
npm installto install the required dependencies - Run
npm testto run the automated tests (some will be failing) - Run
node guessingGame.jsornpm startto see to see the computer make a hard-coded sequence of "guesses" - Get started on the Iterations below.
README.mdis this fileguessingGame.jsis the main file you are meant to runlib/contains the individual functions used to make the gametests/contains automated tests (and a collection of test helper functions)
This project is broken down into iterations. For each iteration, create a new feature branch and submit a pull request against your fork's master branch. Tag an instructor for code review.
The guessingGame.js file contains a simple script that will work correctly once you've gotten through the first few iterations.
The rough sequence of iterations will be:
- Implement the core logic of the game, hard-coding the sequence of guesses we make
- Implement user input, so an actual human is making guesses
- Make it so the user guessing doesn't know the target word beforehand
Keep in mind, we've broken this project down into iterations that reflect the way a more experienced engineer would build it. Part of this is to give you good examples of a well-sequenced project so that you can structure your personal and group projects similarly.
Note: This iteration is complete and serves as a starting point. You don't need to do anything.
The newGuessingGame function create a new guessing game object. A new game looks like this:
{
targetWord: 'hello',
currentWord: '_____',
numGuesses: 0,
}As the user makes guesses, the currentWord entry should be modified to reflect whether they've guessed correct letters or not.
Remember: Do this work on a feature branch, not on master.
Implement the guessingGameMakeGuess function. Use the automated tests in tests/guessingGameMakeGuess.test.js to guide you. Run the tests and read the test file.
Every time a guess is made, game.numGuesses should be incremented. If the guess is correct, game.currentWord should be updated with the filled-in letters.
For example:
let game = newGuessingGame('waffles');
console.log(game.currentWord); // => '_______';
guessingGameMakeGuess(game, 'f');
console.log(game.currentWord); // => '__ff___';
guessingGameMakeGuess(game, 'w');
console.log(game.currentWord); // => 'w_ff___';Remember: Each iteration should be its own feature branch. The idea is to get feedback on each iteration.
Implement the guessingGameIsDone function. Use the automated tests in tests/guessingGameIsDone.test.js to guide you. Run the tests and read the test file.
This function should return true only once the user has guessed all the letters in game.targetWord.
Once both guessingGameIsDone and guessingMakeMakeGuess
Testing dynamic user input is a real pain, so don't worry about doing this for now.
Modify guessingGame.js so that the user is prompted for input with readine-sync, rather than guessing a fixed value. The game should end once the user has guessed every letter.
[v4] Hidden Target Word
The readline-sync module supports a password-like input, as follows:
let readlineSync = require('readline-sync');
let secretInput = readlineSync.question('Enter a secret word: ', { hideEchoBack: true });
console.log(`You entered: ${secretInput}`);We have an example of this type of "hidden input" in our collection of JavaScript Examples. The readline-sync README also has many examples.
Use this to get the target word dynamically and create a new guessing game with that target word.
Have someone else enter a surprise target word and try to guess!
Think of some ways to improve the UI and implement them. Some ideas:
- Clear the screen after every guess
- Use the chalk module to add color (you installed this with
npm install) - Add support for phrases instead of single words
- Add improved error handling, e.g., a user enters multiple letters, a user enters a number or other non-alphabetic character
- Is your game case sensitive? Make it case insensitive
- Something else you think of yourself