Coding ponies

Post » Mon May 14, 2012 11:25 am

Been stuck at this for I don't know how long. How does one initialise an array of objects, and each object contains an array of object? As I keep getting NullPointerException no matter how I try to bend the compiler to my will when calling a method on the object.

And yes, it's Clock Solitaire.
Spoiler
class Pile {	private TwoDeckCard[] cardPile;	private TwoDeckCard Default = new TwoDeckCard(0,0,true,true,'x');	Pile(){		cardPile = new TwoDeckCard[9];//8 layers total, 0 is a dummy layer.		for(int i=0; i<9; i++){//Latest attempt at initialising.			cardPile[i] = Default;		}	}	void setCardToPile(TwoDeckCard newCard, int layer) {		cardPile[layer] = newCard;	}	void setCardVisibility(boolean visible, int layer) {		(cardPile[layer]).setVisible(visible);	}	boolean getCardVisibility(int layer) {		return (cardPile[layer]).getVisible();	}	TwoDeckCard getCardFromPile(int layer) {		return cardPile[layer];	}	int getValourFromPile(int layer){		return (cardPile[layer]).getValour();	}	}class ClockSolitaire {	public static void main(String args[]){		int loss = 0;		int victory = 0;		TwoDeckCard tempCard;		for(int turn=0; turn<10; turn++){			//initiliaze			Pile[] piles = new Pile[14];//All these piles are supposed to be initialised with TwoDeckCard from deck.			DeckOfTwoCards deck = new DeckOfTwoCards();			for(int i=1; i<14; i++){				for(int j=1; j<9; j++){					tempCard = new TwoDeckCard(0,12,true,true,'x');//Placeholder					//tempCard = deck.getCard((i-1)*8 + j-1);//Will fix it to return proper type once NullPointerException is fixed.					(piles[i]).setCardToPile(tempCard, j);//This is where the compiler complains.					(piles[i]).setCardVisibility(false, j);				}			}			if (DrawCards(13, 0, 0, (piles[13]).getCardFromPile(1), piles)) victory++;			else loss++;		}		System.out.println("Victories: " + victory + "\nLosses: " + loss);	}}
User avatar
Victoria Bartel
 
Posts: 3325
Joined: Tue Apr 10, 2007 10:20 am

Post » Mon May 14, 2012 9:32 am

*head explodes* :bonk:

Eh, 42?

Sorry can't help you with that. I have multiple books about that topic, but I never got around to read them.
User avatar
Leonie Connor
 
Posts: 3434
Joined: Mon Mar 12, 2007 4:18 pm

Post » Mon May 14, 2012 8:41 am

Been stuck at this for I don't know how long. How does one initialise an array of objects, and each object contains an array of object? As I keep getting NullPointerException no matter how I try to bend the compiler to my will when calling a method on the object.

And yes, it's Clock Solitaire.
Spoiler
class Pile {	private TwoDeckCard[] cardPile;	private TwoDeckCard Default = new TwoDeckCard(0,0,true,true,'x');	Pile(){		cardPile = new TwoDeckCard[9];//8 layers total, 0 is a dummy layer.		for(int i=0; i<9; i++){//Latest attempt at initialising.			cardPile[i] = Default;		}	}	void setCardToPile(TwoDeckCard newCard, int layer) {		cardPile[layer] = newCard;	}	void setCardVisibility(boolean visible, int layer) {		(cardPile[layer]).setVisible(visible);	}	boolean getCardVisibility(int layer) {		return (cardPile[layer]).getVisible();	}	TwoDeckCard getCardFromPile(int layer) {		return cardPile[layer];	}	int getValourFromPile(int layer){		return (cardPile[layer]).getValour();	}	}class ClockSolitaire {	public static void main(String args[]){		int loss = 0;		int victory = 0;		TwoDeckCard tempCard;		for(int turn=0; turn<10; turn++){			//initiliaze			Pile[] piles = new Pile[14];//All these piles are supposed to be initialised with TwoDeckCard from deck.			DeckOfTwoCards deck = new DeckOfTwoCards();			for(int i=1; i<14; i++){				for(int j=1; j<9; j++){					tempCard = new TwoDeckCard(0,12,true,true,'x');//Placeholder					//tempCard = deck.getCard((i-1)*8 + j-1);//Will fix it to return proper type once NullPointerException is fixed.					(piles[i]).setCardToPile(tempCard, j);//This is where the compiler complains.					(piles[i]).setCardVisibility(false, j);				}			}			if (DrawCards(13, 0, 0, (piles[13]).getCardFromPile(1), piles)) victory++;			else loss++;		}		System.out.println("Victories: " + victory + "\nLosses: " + loss);	}}

Why not use piles.lenght() and DeckOfTwoCards.lenght() instead of actual numbers. Then you'll never get an ArrayOutOfBoundsixception. At what line do you get a null pointer exception?
User avatar
Ryan Lutz
 
Posts: 3465
Joined: Sun Sep 09, 2007 12:39 pm

Post » Mon May 14, 2012 10:39 am

(piles[i]).setCardToPile(tempCard, j);//This is where the compiler complains.
I decided set it up in a more familiar and managable form. It works now. Though I'm a little bit skeptic with the randomized results. "Victories: 41002, Losses: 998" doesn't sound credible. :dry:

Spoiler
class ClockSolitaire {	static String piles[][] = new String[14][9];//13 piles and 8 layers, excluding dummy piles and layers.	static int kingCount;			public static void main(String args[]){		int loss = 0;		int victory = 0;		for(int turn=0; turn<42000; turn++){			//initialize			kingCount = 0;			DeckOfTwoCards deck = new DeckOfTwoCards();			for(int i=1; i<14; i++){				for(int j=1; j<9; j++){					piles[i][j] = (deck.getCard((i-1)*8 + j-1)).toString();				}			}			if (DrawCards(13, 0, piles[13][1], piles)) victory++;			else loss++;		}		System.out.println("Victories: " + victory + "\nLosses: " + loss);	}	static boolean DrawCards(int hour, int visibleCards, String oldCard, String[][] piles) {		int newHour=0;		String tempCard;		do {			if ((kingCount == 8) && (visibleCards < 104)) return false;			else if (visibleCards == 104) return true;			else {				//Set up new data				if ((oldCard.substring(1)).equals("K")) kingCount++;				try {					newHour = Integer.valueOf((oldCard.substring(1)))+1;				}				catch (NumberFormatException e1) {					switch (oldCard.substring(1)) {					case "K": newHour = 13;break;					case "Q": newHour = 12;break;					case "J": newHour = 11;break;					case "A": newHour = 1;break;					}				}					//Move cards				tempCard = piles[newHour][1].substring(0);				for(int i = 2; i<9; i++) {					piles[newHour][i-1] = piles[newHour][i];				}				piles[newHour][8] = oldCard.substring(0);				oldCard = tempCard.substring(0);			}			visibleCards++;		} while (true);	}}
User avatar
Lifee Mccaslin
 
Posts: 3369
Joined: Fri Jun 01, 2007 1:03 am


Return to Othor Games