Pan7h3r
Pan7h3r

Reputation: 5

Method in a loop not looping - Ruby

def playmany
    puts "Please enter in how many games you want to play"
    games_to_play = gets.chomp.to_i
    games_to_play.times do
        puts "\n"
        rules
    end
end

(Absolute ruby noob so apologies in advance if I'm using terms wrong)

I want this to repeat the method 'rules' but it only returns once and then breaks. Is there a way for the 'rules' method to be executed the amount of times the user specifies?

This is what 'rules' contains, I don't know if it's something in the 'playmany' method or the 'rules' method.

def rules
case player = rand(1..3)
    when player = 1
        player = "scissors"
        puts "Player choses: " + player
    when player = 2
        player = "paper"
        puts "Player choses: " + player
    when player = 3
        player = "rock"
        puts "Player choses: " + player
    end
case computer = rand(1..3)
    when computer = 1
        computer ="scissors"
        puts "Computer choses: " + computer
    when computer = 2
        computer ="paper"
        puts "Computer choses: " + computer
    when computer = 3
        computer = "rock"
        puts "Computer choses: " + computer
    end
case player
    when player = "scissors"
        if computer == "rock"
            puts "Player loses"
            $computertally += 1
            main_menu
        elsif computer == "scissors"
            puts "Computer and player tie"
            $tiecount += 1
            main_menu
        else
            puts "Player wins"
            $playertally += 1
            main_menu
        end
when player = "rock"
    if computer == "rock"
        puts "Computer and player ties"
        $tiecount += 1
        main_menu
    elsif computer == "scissors"
        puts "Player wins"
        $playertally += 1
        main_menu
    else
        puts "Player loses"
        $computertally += 1
        main_menu
    end
when player = "paper"
    if computer == "rock"
        puts "Player wins"
        $playertally += 1
        main_menu
    elsif computer == "scissors"
        puts "Player loses"
        $computertally += 1
        main_menu
    else
        puts "Computer and player tie"
        $tiecount += 1
        main_menu
    end
end
end

Update: The main_menu is this

puts "Welcome to rock, paper, scissors" 
puts "--------------------------------"
puts "\n"
puts "What do you want to do?"
puts "-- Type 'play' to have a turn"
puts "-- Type 'playmany' to play multiple games"
puts "-- Type 'score' to see the current scoreboard"
puts "-- Type 'exit' to exit"
def main_menu
case gets.chomp.downcase
    when "play"
        rules
    when "score"
        puts "\n"
        puts "Computer has won #{$computertally} games"
        puts "Player has won #{$playertally} games"
        puts "A tie has occured #{$tiecount} times"
        puts "\n"
        main_menu
    when "exit"
        exit
    when "playmany"
        playmany
    else
        puts "Error with selection, please try again"
        main_menu
    end
end

Upvotes: 0

Views: 60

Answers (3)

Boris Stitnicky
Boris Stitnicky

Reputation: 12578

Your question should properly be closed because SO is not here to debug your code. But since this is your first question, I will instead make a brief answer. Redefine #rules method as follows:

def rules
  puts "Hi, I'm looping!"
end

Now, if you call your #playmany method, you will see that #rules method actually is looping, but has a problem(s) in it. Think about what your method rules is supposed to do, try to write tests for it, and try to modify your method to fulfill the test expectations.

Upvotes: 1

SteveTurczyn
SteveTurczyn

Reputation: 36860

On the main_menu options, are you actually entering "exit" ?

Exit really does exit the program.

Try replacing

when "exit"
exit

with

when "exit"
return

return will exit the main_menu method and allow your program to continue with subsequent steps, including the loop.

Upvotes: 1

stratton1101
stratton1101

Reputation: 16

hard to tell from this, but make sure you're indenting your code so it's easy to read and that all of your loops and conditionals conclude with and 'end' statement.

Also, try calling rules as follows - "rules()"

Upvotes: 0

Related Questions