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

Username:   Password: 
 RegisterRegister   
 need Help as much as possible (arrays, random int)
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
surfed




PostPosted: Tue May 31, 2005 5:16 pm   Post subject: need Help as much as possible (arrays, random int)

Heres the code:

code:
% EASY %
const ROW1P := 230    %Row 1 card position
const ROW2P := 170    %Row 2 card position
const ROW3P := 110    %Row 3 card position

body proc startEasy
    var clr : array 1 .. 12 of int   % stores card colours

    cls

    % Row 1 cards (8 cards)
    for i : 0 .. 360 by 45
        Draw.FillBox (140 + i, ROW1P, 175 + i, ROW1P - 50, gray)
        Draw.Box (140 + i, ROW1P, 175 + i, ROW1P - 50, 7)
    end for

    % Row 2 cards (8 cards)
    for i : 0 .. 360 by 45
        Draw.FillBox (140 + i, ROW2P, 175 + i, ROW2P - 50, gray)
        Draw.Box (140 + i, ROW2P, 175 + i, ROW2P - 50, 7)
    end for

    % Row 3 cards (8 cards)
    for i : 0 .. 360 by 45
        Draw.FillBox (140 + i, ROW3P, 175 + i, ROW3P - 50, gray)
        Draw.Box (140 + i, ROW3P, 175 + i, ROW3P - 50, 7)
    end for

    % assigning the 12 clr variables to random colours to assign to cards
    for x : 1 .. 12
        clr (x) := Rand.Int (8, 14)   
    end for

end startEasy


How I want this game to work:

The point of this game is to match the cards.. When a new game starts, I want colours to be ramdomly assigned to the cards. (like different colours assigned to cards at different positions, not always the same card each time.)

I have 24 cards, and when you click them, I want them to show a circle with the corresponding colour (assigned before) inside them. I want to make it so that each time the card is pressed, the same coloured circle shows (therefore assigning cards to certain colours).

How do i do this "assigning"?

help is very much appreciated, i hope this wont take a lot of time to explain to me
Sponsor
Sponsor
Sponsor
sponsor
[Gandalf]




PostPosted: Wed Jun 01, 2005 10:57 am   Post subject: (No subject)

Hmmm...

code:
var card : array 1 .. 10 of int
for i : 1 .. 10
   card(i) := Rand.Int(1,12)
end for

Will this help you in any way? Now each card is a random integer between 1 and 12.
surfed




PostPosted: Wed Jun 01, 2005 4:09 pm   Post subject: (No subject)

ok, I kinda see where ur going at, but how do I assign values to the 12 remaing cards?? So if the random ints came out like this:

card (1) := 1
card (2) := 2
card (3) := 3
card (4) := 4
card (5) := 5
card (6) := 6
card (7) := 7
card (8 ) := 8
card (9) := 9
card (10) := 10
card (11) := 11
card (12) := 12

How do I make it so that cards 13-24 have the values 1,2,3,4,5,6,7,8,9,10,11,12 in them? Cuz they have to be paired up to be matched like below: I put what needs to be done for cards 13-24 they have the values from 1-12:

card (13) := 4
card (14) := 8
card (15) := 5
card (16) := 2
card (17) := 9
card (18 ) := 10
card (19) := 3
card (20) := 11
card (21) := 7
card (22) := 1
card (23) := 12
card (24) := 6
[Gandalf]




PostPosted: Wed Jun 01, 2005 5:22 pm   Post subject: (No subject)

I think you misunderstood me
code:
var card : array 1 .. 10 of int
for i : 1 .. 10
   card(i) := Rand.Int(1,12)
end for

Will result in card 1 being a random number (1-12), and so on to 10 cards.

I think maybe my mistake confused you, the for loop should have only gone to 10.

For what you're asking, I think this would do:
code:
var cardg1, cardg2 : array 1 .. 12 of int
for i : 1 .. 12
   cardg1(i) := i
   cardg2(i) := cardg1(i)
end for

This would make cards 1-12 worth 1-12 then cards 13-24 worth 1-12.

Or if you want cards 1-24 to be worth random numbers between 1-12 then:
code:
var card : array 1 .. 24 of int
for i : 1 .. 24
   card(i) := Rand.Int(1,12)
   put card(i), "\t" ..
end for


If you don't understand anything, then just ask.[/code]
surfed




PostPosted: Wed Jun 01, 2005 11:05 pm   Post subject: (No subject)

Well first, you didnt make a mistake Surprised ur first post with the for loop went to 10 only. Im very sorry if I didnt explain it properly =[ . But what I actually wanted was to have 12 cards with random integers from 1-7 so like

code:
var card : array 1 .. 12
for i : 1 .. 12
     card (i) := Rand.Int (1, 7)
end for


then the other 12 cards would be randomly paired up with these cards (or have the same value as these cards).. so for example:
code:
card (1):=5     card (23):=5
card (2):=7     card (16):=7
card (3):=2     card (22):=2
card (4):=2     card (24):=2
card (5):=1     card (18):=3
card (6):=3     card (21):=6
card (7):=6     card (17):=6
card (8):=1     card (14):=1
card (9):=7     card (19):=7
card (10):=6     card (20):=6
card (11):=3     card (13):=3
card (12):=4     card (15):=4


so card 2, which is a 7, can be a match with card 16, 9, and 19. This makes sure that no numbers will have odd numbers. Like
code:
var card : array 1 .. 24 of int
for i : 1 .. 24
   card(i) := Rand.Int(1,12)
   put card(i), "\t" ..
end for

In this example, it is possible to get three 7's or five 4's (am i correct?) then at the end two cards will not be able to be matched.
I have one theory that might work but I don't know how to put it in code. What I'm thinking is: After the for loop giving cards 1-12 values, then it would do something like "card (1):= card (Rand.Int (13-24))", then remove that card from the random list so if "card (1) := card (16)" then the next step would be "card (2) := card (Rand.Int (13-15, 17-24))". So basically everytime a card from 13-24 is assigned a value, it would be deleted from the random list. Is that possible?[/code][/quote]
surfed




PostPosted: Thu Jun 02, 2005 11:40 am   Post subject: (No subject)

anyone else know?
McKenzie




PostPosted: Thu Jun 02, 2005 11:51 pm   Post subject: (No subject)

Standard stuff. Fill the array with two occurances of each number then shuffle them.
code:
var card:array 1..24 of int
var tmp,p1,p2:int

for i:1..12
    card(i):=i
    card(i+12):=i
end for

for i:1..24             % a bunch of random swaps == shuffle
    randint(p1,1,24)
    randint(p2,1,24)
    tmp:=card(p1)
    card(p1):=card(p2)
    card(p2):=tmp
end for

surfed




PostPosted: Fri Jun 03, 2005 11:14 pm   Post subject: (No subject)

i tried playing around with that code but I can't seem to get it to work, can you explain the second part step by step please?
Sponsor
Sponsor
Sponsor
sponsor
Cervantes




PostPosted: Sat Jun 04, 2005 6:56 am   Post subject: (No subject)

He's just picking two random cards and swapping them. He does this 24 times (as the for loop indicates).
The reason for the tmp variable is he cannot simply do:
code:

card (p1) := card (p2)
card (p2) := card (p1)

This would result in both elements being equal to the original card (p2). The temporary value is needed to store the value of card (p1) so that, after card (p1) has become card (p2), card (p2) can become the original value of card (p1).
surfed




PostPosted: Sun Jun 05, 2005 9:50 am   Post subject: (No subject)

thanks for the help ppl Very Happy
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  [ 10 Posts ]
Jump to:   


Style:  
Search: