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

Username:   Password: 
 RegisterRegister   
 Playing Card Engine
Index -> Programming, Turing -> Turing Submissions
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Flikerator




PostPosted: Fri Nov 11, 2005 6:45 pm   Post subject: Playing Card Engine

I was planning on making a Poker game, and decided to make this. I don't have that many rems, but its pretty easy to understand.

Anytime you want to shuffle the deck call shuffle. Anytime you want to deal cards out to the players call Handout. If you are unsure how something works just ask. Also feel free to improve it. This can be used for any standard card game. By that I mean a game that uses a regular 52 card deck. If you'd like a multiple deck game I could change the engine a bit. But really, its not that hard.

code:

var deck : array 1 .. 52 of int     %Contains all the deck in order
var deckCheck : array 1 .. 52 of boolean       %Lets program know if a card is in the "deck" or not
var PlayersHand : array 1 .. 5, 1 .. 5 of int  %Holds all the players cards 1stD = # of players. 2ndD = # of cards in hand

for i : 1 .. 52
    deck (i) := i
    deckCheck (i) := true
end for

procedure Shuffle
    var sort1, sort2, hold1, hold2 : int
    for i : 1 .. 100
        sort1 := Rand.Int (1, 52)
        sort2 := Rand.Int (1, 52)
        hold1 := deck (sort1)
        hold2 := deck (sort2)
        if deckCheck (sort1) = true and deckCheck (sort2) = true then
            deck (sort1) := hold2
            deck (sort2) := hold1
        end if
    end for
end Shuffle

procedure Handout
    var num : int
    var count : int := 0
    for i : 1 .. upper (PlayersHand)
        loop
            num := Rand.Int (1, 52)
            if deckCheck (num) = true then
                deckCheck (num) := false
                count += 1
                PlayersHand (i, count) := deck (num)
            end if
            exit when count = 5
        end loop
        count := 0
    end for
end Handout

Shuffle
Handout
Sponsor
Sponsor
Sponsor
sponsor
ZeroPaladn




PostPosted: Mon Nov 14, 2005 9:51 am   Post subject: (No subject)

very cool. I could never figure out how to use the true and false syntax in turing, i just use 1's and 0's for it Smile . Ive allways wondered how people make card games in turing, maybe this wil shed some light on it...
Mr. T




PostPosted: Mon Nov 14, 2005 2:42 pm   Post subject: Alex's Opinion

It's probably best you start getting familiar with booleans (or true or false statements, as you put it) because it is a very powerful programming tool. Think of it as an on or off switch, where true = on and false = off. If you've taken a Compsci course, you'll probably know this is quite similar to way a computer works at a machine code level. Anyway, here is an example showing the effectiveness of booleans.
code:

var switch : boolean

for i : 1 .. 10

    if i <= 5 then
        switch := true
    elsif i > 5 then
        switch := false
    end if

    if switch = true then
        put "The switch is turned on."
    elsif switch = false then
        put "The switch is turned off."
    end if

    delay (100)

end for
Flikerator




PostPosted: Mon Nov 14, 2005 4:06 pm   Post subject: (No subject)

if 1 = num then

elsif 2 = num

end if

In oder for each if statement to "pass" (run the lines of code) it must be true. Unless you have a not= or ~=, then it checks if its false.

var check : boolean := true

if check then

end if

See how you didnt have to put = or anything? Because it already has the value of true or false, and doesnt need to do a comparasin. You could put if check = true, or check = false if you want to make it easier to see, untill you get used to it.

if ~check then

That if statement will run when check is false. (~true)


Not the most simple, or easy to read tutorial on booleans, but hopefully you get the just of them
GlobeTrotter




PostPosted: Mon Nov 14, 2005 4:10 pm   Post subject: (No subject)

For the playing card engine, look into using records and fewer global variables. It make for much better formatting. For example, have a record for each card, and have a procedure shuffle that takes in an array of cards and shuffles them.
Cervantes




PostPosted: Mon Nov 14, 2005 4:19 pm   Post subject: (No subject)

A minor detail:
Flikerator wrote:

In oder for each if statement to "pass" (run the lines of code) it must be true. Unless you have a not= or ~=, then it checks if its false.

No, it still always checks for truth. You have to look at the entire condition, including any nots, as being tested for truth.
code:
if not not not true then

This is evaluated for truth. The expression (or condition)
code:
not not not true

evaluates to false, so the else block is entered, rather than the if block.
codemage




PostPosted: Mon Nov 14, 2005 11:19 pm   Post subject: (No subject)

A shortcut for booleans is that they don't even technically have to be evaluated for true in Turing (and several languages.)

ie
for var flag : boolean := true

code:
if flag = true then


is the same as

code:
if flag then


Which *I* think is a nice little shortcut. Purists will say that the previous method is better programming though.
Flikerator




PostPosted: Sun Nov 20, 2005 2:48 pm   Post subject: (No subject)

codemage wrote:
A shortcut for booleans is that they don't even technically have to be evaluated for true in Turing (and several languages.)

ie
for var flag : boolean := true

code:
if flag = true then


is the same as

code:
if flag then


Which *I* think is a nice little shortcut. Purists will say that the previous method is better programming though.


if flag then

Is the better one, seems better in my opinion. I mentioned that method above already though =P
Sponsor
Sponsor
Sponsor
sponsor
ZeroPaladn




PostPosted: Mon Nov 21, 2005 10:18 am   Post subject: (No subject)

ah, i get it now. thanks guys.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Submissions
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 9 Posts ]
Jump to:   


Style:  
Search: