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

Username:   Password: 
 RegisterRegister   
 using arrays for blackjack???
Index -> Programming, Turing -> Turing Help
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Tony_200004




PostPosted: 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
Sponsor
Sponsor
Sponsor
sponsor
Tony_200004




PostPosted: Sun Jan 11, 2004 8:44 am   Post subject: (No subject)

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




PostPosted: Sun Jan 11, 2004 3:12 pm   Post subject: (No 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]
Tony_200004




PostPosted: Sun Jan 11, 2004 5:54 pm   Post subject: (No 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
Tony_200004




PostPosted: Sun Jan 11, 2004 6:25 pm   Post subject: (No 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
Grey_Wolf




PostPosted: Sun Jan 11, 2004 6:37 pm   Post subject: (No 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.
Tony_200004




PostPosted: Sun Jan 11, 2004 6:44 pm   Post subject: (No 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.
Tony_200004




PostPosted: Sun Jan 11, 2004 6:45 pm   Post subject: (No subject)

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




PostPosted: Sun Jan 11, 2004 6:52 pm   Post subject: (No 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.
Poland13




PostPosted: Sun Jan 11, 2004 7:28 pm   Post subject: (No 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
dude3000




PostPosted: Sun Jan 11, 2004 10:42 pm   Post subject: (No 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
McKenzie




PostPosted: Sun Jan 11, 2004 10:58 pm   Post subject: (No 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
McKenzie




PostPosted: Sun Jan 11, 2004 11:10 pm   Post subject: (No 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.
Poland13




PostPosted: Mon Jan 12, 2004 4:35 pm   Post subject: (No subject)

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




PostPosted: Mon Jan 12, 2004 4:49 pm   Post subject: (No 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
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 2  [ 16 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: