This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Constructor problem. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Constructor problem." Watch "Constructor problem." New topic
Author

Constructor problem.

Richard Goodwinn
Greenhorn

Joined: May 07, 2006
Posts: 19
Hi,
I am working on a solitaire game called Calculation.

The Foundation class provides the methods to manage and display the foundation of the Calculation game. Since the foundation contains four rows of cards, it is natural for the Foundation class to have a field that is an array of four "somethings", where each "something" represents a row of the foundation. So what I have to do is create a FoundationRow class to handle all the logic associated with one row of the foundation. Then later on when I create the Foundation class, it will have an array of four objects of the type FoundationRow.

Anyway, Im trying to write a constructor for class called FoundationRow.

public FoundationRow(int startNumber, Deck deck)

Parameters:
startNumber - the pip count for the first card in the row, which is also the difference in pip values for adjacent cards in this row. This must be a value in the range 1-4 inclusive.

deck - the deck of cards from which the first card whose pip count equals startNumber is extracted as the first card in the row. When the constructor returns, the number of cards in the deck will be one less than where it was called due to the removal of this card. It may be assumed that the gicen deck contains such a card.


The constructor should set up an array of 13 cards in which the first element is the first card in the given deck of the required pip number.

In the game calculation, a card's suit is irrelevant, only the pip count (1-13) is relevant. The game starts with a shuffled deck of cards from which an ace, two, three and four have been extracted. These four extracted cards form the start of four foundation rows. The goal of the game is to build up each foundation row from its start card through to a king. A card can only be added to a foundation row only in the first free position and only if its "pip value" differs from the card to its left by the pip value of the left-most card in the row.
Thus the first row takes cards A,2,3,4..., the second row takes 2,4,6,8,10,Q,A,3... , the third row takes 3,6,9,Q,2,5,8... and the fourth row takes 4,8,Q,3,7,J...

I am having trouble implementing the Constructor as I do not know how to write the code for the Constructors parameters.

Any suggestions?

Here is the code for FoundationRow, with my attempt to write the constructor. (I gave the methods "stub" implementations, so the unit test would be able to compile).



That compiles fine, but the Constructor doesn't pass the unit test. Can anyone see what Ive done wrong?

Here is the class Deck code in case its useful




Thanks for any help

[ May 17, 2006: Message edited by: Richard Goodwinn ]
[ May 17, 2006: Message edited by: Richard Goodwinn ]
Edwin Keeton
Ranch Hand

Joined: Jul 10, 2002
Posts: 214

How about changing the first assignment in your constructor to this:


SCJP, SCWCD
Richard Goodwinn
Greenhorn

Joined: May 07, 2006
Posts: 19
Oops, thanks. Didn't realise I had that around the wrong way.

Still doesn't pass the unit test though
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
What is the code of the unit test that is not passing?


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Richard Goodwinn
Greenhorn

Joined: May 07, 2006
Posts: 19
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
Well testConstructor1() and testConstructor2() won't pass because they depend on the getCards() method being properly implemented, however at the moment getCards() always returns null.
Richard Goodwinn
Greenhorn

Joined: May 07, 2006
Posts: 19
OK, so I need to implement putCards() first?

So the putCard method should add the given card to the end of the current row (i.e., to the next free slot) if and only if the card is non-null and the difference between its pip count and the pip count of the card currently at the end of the row is the right amount for this row. Specifically, the pip-count difference must equal the startNumber parameter passed to the constructor (and hence must also equal the pip count of the first card in the row).
Then theres the problem that I need to deal with the cyclic behaviour of this method, i.e., I need to allow for the fact that after King we "wrap around" again to Ace, 2, 3, ... etc.
I've been thinking for the past couple hours on how I would do that. The best idea Ive come up with is to use the "modulo" operator "%" when checking if a card can legally be added.

Though so far all my attempts at writing code to achieve this feat have proven futile
[ May 17, 2006: Message edited by: Richard Goodwinn ]
Richard Goodwinn
Greenhorn

Joined: May 07, 2006
Posts: 19

Oops, I think I just realised something.

The FOoundationRow class only deals with the logic of one row. Then I have to create a Foundation class which deals with an array of four FoundationRow arrays.

So wouldn't the constructor code for FoundationRow have something like Card[13] instead of Card[4][13]? (Shouldn't the Foundation class be the one that has Card[4][13]?)
Richard Goodwinn
Greenhorn

Joined: May 07, 2006
Posts: 19
Any suggestions?

I really need to finish this FoundationRow class, but I still cannot get it to pass the tests. My code compiles, but I am very bad with arrays so my code is probably nothing what it should look like
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
Originally posted by Richard Goodwinn:

Oops, I think I just realised something.

The FOoundationRow class only deals with the logic of one row. Then I have to create a Foundation class which deals with an array of four FoundationRow arrays.

So wouldn't the constructor code for FoundationRow have something like Card[13] instead of Card[4][13]? (Shouldn't the Foundation class be the one that has Card[4][13]?)


The Foundation class probably shouldn't have a Card[4][13] array but instead should have a FoundationRow[] array of length 4.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Constructor problem.
 
Similar Threads
blackjack java
can't split card shuffle method into a separate class
Help With 5 card stud program
newbie help please with arrays
Need help with constructor and fields