• Post Reply Bookmark Topic Watch Topic
  • New Topic

Poker Game Evaluator  RSS feed

 
marco cucinotta
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to sort the 5 cards in decreasing order and this is exactly what my professor gave us but it has errors.

Line 6 has an error with the < operator.
Line 11&13 says cannot convert from card to Int.
This method is passing in Card[] hand
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whatever "Card" is, it is apparently not an int. So you can't assign it to an int (lines 11 and 13), and you can't use comparison operators on it. The former problem could be solved by using a Card datatype in line 11 instead of an int. Solving the latter problem depends on what Card is, and on what you are trying to achieve by the comparison.
 
Knute Snortum
Sheriff
Posts: 4276
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Say the Card class has a value field of int from 2 to 14. Create an accessor to this field. Then you can do something like:

 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can create an enumerated type for cards. You can learn about enumerated types in the Java Language Specification. It even gives examples, if I remember correctly.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
marco cucinotta wrote:Line 6 has an error with the < operator.
Line 11&13 says cannot convert from card to Int.
This method is passing in Card[] hand

Which suggests that there's clearly lots of code you haven't shown us. We don't want a mind-numbing "core dump" of everything you've written (unless it fits into 100 lines or less), but we do need to see the method and class declarations, and any relevant member fields you've defined.

HIH

Winston
 
marco cucinotta
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have solved the sort problem for my poker game. But after writing code for each type of hand, if I test a hand with the correct hand type and the others commented out, it works fine, but if each hand type's code is there, I'll just get an indexoutofbounds error.
 
Knute Snortum
Sheriff
Posts: 4276
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Think about what happens when i = hand.length
 
marco cucinotta
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Still working on that issue but when I call my shuffle method in another class and run it, the error says illegalargumentexception: n must be positive. I thought maybe i should start at 1, but then error was 52. I thought this shuffle method was correct.
 
Carey Brown
Saloon Keeper
Posts: 3315
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
marco cucinotta wrote:Still working on that issue but when I call my shuffle method in another class and run it, the error says illegalargumentexception: n must be positive. I thought maybe i should start at 1, but then error was 52. I thought this shuffle method was correct.

Change to randgen.nextInt(52)
 
marco cucinotta
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks I think that worked but the new error is now the older one with the different hand types on line 3. This error reads indexoutofboundsexception: 5
 
marco cucinotta
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Line 3 on very long code, not the code just posted
 
Carey Brown
Saloon Keeper
Posts: 3315
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
marco cucinotta
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How come the[i+1] etc. is out of bounds with my shuffled deck, but when I manually put in a tester hand that is a straight flush, it prints out right?
 
Knute Snortum
Sheriff
Posts: 4276
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lots of programs work fine when you feed them hand-picked data, but with random data that program won't work.
 
Carey Brown
Saloon Keeper
Posts: 3315
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:

Think about it, if hand.length is 7, then when i is 4 then i+3 is 7 which is out of bounds (only indices 0 through 6 are in bounds).

Your hand picked example works because the condition is true and the method returns before reaching the out-of-bound state.
 
marco cucinotta
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your right I took out the i's and it works now. But since it is working I see the same hand prints out if I want say, 10 hands. It isn't dealing new cards each time.

The nextCard was set to 0 initially.
 
Knute Snortum
Sheriff
Posts: 4276
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Think about what happens to topCard after you exit the dealCards() method.
 
marco cucinotta
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Doesn't the topCard increase though because of the topCard++? I tried changing the return 52 to 0 and then 5 and the hands were all different but they were all different decks too. So after one hand, a new deck was being used. That's a step forward for me but isn't correct.
 
Knute Snortum
Sheriff
Posts: 4276
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
topCard only changes within dealCards(). So every time dealCards() is called, topCard starts at zero again, so you get the same hand.
 
Carey Brown
Saloon Keeper
Posts: 3315
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Requirement (3) asks you to use and update nextCard appropriately.
 
marco cucinotta
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for pointing that out Carey, the topCard should have been nextCard from earlier in my program. This code works for up to 5 hands, but then gets an outofbounds: 55
 
Carey Brown
Saloon Keeper
Posts: 3315
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
marco cucinotta wrote:Thanks for pointing that out Carey, the topCard should have been nextCard from earlier in my program. This code works for up to 5 hands, but then gets an outofbounds: 55

Line 2 causes you to skip blocks of cards in the deck, which I don't think is your intent.
 
Knute Snortum
Sheriff
Posts: 4276
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What happens when you get to the end of the deck?
 
Carey Brown
Saloon Keeper
Posts: 3315
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
marco cucinotta wrote:This code works for up to 5 hands, but then gets an outofbounds: 55

To debug the outOfBounds exception you might add a temporary print statement like this:
 
marco cucinotta
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried it and it does skip 5 cards every hand because of that line. It looks like this: How can I correct this?
*** new deck ***
nextCard=5
nextCard=6
nextCard=7
nextCard=8
nextCard=9
[HA, CK, SQ, H7, C6] - High Card
nextCard=15
nextCard=16
nextCard=17
nextCard=18
nextCard=19
[HT, S8, H6, H5, H2] - High Card
nextCard=25
nextCard=26
nextCard=27
nextCard=28
nextCard=29
[ST, DT, H9, S9, H4] - Two Pair
nextCard=35
nextCard=36
nextCard=37
nextCard=38
nextCard=39
[DQ, CT, C8, S7, S6] - High Card
nextCard=45
nextCard=46
nextCard=47
nextCard=48
nextCard=49
[DA, SA, HK, D3, D2] - One Pair
nextCard=55
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 55
at marco.CardDeck.dealCards(CardDeck.java:93)
at marco.PokerHands.computeHandOdds(PokerHands.java:80)
at marco.PokerHands.main(PokerHands.java:10)
 
marco cucinotta
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I set nextCard = nextCard and it works for 10 hands now. But anything above 10 gets an error outofbounds: 52. Is this error still because of the nextCard algorithm? or from somewhere else in my code?
 
Carey Brown
Saloon Keeper
Posts: 3315
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where are you incrementing nextCard by 5? Where are you incrementing nextCard by 1? What is the purpose of incrementing by 5? What is the purpose of incrementing by 1?

I feel that at this point you need to get out paper and pencil and play computer, following your own code as written. Is it doing what you intended it to do?
 
Carey Brown
Saloon Keeper
Posts: 3315
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
marco cucinotta wrote:I set nextCard = nextCard and it works for 10 hands now. But anything above 10 gets an error outofbounds: 52. Is this error still because of the nextCard algorithm? or from somewhere else in my code?

See Knute's comment.
 
marco cucinotta
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It was somewhere else in my code, but I got it! Thanks for the help
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!