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.
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
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
}
}
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.
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[]
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
}
}