Thread: Allocation

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    3

    Allocation

    Hi guys,
    I've been with some difficulties on dynamic allocation. I have to code a kind of simplified FreeCell within an array of 60 positions (all 52 cards must be inside it) and I don't know how the stacks can be allocated in a way that the overflow doesn't happen.
    Does anybody know how to do it?
    Thanks a lot.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Quote Originally Posted by freecell help
    The game area consists of four home cells, four free cells, and a deck of cards, which is dealt face up in eight columns at the beginning of the game.
    Any given card must always be in one of those 16 places (each can be viewed as a list with zero or more cards associated with it).

    The free cells are special in that they can only have either 0 or 1 card. All the others can be an arbitrary list of cards.

    If you create 52 cards, each with suit, rank, list identity, I don't see why you would need any extra allocations.

    All the operations you can do take 'n' (1 or more) cards from the tail of one list and append it to the tail of another list. Again, there is an exception that the home cells can only be appended to. Once you've sent a card home, you can't get it back.
    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.

  3. #3
    Registered User
    Join Date
    Aug 2007
    Posts
    3
    Salem,
    I really appreciate your help, but my problem is the stacks. How can I put 8 columns, 4 free cells and 4 home cells inside an limited area (60 positions of an array) without overflowing it? I know that there's enough space for the operations, however, I don't know how to implement my logic here. I can't limit the stacks, because each column of FreeCell supports more than 7 cards (for example). My doubt is what can I do to avoid overflowing (first stack's top with second stack's base, for example).
    Sorry, I don't have enough experience to do this kinds of code, but I've been asked for it at university and now I have to do it so.
    And thanks a lot for your comprehension and help.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Why do you think you need to do this in an array with 60 positions - it doesn't seem quite sensible.

    An approach where you have a deck of cards and then a set of "places" for the list of cards to be held would be the right solution, I would think.

    --
    Mats

  5. #5
    Registered User
    Join Date
    Aug 2007
    Posts
    3
    Mats,
    these is not my specification. It's professor's.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Ok, so whilst the 60-position solution still (despite coming from a professor) doesn't seem like the solution I would prefer, I think you can work something out with it. You don't need more than 52 positions to hold 52 cards, so doing so with 8 more positions seems doable.

    --
    Mats

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    I don't see what the other 8 would be used for, and still be able to call it a 'card'.

    Sure the 4 free cell positions could be implemented as a single card, but the 4 home cells and the 8 files(?) need to be another kind of data structure (an "x" of cards).

    Whether "x" is a stack, list, or whatever is entirely up to you.
    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.

  8. #8
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    • Freecell has a finite number of stacks across the screen
    • Each stack can have an infinite (theoretically) amount of cards in it.
    • The game has finite number of 'free' cells (4)


    With this information you should be able to devise some data structures for the game.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. dynamic allocation from 1 instead of zero
    By cfdprogrammer in forum C Programming
    Replies: 27
    Last Post: 04-28-2009, 08:21 AM
  2. pointer to array with dynamic allocation
    By cfdprogrammer in forum C Programming
    Replies: 22
    Last Post: 04-07-2009, 09:56 AM
  3. Difference between straight and dynamic allocation?
    By darsunt in forum C++ Programming
    Replies: 10
    Last Post: 06-04-2008, 05:47 PM
  4. redundant allocation
    By George2 in forum C++ Programming
    Replies: 22
    Last Post: 03-06-2008, 06:43 PM
  5. Dynamic allocation (I thought it would crash)
    By Baaaah! in forum C Programming
    Replies: 16
    Last Post: 11-30-2005, 05:10 PM