Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by David_29 · Sep 10, 2015 at 02:31 PM · c#bugscardnumerical

Detecting Correct Numerical Order For Detecting Straight Flush

I am currently debugging and testing for my card game. The goal of this task is to see if all card number has to be in exact order and the number of cards picked must be at least 3. Not relatively in order but exact order without skipping, regardless what card number started. In case if all numbers picked in the wrong order, I stored it in a List object and using the Sort() method to automatically arranged in order numerically and check again if all the numbers stored are arrange in order exactly.

Let's say here is what I am expecting the output result when computing the correct order:

 Example 1
 CARD INPUT CHOSEN: 1, 2, 3, 4
 RESULT: true
 REASON: All numbers are in exact order and at least 3 cards have met.
 
 Example 2
 CARD INPUT: 1, 2, 5, 6
 RESULT: false
 REASON: All numbers are in order. However, no. 3 and no. 4 is skipped.
 
 Example 3
 CARD INPUT: 4, 5
 RESULT: false
 REASON: All numbers are in exact order. However, player picked only two cards.

 Example 4
 CARD INPUT CHOSEN: 3, 4, 5
 RESULT: true
 REASON: Same as "Example 1".

These examples above are my expectations of the result. Now, for reality, when I tried, run, and tested the program and I got this error like this.

 CARD INPUT: 3, 4, 5
 RESULT: false

Say no. 3, no. 4, and no. 5 and arranged in that order are supposed to be valid. Technically, the reason I got this bug and I'm still figuring out yet for an alternate solution in order to check for exact order. Here is the code I've used in C# and see comments for details:

 // See if the current card value deducted by next card value always 1 for every index.
 for(int i = 0; i < temp.Length; i++) {
 
     //    The problem with this if-else condition is that the last array of the list  
     // and checking this index beyond the last resulted in an array index error. 
     // To remedy this index out of bounds error, the condition result jumped 
     // from "check[i] = true" to "check[i] = false".
 
     if((temp[i+1] - temp[i]) == 1) {
                 
         check[i] = true;
                 
     } else {
                 
         check[i] = false;
                 
     }
             
 }
         
 // Finally, see if all cards are in correct order.
 for(int i = 0; i < check.Length; i++) {
             
     if(i != check.Length) {
                 
         if(check[i]) {
                     
             isStraight = check[i];
             print ("[ COMBO CHECK - Straight Flush ] --> Combo valid. INDEX " + 
                      i + " - " + isStraight);
                     
         } else {
                     
             isStraight = false;
             print ("[ COMBO CHECK - Straight Flush ] --> Combo invalid. INDEX " + 
                      i + " - " + isStraight);
                     
         }
                 
     }
             
 }





Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by RudyTheDev · Sep 10, 2015 at 06:30 PM

You get index range exception because your loop goes up to temp.Length. You should have it to temp.Length-1. After all, once you compare last card to previous card, you don't need to go to last card and compare it to... well there's nothing left.

A simple way (only your mentioned constraints) would be:

 private bool IsStraight(int[] ints)
 {
     // Less than 3 cards cannot form a straight
     if (ints.Length < 3)
         return false;

     // Check that every card is followed by a card exactly 1 rank higher
     for (int i = 0; i < ints.Length - 1; i++)
         if (ints[i + 1] - ints[i] != 1)
             return false;

     // No problems with card ranking, so we are good
     return true;
 }

This is exactly the logic you had, but the loop runs 1 less iteration. It's also a more readable method. This returns:

 IsStraight(new int[] { 2, 3, 4, 5 }); // true
 IsStraight(new int[] { 2, 3, 5, 6 }); // false
 IsStraight(new int[] { 5, 4, 3, 2 }); // false
 IsStraight(new int[] { 2, 3, 4 }); // true
 IsStraight(new int[] { 2, 3 }); // false
 
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
-1

Answer by NoseKills · Sep 10, 2015 at 03:43 PM

Maybe

 public bool isStraight(int[] cards) {
     if (cards.Length<3) return false;
 
     Array.Sort(cards);
     int straight = 0;
     int previous = cards[0];
     for (int i=1; i < cards.Length; i++){
         if (cards[i] == previous+1 && previous != 0) {
             if (++straight > 2) return true;
         } else {
             straight = 0;
         }
         return false;
     }
 }

On mobile. Hope that's right.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image NoseKills · Sep 11, 2015 at 06:04 AM 0
Share

Yay! Downvote without any explanation of why this is a bad answer :) constructive and helpful.

The question doesn't mention if it's necessary but this should also handle repeated cards (3,3,4,5) and "missing/unmade selections" ( 0 ) if that's possible (0,0,1,2,3)

Yeah and the sorting isn't needed if it's done already but i wasn't 100% sure reading the question

avatar image RudyTheDev NoseKills · Sep 11, 2015 at 06:53 PM 0
Share

From what I see, your code won't compile (you don't have a return statement), and it won't produce the correct results.

The loop exits after the first iteration. So let's say return false is meant to go after the for loop.

previous is never modified after the first assignment, so it always compares to the first value. So let's say there's previous = cards[i]; at the end of each iteration.

Not sure if previous != 0 is relevant in OP's case (and why not count cards from 0? or 2 if we follow ranks?) and that still allows negatives. So let's remove that for now.

You start the count at 'straight = 0;', which ignores the very first value, so 2,3,4 or 2,5,6,7 doesn't match. So let's say it's straight = 1; because every value is the first in a straight.

Now straight>2 would indeed match 3+ consecutive cards (though OP asked for all cards). But it wouldn't match 2,3,3,4, because cards[i] == previous+1 would fail. So let's add if (cards[i] == previous) continue; at the start of each iteration.

So this would work for partial straight search:

 public bool isStraight(int[] cards)
 {
     if (cards.Length < 3) return false;

     Array.Sort(cards);
     int straight = 1;
     int previous = cards[0];
     for (int i = 1; i < cards.Length; i++)
     {
         if (cards[i] == previous) continue;

         if (cards[i] == previous + 1)
         {
             if (++straight > 2) return true;
         }
         else
         {
             straight = 1;
         }
         previous = cards[i];
     }

     return false;
 }

That's 4 errors. There's no code explanation for someone who is clearly a beginner. And the code doesn't do what OP asked.

avatar image NoseKills · Sep 12, 2015 at 11:14 AM 0
Share

That's more like it. Thanks for the explanation :) shouldn't do program$$anonymous$$g on a cellphone.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Is it possible to copy and paste something from the Hierarchy in one Unity and paste it in another? 1 Answer

Object not at 0 1 Answer

How to sort cards in order from smallest to the largest amount of strength? 0 Answers

PLEASE HELP ACTIVE SELVE NOT WORKING :) 2 Answers

Creating a dynamic animation system for a card game. 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges