please help with arrays


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Page 1 of 2 12 LastLast
Results 1 to 15 of 23

Thread: please help with arrays

  1. #1
    Join Date
    May 2006
    Posts
    11

    please help with arrays

    hi guys!!

    I am working on a freeCell assignment and have been going well until i reached the part where i hav to print a string representation of tableau :
    the unit test dosent pass.

    There are 8 piles in this tableau and i put to put a card into each untill all th cards are used up....

    am able to put the first card properly but dnt know how to get then rest

    this is my code:

    System.out.println("Tableau:");
    for(int i = 8; i < 16; i++)
    {
    pileRow[i] = new Pile();
    pile.put(deck.nextCard());
    String result = i + ": ";
    System.out.println (result+ pile.toString());
    }

    this gives me :
    Tableau:
    8:[DK]
    9:[DK,D10]
    10:[DK,D10,SJ]
    11:[DK,D10,SJ,C7]
    12:[DK,D10,SJ,C7,H7]
    13:[DK,D10,SJ,C7,H7,S3]
    14:[DK,D10,SJ,C7,H7,S3,D2]
    15:[DK,D10,SJ,C7,H7,S3,D2,,H5]

    WHEREAS IT SHULDD BE :
    we have to keep adding cards to te pile till there is no cards remaining.
    Tableau:
    8: [DK,D7,C3,D5,SA,C6,C8]
    9: [D10,S7,H8,C2,CQ,H4,D8]
    10: [SJ,CJ,S2,S4,SK,HA,HJ]
    11: [C7,CK,H2,C9,H10,S6,DJ]
    12: [H7,HQ,HK,C5,S10,S5]
    13: [S3,D3,H6,CA,C4,C10]
    14: [D2,S9,D4,H9,D9,H3]
    15: [H5,SQ,D6,DQ,S8,DA]

    i have also attached the Deck codes.....any help will be much appreciated.

    thannks

    PAri.
    Attached Files Attached Files

  2. #2
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    What you are doing is creating a 2 dimensional array. Add a second loop to your structure ... the outer loop counts 1 to n ( for the "column" position in the array while there are still cards) and the inner loop counts from 8 to 15 (for each "row" array )

  3. #3
    Join Date
    Aug 2006
    Posts
    7
    Quote Originally Posted by pari
    hi guys!!

    I am working on a freeCell assignment and have been going well until i reached the part where i hav to print a string representation of tableau :
    the unit test dosent pass.

    There are 8 piles in this tableau and i put to put a card into each untill all th cards are used up....

    am able to put the first card properly but dnt know how to get then rest

    this is my code:

    System.out.println("Tableau:");
    for(int i = 8; i < 16; i++)
    {
    pileRow[i] = new Pile();
    pile.put(deck.nextCard());
    String result = i + ": ";
    System.out.println (result+ pile.toString());
    }

    this gives me :
    Tableau:
    8:[DK]
    9:[DK,D10]
    10:[DK,D10,SJ]
    11:[DK,D10,SJ,C7]
    12:[DK,D10,SJ,C7,H7]
    13:[DK,D10,SJ,C7,H7,S3]
    14:[DK,D10,SJ,C7,H7,S3,D2]
    15:[DK,D10,SJ,C7,H7,S3,D2,,H5]

    WHEREAS IT SHULDD BE :
    we have to keep adding cards to te pile till there is no cards remaining.
    Tableau:
    8: [DK,D7,C3,D5,SA,C6,C8]
    9: [D10,S7,H8,C2,CQ,H4,D8]
    10: [SJ,CJ,S2,S4,SK,HA,HJ]
    11: [C7,CK,H2,C9,H10,S6,DJ]
    12: [H7,HQ,HK,C5,S10,S5]
    13: [S3,D3,H6,CA,C4,C10]
    14: [D2,S9,D4,H9,D9,H3]
    15: [H5,SQ,D6,DQ,S8,DA]

    i have also attached the Deck codes.....any help will be much appreciated.
    THANKS
    thannks

    PAri.

    Can you please eloborate litte more ?

  4. #4
    Join Date
    Aug 2006
    Posts
    7

    doubt

    Is this program can work with j2sdk1.4

  5. #5
    Join Date
    May 2006
    Posts
    11
    You have to put each card into pile one at a time and make a tableau like in any basic FreeCell game.

  6. #6
    Join Date
    Sep 2006
    Posts
    9
    Hi there,

    I think we might be working on the same assignment. Or a similar one. As I am also working on a Freecell Java assignment at the moment.

    You should have a class which represents a pile of cards right? Not a specific pile like the stock, but just a class which can be used to represent a foiundation pile, a tableau or a free cell pile.
    The code of your tablaue class draws upon the pile class. Do you have the pile class code?

  7. #7
    Join Date
    May 2006
    Posts
    11
    yes i have ...tell me what u wanna know in it and i will help u as much as possible...

  8. #8
    Join Date
    Sep 2006
    Posts
    9
    Lol, that was a quick response!

    Basically just need to know the layout of it, like the constructor and whatever methods it contains.

    Because in my freecell game, the tablaue class draws upon a couple methods of the Pile class. not sure if yours will need to.
    Hopefully you will have a method in the pile class which shows a string representation of the array? Because then you can draw upon that method in the tableau class.

  9. #9
    Join Date
    May 2006
    Posts
    11
    import java.awt.*;

    public class Pile
    {
    Card card;
    Card[] pile;
    int count=1;
    public static final int MAX_PILE_NUM = 52;
    private int pileArrayCount;
    private int x;
    private int y;
    public Pile()
    {
    pile = new Card[MAX_PILE_NUM];
    pileArrayCount=0;
    }

    public Pile(int x,int y, int deltaY) {

    pile = new Card[MAX_PILE_NUM];
    pileArrayCount=0;
    this.x=x;
    this.y=y;

    }

    public void draw(Graphics g) {
    CardImage image = new CardImage();
    int cardWidth = image.getWidth();
    int y2= 0;
    for (int i = 0;i<48;i++) {
    Card card=pile[i];
    if (i==0) {
    if (card !=null) {
    image.draw(card,g,x,y,true,false);
    }
    else {
    image.drawNullCard(g,x,y);
    }
    }
    else if (card!=null) {
    y2=y+(20*i);
    image.draw(card,g,x,y2,true,false);
    }
    else {
    break;
    }
    }
    count =count+1;
    }


    public boolean put(Card card) {
    boolean theCard = false;
    this.card = card;
    if (card != null) {
    theCard = true;
    pile[pileArrayCount] = card;
    pileArrayCount++;
    }
    return theCard;
    }

    public Card takeTop() {
    Card result = null;
    if (pile[0]!=null) {
    result =pile[pileArrayCount-1];
    pile[pileArrayCount-1] = null;
    pileArrayCount --;
    }
    return result;
    }

    public Card top() {
    Card result = null;
    if (pile[0]!=null) {
    result =pile[pileArrayCount-1];
    }
    return result;
    }

    public java.awt.Rectangle topCardRect()
    {
    return null;

    }

    public java.lang.String toString()
    {
    String s = "";
    String y ;
    for (int i = 0;i <pileArrayCount;i++)
    {

    Card top = pile[i];
    if (top == null)
    {

    break;
    }
    else if (i==0) {

    s = s + top ;

    }
    else {

    s = s+ "," + top;

    }

    }

    return "["+s + "]";
    }
    }

    thats what i did in the pile class ...
    i am in step 3....please help if you finish it....cheers

  10. #10
    Join Date
    May 2006
    Posts
    11
    dont mind the graphics bit ...it might be wrong =P

  11. #11
    Join Date
    Sep 2006
    Posts
    9
    Hmm, well your pile code is different to mine, but hopefully it sholdnt make too much difference.

    How are you representing the various piles in step3? THere is 3 different ways to represent them given in the specifications. Which one are you using?

    Anyway I got to go to class now


    Will try and work this out later today.

  12. #12
    Join Date
    May 2006
    Posts
    11
    ii am using max_array 16...and 0-3 as free cell 4 - 7 as foundation and 8 - 15 as tableau....i am getting free cell and foundation row correct but ....dont know how to get tableau .....please help..

  13. #13
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    To produce a string representation of your pile class, since you are using arrays as the data structure for the cards in the pile, you can use the Arrays.toString() method which invokes the toString method of each object stored in the array, from element 0 to element n-1. Call Arrays.toString( pile ) .... make sure your Card class has a toString method.

  14. #14
    Join Date
    Sep 2006
    Posts
    9
    Oh, I am using a different representation of the piles than the one you are

    I think I got it working for mine. I will see if I can do it for your one.

  15. #15
    Join Date
    Sep 2006
    Posts
    9
    Actually, it would be quicker if you could post the code of that class, as It will save me time.

    Hopefully the thing I did will be able to work for you too.

Similar Threads

  1. Control Arrays in VB.NET
    By Gary Nelson in forum .NET
    Replies: 277
    Last Post: 10-01-2003, 12:00 AM
  2. Copy/Duplicate Arrays
    By Steve Cochran in forum .NET
    Replies: 8
    Last Post: 10-18-2002, 12:10 PM
  3. Replies: 15
    Last Post: 05-09-2001, 04:40 AM
  4. VB Arrays and memory allocation
    By Brian Leung in forum VB Classic
    Replies: 12
    Last Post: 06-20-2000, 03:06 PM
  5. VB Arrays and memory allocation
    By Brian Leung in forum VB Classic
    Replies: 0
    Last Post: 06-20-2000, 09:47 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center