Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Guessing game #16

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
70 changes: 5 additions & 65 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,68 +2,8 @@

## Description

Create a number-guessing game.

## Objectives

### Learning Objectives

After completing this assignment, you should...

* Understand basic ways in which a Ruby program can present information to the user and request information from the user.
* Understand what methods are and how they can be used to:
* Reduce code repetition
* Improve readers' ability to reason about code
* Understand how ruby code resides on the file system and how it is executed

### Performance Objectives

After completing this assignment, you should be able to effectively...

* Use puts and gets
* Fork a github repository
* Add, commit, and push to github
* Create, add to, and check for presence in an array
* Run ruby code from the command line

## Details

### Deliverables

Fork this git repository and check it out to your local machine. Create a ruby file called `game.rb`.

After you get each requirement working, run `git add .`, then `git commit -m "TEXT HERE"`, where TEXT HERE should be replaced with the description of the requirement you just finished.

After your work is complete, make sure to `git push` to get your changes up to github. Feel free to do this more than once along the way, of course.

Finally, create a pull request for your completed project. This will count as turning in your work.

### Requirements

* The gameplay should match the instructions given in the Mode(s) below.
* After 5 incorrect guesses, the program should tell you that you lose.
* If you guess the same number twice, the program should ask you if you're feeling all right (or something similarly sarcastic).
* Your code should include at least two methods.
* This game should be run from the command line by typing "ruby game.rb".

## Normal Mode

Your goal for tonight is to create a number guessing game. Your program should pick a random number between 1 and 100 and ask you for a guess. If your guess is less than the program's number, it should tell you that you were low and let you guess again. If the guess is greater than the program's number, it should tell you that you were high and let you guess again. If your guess is correct, the program should tell you that you win and then quit.

## Hard Mode

Hard Mode has two parts to it:

1. In normal mode, you will probably use the "rand" method. See if you can find another way!
2. The program should also comment on your behavior if you make a guess that doesn't help you. For example, you might say "50" and then be told "that's too low." If you then guess "25," you're just wasting a guess.

## Nightmare Mode

Write the opposite program as well: you, the user, pick a number between 1 and 100. The computer has to guess the number. You tell it whether it's too high, too low, or correct after each guess. The computer gets five guesses. The computer should tell you if you are lying to it. (e.g. Computer guesses 50, you say "High." Computer then guesses 49 and you say "Low." You'd be lying, as there are no numbers between them.)

## Additional Resources

If you do Nightmare Mode, you might want to:

* [Watch this video](https://www.youtube.com/watch?v=JQhciTuD3E8) about binary search
* [Read this article](http://en.wikipedia.org/wiki/Binary_search_algorithm) about binary search
This is a number-guessing game. The player is prompted to guess a number between 1 and 100.
* If the player guesses the same number more than once, the program informs the player that he or she has already guessed that number and does not count the guess toward the five-guess limit.
* If the player guesses a number that is less than 1 or greater than 100, the program informs the player that the guess is invalid and does not count the guess toward the five-guess limit.
* After five incorrect guesses, the program tells the player that he or she has lost the game and asks the player whether he or she would like to play again.
* If the player guesses the correct number, the program congratulates the player and exits.
72 changes: 72 additions & 0 deletions game.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
class Game

def initialize
@chosen = rand(1..100)
@count = 0
@previous_guesses = []
end

def pick_number
if @count == 0
puts "Please select a number from 1 to 100 and press enter."
@number = gets.chomp.to_i
evaluate_guess
elsif @count < 5
puts "Please guess again."
@number = gets.chomp.to_i
#@previous_guesses = []
evaluate_guess
else
puts "Sorry, you're out of guesses. You lose."
puts "Would you like to play again? (Y/N)"
@response = gets.chomp.downcase
if @response == "y" || @response == "yes"
Game.new.pick_number
else
exit
end
end
end

def evaluate_guess
#puts @chosen
#puts @number
puts @previous_guesses
if @previous_guesses.include?(@number)
puts "You already guessed that, silly! Guess a new number."
pick_number
#guesses
elsif @number == @chosen
puts "Congratulations! You guessed the correct number!"
exit
elsif @number > @chosen && @number < 101
puts "Your number is too high."
@previous_guesses << @number
@count = @count + 1
pick_number
#guesses
elsif @number < @chosen && @number > 0
puts "Your number is too low."
@previous_guesses << @number
@count = @count + 1
pick_number
#guesses
else
puts "You have chosen an invalid number."
#@previous_guesses << @number
pick_number
#guesses
end
end

# def guesses
# #puts @number
# @previous_guesses = []
# number = @number
# @previous_guesses << @number
# pick_number
# end
end

game = Game.new
game.pick_number