Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

trouble with an array  RSS feed

 
Richard Goodwinn
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am currently working on a project which is too create a FreeCell solitaire game.

I am working on a class Pile (which is just a pile of cards).

It involves making an array and implementing a few methods.

This is my code so far



THe takeTop method shold remove and return the top card of the Pile.

It compiles fine, but when I run it through a unit test we are provided with, the takeTop() method does not pass (the top() and put() methods pass the test)

Ive tried a few alterations to the takeTop() method but nothing I do gets it to pass the test.

This is the unit test code


Ive spent hours trying to figure this out and am at my wits end as it is a relatively easy step in the project

Any help is appreciated

[ September 10, 2006: Message edited by: Richard Goodwinn ]

[ September 10, 2006: Message edited by: Richard Goodwinn ]
[ September 10, 2006: Message edited by: Richard Goodwinn ]
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16028
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The testTakeTop() method should provide you with a message that gives you a hint about what's wrong, so that you get a clue about where you should be looking. What does it say?
 
Richard Goodwinn
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

yea the message it gives me is "junit.framwork.AssertionFailedError: A second call
to takeTop should yield the first card in the pile expected:SA but
was: DK

So it expected the Aceof spades but instead returned King of diamonds.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16028
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a good look at your method takeTop().

Ask yourself: What is the program doing when you call this method? Try to imagine what happens, with an example.

The test makes a new Pile and puts an ace of spades and a king of diamonds on the pile. Then it calls takeTop(). Now watch closely what happens.

- There are two cards on the pile, so numCards == 2. You go into the "else" branch.
- You call top() to get the top card. This returns pile[numCards]. I would expect that you get an ArrayIndexOutOfBoundsException here, because numCards == 2, and if the array really contains 2 elements, then 2 is an illegal index (legal indices go from 0 to length of the array minus one).

I'll ignore this for now and continue to see what happens in takeTop().

- So now, the variable topCard references the king of diamonds.
- Now you do this:

pile[--numCards] = topCard;

What does this do? It sets the second card on the pile to the top card. In other words, you set the second card to king of diamonds. Is there a reason why you programmed it that way?
[ September 12, 2006: Message edited by: Jesper Young ]
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!