Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

So er im trying to learn how to program

for quite a while, on and off, and it's not going so well.

Anyways, I'm trying to learn ruby, currently using some really new version of it. my editor is scite, cause i really like how it looks.
and anyways, I just have a bunch of really small problems that I figured i can just post up here. I'm quite sure they are amazingly noob, and googling it is hard because it's like something i feel everyone should already know so...
whatev.

anyways, when using "x .. y", with x and y as two different numbers, it means the numbers between x and y, right? just checking

anyways, im trying to develop a small game where a random number is generated from 0 to 100, and you have seven chances to get the correct number.
im messing up somewhere. it repeats at the first while control part, and the error the scite is saying is that there's a "warning: integer literal in conditional range"

anyways can anyone check it for me? probably be posting my other futile attempts later as well

Code:
number = rand(101)

x = 0

count = 0

 

 

puts "Input a number from 0 to 100."

puts "You have seven chances."

 x = gets.chomp

while x != 1 .. 100

    puts "Please input a number from 0 to 100."

    x = gets.chomp

      if x == 1 .. 100

      break

      end

end

 

 

end

 

 

while count < 7

  if x >= number

    puts x.to_s + " is too high!"

    count = count + 1

    if count == 6

      puts "You have 1 chance left."

      else puts "You have " + count.to_s + " chances left."

    end

  elsif x <= number

    puts x.to_s + " is too end!"

    count = count + 1

    if count == 6

      puts "You have 1 chance left."

      else puts "You have " + count.to_s + " chances left."

    end

  elsif x == number

     puts "Correct! You have the right number!"

    break

  end

     x = gets.chomp

end

 

 if count == 7

    puts "You've lost."

    x = 0

    count = 0

    number = rand(101)

end

 

 
 
x..y are ranges and cannot be used in if statements. What you want to write is:
if x >= 1 and x <= 100
(or < and > for exclusive).

Also, slightly unrelated, but get into the habit of good formatting and indentation. It really helps with proofreading and checking for errors.
 

Zeriab

Sponsor

x..y is syntactic sugar for Range.new(x, y)

Here is a little code snippet to help you understand:
[rgss]p (1..5) == Range.new(1, 5)
p (1...5) == Range.new(1, 4)
p (1...5) == Range.new(1, 5, true)
[/rgss]

You can use for x in range because it has an .each method which yields all the members. It also implements the Enumerable module.

About your code let me first tell you that gets.chomp returns a string so you'd want to use the string.to_i method to parse an integer from it.
Secondly you are checking if x is equal to a range, which it is not and never will be. You'd want to use Range's === for that.

Let me suggest using this method for retrieving a number in the range you want.
[rgss]def gets_number(range = 0..100)
  loop do
    x = gets.chomp
    return :cancel if x.empty?
    num = x.to_i
    return num if range === num
  end
end
[/rgss]

This returns the :cancel symbol when an empty string is entered and otherwise loops until a number in the given range is entered.
Remember to handle this canceling.
Mind that you must put this before any use of gets_number.

I hope this will help you on your way :cute:

*hugs*
 
so er tried doing a lot of things the last hour or so
there's less functionality, but i'm pretty sure im getting close. just gotta solve a couple of things.
i put the things in methods and stuff to make it cleaner, while adding a lot of comments to keep track of things and give myself a delusionary sense of professionalism
anyways, a bunch of problems, surrounding my count checking method and i think where i placed some things.

thanks for all the advice and stuff then everyone
Code:
#==== Initializing base bvariables ====#

g_number =7                    #the number of times you want to allow guessing before messing up

count_x = 0                    #the number of guesses made

number = rand(101)            #the guessing number

input_x = 0                    #the number that you input

game_end = 0                    #check to end the game. 0 = don't end, 1 = end

 

#==== Method of initializing the numbers - setting this up for a try again option ====#

def new

    number = rand(101)        #randomizing the number for the next game. between 0 and 100 with (101)

    count_x = 0            

end

 

#==== Method to check if the inputted is a correct integer - thx Zeriab, although im not sure if i used it correctly ====#

def gets_number(range = 0..100)

  loop do

    input_x = gets.chomp

    return :cancel if input_x.empty?    

    num = input_x.to_i

    return num if range === num

  end

end

 

 

#==== Method  to check if the number of guesses is done and to ouput a statement saying whatevers====#

def count_check

    count_x = count_x++                #it sometimes states a problem right here. undefined method + for when i tired count_x = count_x + 1. o.0

    if count_x == 6

        puts "You have 1 chance left."

        else puts "You have used " + count_x + " chances out of " + g_number + "."        #problems here? cannot convert nil to string type error or something. Not seeing how there's a problem.

    end

end

 

 

#===game code===#

 

puts "Please input a number from 0 to 100."

puts "You have seven chances to get the right number."

 

count_x = 0

number = rand(101)                            #aka the new, method

 

while game_end == 0                            #Setting this up as a true/false loop, but to allow some sorta possibility of a new game thing

    if count_x == g_number

        puts "You have lost the game!"

        game_end == 1

        break

    end

    gets_number                                #inputting number, checing shit, etc. pretty sure most of the problems are right here

    while count_x != number                    #if the inputted number is not the right number then

        if input_x >= number                    #check to see if it's higher or lower, then output something saying so

            puts x.to_s + " is too high!"

            count_check                        #count check thing to add and ouput statement

        end

        if input_x <= number

            puts input_x.to_s + " is too low!"

            count_check                        #problems here too. blarch.

        end

    end

        if input_x == number

            puts "You've won!"

            game_end = 1                        # break the loop, ending the game.

        end

end
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top