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

Username:   Password: 
 RegisterRegister   
 Card Game "war"
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
zero-impact




PostPosted: Wed Apr 16, 2008 9:59 am   Post subject: Card Game "war"

Hi i am making the card game war as a side project at school.
So far i have the game shuffle the deck give half the deck to the user and half the deck to the computer. When you click on the "draw" button it draws a card for both players and finds out who won the hand.
I need help giving the cards to the player that won the hand. I have a sinking feeling that i have been going about coding this the wrong way. I have only been programming for about a month and a half. Sorry if my commenting is bad i need to work on that.
So here is my code

code:

import GUI
type card :
    record
 %acces by using var kingodspades:card
        value : int    %               kingofspades.suit:=spades
        pic : int
        number : int
    end record


var c : array 0 .. 53 of card
c (0).pic := Pic.FileNew ("0.bmp")
c (1).pic := Pic.FileNew ("1.spade.bmp")
c (2).pic := Pic.FileNew ("1.dia.bmp")
c (3).pic := Pic.FileNew ("1.club.bmp")
c (4).pic := Pic.FileNew ("1.heart.bmp")
c (5).pic := Pic.FileNew ("2.spade.bmp")
c (6).pic := Pic.FileNew ("2.dia.bmp")
c (7).pic := Pic.FileNew ("2.club.bmp")
c (8).pic := Pic.FileNew ("2.heart.bmp")
c (9).pic := Pic.FileNew ("3.spade.bmp")
c (10).pic := Pic.FileNew ("3.dia.bmp")
c (11).pic := Pic.FileNew ("3.club.bmp")
c (12).pic := Pic.FileNew ("3.heart.bmp")
c (13).pic := Pic.FileNew ("4.spade.bmp")
c (14).pic := Pic.FileNew ("4.dia.bmp")
c (15).pic := Pic.FileNew ("4.club.bmp")
c (16).pic := Pic.FileNew ("4.heart.bmp")
c (17).pic := Pic.FileNew ("5.spade.bmp")
c (18).pic := Pic.FileNew ("5.dia.bmp")
c (19).pic := Pic.FileNew ("5.club.bmp")
c (20).pic := Pic.FileNew ("5.heart.bmp")
c (21).pic := Pic.FileNew ("6.spade.bmp")
c (22).pic := Pic.FileNew ("6.dia.bmp")
c (23).pic := Pic.FileNew ("6.club.bmp")
c (24).pic := Pic.FileNew ("6.heart.bmp")
c (25).pic := Pic.FileNew ("7.spade.bmp")
c (26).pic := Pic.FileNew ("7.dia.bmp")
c (27).pic := Pic.FileNew ("7.club.bmp")
c (28).pic := Pic.FileNew ("7.heart.bmp")
c (29).pic := Pic.FileNew ("8.spade.bmp")
c (30).pic := Pic.FileNew ("8.dia.bmp")
c (31).pic := Pic.FileNew ("8.club.bmp")
c (32).pic := Pic.FileNew ("8.heart.bmp")
c (33).pic := Pic.FileNew ("9.spade.bmp")
c (34).pic := Pic.FileNew ("9.dia.bmp")
c (35).pic := Pic.FileNew ("9.club.bmp")
c (36).pic := Pic.FileNew ("9.heart.bmp")
c (37).pic := Pic.FileNew ("10.spade.bmp")
c (38).pic := Pic.FileNew ("10.dia.bmp")
c (39).pic := Pic.FileNew ("10.club.bmp")
c (40).pic := Pic.FileNew ("10.heart.bmp")
c (41).pic := Pic.FileNew ("11.spade.bmp")
c (42).pic := Pic.FileNew ("11.dia.bmp")
c (43).pic := Pic.FileNew ("11.club.bmp")
c (44).pic := Pic.FileNew ("11.heart.bmp")
c (45).pic := Pic.FileNew ("12.spade.bmp")
c (46).pic := Pic.FileNew ("12.dia.bmp")
c (47).pic := Pic.FileNew ("12.club.bmp")
c (48).pic := Pic.FileNew ("12.heart.bmp")
c (49).pic := Pic.FileNew ("13.spade.bmp")
c (50).pic := Pic.FileNew ("13.dia.bmp")
c (51).pic := Pic.FileNew ("13.club.bmp")
c (52).pic := Pic.FileNew ("13.heart.bmp")

var font1 : int
font1 := Font.New ("serif:40") %fonts
assert font1 > 0
%%%%%Game variables%%%%%%
var name : string
var size : int := 52  %size of deck to be shuffled
var deck : array 1 .. size of card %array to store card values randomized
var userDeck : array 1 .. 26 of card
var cpuDeck : array 1 .. 26 of card
var count : int := 1 %for the draw procedure and playg


%%%%%%%%%%%%%%%%%%%%%%%%%
procedure shuffle
    var temp, num : int % num and temp are used to flip the variables in the array around
    for i : 1 .. size
        deck (i).number := i %gives a value in order from 1 to size to deck
    end for
    for i : 1 .. size
        randint (num, 1, size)   %assigns  a random card number to num
        temp := deck (num).number %assigns a random number out of the deck array to temp
        deck (num) := deck (i)
        %switches the numbers
        deck (i).number := temp
    end for
    var count : int := 0
    var count4 : int := 4
    for k : 1 .. 13
        for i : 1 .. 52
            if deck (i).number <= count4 and deck (i).number > count then %This gives all the card numbers a value
                deck (i).value := k
            end if
        end for
        count += 4
        count4 += 4
    end for
    for i : 1 .. 26
        userDeck (i).number := deck (i).number   %gives the first 26 cards from the deck to the user
        userDeck (i).value := deck (i).value
    end for
    var counter : int := 27
    for i : 1 .. 26
        cpuDeck (i).number := deck (counter).number     %gives the last 26 cards to the computer
        cpuDeck (i).value := deck (counter).value
        counter += 1
    end for
end shuffle

procedure gfx
    var font1 : int
    font1 := Font.New ("serif:40")
    assert font1 > 0
    Font.Draw (name, 1, 1, font1, red)
    Pic.Draw (c (0).pic, 50, 50, picCopy) %user card deck
    Font.Draw ("CPU", 1, maxy - 50, font1, red)
    Pic.Draw (c (0).pic, 50, maxy - 150, picCopy) %pc card deck
end gfx

procedure greet
    colourback (15)
    locate (maxrow div 2, maxcol div 2 - 20)

    put "Please enter your name: " ..
    get name
    drawfillbox (0, 0, maxx, maxy, 15)

end greet

procedure WAR
    Font.Draw ("WARRRRRRRRRRRRR!!!!!", 400, maxy - 375, font1, red)
end WAR

procedure USERWIN
    Font.Draw ("YOU WIN!", 400, maxy - 375, font1, red)
end USERWIN

procedure PCWIN
    Font.Draw ("COMPUTER WINS!", 400, maxy - 375, font1, red)
end PCWIN
var deckC : int := 1  %variable for the next card in the deck

proc button
    for i : 1 .. 52 %loop checks to see what picture to draw for the card drawn from the players hand
        if userDeck (deckC).number = (i) then
            Pic.Draw (c (i).pic, 300, 250, picCopy)
        end if
        if cpuDeck (deckC).number = (i) then
            Pic.Draw (c (i).pic, 300, maxy - 350, picCopy)
        end if
        if userDeck (deckC).value > cpuDeck (deckC).value then
            USERWIN
        elsif userDeck (deckC).value < cpuDeck (deckC).value then
            PCWIN
        else
            WAR
        end if

    end for
    deckC += 1
    delay (1000)
    drawfillbox (400, maxy - 475, 8000, maxy - 100, 15) %covers up the text when someone wins
end button

proc check
    if userDeck (deckC).value > cpuDeck (deckC).value then
        USERWIN
    elsif userDeck (deckC).value < cpuDeck (deckC).value then
        PCWIN
    else
        WAR
    end if

    drawfillbox (400, maxy - 475, 8000, maxy - 100, 15) %covers up the text when someone wins
end check

%start%
setscreen ("graphics:1000;700,nobuttonbar")
drawfillbox (0, 0, maxx, maxy, 15)
shuffle
greet
gfx
var drawbutton : int := GUI.CreateButton (150, 50, 0, "Draw", button) %experimental button drawing
loop
    exit when GUI.ProcessEvent
end loop
Sponsor
Sponsor
Sponsor
sponsor
Sean




PostPosted: Wed Apr 16, 2008 12:09 pm   Post subject: Re: Card Game "war"

Well, for us to test it, you need to upload the pictures for us to use. Can't do anything to help you really if we can't see what is going on ourselves.

Once you do, I'll gladly assist you.
zero-impact




PostPosted: Wed Apr 16, 2008 3:22 pm   Post subject: Re: Card Game "war"

Woops sorry about that!
I completely forgot Shocked
Here is a zip



war.zip
 Description:

Download
 Filename:  war.zip
 Filesize:  58.16 KB
 Downloaded:  433 Time(s)

zero-impact




PostPosted: Fri Apr 18, 2008 6:41 am   Post subject: Re: Card Game "war"

did you take a look at it sean?
Sean




PostPosted: Fri Apr 18, 2008 6:59 am   Post subject: Re: Card Game "war"

Have had a slight look in business class. Had to close, since I was caught.

However, tonight I will be taking a longer look at it for you.
zero-impact




PostPosted: Sat Apr 19, 2008 7:54 am   Post subject: Re: Card Game "war"

Any luck?

I have read a bit and i think i will have to use a flexible array. Would I be correct in assuming that? And could anybody give me a bit of guidance on how to implement that if need be?
Thanks
zero-impact




PostPosted: Sat Apr 19, 2008 3:17 pm   Post subject: Re: Card Game "war"

How can i have 139 views and no meaningful replys?
syntax_error




PostPosted: Sat Apr 19, 2008 3:50 pm   Post subject: RE:Card Game "war"

double posting doesn't make it more attractive for one to help out; more so heres a thought.

after you deal out the hand make sure you remove those cards from the array and have to cards transfered to the users hand array that way, the same cards are not in use, and after wards if you wish to expand you can have each users card collection to go against the other users hand?

I do not know if that will fit or work with your code, for I have not looked, nonetheless, that is how it should be done, imo.
Sponsor
Sponsor
Sponsor
sponsor
zero-impact




PostPosted: Sat Apr 19, 2008 4:16 pm   Post subject: Re: Card Game "war"

Thanks but i understand what i need to do i just don't know exactly how to do it in turing. I know when the player wins I have to take both cards remove them from one deck and then add them to the other. The user and computer decks are both set to 26 so i don't know how I'm going to go about adding and removing cards? :S

And well sorry about the double posting just I posted a few days ago and it is frustrating seeing my post go straight to the bottom while others with less meaningful posts are getting helped out.
Nick




PostPosted: Sat Apr 19, 2008 4:19 pm   Post subject: RE:Card Game "war"

flexible arrays, the Turing Walkthrough
zero-impact




PostPosted: Sat Apr 19, 2008 5:07 pm   Post subject: Re: Card Game "war"

Thanks but i already read through that a couple times but still don't understand how i could use it in this particular instance.
Could you maybe explain it in words or a little pseudo code? If so it would be greatly appreciated.
Thanks
Nick




PostPosted: Sat Apr 19, 2008 5:40 pm   Post subject: RE:Card Game "war"

code:
var hand:flexible array 1..0 of card
if card recieved then
    new hand, upper(hand) + 1 %upper returns the upper bound of the array
    do all needed when a new card is received
end if
zero-impact




PostPosted: Sat Apr 19, 2008 7:45 pm   Post subject: Re: Card Game "war"

Wow that actually helps a lot. Thanks ille post back if i get it working. Very Happy
zero-impact




PostPosted: Mon Apr 21, 2008 5:34 am   Post subject: Re: Card Game "war"

Got that part working! Thanks nick!
Just a few more little basic things and I will be done.
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 1  [ 14 Posts ]
Jump to:   


Style:  
Search: