Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 BlackJack help - Variable has no value
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
JamesV




PostPosted: Sat Jan 08, 2011 10:16 am   Post subject: BlackJack help - Variable has no value

What is it you are trying to achieve?
I am trying to create a simple game of blackjack

What is the problem you are having?
When I add more then 3 cards (Hit more then 1) to the users hand (which is an array), I am not able to calculate the total. I get variable has no value.

Describe what you have tried to solve this problem
Ive tried to move code out of the procedures, but I end up messing it up more because its easier to call code from a procedure when im using a GUI.CreateButtonFull. Ive also tried to determine if there is a problem adding more then 3 cards.

FYI:
When I call cards from the txt file, I simply have them in this format
1H
2H
3H
1D
2D
3D

1 = A
11 = J
12 = Q
13 = K

Turing:


import GUI

var deck : array 1 .. 53 of string % Creates a deck of 52 - First value is a comment and will be ignored later on
var getcards : int % Get the card file
var count : int := 0 % Count for card file
var random_cards : flexible array 1 .. 4 of int % Make array for 4 random cards
var your_cards : flexible array 1 .. 2 of string % Make array for 2 cards for you
var dealer_cards : flexible array 1 .. 2 of string % Make array for 2 cards for dealer
var number_of_your_cards : int := 2 % Keep track of the number of cards in your hand
var total : int := 0 % Keep track of the total of all your
var num : int := 0 % Dummy values

procedure your_total
    for a : 1 .. number_of_your_cards
        if length (your_cards (a)) = 2 then % Checks to see if the number is less then 10, by determining the number of spaces in the card name
            num := strint (your_cards (a) (1)) % Gives a the value of the card to num (First character in the string), by changing it from an string, into an int.
            total := total + num % Calculates your total
        elsif length (your_cards (a)) = 3 then
            total += 10 % All of the cards that have are 3 charaters long are worth 10
        end if
    end for
    put "Your Total is: ", total
    total := 0
end your_total

procedure hit
    number_of_your_cards += 1
    new random_cards, upper (random_cards) + 1 % Expands the array for a random card
    new your_cards, upper (your_cards) + 1 % Expands the number of cards in your hand
    random_cards (5) := Rand.Int (2, 53) % Picks a random card
    your_cards (3) := deck (random_cards (5)) % Assigns that card to your hand
    put your_cards (3) : 5 ..
    your_total
end hit

open : getcards, "cards.txt", get % Open list of cards

loop % Assigns Card values into the array
    count += 1
    exit when eof (getcards)
    get : getcards, deck (count) : *
end loop

close : getcards

for a : 1 .. 4 % Assign Random Cards
    random_cards (a) := Rand.Int (2, 53)
end for

your_cards (1) := deck (random_cards (1)) % Assign Two of the random cards to you
your_cards (2) := deck (random_cards (2))

dealer_cards (1) := deck (random_cards (3)) % Assign Two of the random cards to the dealer
dealer_cards (2) := deck (random_cards (4))

put "Your Cards" : 15, your_cards (1) : 5, your_cards (2) : 5
put "Dealers Cards" : 15, dealer_cards (1) : 5, "--"

your_total % Checks your total of your cards

var hit_button : int := GUI.CreateButtonFull (50, 50, 0, "Hit", hit,0, '^D', true) % Make a button that says hit, and run procedure called hit

loop
    exit when GUI.ProcessEvent
end loop




Please specify what version of Turing you are using
Turing 4.1.1
Sponsor
Sponsor
Sponsor
sponsor
Insectoid




PostPosted: Sat Jan 08, 2011 11:23 am   Post subject: RE:BlackJack help - Variable has no value

Why is your_cards an array of strings? That's just making more work for yourself.
TokenHerbz




PostPosted: Sat Jan 08, 2011 11:40 am   Post subject: RE:BlackJack help - Variable has no value

your proc hit seems wrong, (From what i can see of it) and it could only be working for 2 cards because your specifically give those 2 cards the values which they don't get (or the 3rd card) from the proc hit. have you tried to manual enter the 3rd card in the way you did 1,2 under that proc?

what happens if you try that?
JamesV




PostPosted: Sat Jan 08, 2011 1:08 pm   Post subject: Re: BlackJack help - Variable has no value

Ok, thanks guys. I ended up switching the procedure hit too:

procedure hit
hitcount += 2
numberofcards += 1
your_cards (numberofcards) := deck (random_cards (hitcount))
put your_cards (numberofcards)
your_total
end hit

And it fixed the problem. Thanks for your help Smile
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 4 Posts ]
Jump to:   


Style:  
Search: