9 Replies - 2602 Views - Last Post: 29 January 2013 - 05:28 PM Rate Topic: -----

#1 loopConfused  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 10
  • Joined: 18-January 13

Rock, paper, sicssiors game assistance

Posted 28 January 2013 - 10:40 AM

So there are 3 things I am not understanding. the first thing that I'm not uderstanding is how to make the game go for x amount of times without starting the game over. So if a player wants to play 10 games at once I want them to be able to play that much. The second thing is where to put another if statement to ask the player if they want to quite the game.I think it would go in the game area, but I'm not 100% sure. The last thing is to keep track of the score of wins, loses and tie between the player and the computer. Thanks for the assistance and the understanding

 puts "Would you like to play a Game?"
     puts "Yes/No"
     player_input = gets.chomp.downcase
     if player_input == 'yes'
       puts "Welcome to Rock, Paper, Scissors!"
       puts "Press r for Rock"
       puts "Press s for Scissors"
       puts "Press p for Paper"
       computer = "rsp"[rand(3)].chr
       player = $stdin.gets.chomp.downcase
       case [player, computer]
         when ['p','r'], ['s','p'], ['r','s']
           puts "You Win!"
         when ['r','r'], ['s','s'], ['p','p']
           puts "You Tied!"
         else
           puts "You Lose!"
       end
       puts "The computer chose: #{computer.upcase}"
       
     else player_input = "no"
       puts "Come again soon. :)/>"
     end


Is This A Good Question/Topic? 0
  • +

Replies To: Rock, paper, sicssiors game assistance

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4332
  • View blog
  • Posts: 12,127
  • Joined: 18-April 07

Re: Rock, paper, sicssiors game assistance

Posted 28 January 2013 - 10:54 AM

Well one way you can make it play multiple games is to simply wrap this code into an object and then when they FIRST start ask them how many games they want to play at once. If they say 10 then you create an array of 10 "Game" objects and loop through them looking for which games have been played out etc.

The if statement asking of quitting can be placed anywhere really. Usually you would want to put it anywhere you are about to ask the user for input. But don't you essentially already ask that when you say "Would you like to play another game?" and then they can enter "No" and it will quit.

To keep score simply add counter variables and where you print "you win!" you would increment a variable right after. Same with the Tied and Lose print statements.

Hope this gives you the direction you were looking for. :)
Was This Post Helpful? 2
  • +
  • -

#3 loopConfused  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 10
  • Joined: 18-January 13

Re: Rock, paper, sicssiors game assistance

Posted 28 January 2013 - 12:33 PM

for the x amount of times a player can play I should have said infinite amount of time until the player tells the game that they want to quite. sorry for the miss typing
Was This Post Helpful? 0
  • +
  • -

#4 NotarySojac  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 53
  • View blog
  • Posts: 428
  • Joined: 30-September 10

Re: Rock, paper, sicssiors game assistance

Posted 28 January 2013 - 02:10 PM

View PostloopConfused, on 28 January 2013 - 12:33 PM, said:

for the x amount of times a player can play I should have said infinite amount of time until the player tells the game that they want to quite. sorry for the miss typing


Here's the absolute cheapest way to terminate the console game:

1) Tell the player at the start of the game "Type QUIT at anytime to exit"
2) Add a condition where you do STDIN.gets to look for "QUIT" and terminate the application upon finding that code.

But, before you do that, think about wrapping all your code into a class so it can be instantiated as an object (or atleast wrap it up into a method so you can loop over it multiple times cleanly, with out having to worry about introducing another layer of nest`age).

Martyr2 is suggesting that the top level of your code could look like this (which is so nice to look at):
game = RockPaperScissors.new

while true # loops forever
  game.play_round
end



in your "Come again soon. :)/>" codepath, you could add an Exit! expression to force your repl program to stop playing.


Are you comfortable with classes and objects in ruby yet? They're especially easy to use in Ruby cause Matz is great.

class RockPaperScissors
  def play_round
    ...Your code...
  end
end



You might even think of introducing complexity like score keeping and having multiple users once your code base becomes so trivially simplified by introducing a class based approach.

username = STDIN.gets
password = STDIN.gets
user = User.where(:username => username, :password => password)  # lookups via ActiveRecord database, which I've never done outside of rails but I think is possible

user.wins
# => 10


Was This Post Helpful? 1
  • +
  • -

#5 loopConfused  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 10
  • Joined: 18-January 13

Re: Rock, paper, sicssiors game assistance

Posted 29 January 2013 - 10:37 AM

[quote name='NotarySojac' date='28 January 2013 - 02:10 PM' timestamp='1359407416' post='1792350']

View PostloopConfused, on 28 January 2013 - 12:33 PM, said:

for the x amount of times a player can play I should have said infinite amount of time until the player tells the game that they want to quite. sorry for the miss typing


Here's the absolute cheapest way to terminate the console game:

1) Tell the player at the start of the game "Type QUIT at anytime to exit"
2) Add a condition where you do STDIN.gets to look for "QUIT" and terminate the application upon finding that code.

But, before you do that, think about wrapping all your code into a class so it can be instantiated as an object (or atleast wrap it up into a method so you can loop over it multiple times cleanly, with out having to worry about introducing another layer of nest`age).

Martyr2 is suggesting that the top level of your code could look like this (which is so nice to look at):
game = RockPaperScissors.new

while true # loops forever
  game.play_round
end



how would this look like or what would I put into it?
Was This Post Helpful? 0
  • +
  • -

#6 loopConfused  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 10
  • Joined: 18-January 13

Re: Rock, paper, sicssiors game assistance

Posted 29 January 2013 - 12:36 PM

So this is what my code looks like right now, but I can not play the game. what is missing from it.
puts "Would you like to play a Game?"
puts "Yes/No"

player_input = gets.chomp.downcase

class  RockPaperScissors
  def play_round
    while true
      game.play_round
      if player_input == 'yes'
        puts "Welcome to Rock, Paper, Scissors!"
        puts "Press r for Rock"
        puts "Press s for Scissors"
        puts "Press p for Paper"
        computer = "rsp"[rand(3)].chr
        player = $stdin.gets.chomp.downcase
        case [player, computer]
          when ['p','r'], ['s','p'], ['r','s']
            puts "You Win!"
          when ['r','r'], ['s','s'], ['p','p']
            puts "You Tied!"
          else
            puts "You Lose!"
        end
        puts "The computer chose: #{computer.upcase}"

      else player_input = "no"
      puts "You no come play. :(/>"
      end
    end
  end
end


Was This Post Helpful? 0
  • +
  • -

#7 NotarySojac  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 53
  • View blog
  • Posts: 428
  • Joined: 30-September 10

Re: Rock, paper, sicssiors game assistance

Posted 29 January 2013 - 03:25 PM

View PostloopConfused, on 29 January 2013 - 12:36 PM, said:

So this is what my code looks like right now, but I can not play the game. what is missing from it.


Well, it's been suggested that you wrap your code into a class so it's easier to implement. A class can be thought of as a chunk of code that doesn't do anything until you use it. For instance, check out this class which consists of 2 'static' methods. (static methods are functions that you can call with out needing to worry about instantiated classes which you might not be ready to start thinking about):

(talker class)
class TalkerClass
  def self.ask_if_user_wants_to_play
    puts "Would you like to play a Game?"
    puts "Yes/No"
	 
    player_input = gets.chomp.downcase
    if player_input == "yes"
      return true
    else
      return false
    end
  end

  def self.say_goodbye
    puts "You no come play. :(/>/>/>"
  end
end



So if you put that alone in a file, and the run the file, NOTHING will happen. And that's expected. BUT, if you put this code at the end of the file, you'll see that things will happen.

(top level algo)
user_wants_to_play = talker.ask_if_user_wants_to_play

if user_wants_to_play
  puts "WE KNOW THE USER WANTS TO PLAY"
else
  puts "WE KNOW THE USER DOESN'T WANT TO PLAY"
end


# talker.say_goodbye




Do you notice how the "top level algo" code section looks really simple and is easy to follow? And we call the 'guts' of the program with cute little names like "talker.ask_if_user_wants_to_play" and so on? So the point of putting your code into classes is to get it out of the way so it's easier to maintain and look at.

Your second attempt is ok, but it tells me that you might not be comfortable working with methods (aka functions) yet. If that's the case, you might want to focus on creating functions more often and come to classes when you think you're ready.

See if you can call (invoke) the methods in your class to produce a desired result. You'll have trouble and need to reshape things. Also keep in mind that the classes need to be defined before you can use them (so they will appear at the tops of your script, and then ultimately in their own separate files when they grow too big).

I'll try to start you off with out spoiling it. Use this code as your starting point:



class  RockPaperScissors
  def self.play_round    # this is a static method because I used the prefix 'self.'
    puts "Welcome to Rock, Paper, Scissors!"
    puts "Press r for Rock"
    puts "Press s for Scissors"
    puts "Press p for Paper"
    computer = "rsp"[rand(3)].chr
    player = $stdin.gets.chomp.downcase
    case [player, computer]
      when ['p','r'], ['s','p'], ['r','s']
        puts "You Win!"
      when ['r','r'], ['s','s'], ['p','p']
        puts "You Tied!"
      else
        puts "You Lose!"
    end
    puts "The computer chose: #{computer.upcase}"
  end
  
  def self.ask_user_if_they_want_to_play
    # refactor your code here
  end
end


while true
  puts "Would you like to play a Game?"
  puts "Yes/No"

  player_input = gets.chomp.downcase

  RockPaperScissors.play_round # play a single round
end




The top level algo is everything after the class. It's still pretty sloppy, and what's more, I've introduced a bug where even if they player types "no" the game will continue (shut it down with ctrl+c). See if you can move more code out of the top level algo (put it into the self.ask_user_if_they_want_to_play function that I created for you). After that, see if you can figure out how to fix the bugs. Let me know if you run into any other trouble. Also you might wanna checkout http://rubylearning....by_methods.html if you need help understanding methods. It's almost easier googling about functions in other languages than it is Ruby for some reason.

This post has been edited by NotarySojac: 29 January 2013 - 03:27 PM

Was This Post Helpful? 0
  • +
  • -

#8 loopConfused  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 10
  • Joined: 18-January 13

Re: Rock, paper, sicssiors game assistance

Posted 29 January 2013 - 04:08 PM

Here is the new code that I've been working on.
puts "Would you like to play a Game?"
  puts "Yes/No"
  puts "If you want to quit the game at anytime just type quit."
    player_input = gets.chomp.downcase
    #
     while true #loops forever

      if player_input == 'yes'
        puts "Welcome to Rock, Paper, Scissors!"
        puts "Press r for Rock"
        puts "Press s for Scissors"
        puts "Press p for Paper"
        computer = "rsp"[rand(3)].chr
        player = gets.chomp.downcase
        case [player, computer]
          when ['p','r'], ['s','p'], ['r','s']
            puts "You Win!";
          when ['r','r'], ['s','s'], ['p','p']
            puts "You Tied!";
          else
            puts "You Lose!";
        end
        puts "The computer chose: #{computer.upcase}"
      else player_input == "no"
      puts "You no come play. :(/>"
        exit
      end
      break if player_input = "quit"
     end

The problem I'm having is make the loop exit. I have tried
[code}break if user_input == quit
#and
while user_input != "quit"
#and
[/code]
Was This Post Helpful? 0
  • +
  • -

#9 loopConfused  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 10
  • Joined: 18-January 13

Re: Rock, paper, sicssiors game assistance

Posted 29 January 2013 - 04:13 PM

update found my error.
Was This Post Helpful? 0
  • +
  • -

#10 NotarySojac  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 53
  • View blog
  • Posts: 428
  • Joined: 30-September 10

Re: Rock, paper, sicssiors game assistance

Posted 29 January 2013 - 05:28 PM

View PostloopConfused, on 29 January 2013 - 04:08 PM, said:

Here is the new code that I've been working on.
puts "Would you like to play a Game?"
  puts "Yes/No"
  puts "If you want to quit the game at anytime just type quit."
    player_input = gets.chomp.downcase
    #
     while true #loops forever

      if player_input == 'yes'
        puts "Welcome to Rock, Paper, Scissors!"
        puts "Press r for Rock"
        puts "Press s for Scissors"
        puts "Press p for Paper"
        computer = "rsp"[rand(3)].chr
        player = gets.chomp.downcase
        case [player, computer]
          when ['p','r'], ['s','p'], ['r','s']
            puts "You Win!";
          when ['r','r'], ['s','s'], ['p','p']
            puts "You Tied!";
          else
            puts "You Lose!";
        end
        puts "The computer chose: #{computer.upcase}"
      else player_input == "no"
      puts "You no come play. :(/>/>"
        exit
      end
      break if player_input = "quit"
     end

The problem I'm having is make the loop exit. I have tried
[code}break if user_input == quit
#and
while user_input != "quit"
#and
[/code]


I take it you're still a little too new to start using methods?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1