Making a time counter while waiting for user input in ruby - ruby

I'm trying to create a "tamagoshi" like game to practice ruby. I want the program to wait for user input to do something, but from time to time (right now I am using 10 seconds until I get it right) I want a method to occur (the pet looses energy, and becomes sadder with time). But if the user inputs an action, I want the timer to stop e reset.
I'm having some problems, tried different things and right now, I am only able to do one action. After that, nothing happens. I guess I'm not creating a new thread after doing ".join" but I'm not sure.
I very much welcome all critics on the rest of the code to improve my skills and knowledge as I just started learning ruby.
dog = Dog.new 'Yoshi'
def play pet
user_action = nil
timer_thread = Thread.new do
while !user_action
(1..10).each do |number|
sleep(1)
puts number
end
pet.time_goes_on
user_action = nil
end
end
user_action = gets.chomp
case user_action
when "play"
pet.play_fetch
when "eat"
pet.feed
when "train"
pet.training
when "sleep"
pet.sleep
when "walk"
pet.take_for_a_walk
else
puts "invalid command, try again"
play(pet)
end
timer_thread.join
end
while dog.is_alive
play(dog)
end
Here's the full code at repl.it:
tama'yoshi'

I guess I found a solution!
dog = Dog.new 'Yoshi'
def play pet
while pet.is_alive
user_action = nil
timer_thread = Thread.new do
while user_action == nil
(1..10).each do |number|
sleep(1)
number
if number == 10
pet.time_goes_on
end
break if (user_action != nil)
end
end
end
user_action = gets.chomp
case user_action
when "play"
pet.play_fetch
when "eat"
pet.feed
when "train"
pet.training
when "sleep"
pet.sleep
when "walk"
pet.take_for_a_walk
else
puts "invalid command, try again"
play(pet)
end
timer_thread.join
end
end
play(dog)

Related

Undefined Method "attack" for Player::Class

So I am trying to make a text based game with classes for an assignment. I have worked for 2 hours and could not find what the problem is.
class Rankuun
attr_accessor :rankuun_damage, :rankuun_health
def initialize
rankuun_health = 200
rankuun_damage = 100
end
def monolouge
puts 'Rankuun: "So, I see that you have lived this long. I am suprised.'
puts "Not a single libing creature has lived for this long inside my dungeon."
puts "But it's time that your endless slaughter of my brethren are halted."
puts "Now face what true fear really is!"
puts "Hoc vanitas est, et non est fere ut serves!"
puts "You see a mystical aura rise around Rankuun, and hear the shouts of agony"
puts "Rankuun has grown twice in size, and has taken the form of some kind of lich"
puts 'Rankuun: WELCOME TO DIE!"'
end
end
class Player
attr_accessor :health, :gold
def initialize
health = 100
money = 200
puts "Health: #{health}"
puts "Gold: #{money}"
end
def attack
puts "You attack the monster!"
hitmiss = 1
if hitmiss == 1
dmg = rand(5..10)
puts "You hit the monster, and do #{dmg} damage!"
monster_health = monster_health - dmg
elsif hitmiss == 2
puts "You missed!"
end
end
def guard
puts "You attempt to defend yourself"
guard = rand(1..2)
if guard == 1
counter = rand(5..10)
puts "You block the damage, and counterstrike for #{counter} damage"
monster_health = monster_health - counter
elsif guard == 2
monster_counter = rand(1..5)
puts "You try to guard, but the enemy hits harder than you expected, and you get dealt #{monster_counter}"
health = health = monster_counter
end
end
def loot
puts "You search the room and find:"
loot_item = rand (2..3)
if loot_item == 2
puts "You find some gold!"
money = money + 50
puts "Health: #{health}"
puts "Gold: #{money}"
elsif loot_item == 3
puts "You find a curious potion that seems to heal you"
health = health + 50
puts "Health: #{health}"
puts "Gold: #{money}"
end
end
def encounter
encounter = rand(1..2)
if encounter == 1
puts "A monster confronts you!"
monster = Monster.new
elsif encounter == 2
puts "There appears to be no monsters in this room"
end
end
end
class Monster
attr_accessor :monster_health, :monster_damage
def initialize
monster_health = 50
monster_damage = 10
end
def monster_attack
puts "The monster attacks you!"
end
end
puts "There has been a saying in your town for as long as you can remember:"
puts "Ne pas entrer dans le Donjon De Rankuun"
puts 'It means: "Do not enter The Dungeon of Rankuun"'
puts "Many adventurers died inside, and the only living creature in there is the man named Rankuun"
puts "He has great power over the Dungeon, reviving the dead and casting black magic"
puts "You have been selected by the village to go into the Dungeon and exterminate Rankuun"
puts "You have been given a sword, a shield, and some gold. Now you must enter:"
puts "T H E D U N G E O N O F R A N K U U N!"
puts ""
puts ""
player = Player.new
player.encounter
room1 = gets.chomp
if room1 == "attack"
player.attack
elsif room1 == "loot"
player.loot
end
It would be great if this problem were solved. Thanks for responding and aiding me in my assignment.
Welcome to the exciting world of object-oriented design. Many adventurers died inside.
I think you may have a small misunderstanding about the difference between classes and instances. If so, I strongly advise you to read about it before continuing.
You created a new instance of Player when you called Player.new. Your first mistake was not putting it in a variable.
Try something like this:
my_player = Player.new
Secondly, you are trying to call encounter on the Player class, while you should call it on the new instance.
my_player.encounter
You do the same thing inside the Monster class with Player.attack.
I could tell you how to solve each of these problems individually, but I think you would benefit more from redesigning some parts of the project to be easier to change in the future. Hopefully, most of the problems will resolve themselves along the way.
Generally speaking, the shorter a method is, the better. When you tell the Player to attack, that is all it should do. Instead, it does all sorts of things, including getting the monster to attack!
It suddenly becomes apparent that the two classes have quite a lot in common: they both attack; they both take damage, and they both die. It's time to make a superclass. (If you are not familiar with how classical inheritance works, you should learn - this truly is the perfect use case for it.)
class Character
attr_accessor :health
def attack damageable, damage
damageable.take_damage damage
end
def take_damage damage
health -= damage # Equivenent to health = health - damage
potential_death
end
def potential_death
if dead?
die
end
end
def dead?
health <= 0 # With random damage, it could be less than 0.
end
def die # overruled by subclass
end
end
The greatest advantage to doing it like this is you only have to write the code in one place, and it will work for everything. If you change your mind about a design decision, you can change it in one place and know that everything will be adjusted.
You can make a subclass similar to this:
class Monster < Character
def die
super # Call the copy of die in Character, in case it contains something important
reward killer
puts "You kill the monster..."
end
def reward rewardable
rewardable.gain_money 30
end
end
class Player < Character
def die
super # Call the copy of die in Character, in case it contains something important
puts "You died..."
game.over
end
end
(These are only examples; they are not as complete as the code you already have.)
Do you see how each method only does one thing? If you apply that principle to everything you write, it will become much easier to reuse bits and pieces.
I hope this has been useful. If you decide to stick with what you have and just fix the errors, just say so in the comments, and I'll help you with that.
Good luck!

How to restart script from the beginning?

I decided to make a number-guessing game. Here is the code.
print "Guess a number from 1-20. You have 5 guesses!"
guess1=gets.chomp
guess1=guess1.to_i
random=1 + rand(20)
random=random.to_i
if guess1 == random
puts "Correct! You win!!!"
sleep(5)
Kernel.exit
elsif guess1 > random
puts "Wrong! Too high. Try again!"
else
puts "Wrong! Too low. Try again!"
end
guess2=gets.chomp
guess2=guess2.to_i
if guess2 == random
puts "Correct! You win!!!"
sleep(5)
Kernel.exit
elsif guess2 > random
puts "Wrong! Too high. Try again!"
else
puts "Wrong! Too low. Try again!"
end
guess3=gets.chomp
guess3=guess3.to_i
if guess3 == random
puts "Correct! You win!!!"
sleep(5)
Kernel.exit
elsif guess3 > random
puts "Wrong! Too high. Try again!"
else
puts "Wrong! Too low. Try again!"
end
guess4=gets.chomp
guess4=guess4.to_i
if guess4 == random
puts "Correct! You win!!!"
sleep(5)
Kernel.exit
elsif guess4 > random
puts "Wrong! Too high. Try again!"
else
puts "Wrong! Too low. Try again!"
end
guess5=gets.chomp
guess5=guess5.to_i
if guess5 == random
puts "Correct! You win!!!"
sleep(5)
Kernel.exit
elsif guess5 > random
puts "Wrong! Too high. Game over!"
sleep(5)
Kernel.exit
else
puts "Wrong! Too low. Game over!"
sleep(5)
Kernel.exit
end
How would I add a try-again option at the end that would restart the game?
As Sergio said, a very clean solution would be to learn about functions, and divide up your game logic, so that you can call the function(s) when needed.
Another solution would be to use loops, specifically a do...while loop.
Side note: The begin...while style of looping in the tutorial link I posted is not recommended by Matz, the creator of Ruby. He recommends the loop do...break end style, which is what I will demonstrate.
I'll let you read the tutorials to learn more about whats going on, but the gist is that this particular style of loop will run your code once, and it will either loop back and run it again, or exit the loop and the program will end, depending on the result of our "control" variable.
Unfortunately, because of the way you've written your code, wrapping the program in a simple do...while is very awkward because of the Kernel.exit lines, and the fact that you've hard coded 5 guesses which are run in sequence. Since my answer involves loops, I'll quickly show you a good way to refactor the code without too much pain. Please note the comments in the code to understand what's going on.
loop do # The start of the main game loop
random=1 + rand(20)
random=random.to_i
guess_count = 0 # Tracks the number of times the user has guessed
print "Guess a number from 1-20. You have 5 guesses!"
while guess_count < 5 # The guess loop; keep looping up to a max of 5 times
guess=gets.chomp
guess=guess.to_i
if guess == random
puts "Correct! You win!!!"
sleep(5) # 5 seconds is a very long time, I would reduce this to 1 at most
break # This causes the 'guess loop' to end early
elsif guess > random
puts "Wrong! Too high. Try again!"
else
puts "Wrong! Too low. Try again!"
end
guess_count+= 1 # increment the guess count if the user guessed incorrectly
end
puts "Would you like to play again? (y/n)"
play_again = gets.chomp
break if play_again != "y" # exit the main loop if the user typed anything except "y"
end # The end of the main loop, and thus the entire program
# No need for Kernal.exit. The program is done at this point.
Note: I removed your comments so they don't interfere with the explanation. Feel free to put them back in your version.
Take a look at loops in the tutorials for more details. Hope thats clear.
What you want to do is make the main thread of your game a loop:
loop do
# Prompt
end
This will execute endlessly whatever is inside it until an exception is raised or the program is interrupted.
Now, to make it effective, you'll want to start wrapping up your game logic in an Object.
For instance
class GuessingGame
def initialize
#secret = 1 + rand(20)
#guesses_remaining = 5
end
def is_it?(number)
#guesses_remaining -+ 1
#secret == number
end
end
Now, when you start the game look, you just make GuessingGame.new, and after the game is over, by running out of guesses, or getting it right, you can just prompt to retry and make a new guessing game
First, extract the repeated part into a method:
def correctly_guessed?(target)
guess = gets.to_i
if guess == target then puts "Correct! You win!!!"; sleep(5)
elsif guess > target then puts "Wrong! Too high. Try again!"
else puts "Wrong! Too low. Try again!"
end
end
Then, restructure your code:
loop do
print "Guess a number from 1-20. You have 5 guesses!"
target = 1 + rand(20)
5.times{break if correctly_guessed?(target)}
print "Restart the game? (Y to restart)"
break unless gets.chomp == "Y"
end

Ruby: How to exit a function and then return to it

I'm writing a text-adventure game for exercise 36 of Learn Ruby the hard Way: http://ruby.learncodethehardway.org/book/ex36.html
I want to include 'instructions' as an option players can use anytime, but once the player exits the Room() function and enters the intructions() function, I am unsure how to return them to the appropriate room. I can have the instructions() always return player to the start after, but is there a way to get them back to the same location?
Here is a quick example, sorry it's incomplete...I'm still in the middle of building it:
puts
puts <<INTRO
"Welcome to the Cave of Indifference.
It doesn't much care for you. Beware!
There are deadly areas of this cave, but if you seek
it you may find the secret treasure and escape with your life."
INTRO
puts
puts "Type \'Instructions\' at any time for direction"
puts
sword = false
monster = true
treasure = false
def Command()
puts ">>> "
end
def dead(how)
puts how.to_s
puts "PLAYER DEAD!"
Process.exit(0)
end
def instruction()
puts "Rooms will have individual instructions"
puts "but here are some general items."
puts "west, east, north, south: goes that direction"
puts "look: look around the room"
puts "take: to take item or object"
end
def Room1()
puts "You are now at the cave entrance."
puts "You may go west or east. OR exit with your life!"
Command(); choice = gets.chomp()
if choice.downcase == "exit" && treasure = true
puts "Congratulations! You win!"
Process.break
elsif choice.downcase == "exit" && treasure = false
puts "Seriously?! Giving up already?"
puts "Fine. Here is what happens:"
dead("You stumble on your exit from the cave and trip
on a rock. The fall cracks your skull and you bleed
to death. Bye bye!")
elsif choice.downcase.include? "right"
#INPUT
elsif choice.downcase.include? "left"
#INPUT
elsif choice.downcase.include? "instructions"
instructions()
else
"That command makes no sense, try again."
end
end
Room1()
I also assume there are many issues with the code and would be very appreciative of your help, but no worries I am going to keep working on this and make it really fun to play :)
You can give the instruction method the last location.
def instruction(last_room)
#do stuff
last_room.call()
end
You would call that function like so:
instructions(method(:Room1)), where Room1 is the name of the method you want to return to.
The problem you have isn't having instructions return to Room1 (or RoomX). It'll do that without you doing anything special. What you need is something like:
#room = :Room1
while true
send(#room)
end
and then set the variable #room to control which room you're in.
It's not the greatest way in the world to do that, but it'll get you started.

Catch and throw not working in ruby

I am trying to make a number guessing game in Ruby but the program exits after I type in yes when I want to play again. I tried using the catch and throw but it would not work. Could I please get some help.
Here is my code.
class Game
def Play
catch (:start) do
$a=rand(11)
puts ($a)
until $g==$a
puts "Guess the number between 0-10."
$g=gets.to_i
if $g>$a
puts "The number you guessed is too high."
elsif $g==$a
puts "Correct you won!!!"
puts "Would you like to play again?"
$s=gets()
if $s=="yes"
$c=true
end
if $c==true
throw (:start)
end
elsif $g<$a
puts "The number you guessed is too low."
end
end
end
end
end
Game.new.Play
Edit: Here's my new code after trying suggestions:
class Game
def Play
catch (:start) do
$a=rand(11)
puts ($a)
while $s=="yes"
until $g==$a
puts "Guess the number between 0-10."
$g=gets.chomp.to_i
if $g>$a
puts "The number you guessed is too high."
elsif $g==$a
puts "Correct you won!!!"
puts "Would you like to play again?"
$s=gets.chomp
if $s=="yes"
throw (:start)
end
elsif $g<$a
puts "The number you guessed is too low."
end
end
end
end
end
end
Game.new.Play
Your first problem is here:
$s=gets()
if $s=="yes"
$c=true
end
The gets method will read the next line including the new line character '\n', and you compare it to only "yes":
> gets
=> "yes\n"
The idiomatic way to fix this in Ruby is the chomp method:
> gets.chomp
=> "yes"
That said, your code has two other deficiencies.
You may come from a language such as PHP, Perl, or even just Bash scripting, but Ruby doesn't require the dollar sign before variables. Using a $ gives a variable global scope, which is likely not what you want. In fact, you almost never want a variable to have global scope.
Ruby uses three types of symbol prefixes to indicate scope - # for instance, ## for class, and $ for global. However the most common type of variable is just local which doesn't need any prefix, and what I would suggest for your code.
I have always been told that it is very bad practice to use exceptions for control structure. Your code would be better served with a while/break structure.
When you do gets(), it retrieves the full line with a '\n' in the end. You need to trim the new line character by using:
$g=gets.chomp.to_i
Same for other gets
Based on your updated code (where you fixed the newline problem shown by others), your new problem is that you have wrapped all your game inside while $s=="true". The very first time your code is run, $s is nil (it has never been set), and so you never get to play. If you used local variables instead of global variables (s instead of $s) this would have become more obvious, because the code would not even have run.
Here's one working way that I would re-write your game.
class Game
def play
keep_playing = true
while keep_playing
answer = rand(11) # Make a new answer each time
puts answer if $DEBUG # we don't normally let the user cheat
loop do # keep going until I break from the loop
puts "Guess the number between 0-10."
guess = gets.to_i # no need for chomp here
if guess>answer
puts "The number you guessed is too high."
elsif guess<answer
puts "The number you guessed is too low."
else
puts "Correct you won!!!",
"Would you like to play again?"
keep_playing = gets.chomp.downcase=="yes"
break
end
end
end
end
end
Game.new.play
I know this doesn't really answer your question about why your code isn't working, but after seeing the code you posted I just had to refactor it. Here you go:
class Game
def initialize
#answer = rand(11)
end
def play
loop do
guess = get_guess
display_feedback guess
break if guess == #answer
end
end
def self.play_loop
loop do
Game.new.play
break unless play_again?
end
end
private
def get_guess
puts "Guess the number between 0-10."
return gets.chomp.to_i
end
def display_feedback(guess)
if guess > #answer
puts "The number you guessed is too high."
elsif guess < #answer
puts "The number you guessed is too low."
elsif guess == #answer
puts "Correct you won!!!"
end
end
def self.play_again?
puts "Would you like to play again?"
return gets.chomp == "yes"
end
end
Game.play_loop

Is there a way to 'cd ..' up a nested "if" statement tree?

I'm curious if there's a way to have the program go back up the if statement stack?
Ideally, the program would return to line 2 and prompt the user for the input variable, then continue to evaluate like it did the first time. Think of it like a cursor in a text editor, I just want to move it from either of those two comments back up to line 2. The two places of interest are commented out below:
while true
input = gets.chomp
if input != input.upcase
puts "HUH?! SPEAK UP, SONNY!"
elsif input == 'BYE'
puts "HUH?! SPEAK UP, SONNY!"
input = gets.chomp
if input == 'BYE'
puts "HUH?! SPEAK UP, SONNY!"
input = gets.chomp
if input == 'BYE'
puts "GOOD BYE!";
break
else
# return to top-level if statement
end
else
# return to top-level if statement
end
else
random_year = rand(1930..1950)
puts "NO, NOT SINCE #{random_year}!"
end
end
In the code you show, you don't need to do anything to make the flow of execution go back to line 2. Just omit the else clauses in the two places you marked. The flow of execution will drop down to the bottom of the while loop, then loop back to the top, then go back to line 2.
You need to use a while statement to set a condition flag and check it, which will loop back to the while statement if you don't change the flag:
flag = 0
while flag1 == 0
if var = "string"
then ...statements...
flag1 = 1 ; this allows us to break out of this while loop
else ...statements...
end
end
If flag1 is not 0 at the end of the while statement, the while statement will loop back. For two such conditions, you need to nest the while loops. You might have to re-order your statements to make multiple while loops work this way.
You can avoid this level of neasted ifs with:
byecount = 0
while byecount < 3
input = gets.chomp
if input == "BYE"
byecount += 1
next
else
byecount = 0
end
if input != input.upcase
puts "HUH?! SPEAK UP, SONNY!"
else
puts "NO, NOT SINCE #{rand(1930..1950)}!"
end
end
puts "GOOD BYE!"
Or you can write a catch..throw flow structure. (Really.. if you need to use it, something is wrong with your design)
catch :exitloop do
while ...
if ...
if ...
if ...
throw :exitloop
end
end
end
end
end
Here's how I'd write a similar exercise:
BYE = 'BYE'
HUH = "HUH?! SPEAK UP, SONNY!"
loop do
input = gets.chomp
if input != input.upcase
puts HUH
next
end
if input != BYE
random_year = rand(1930..1950)
puts "NO, NOT SINCE #{random_year}!"
next
end
puts HUH
input = gets.chomp
if input == BYE
puts HUH
input = gets.chomp
if input == BYE
puts "GOOD BYE!";
break
end
end
end
I used loop instead of while. Matz, the main man for Ruby, recommends loop. See "Is there a “do … while” loop in Ruby?" for further discussion about it.

Resources