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

Username:   Password: 
 RegisterRegister   
 First shot at classes with Turing
Index -> Programming, Turing -> Turing Submissions
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
neufelni




PostPosted: Wed Feb 14, 2007 8:24 am   Post subject: First shot at classes with Turing

So I decided the other day to start making a card game with Turing. Shortly after starting I decided to give it a try using classes. Never used classes before in Turing, but here's what I've got so far. For now it just shuffles and deals.
Turing:
% Nick Neufeld
% February 10, 2007
% Start of an object oriented card game

type Card :
    record
        suit : string
        value : string
    end record

% class for full deck of cards
class FullDeck
    import Card
    export initialize, shuffle, deal

    % function that generates the original deck of cards
    fcn initialize : array 1 .. 52 of Card
        var cards : array 1 .. 52 of Card
        var suits : array 1 .. 4 of string := init ("Hearts", "Diamonds", "Spades", "Clubs")
        var values : array 1 .. 13 of string := init ("Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King")
        var count : int := 1

        for i : 1 .. 13
            for q : 1 .. 4
                cards (count).value := values (i)
                cards (count).suit := suits (q)
                count += 1
            end for
        end for
        result cards
    end initialize

    % function that shuffles the deck of cards
    fcn shuffle (cards : array 1 .. 52 of Card) : array 1 .. 52 of Card
        var shuffled : array 1 .. 52 of Card
        var doneShuffle : array 1 .. 52 of boolean
        var randPos : int := 0
        var count : int := 1
        for i : 1 .. 52
            doneShuffle (i) := false
        end for

        loop
            randPos := Rand.Int (1, 52)
            if not doneShuffle (randPos) then
                shuffled (randPos) := cards (count)
                doneShuffle (randPos) := true
                count += 1
            end if
            exit when count = 53
        end loop
        result shuffled
    end shuffle

    % function that deals the cards to four players
    fcn deal (cards : array 1 .. 52 of Card) : array 1 .. 4, 1 .. 13 of Card
        var dealtCards : array 1 .. 4, 1 .. 13 of Card
        var count : int := 1
        for i : 1 .. 13
            for q : 1 .. 4
                dealtCards (q, i) := cards (count)
                count += 1
            end for
        end for
        result dealtCards
    end deal

end FullDeck

var deck : pointer to FullDeck
new FullDeck, deck

var allCards := deck -> shuffle (deck -> initialize)

/* for i : 1 .. 52
    put allCards (i).value, " of ", allCards (i).suit
end for */


var pcards := deck -> deal (allCards)
put ""

for i : 1 .. 4
    put "Player ", i, " cards: "
    for q : 1 .. 13
        put pcards (i, q).value, " of ", pcards (i, q).suit
    end for
    put ""
end for



Let me know how I can make improvements to this.
Sponsor
Sponsor
Sponsor
sponsor
ericfourfour




PostPosted: Wed Feb 14, 2007 5:25 pm   Post subject: RE:First shot at classes with Turing

Have you read cervantes' tutorials on object orientation? I think you are missing the point. If you noticed, you can take those methods out of the class and they will still run fine. Basically, the class is useless in this program.

I would start with a card class. This will contain all of the attributes for a card and any actions the card may need to do.

Then have a deck class. This will contain a list of cards and shuffle them.

You could also have a dealer and player class. The dealer can pop a card off the top of the deck and give it to a player. The dealer can also take a card from a player and push it onto the bottom of the deck.
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  [ 2 Posts ]
Jump to:   


Style:  
Search: