New to ruby questions

J

Jeremy Woertink

I am still new to ruby and programming itself. This is my first
programming language. My task that was given to me for practice was to
recreate the game BlackJack.

I have all my classes and all my methods defined, now I just want to
make the actual game. I am not doing any sort of GUI. Here is my code.

puts 'Welcome to the crazy world of Black Jack.'
puts "To start a new game type 'deal'."
puts "To quit the game type 'quit'."

game = Game.new

game.get_player_command

while true
puts 'Your hand is: '
@player.player_hand
puts 'The dealer hand is: '
@dealer.dealer_hand
puts "Do you want to 'hit' or 'stay'?"
@game.get_player_command
if @player.bust?
puts 'You lose...'
@game.game_over
else
@dealer.dealer_hit
if @dealer.bust?
puts 'You Win!'
@game.game_over
end
end
end


The problem I am running into is I get the error " uninitialized
constant Game (Name Error). How do I tell ruby to make a new game class
to start the game.
the whole code is here http://rafb.net/paste/results/3kQ9pr90.html
Also, if anyone can help me to understand how to end the game or any
other things you might find that can help I would greatly appreciate it.
 
T

Tim Hunter

Jeremy said:
The problem I am running into is I get the error " uninitialized
constant Game (Name Error). How do I tell ruby to make a new game class
to start the game.
the whole code is here http://rafb.net/paste/results/3kQ9pr90.html
Also, if anyone can help me to understand how to end the game or any
other things you might find that can help I would greatly appreciate it.

Welcome to Ruby, Jeremy!

Ruby programs are executed from the top down. You should move all your
class definitions to the top of your program file so that Ruby will read
their definitions before it reads your code that actually uses them.
 
M

matt neuburg

Jeremy Woertink said:
game = Game.new

game.get_player_command

while true
puts 'Your hand is: '
@player.player_hand
puts 'The dealer hand is: '
@dealer.dealer_hand
puts "Do you want to 'hit' or 'stay'?"
@game.get_player_command
if @player.bust?
puts 'You lose...'
@game.game_over
else
@dealer.dealer_hit
if @dealer.bust?
puts 'You Win!'
@game.game_over
end
end
end


The problem I am running into is I get the error " uninitialized
constant Game (Name Error). How do I tell ruby to make a new game class
to start the game.
the whole code is here http://rafb.net/paste/results/3kQ9pr90.html

Order is kinda crucial. Declaration of class Game must precede its use.
m.
 
A

Amos King

Start by placing your class defs at the top of the file. There are
many other problems you need to work out after that.
 
B

Bira

The problem I am running into is I get the error " uninitialized
constant Game (Name Error). How do I tell ruby to make a new game class
to start the game.

Like this:

class Game
def initialize(...) #parameters go here
#initialization code goes here
end

#other methods go here
end

The "initialize" method defines what happens when you create a new
instance of the class ("Game.new"), and it should take the same
parameters you intend to pass to "new". You should also define any
methods you wish to call later on, such as "get_player_command" and
"game_over".
 
S

Santiago R. Lunar M.

Tim Hunter escribió:
Welcome to Ruby, Jeremy!

Ruby programs are executed from the top down. You should move all your
class definitions to the top of your program file so that Ruby will
read their definitions before it reads your code that actually uses them.
Welcome Jeremy,

Despite I'm new to Ruby too, i can see that your problem is your
programming algorithm. I was looking at [0], and i can recommend you,
first of all, to include all your classes definitions so you can call
them to work later. That's where: "uninitialized constant Game (Name
Error)" comes from, you're invoking a class that you haven't created yet.

[0] = http://rafb.net/paste/results/3kQ9pr90.html
 
J

Jeff Cohen

I have all my classes and all my methods defined, now I just want to
make the actual game. I am not doing any sort of GUI. Here is my code.

Welcome to Ruby, Jeremy!

First, Ruby interprets code as it goes, so you need to put your class
definitions higher than the code that tries to reference them. So move
all your class definitions to the top.

Secondly, just use 'exit' if you need to stop the script prematurely. A
better way is for your big while loop to use a variable to tell it when
to stop looping, and set this variable to true after you call game_over.

Does this help? Feel free to ask more questions if something's unclear.

Jeff
softiesonrails.com
 
J

Jeremy Woertink

Jeff said:
Welcome to Ruby, Jeremy!

First, Ruby interprets code as it goes, so you need to put your class
definitions higher than the code that tries to reference them. So move
all your class definitions to the top.

Secondly, just use 'exit' if you need to stop the script prematurely. A
better way is for your big while loop to use a variable to tell it when
to stop looping, and set this variable to true after you call game_over.

Does this help? Feel free to ask more questions if something's unclear.

Jeff
softiesonrails.com

Thanks man. It does help. I will be asking plenty of other questions,
but for now, back to my code.
 
J

Jeremy Woertink

I do have another question. I want to do something to set the point
values of the cards, I'm not quite sure how to go about doing so.
I did come up with
def points
if @value == 'Jack' then 'Jack' = 10,
@value == 'Queen' then 'Queen' = 10,
@value == 'King' then 'King' = 10,
@value == 'Ace' then 'Ace' = 11 unless @player.player_hand > 10
end
end
but, this code doesn't work. I was thinking it would work similar to
this. If you or anyone else has any ideas please let me know. thanks.


again, the full code is http://rafb.net/paste/results/3kQ9pr90.html
 
B

Bira

I do have another question. I want to do something to set the point
values of the cards, I'm not quite sure how to go about doing so.
I did come up with
def points
if @value == 'Jack' then 'Jack' = 10,
@value == 'Queen' then 'Queen' = 10,
@value == 'King' then 'King' = 10,
@value == 'Ace' then 'Ace' = 11 unless @player.player_hand > 10
end
end
but, this code doesn't work. I was thinking it would work similar to
this. If you or anyone else has any ideas please let me know. thanks.

Looking just at this snippet, the problem I see is that the names of
the cards and their point values are both literals. 'Jack' = 10 is not
valid syntax because you can't assign a literal to another. Also, the
"if" syntax isn't entirely correct.

There are several ways you could go about this. You could, for
example, use constants:

JACK = 10
QUEEN = 10

Or you could create a "Card" class that has both name and value
attributes, and do something like this (the @card variable would hold
a Card object):

if @card.name == 'Jack' || @card.name =='Queen' || @card.name == 'King'
@card.value = 10
elsif @card.name == 'Ace'
...
end

You could also keep a player's total number of points in a separate
variable, and just add to it depending on the card's name.

if @value == 'Jack' || @value =='Queen' || @value == 'King'
@point_total += 10
elsif @card.name == 'Ace'
...
end

This last one is probably the simplest approach.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,281
Latest member
Pedroaciny

Latest Threads

Top