Cutting a deck of Cards

Writing a program to play a game of War, and cutting a deck is one of the functions. I wrote the function. I was hoping someone could look it over for me and see if I am syntactically correct. One other thing I was unsure how to do is once the deck is in two halve the upper half must go to the bottom of the deck.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void Deck::cutDeck(Deck)
{
     int cutDeck = 0;

     for (int i = 0; i < 52; i++)
{
     Deck [i] = i;
}
     int cut = 0;
     int cutNumber = rand() %52;

     for (int j = 0; j < 52; j++)
{
     cutDeck[j] = Deck[(cutNumber ++)%52]
}
     for (cut = 0; cut <52; cut++)
{
     cout cutDeck[cut];
}

return;
}


Thank you very much for your help.
Last edited on
i can't really tell .. but one way to test, is to print the deck as is, then reprint the deck after the cut.. then check your output .. you should see the same series of numbers somewhere in the list where it was cut...

say your deck started with As Ks Qs Js

look for that combo at say the card 26 mark, (half cut) ... then follow it through
Please indent your code
if I am syntactically correct
That's the compiler job.
the other thing i noticed, Deck isn't a variable its a class, thus you would have to define a class Deck to be used ...
or just leave it blank if you ant to operate on the same deck where you call x.cutDeck();
for instance

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void Deck::cutDeck(){
     
     Deck temp(); //new deck
     temp = this; //copy the values (need to overload assignment operator)

     int cutDeck = 0;
     cutNumber = rand() %52;
     
    for(int i = cutNumber; i < 52; i++){
     this->deck[i] = temp.deck[cutNumber];  //puts the values from the middle of the deck to the top
    }

   for(int j = 0; j < cutNumber; j++){
    this->deck[cutNumber] = temp.deck[j]; //puts the values of the original top of the deck to the middle
   }     

}
Last edited on
Thanks for the help. We havent used pointers yet in my class. What would I use instead of the "this" pointer? Im sure we'll get to them soon enough. Also, we do have a class Deck for this project... so the whole program is not written yet. At this point we are just writing the functions.
Last edited on
um, how do you execute the function to cut the deck?

is this a private or public function?

how are the cards stored?
you can actually still use mine, you would just have to do one extra step...

like so

the this pointer refers to the pointer of the object you are sending.. for instance
if you create a deck called x, you are refering to the stuff in x when you use a this pointer

much like doing x.deck[] , you simply do this->deck[] also you don't even need to do them, it just helps with clarity (to me anyways) as to which class you are referring, so this->deck[] is the same as doing deck[]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void Deck::cutDeck(){
     
     Deck temp(); //new deck
     for(int i = 0; i < 52; i++){
     temp.deck[i] = deck[i];
    }

     int cutDeck = 0;
     cutNumber = rand() %52;
     
    for(int i = cutNumber; i < 52; i++){
     deck[i] = temp.deck[cutNumber];  //puts the values from the middle of the deck to the top
    }

   for(int j = 0; j < cutNumber; j++){
     deck[cutNumber] = temp.deck[j]; //puts the values of the original top of the deck to the middle
   }     
   
}

Ok, I see now... pointers would be the better way to go, but being as the prof hasnt taught that yet, I would be hesitant to use it.

Thank you very much for your help.

Chris
Topic archived. No new replies allowed.