Thread: Freecell game

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    4

    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.
    Last edited by SmellsLikeToast; 12-10-2006 at 02:24 PM.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Location
    Estonia
    Posts
    131
    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

  3. #3
    Registered User
    Join Date
    Aug 2006
    Posts
    163
    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).

  4. #4
    Registered User
    Join Date
    Dec 2006
    Posts
    4
    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.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Dec 2006
    Posts
    4
    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];
    		}
    	}

  7. #7
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    You may want to encapsulate some of your main into a function.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  8. #8
    Registered User
    Join Date
    Dec 2006
    Posts
    4
    Yea, I need the display to be something I can call on after the move function.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how do the game engine and the api interact?
    By Shadow12345 in forum Game Programming
    Replies: 9
    Last Post: 12-08-2010, 12:08 AM
  2. Open Source / Semi Open source game idea. Help needed
    By CaptainPatent in forum Projects and Job Recruitment
    Replies: 10
    Last Post: 05-16-2007, 10:44 AM
  3. game engine advice?
    By stien in forum Game Programming
    Replies: 0
    Last Post: 01-23-2007, 03:46 PM
  4. My Maze Game --- A Few Questions
    By TechWins in forum Game Programming
    Replies: 18
    Last Post: 04-24-2002, 11:00 PM