Computer Science Canada

using arrays for blackjack???

Author:  Tony_200004 [ Sun Jan 11, 2004 8:27 am ]
Post subject:  using arrays for blackjack???

Yeah i asked a question earlier and someone mentioned something about using arrays... i looked at the tutiorial and i still dont understand them.... can someone just make a really small example blackjack program so i can see whats going on... i am more of a hands on person need to see it.. cant just read it... thanks

Author:  Tony_200004 [ Sun Jan 11, 2004 8:44 am ]
Post subject: 

or a small game which randomizes numbers and outputs them just something simple that can show me how to do it

Author:  Thuged_Out_G [ Sun Jan 11, 2004 3:12 pm ]
Post subject: 

simple program to store an array with random numbers and output each number

code:

var num:int
var numbers:array 1..25 of int

for i:1..25 %a for loop to run 25 times
num:=Rand.Int(1,500) %gives num a random value between 1 and 500
numbers(i):=num %stores the random value of num into the ith element of the array
end for

for i:1..25
put numbers(i)
end for
[/code]

Author:  Tony_200004 [ Sun Jan 11, 2004 5:54 pm ]
Post subject: 

yeah i took that code you gave me and did this... want to know if it is any good for a first try and if it is alright

code:

var num : int
var numbers : array 1 .. 10 of int
var fcard : int := 0
var ans : string (1)
loop
    put "You have, ", fcard, "."
    put "Want to hit(y or n)? "
    getch (ans) %gets the users answer
    if ans = "n" then %if the answer is no then it completes this task
        put "You have, ", fcard, "."
        put "Press any key to continue... " ..
        getch (ans)
        delay (200)
        cls %clears the screen
    else %if none of the "if" or "elsif" statements happen this happens
        for i : 1 .. 2         %a for loop to run 2 times
            num := Rand.Int (1, 10) %gives num a random value between 1 and 10
            fcard := fcard + num
            numbers (i) := fcard %stores the random value of num into the ith element of the array
            if numbers (i) > 21 then %if you hand is over 21 it completes this task
                put "You have, ", fcard, "."
                put "You bust."
                put "Press any key to continue... " ..
                getch (ans)
                delay (200)
                cls
                fcard := 0     %resets your hand
            elsif numbers (i) = 21 then     %if you have equals 21 it completes this task
                put "You have, ", fcard, "."
                put "You have backjack."
                put "Press any key to continue... " ..
                getch (ans)
                delay (200)
                cls
                fcard := 0   %resets your hand
            end if
        end for
    end if
end loop

Author:  Tony_200004 [ Sun Jan 11, 2004 6:25 pm ]
Post subject: 

Okay well this is my latest update to that.... if anyone can help me with this please do so....

code:

var num : int
var numbers : array 1 .. 10 of int
var fcard, hitcount, scard, cloop : int := 0
var ans : string (1)

loop
    hitcount := hitcount + 1
    if cloop = 0 then %if this counter equals zero it starts this loop
        loop
            randint (scard, 2, 21)
            fcard := scard %makes fcard(finalcard) equal to scard(startcard)
            cloop := cloop + 1 %makes a counter to exit this loop when it has completed this once
            if cloop = 1 then
                exit
            end if
        end loop
        if fcard = 21 then
            hitcount := 0
            put ""
            put "You have, ", fcard, ". You have backjack."
            put "Press any key to continue... " ..
            getch (ans)
            delay (200)
            cls
            fcard := 0         %resets your hand
            hitcount := 0
            cloop := 0         %resets the starting round counter
        end if
    else
        put "You have, ", fcard, ". Want to hit(y or n)? "
        getch (ans)     %gets the users answer
        if ans = "n" then     %if the answer is no then it completes this task
            cloop := 0     %resets the starting round counter
            put ""
            put "You have, ", fcard, "."
            put "Press any key to continue... " ..
            getch (ans)
            delay (200)
            cls     %clears the screen
            fcard := 0     %resets your hand
            hitcount := 0
        else     %if none of the "if" or "elsif" statements happen this happens
            for i : 1 .. 1     %a for loop to run 2 times
                num := Rand.Int (1, 10)     %gives num a random value between 1 and 10
                fcard := fcard + num
                numbers (i) := fcard     %stores the random value of num into the ith element of the array
                if numbers (i) > 21 then     %if you hand is over 21 it completes this task
                    hitcount := 0
                    put ""
                    put "You have, ", fcard, ". You bust."
                    put "Press any key to continue... " ..
                    getch (ans)
                    delay (200)
                    cls
                    cloop := 0     %resets the starting round counter
                    fcard := 0     %resets your hand
                    hitcount := 0
                elsif numbers (i) = 21 then     %if you have equals 21 it completes this task
                    hitcount := 0
                    put ""
                    put "You have, ", fcard, ". You have backjack."
                    put "Press any key to continue... " ..
                    getch (ans)
                    delay (200)
                    cls
                    fcard := 0     %resets your hand
                    hitcount := 0
                    cloop := 0     %resets the starting round counter
                elsif hitcount = 5 then
                    put ""
                    put "You have 5 cards without exceeding 21! You win."
                    put "Press any key to continue... " ..
                    getch (ans)
                    delay (200)
                    cls
                    cloop := 0     %resets the starting round counter
                    fcard := 0
                    hitcount := 0
                end if
            end for
        end if
    end if
end loop

Author:  Grey_Wolf [ Sun Jan 11, 2004 6:37 pm ]
Post subject: 

no suggestion about the code but i would suggest you leave out comments like for

for i:1..2 %a for loop to run 2 times

it clutters things up and insults the intellegence of the person reading it.

Author:  Tony_200004 [ Sun Jan 11, 2004 6:44 pm ]
Post subject: 

yeah it does clutter it up, but it makes it easier if i need help anytime, easier for someone to understand my code, and easier to find something that i cant remember what it is called.

Author:  Tony_200004 [ Sun Jan 11, 2004 6:45 pm ]
Post subject: 

also it is a habbit my computer science teacher has gotten me use to

Author:  poly [ Sun Jan 11, 2004 6:52 pm ]
Post subject: 

Grey_Wolf wrote:
no suggestion about the code but i would suggest you leave out comments like for

for i:1..2 %a for loop to run 2 times

it clutters things up and insults the intellegence of the person reading it.

it doesnt really insult the intillegent's of the person reading it, because if your just reading the code you might not realize exactly why thats there, so the comments do help both the coder and reader understand the code.

Author:  Poland13 [ Sun Jan 11, 2004 7:28 pm ]
Post subject: 

IM doing blackjack to

var deck : array 1 .. 52 of string
var cardz : array 1 .. 13 of string := init ("2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K", "A")
var suit : array 1 .. 4 of string := init ("C", "D", "S", "H")
card := 1


function DECK


for x : 1 .. 4
for y : 1 .. 13
deck (card) := cardz (y) + suit (x)
card := card + 1
end for
end for


for i : 1 .. 52
randint (a, 1, 52)
randint (b, 1, 52)
temp := deck (a)
deck (a) := deck (b)
deck (b) := temp
end for

result deck

end DECK

can neone tell me y this doesnt work

Author:  dude3000 [ Sun Jan 11, 2004 10:42 pm ]
Post subject: 

Poland13 wrote:
IM doing blackjack to

var deck : array 1 .. 52 of string
var cardz : array 1 .. 13 of string := init ("2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K", "A")
var suit : array 1 .. 4 of string := init ("C", "D", "S", "H")
card := 1


function DECK


for x : 1 .. 4
for y : 1 .. 13
deck (card) := cardz (y) + suit (x)
card := card + 1
end for
end for


for i : 1 .. 52
randint (a, 1, 52)
randint (b, 1, 52)
temp := deck (a)
deck (a) := deck (b)
deck (b) := temp
end for

result deck

end DECK

can neone tell me y this doesnt work


This doesn't work because you need to declare temp, a and b

Author:  McKenzie [ Sun Jan 11, 2004 10:58 pm ]
Post subject: 

First. Program is looking good
Second. Wolf is right. obvious comments are harmfull to clarity.
Third. It's only called a blackjack if you get it on your fist two cards

Author:  McKenzie [ Sun Jan 11, 2004 11:10 pm ]
Post subject: 

Poland,
I like the way you shuffle, but if you think about it there is no reason to stop at 52. Run it up to 100 or so to be sure they are well shuffled. Aside from the a,b problem that dude correctly pointed out your big problem is trying to result an an array of string from your function. Make it a procedure and pass your array as a parameter like:
code:
var deck : array 1 .. 52 of string
var cardz : array 1 .. 13 of string := init ("2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K", "A")
var suit : array 1 .. 4 of string := init ("C", "D", "S", "H")

proc buildDeck(var deck : array 1 .. 52 of string)
var card := 1
var a,b:int
    for x : 1 .. 4
        for y : 1 .. 13
            deck (card) := cardz (y) + suit (x)
            card := card + 1
        end for
    end for

    for i : 1 .. 52
        randint (a, 1, 52)
        randint (b, 1, 52)
        temp := deck (a)
        deck (a) := deck (b)
        deck (b) := temp
    end for
end buildDeck

oh I changed the name because it is confusing to have two very different things in your program so similarly named and you should only use ALL CAPS for constants.

Author:  Poland13 [ Mon Jan 12, 2004 4:35 pm ]
Post subject: 

thanks but i figured all of that out now all i have left to do is assign values to the cards

Author:  Poland13 [ Mon Jan 12, 2004 4:49 pm ]
Post subject: 

i used a function instead of a procedure thoguh

function DECK : array 1 .. 52 of string

var deck : array 1 .. 52 of string
var cardz : array 1 .. 13 of string := init ("2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K", "A")
var suit : array 1 .. 4 of string := init ("C", "D", "S", "H")

for x : 1 .. 4
for y : 1 .. 13
deck (card) := cardz (y) + suit (x)
card := card + 1
end for
end for


for i : 1 .. 52
randint (a, 1, 52)
randint (b, 1, 52)
temp := deck (a)
deck (a) := deck (b)
deck (b) := temp
end for

result deck

end DECK

Author:  Poland13 [ Mon Jan 12, 2004 5:31 pm ]
Post subject: 

well i have most of my program figured out but i was having some trouble with assigning values to the cards

the only error is substring index is greater then the length of the string i think this means cards is greater then deck but how would i fix this

var name, temp, option : string
var bet : int := 100
var card, a, b : int



card := 1


function DECK : array 1 .. 52 of string

var deck : array 1 .. 52 of string
var cardz : array 1 .. 13 of string := init ("2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K", "A")
var suit : array 1 .. 4 of string := init ("C", "D", "S", "H")

for x : 1 .. 4
for y : 1 .. 13
deck (card) := cardz (y) + suit (x)
card := card + 1
end for
end for


for i : 1 .. 52
randint (a, 1, 52)
randint (b, 1, 52)
temp := deck (a)
deck (a) := deck (b)
deck (b) := temp
end for

result deck

end DECK

function Getvalue (deck: string) : int
var value : int:=0
if index (deck(card)(1),"T,J,Q,K,") >0 then
value := 10
end if


result value
end Getvalue

View.Set ("nocursor")
var count : int := 1
var deck : array 1 .. 52 of string
var value : int := 1
var points : int
put " Enter your name "
get name : *
put name, " You have 100$ to start with what is your bet "
get bet
put " Your bet is ", bet, " dollars Good Luck !!!!! "
deck := DECK
loop
put "Would you like to Hit or Stand (you must hit on your first turn) "
get option : *
put "Your card is ", deck (count)
count := count + 1
points := Getvalue (deck (count))
put "ur total", points
exit when option = "stand"
end loop


: