AeriaGlorisia AeriaGlorisia - 24 days ago 8
Java Question

How do I write an efficient/optimized loop for comparing several conditions at once? Badugi cardgame method

Say, I'm making a simple badugi card game where the Hand is represented by 10 characters in a string. E.g:
2s3h5dQs - 2 of spades, 3 of hearts, 5 of diamonds, Queen of spades

Now, in this badugi card game I want to create two loops where the first loop checks if all the ranks are different(none of them can be the same) and the other loop checks if all the suits are different. If both of these conditions return as true where they all have different ranks and suits, the hand has drawn a badugi(please excuse my lack of terminology where necessary.)

Now, how can I create an efficient loop for such a situation? I was thinking that I could create several if statements as such:

if (hand.charAt(0) != hand.charAt(2) && hand.charAt(0) != hand.charAt(4) && hand.charAt(0) != hand.charAt(6))
if (hand.charAt(2) != hand.charAt(0) && hand.charAt(2) != hand.charAt(4) && hand.charAt(2) != hand.charAt(6))


... and so forth comparing every single index to one another. But this gets tedious and seems very unprofessional. So my question is, how do I write an efficient loop for this scenario? How can I compare/check if there are no matches at these specific index points to one another?

If I haven't explained properly then please let me know.

Please keep in mind, I am not allowed freedom of how to formulate a hand. It has to be in the format above

Answer

You are putting your energy into the wrong place.

You do not need to worry about efficiency at all.

Instead, you should worry about creating a clean design (based on reasonable abstractions) and then write code that is super-easy to read and understand.

And your current approach fails both of those ideas; unfortunately completely.

In other words: you do not represent hands and values as characters within a String.

You create a class that abstracts a Card (with its value and face). And then a "hand" becomes a List / array of such Card objects. And then you can use concepts such as Comparator to compare card values, or you can make use of equals() ...

And even when you wish to keep your (actually over-complex) naive, simple approach of using chars within a string; then you should at least use some kind of looping so that you don't compare charAt(0) against charAt(2); but maybe charAt(i) against charAt(j).

And following your edit and the excellent comment by jsheeran: even when you are forced to deal with this kind of "string notation"; you could still write reasonable code ... that takes such string as input, but transforms them into something that makes more sense.

For example, the Card class constructor could take two chars for suite/value.

But to get you going with your actual question; you could something like:

public boolean isCardSameAsAllOthers(int indexToCheck) {
  for (int i=0; i<cardString.length-1; i+=2) {
    if (cardString.charAt(indexToCheck) != cardString.charAt(i)) {
      return false;
    }
  }
  return true;
}

( the above is just an idea how to write down a method that checks that all chars at 0, 2, 4, ... are not matching some index x).