I need the game to end when one player has twice as many cards as the other player.

the p1CardPile and p2CardPile are arrayLists that have the hands of player1 and player2.

As the game progresses, it removes cards from the loosing player and puts them into the bottom of the other players stack.

Is there anything obviously wrong with my logic on the while loop? I can't seem to see what I'm doing wrong.

You might want to consider doing it with multiplication:

And I think Campbell is correct...this looks like the body will run only when one pile is twice the size of the other...

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors

To explain, it's a game of war with an extra deck called trumpPile. This deck has cards that, if either player plays, will automatically 'trump' the other player's card and they win.

I need to keep track of current win streak (which is broken with a tie) and longest win streak.

I'm having an issue with my logic after the "test for higher trump" for loops. It gives me "p2 wins (had higher non-trump)" when it should be giving "p1 wins (with a trump)"

Sorry if this is confusing!

end when one player has twice as many cards as the other player.

In other words the loop should continue until one player has twice as many cards as the other player.

If the players have the same number of cards the loop should continue.

The (corrected) while() statement you posted would only execute if one of the players had exactly twice the cards of the other player.

Norm Radder wrote:

end when one player has twice as many cards as the other player.

In other words the loop should continue until one player has twice as many cards as the other player.

If the players have the same number of cards the loop should continue.

The (corrected) while() statement you posted would only execute if one of the players had exactly twice the cards of the other player.

I'm confused. Shouldn't the while in my last post run while one player's cards are not twice the size of the other? And then end when it reaches twice the size of the other?

Is it possible for one player to get more than twice the other player's count? The != test might miss that case.

gives me an "java.lang.IndexOutOfBoundsException"

You need to copy the full text of the error message and paste it here if you need help with it.

If the count can change by 2, then the != test won't work. The test needs to compare greater or less than as appropriate to catch the case where one total is more than 2 times the other.

why is this not working?!?

The fact I'm using > and not >= should make sure that one card pile is more than twice the size of the other, right?

I'm submitting through hypergrade and all it tells me is

why is this not working?!?

//stops when one card pile is more than twice the size of the other

while(p1CardPile.size()*2 > p2CardPile.size() || p1CardPile.size() < p2CardPile.size()*2) {

That says to loop when

p1CardPile.size()*2 > p2CardPile.size()

or

p1CardPile.size() < p2CardPile.size()*2

The first will not be true until p1 gets larger

the second will be true until p1 gets larger

**loop to continue**while

both conditions are true

or if only one condition is true?

If you want

**both**to be true the subexpressions need to be connected with the AND operator

if you want

**only one**to be true, the subexpressions need to be connected with the OR operator

For the subexpressions:

Say p1 has 10

if p2 has 18 then 10*2 > 18 means continue the loop

if p2 has 22 then 10*2 < 22 means exit the loop

High level code like your game loop has no business knowing about low-level logic like pile size and all that other stuff. A pile of cards should be able to tell if it's bigger than another pile of cards. But because your code has implementation details written all over the place, it's difficult to wrangle the logic out of it.

*Practice only makes habit, only perfect practice makes perfect.
Practice mindfully by doing the right things and doing things right.*— Junilu

[How to Ask Questions] [How to Answer Questions]

Then the other method only has to change slightly to

*Practice only makes habit, only perfect practice makes perfect.
Practice mindfully by doing the right things and doing things right.*— Junilu

[How to Ask Questions] [How to Answer Questions]

By extracting parts of that code out and focusing on one piece of logic at a time, you can start dividing the problem up into smaller, more manageable chunks. It's easier to find a needle in a small pile of hay than it is to find it in one huge pile of hay, even if you have to go through multiple small piles of hay.

*Practice only makes habit, only perfect practice makes perfect.
Practice mindfully by doing the right things and doing things right.*— Junilu

[How to Ask Questions] [How to Answer Questions]