Computer Science Canada

basic game

Author:  gh0stz [ Tue May 16, 2006 3:49 pm ]
Post subject:  basic game

I'm reallly new to turing and I'm trying to make a blackjack game, I have the basic idea of what I'm going to do down, I just need to know if I can make the following code shorter.



code:
y := 0
z := 0
%Dealer cards and values
proc dOne
    a := 1
    z := z + 1
    dcard (z) := a
end dOne

proc dTwo
    a := 2
    z := z + 1
    dcard (z) := a
end dTwo

proc dThr
    a := 3
    z := z + 1
    dcard (z) := a
end dThr

proc dFor
    a := 4
    z := z + 1
    dcard (z) := a
end dFor

proc dFiv
    a := 5
    z := z + 1
    dcard (z) := a
end dFiv

proc dSia
    a := 6
    z := z + 1
    dcard (z) := a
end dSia

proc dSev
    a := 7
    z := z + 1
    dcard (z) := a
end dSev

proc dEgh
    a := 8
    z := z + 1
    dcard (z) := a
end dEgh

proc dNin
    a := 9
    z := z + 1
    dcard (z) := a
end dNin

proc dTen
    a := 10
    z := z + 1
    dcard (z) := a
end dTen

proc dElv
    a := 11
    z := z + 1
    dcard (z) := a
end dElv

proc ddeal
    randint (a, 1, 11)
    if a = 1 then
        dOne
    elsif a = 2 then
        dTwo
    elsif a = 3 then
        dThr
    elsif a = 4 then
        dFor
    elsif a = 5 then
        dFiv
    elsif a = 6 then
        dSia
    elsif a = 7 then
        dSev
    elsif a = 8 then
        dEgh
    elsif a = 9 then
        dNin
    elsif a = 10 then
        dTen
    elsif a = 11 then
        dElv
    end if
end ddeal

%player cards and values

proc pOne
    x := 1
    y := y + 1
    pcard (y) := x
end pOne

proc pTwo
    x := 2
    y := y + 1
    pcard (y) := x
end pTwo

proc pThr
    x := 3
    y := y + 1
    pcard (y) := x
end pThr

proc pFor
    x := 4
    y := y + 1
    pcard (y) := x
end pFor

proc pFiv
    x := 5
    y := y + 1
    pcard (y) := x
end pFiv

proc pSix
    x := 6
    y := y + 1
    pcard (y) := x
end pSix

proc pSev
    x := 7
    y := y + 1
    pcard (y) := x
end pSev

proc pEgh
    x := 8
    y := y + 1
    pcard (y) := x
end pEgh

proc pNin
    x := 9
    y := y + 1
    pcard (y) := x
end pNin

proc pTen
    x := 10
    y := y + 1
    pcard (y) := x
end pTen

proc pElv
    x := 11
    y := y + 1
    pcard (y) := x
end pElv


proc pdeal
    randint (x, 1, 11)
    if x = 1 then
        pOne
    elsif x = 2 then
        pTwo
    elsif x = 3 then
        pThr
    elsif x = 4 then
        pFor
    elsif x = 5 then
        pFiv
    elsif x = 6 then
        pSix
    elsif x = 7 then
        pSev
    elsif x = 8 then
        pEgh
    elsif x = 9 then
        pNin
    elsif x = 10 then
        pTen
    elsif x = 11 then
        pElv
    end if
end pdeal


basically, the first part is the dealer's cards, each proc represents a value of a card. After is a randint that selects a number from 1 to 11, if say 1 is selected, then it would run the dOne process, and set the value of the first dealer card to one.

Is there any way I can make this shorter?

Author:  gh0stz [ Tue May 16, 2006 3:53 pm ]
Post subject: 

i forgot to the meaning of the variables..

z and y identify which card it is that is being delt, so by default z/y = 0, then when i run the program and deal 2 cards, z/y gets added by 1, then 2. z is for dealer cards, and y is for player cards.

a and x is the value of the card. so in dOne, a := 1, so the value that assigns dcard would be 1. a is for dealer cards, and x is for player cards.

Author:  Clayton [ Tue May 16, 2006 4:10 pm ]
Post subject: 

you could do something like this
[syntax="Turing]

var cards:array 1..4 of int
var card:int
var count:=1

for x:1..4
card:=Rand.Int(1,11)
cards(x):=card
end for
[/syntax]
something as simple as that can work and in considerably less lines Smile

Author:  gh0stz [ Tue May 16, 2006 4:25 pm ]
Post subject: 

omfg that was easy.. Embarassed

heres another one of the same problem that'll make me look like more of an ass.

this code isn't done, but the basic structure is so...

code:
%start of dealing
put "You got ", pcard (1), " and ", pcard (2), ", with a total of ", pcard (1) + pcard (2)
ptotal (1) := pcard (1) + pcard (2)
put skip
put "Do you want to hit?"  %ask if user wants to keep playing
get ans
if ans = "y" then
    ptotal (2) := ptotal (1) + pcard (3)
    put "You got ", pcard (3) %another card
    put "You have a total of ", ptotal (1) + pcard (3)
    put "Do you want to hit?" %asks if user wants to hit again 2nd time
    get ans
    if ans = "y" then
        put "You got ", pcard (4)
        ptotal (3) := ptotal (2) + pcard (4)
        put "You have a total of ", ptotal (2) + pcard (4)
        put "Do you want to hit?"
        get ans % ask if user wants to hit again
        if ans = "y" then
        else
        end if

        put skip

        put "You got ", pcard (5)
        ptotal (4) := ptotal (3) + pcard (5)
        put "You have a total of ", ptotal (3) + pcard (5)
    else %if user chooses to stand 2nd time
        put "Dealer's turn."
        put skip  %dealer's turn; deals 2 cards for dealer
        dtotal (1) := dcard (1) + dcard (2)
        put "The dealer got ", dcard (1), " and ", dcard (2), ".  With a total of ", dtotal (1)
        if dcard (1) + dcard (2) >= 17 then %winning condition, and if dealer keeps going
            if ptotal (2) > dtotal (1) then %if dealer's cards are less than 17 then it keeps going
                put "You Win!"
            end if
            if ptotal (2) = dtotal (1) then
                put "Tie game"
            end if
            if ptotal (2) < dtotal (1) then
                put "You Lose!"
            end if
        else
            dtotal (2) := dtotal (1) + dcard (3)
            put "Dealer got ", dcard (3), " and now dealer has total of ", dtotal (2)
            if dtotal (2) >= 17 then
                if ptotal (2) > dtotal (2) then
                    put "You win"
                end if
                if ptotal (2) = dtotal (2) then
                    put "Tie game"
                end if
                if ptotal (2) < dtotal (2) then
                    put "You Lose"
                end if
            else
                dtotal (3) := dtotal (2) + dcard (4)
                put "Dealer got ", dcard (4), " and now dealer has total of ", dtotal (3)
                if dtotal (3) >= 17 then
                    if ptotal (2) > dtotal (3) then
                        put "You Win!"
                    end if
                    if ptotal (2) = dtotal (3) then
                        put "Tie Game!"
                    end if
                    if ptotal (2) < dtotal (3) then
                        put "You Lose!"
                    end if
                else
                    dtotal (4) := dtotal (3) + dcard (5)
                    put "Dealer got ", dcard (5), " and now the dealer has total of ", dtotal (4)
                    if dtotal (4) <= 21 and dtotal (4) > ptotal (2) then
                        put "You Lose!"
                        if dtotal (4) > 21 then
                            put "You Win!"
                        end if
                    end if
                end if
            end if
        end if
    end if
    put skip
else
    put "Dealer's turn."
    put skip
    dtotal (1) := dcard (1) + dcard (2)
    put "The dealer got ", dcard (1), " and ", dcard (2), ".  With a total of ", dtotal (1)
    if dcard (1) + dcard (2) >= 17 then
        if ptotal (1) > dtotal (1) then
            put "You Win!"
        end if
        if ptotal (1) = dtotal (1) then
            put "Tie game"
        end if
        if ptotal (1) < dtotal (1) then
            put "You Lose!"
        end if
    else
        dtotal (2) := dtotal (1) + dcard (3)
        put "Dealer got ", dcard (3), " and now dealer has total of ", dtotal (2)
        if dtotal (2) >= 17 then
            if ptotal (1) > dtotal (2) then
                put "You win"
            end if
            if ptotal (1) = dtotal (2) then
                put "Tie game"
            end if
            if ptotal (1) < dtotal (2) then
                put "You Lose"
            end if
        else
            dtotal (3) := dtotal (2) + dcard (4)
            put "Dealer got ", dcard (4), " and now dealer has total of ", dtotal (3)
            if dtotal (3) >= 17 then
                if ptotal (1) > dtotal (3) then
                    put "You Win!"
                end if
                if ptotal (1) = dtotal (3) then
                    put "Tie Game!"
                end if
                if ptotal (1) < dtotal (3) then
                    put "You Lose!"
                end if
            else
                dtotal (4) := dtotal (3) + dcard (5)
                put "Dealer got ", dcard (5), " and now the dealer has total of ", dtotal (4)
                if dtotal (4) <= 21 and dtotal (4) > ptotal (1) then
                    put "You Lose!"
                    if dtotal (4) > 21 then
                        put "You Win!"
                    end if
                end if
            end if
        end if
    end if
end if


as you can see its incrediblly long and tedious just like my last one... but hey i'm new Sad

so what i'm trying to accomplish with that is when the user says no to hitting, then it begins the dealer rounds. and in between the dealer hitting rounds, i have an if statement saying if dealer's cards total 17 or over, then the dealer can't hit anymore. If I were to finish it that way, i would have to repeat the above mentioned algorithim 5 times for every card that the player gets, and that is very long and probably repetitive. I'm pretty sure there's a way to manipulate this with for loops but I don't know how, so pleaseeee help meee

Smile

Author:  TheOneTrueGod [ Tue May 16, 2006 4:29 pm ]
Post subject: 

I think he's aiming for a style where you can't wind up with 5 "fives" in the deck...

This quesiton has been asked many times, and there is a search buggon on the top bar that will help you find help on it.

I posted an answer similar to this in a question a few posts down, but i'll sum it up here.

If you have an array of numbers from 0 to 51, each number can represent a card. 0 = two, 1 = three, 2 = four, ... , 12 = Ace
to get the value of the card, you do:
number mod 13. This will return a value between 0 and 12 (since there are twelve unique numbers in each deck).

To get the suit, you just div the number by 13. (this will get you a value between 0 and 3)
0 = Spades
1 = Hearts
etc...
or whatever you want them to be.

So basically, to get what the card "50" is:
numerical value: 50 mod 13
suit value: 50 div 13
(This will give you the numbers 11 and 3, signifying the king of whatever you chose 3 to be)
Now its easy to use the numbers 0 to 51 to represent unique cards.

I'm sorry if you didn't understand this, but if you use the Search button above, you'll be able to find some better help on it.

Author:  gh0stz [ Tue May 16, 2006 5:31 pm ]
Post subject: 

i think you misunderstood my question cus my code is so bullshit.

i'm trying to reduce the number of "if" statements used in teh code

now if you answered that in your response then i'm too bullshit to understand Razz

Author:  TheOneTrueGod [ Tue May 16, 2006 8:39 pm ]
Post subject: 

Its a concept. If you apply that concept, you won't have to have a shitload of variables like

code:

JackOfSpades
QueenOfSpades
KingOfSpades


eugh...

All you have to do is apply the concept I gave you, and you can reduce a blackjack game to possibly 100 lines ish.


: