0

I am trying to make a multiplayer poker game in Node.js, and I've been having a lot of issues lately. This is a major one. This code is supposed to identify a Straight hand from an array. However, my code apparently isn't universal. I made 2 arrays as test cases and different results are produced when even just of the arrays is identified as a straight. Please help.    

Here's the code: 

    var arr = [9,1,2,11,8,12,10];   // first array
    var arr2 = [9,1,8,4,5,3,2];     // second array
    var straight = [];

    // Removes duplicate elements in an array
    /* example:
    array = removeDuplicates(array)
    */
    function removeDuplicates(arr){
        let unique_array = []
        for(let i = 0;i < arr.length; i++){
            if(unique_array.indexOf(arr[i]) == -1){
                unique_array.push(arr[i])
            }
        }
        return unique_array
    }

    //Sorts the array
    arr.sort(function(a,b){return b-a});

    //Removes duplicates
    arr = removeDuplicates(arr);

    // Displays sorted and cleaned up array
    console.log(arr)

    /*Basic translation: loops through the array
    and if the difference between the a term and
    the term after it is 1, it will append it to the 
    array 'straight'. It will break if the difference 
    is greater than 1. Then it will remove those elements
    from the original array and retry to append consecutive
    elements in the 'straight' array.
    */
    for (var i=1; i<arr.length+1; i++) {
      if (arr[i-1] - arr[i] === 1) {
        straight.push(arr[i-1],arr[i]); // error occurs at this line
      } else if (arr[i-1] - arr[i] > 1){
        break; }

        if (straight.length === 2) {
          arr.splice(arr.indexOf(straight[0]),1)
          arr.splice(arr.indexOf(straight[1]),1)
          straight = [];

          for (var i=1; i<arr.length; i++) {
            if (arr[i-1] - arr[i] === 1) {
              straight.push(arr[i-1],arr[i]);
            }
          }
        }
    };

    // There are duplicates and I don't know why sometimes
    straight = removeDuplicates(straight)
    console.log(straight);

This doesn't work for some reason. But it will work fine ONLY for the first array if you change

straight.push(arr[i-1],arr[i]); 

to

straight.push(arr[i-1],arr[i],arr[i]);

It works ONLY for the second array if you switch the variable names:

  var arr2 = [9,1,2,11,8,12,10];   // first array
  var arr = [9,1,8,4,5,3,2];     // second array

and run the code without further changes, I don't know why it does this. I even went as far as logging the boolean

    arr[i-1] - arr[i] === 1

to the console (in the loop, I mean), and it comes out true four times in a row (going through the first 5 indexes of the array), so I don't know why it stops at 11 for the first array and decides 11-10 isn't 1.

sumguy37
  • 43
  • 7
  • I'm wondering why you need the variable `straight`. Don't you just need to check that each element was greater than the last in an sorted array? – Spencer Wieczorek Jun 02 '18 at 03:19
  • Yes, but I created the array `straight` so that I could manipulate the elements in the first array and put it into `straight` so that I could later reuse the original array if I needed to. – sumguy37 Jun 02 '18 at 03:38

2 Answers2

1

your logic is a bit hard to follow - I think the issue you're seeing is due to clearing the straight array in the if (straight.length === 2) part. Here's my shot at simplifying things:

const isStraight = a => { 
  const uniq = a.filter((val, idx) => a.indexOf(val) === idx);
  uniq.sort((a, b) => a-b); 
  const tries = uniq.length - 4;
  for (var i=0; i<tries; i++) {
    if (uniq[i + 4] - uniq[i] === 4) {
      return true;
    }
  }
  return false; 
}

console.log(isStraight([9,1,2,11,8,12,10]));
console.log(isStraight([9,1,8,4,5,3,2]));
console.log(isStraight([2,5,4,3,6,8,7]));
console.log(isStraight([2,5,4,3,6,8,7,10]));
console.log(isStraight([2,5,2,4,7,3,6,8,8,8,8,7]));
console.log(isStraight([1,2,3,4,6,7,8,9,11,12,13,13]))
ic3b3rg
  • 14,629
  • 4
  • 30
  • 53
  • Thank you, but that doesn't really work with what I need it to do.... and I'm somewhat new to js so I don't exactly know how to fit your code to help me.... The `straight` array is only supposed to be cleared after removing the non-consecutive values from the original array (which are consecutive in the first array). Thank you, though. – sumguy37 Jun 02 '18 at 04:12
  • my bad - I forgot it's poker, so you only need 5 in a row - I updated my answer – ic3b3rg Jun 02 '18 at 04:30
  • Thanks a million, I've been at this for hours. I understand your code too, it's great. Basically looks for values within a range of 4. – sumguy37 Jun 02 '18 at 04:48
0

let arr = [9,1,2,11,8,12,10];

function checkStraight(arr) {
  let answer = [];
  
  if(arr.length < 5)
    return false;

  arr = [...new Set(arr)];
  arr.sort(function(a,b){return b-a});

  for(let index=0; index < arr.length; index++){
    if(answer.length === 5) break;

    if(answer.length === 0){
      answer.push(arr[index])
    }

    if(answer[answer.length-1] - arr[index] === 1){
     answer.push(arr[index]);
    }else{
      answer = [];
      answer.push(arr[index])
    }
  } 
  
  return answer
}

console.log(checkStraight(arr));

You can try to run thru the code, should be quite simple. Basically instead of comparing between elements inside own array, we compare between two array, and conditionally push the matched straight card into new array

**assumptions: ** Since we are playing poker, assuming once 5 consecutive card been found, it's consider a straight and no further checking needed

Isaac
  • 12,042
  • 16
  • 52
  • 116