Create a 2-player guessing game where players take turns to answer simpsele addition problems. The math questions are automatically generated by the game. It should do this by picking two numbers between 1 and 20.
Example prompt: "Player 1: What does 5 plus 3 equal?"
Both players start with 3 lives. They lose a life if they mis-answer a question. If a player gets a question wrong, the game should output the new scores for both players, so players know where they stand.
The game doesn’t end until one of the players loses all their lives. At this point, the game should announce who won and what the other player’s score is.
As before, you can use gets.chomp
to get input from users and puts
for output.
Please read all of the tips prior to starting your game.
This assignment should be created as a new Github repository. Remember to commit and push your code frequently.
Before starting on the code, consider writing out the pseudocode (in a markdown file or on paper)
Pseudocode is still written like code, but is not language specific:
Pseudocode (derived from pseudo and code) is a description of a computer programming algorithm that uses the structural conventions of programming languages, but omits detailed subroutines or language-specific syntax.
-- Wikipedia
initialize passes to zero
initialize failures to zero
initialize student counter to zero
initialize input
loop until input equals "done"
get input about next exam result
if the student passed
add one to passes
else
add one to failures
add one to student counter
print the number of passes
print the number of failures
if eight or more students passed
print "party time!"
Read the pseudocode above and:
- Note it's properties (indentation, style)
- Understand completely what it does
Build your code incrementally.
Try to follow this (very important) rule as you build your application:
Write as few lines of executable code as possible, run your program to make sure it works as you would have expected those lines to work. Fix and repeat.
Remember to have small and short methods that are responsible for discrete tasks.
Some example methods to get you started:
generate_question
prompt_player_for_answer
verify_answer
Remember to also seperate your I/O (Input/output) methods from your logic methods. Logic methods will not use puts
or gets
and instead change the state of the game or perform other logic that other methods will perhaps tell or ask the user about.
For variables outside any method, use @instance
variables.
For example, start by declaring instance variables to keep track of the two players.
This is because some of the other methods will require access to the player information and without instance or global variables, this is not possible.
Remember that instance variables are something we used in yesterday's work.
Add colour to your output. You can do what you like. A simple yet acceptable approach is to output bad outcomes as red and good outcomes as green.
Tip: Look for gems that can help make this easier for you.
Instead of just prompting the user for addition questions, randomize that part too. Ask either addition, subtraction or multiplication questions.
When the game starts, asks for Player 1’s name, and then Player 2’s name. From that point on, use their names instead of "Player 1"
or "Player 2"
when prompting them.
Instead of having to restart the ruby script, when the game finishes, ask if they want to play again and do so based on their response.
Restarting the game does not ask the players for their name again, it assumes that the same people are playing again. Now you can keep track of their score and at the end of each game let them know the player scores.