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

Card Game Programming Question  RSS feed

 
Mimi Udasco
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone!
I'm new to java and I am having trouble with one of my assignment questions.  This is a question about a card game where I want to draw four cards and make sure that none of the cards are the same as each other in terms of suits and number!
Here is the question:
In the exotic draw poker variation of badugi, the players aim to draw a four-card hand so that no two cards have the same suit or rank. Write a method boolean hasFourCardBadugi(String hand) that checks whether the given four-card hand is a badugi, that is, does not contain two cards of the same rank, nor two cards of the same suit. For example, given the hand “2d7cKhJs” the method would return true, but given the hands “2d2cKhJs” (that has two deuces) or “2d7cKhJd” (that has two diamonds), the method would return false.

Here is my code:



WHY IS IT NOT RUNNING MY J LOOP???
 
Tobias Bachert
Ranch Hand
Posts: 85
18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your inner loop is running - you are currently comparing a String with a Character (substring and charAt), which always resolves to false -> the block in the if-statement is never executed.
(Nonetheless, there is something wrong with the bounds of your inner for-loop.)

You can declare your 'temp' variable within your for-loop, try to keep variable scope as small as possible (and you don't have to initialize it with an empty string).
Does the card-set contain the value 10?

(Sidenote: This is one of the tasks where I would prefer using a RegEx/Pattern as it would reduce the task to one line.)
 
Mimi Udasco
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks!!! Your comment was very very helpful and I got it to work! I changed it so that they all use str.substring().  They dont have any values of 10, it just goes up to "23456789TAJQK".

Anyway here is the final code:

 
Campbell Ritchie
Marshal
Posts: 55751
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Please always use the code button and don't use so many blank lines. I have edited your posts so you can see how much better it looks. As on every website, don't write all UPPER CASE.
 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The formatting still needs work to show the }s on lines 16, 17 and 18.  They should be indented about 3 more columns so they line up vertically below the lines with the matching {s.
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tobias Bachert wrote:(Sidenote: This is one of the tasks where I would prefer using a RegEx/Pattern as it would reduce the task to one line.)

As a connoisseur of regexes (and since the OP has provided their final code) I'd love to see that line!
 
Tobias Bachert
Ranch Hand
Posts: 85
18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The main-reason for a pattern was to support two digit values like 10 (without this requirement the pattern becomes even smaller).
(As each value contains only one char, using chars().distinct() instead may be preferable.)

The patterns a and b behave exactly like the for-loop.
Pattern c requires additionally that the provided argument is well formed.

 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you add some comments describing what each Pattern is supposed to do?
 
Tobias Bachert
Ranch Hand
Posts: 85
18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There isn't much to describe as the patterns do nearly the same (and behave like the for loop):
The first and third pattern check for each char that none of the following chars is equal to it.
The second pattern does the same, just inversed (tries to find a char that is followed by a char equal to it with any count of chars inbetween).

Example:
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I sometimes break up the string I use as a pattern to comment it:
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!