Freecell game

• 12-10-2006
SmellsLikeToast
Freecell game (moving objects between arrays)
Hello. I'm doing a group project at school where we've written a very rudimentary text-based freecell program. I've wracked my brain trying to figure out how to move a card from one column (using arrays) to the other. It's the only thing I have left to do and I was wondering if someone could give me some advice on how to accomplish this.

Thanks in advance to anyone who may be able to help.
• 12-10-2006
hardi
what do the arrays look like and how are they arranged. don't expect us to answer you if we don't know the conditions
• 12-10-2006
System_159
You'll probably want to make a function that takes two of your arrays as parameters. It looks at the top card in the first array, and copies it to be the top card in the second array, then it deletes the top card from the first array(just decrease the working size of array by one).
• 12-10-2006
SmellsLikeToast
Quote:

Originally Posted by hardi
what do the arrays look like and how are they arranged. don't expect us to answer you if we don't know the conditions

The cards are randomized and setup just as a regular freecell board. 4 columns of 7 and 4 columns of 6.

Quote:

Originally Posted by System_159
You'll probably want to make a function that takes two of your arrays as parameters. It looks at the top card in the first array, and copies it to be the top card in the second array, then it deletes the top card from the first array(just decrease the working size of array by one).

That's what I was looking at doing, but what's bugging me is that the maximum size of each column (array) is 25, but the actual occupied space is either 6 or 7 cards depending on which column you're working with.
• 12-10-2006
Salem
So if they're strings of chars say, then it's just a matter of using strncpy() to copy the 'n' tail cards which constitute a run of cards from the end of one array to the end of another array.

Draw a few pictures on paper of what "before" and "after" look like, then figure out an algorithm to move a 'run' from one column to another.
• 12-10-2006
SmellsLikeToast
Quote:

Originally Posted by Salem
So if they're strings of chars say, then it's just a matter of using strncpy() to copy the 'n' tail cards which constitute a run of cards from the end of one array to the end of another array.

Draw a few pictures on paper of what "before" and "after" look like, then figure out an algorithm to move a 'run' from one column to another.

Well, they're not really strings though. It's like each object in each array is a card. It has a suit, rank, and color so you can determine whether or not the move (done one card at a time) is legal.

Currently the program prints out the "board" as it's initialized. What I need to make it do is ask you what card you want to move (which will be the last card in a certain array) to either a play column (PC1-PC8), a foundation column (F1-F4) or a freecell (F1-F4). Then you will enter which column you wish to move which card to and the program will check to see if it is a legal move. That part I also haven't quite figured out yet, but I know it's going to have to check that the rank on the card under the moved card is one less and that the color is opposite, or if the column is empty. I haven't quite made out the syntax for that check yet though.

I'll post the code to give a better idea of what I'm dealing with.

Code:

```#include "Deck.h" #include <iostream> void printCard(Card c); void printColumn(Card ar[]); int main() {         Deck myDeck;         myDeck.Shuffle();         Card FC1;         Card FC2;         Card FC3;         Card FC4;         Card F1[13];         Card F2[13];         Card F3[13];         Card F4[13];         Card PC1[25];         Card PC2[25];         Card PC3[25];         Card PC4[25];         Card PC5[25];         Card PC6[25];         Card PC7[25];         Card PC8[25];         int i = 0;         for (int j = 0; j < 7; j++)         {                 PC1[j] = myDeck.Draw(i);         }         for (int j = 0; j < 7; j++)         {                 PC2[j] = myDeck.Draw(i);                 i++;         }         for (int j = 0; j < 7; j++)         {                 PC3[j] = myDeck.Draw(i);                 i++;         }         for (int j = 0; j < 7; j++)         {                 PC4[j] = myDeck.Draw(i);                 i++;         }         for (int j = 0; j < 6; j++)         {                 PC5[j] = myDeck.Draw(i);                 i++;         }         for (int j = 0; j < 6; j++)         {                 PC6[j] = myDeck.Draw(i);                 i++;         }         for (int j = 0; j < 6; j++)         {                 PC7[j] = myDeck.Draw(i);                 i++;         }         for (int j = 0; j < 6; j++)         {                 PC8[j] = myDeck.Draw(i);                 i++;         }         cout << "Freecell 1 is: " << endl;         printCard(FC1);         cout << "Freecell 2 is: " << endl;         printCard(FC2);         cout << "Freecell 3 is: " << endl;         printCard(FC3);         cout << "Freecell 4 is: " << endl;         printCard(FC4);                 cout << "Foundation 1 is: " << endl;         printColumn(F1);         cout << "Foundation 2 is: " << endl;         printColumn(F2);         cout << "Foundation 3 is: " << endl;         printColumn(F3);         cout << "Foundation 4 is: " << endl;         printColumn(F4);         cout << "Play Column 1 is: " << endl;         printColumn(PC1);         cout << "Play Column 2 is: " << endl;         printColumn(PC2);         cout << "Play Column 3 is: " << endl;         printColumn(PC3);         cout << "Play Column 4 is: " << endl;         printColumn(PC4);         cout << "Play Column 5 is: " << endl;         printColumn(PC5);         cout << "Play Column 6 is: " << endl;         printColumn(PC6);         cout << "Play Column 7 is: " << endl;         printColumn(PC7);         cout << "Play Column 8 is: " << endl;         printColumn(PC8);         return 0; } void printCard(Card c) {         char rank;         char suit;         char color;         switch(c.getRank())         {         case 1: rank = 'A';                         break;         case 2: rank = '2';                         break;         case 3: rank = '3';                         break;         case 4: rank = '4';                         break;         case 5: rank = '5';                         break;         case 6: rank = '6';                         break;         case 7: rank = '7';                         break;         case 8: rank = '8';                         break;         case 9: rank = '9';                         break;         case 10: rank = 'T';                         break;         case 11: rank = 'J';                         break;         case 12: rank = 'Q';                         break;         case 13: rank = 'K';                         break;         default: rank = '0';                         break;         }         switch(c.getSuit())         {         case 1: suit = 'S';                         break;         case 2: suit = 'C';                         break;         case 3: suit = 'H';                         break;         case 4: suit = 'D';                         break;         default: suit = '0';                         break;         }         switch(c.getColor())         {         case 1: color = 'B';                         break;         case 2: color = 'R';                         break;         default: color = '0';                         break;         }         cout << "Rank is: " << rank << ". Suit is: " << suit << ". Color is: " << color << endl; } void printColumn(Card ar[])         {                 Card temp;                 int i = 0;                 temp = ar[i];                 while(temp.getRank() != 0)                 {                         printCard(temp);                         i++;                         temp = ar[i];                 }         }```
• 12-10-2006
manutd
You may want to encapsulate some of your main into a function.
• 12-10-2006
SmellsLikeToast
Yea, I need the display to be something I can call on after the move function.