How to find a lower straight in a subarray without sorting

Hello,

I am having some trouble writing the code to find a lower straight in C++.

Here is what I have so far for that portion of code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int lowerStraight(int arr[][5]) {
    //lower straight code
    int lowerStraightSum = 0;
    int minNum, nextNum, threeNum, fourNum;
    minNum = findMin(arr);
    nextNum = minNum + 1;
    threeNum = nextNum + 1;
    fourNum = threeNum + 1;
    for (int i = 0; i < 5; i++) {
        if (arr[9][i] == nextNum) {
            if (arr[9][i] == threeNum) {
                if (arr[9][i] == fourNum) {
                    lowerStraightSum = 30;
                }
            }
        }
    }
    cout << lowerStraightSum;
    return lowerStraightSum;
}

This returns a 0 even though the subarray is set to {1, 2, 5, 3, 4}. Does anyone know what the issue is? Also, I realized that finding the minimum value does not always work since the array could be {1, 3, 4, 5, 6} and still be valid (we only need to find 4 consecutive in a row and 5 for a higher straight). Does anyone know what to do to get that to run properly as well?

Edit: In case you want to see all of the code for pertaining to the lower straight, here it is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int main() {
    int arr[13][5] = { {1, 3, 1, 1, 2},//aces*
              {1, 2, 4, 5, 6},//twos*
              {6, 3, 4, 3, 5},//threes*
              {3, 1, 1, 4, 4},//fours*
              {5, 5, 5, 5, 3},//fives*
              {6, 2, 6, 6, 6},//sixes*
              {1, 4, 3, 3, 3},//three of a kind*
              {3, 3, 3, 3, 3},//four of a kind*
              {4, 1, 1, 4, 1},//full house
              {1, 2, 5, 3, 4},//lower straight
              {2, 3, 5, 4, 1},//higher straight
              {4, 4, 4, 4, 4},//yahtzee*
              {3, 3, 4, 5, 6} };//chance*

    //yahtzeeScoreCalc(arr);
    lowerStraight(arr);
	return 0;
}


1
2
3
4
5
6
7
8
9
int findMin(int arr[][5]) {
    int minNum = arr[9][0];
    for (int i = 0; i < 5; i++) {
        if (arr[9][i] < minNum) {
            minNum = arr[9][i];
        }
    }
    return minNum;
}

Last edited on
So why don't you want to sort the array before trying to determine the hand?

If your answer is "performance", realise that your solution basically involves "permutation" which is far more expensive than sorting.

If you don't want to modify the original array, make a copy of it to sort, then work out your hand using relatively simple code.
define sorting?
a modified counting sort would give you the answer without being a direct sort.
the final answer is something like &countarray[last found card]- &countarray[first found card] == 5.
you can also do something math screwy most likely.
what happens if you subtract the smallest card from all the cards and add it up?
5,6,7,8,9
-5
0,1,2,3,4 -> added is 10
3,5,7,9,10
0,2,4,6,7 -> 19, not 10...
Last edited on
Rather than sorting, put the die into a bitmap. Shift the bitmap down the LSB is 1 to reduce the values that must be checked. This program reads 5 white-space separated numbers at a time and says whether they are a low straight or not. The input numbers are assumed to be in the range [1,6]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>

using std::cin;
using std::cout;

int main(int argc,char* argv[])
{  
    unsigned die;
    unsigned bits;
    while (cin >> die) {
	bits = 1 << die;
	for (unsigned i=0; i<4; ++i) {
	    cin >> die;
	    bits |= 1 << die;
	}
	cout << "Before bits = " << std::hex << bits;

	// Shift the bits down until the LSB is 1
	while (!(bits & 1)) bits >>= 1;
	cout << " After bits = " << std::hex << bits << '\n';

	// Now there are just 3 possible low straights
	if (bits == 0b1111 || bits == 0b111101 || bits == 0b101111) {
	    cout << "Low straight\n";
	} else {
	    cout << "Not a low straight\n";
	}
    }
}


Topic archived. No new replies allowed.