Reputation: 193
I've been making a poker simulator and I've managed to make a function which can identify an array which has consecutive numbers.
def straightCheck(playerHand):
playerHand.sort()
print(playerHand)
for i in range(len(playerHand)-1):
if playerHand[i] != playerHand [i+1] - 1:
return False
print(handstrength)
return True
print(handstrength)
The only problem is that I want the function to identify 5 consecutive numbers in the array when the length of the array is greater than 5. For example: I want the array [1,2,3,4,5,6,7]
to return True but i also want the array [1,3,4,5,6,7,9]
to return True.
Upvotes: 1
Views: 70
Reputation: 111
def straightCheck(playerHand):
playerHand.sort()
print(playerHand)
count = 0;
for i in range(len(playerHand)-1):
if playerHand[i] == playerHand [i+1] - 1:
count += 1
if count >= 5:
return True
else:
count = 0
return False
Upvotes: -2
Reputation: 149736
If you have a working function, you could just process all the 5-card sets in a loop:
for i in range(len(values) - 4):
is_straight = straightCheck(values[i:i+5])
if is_straight:
break
print(is_straight)
Upvotes: 1
Reputation: 3091
Right now you check if numbers are not consecutive and then return false. I think you could better check if numbers are consecutive and if so raise a counter and if not then reset it. That way you know how many numbers are consecutive. If that is 5 or higher you should return True.
Upvotes: 1
Reputation: 4134
You're returning False too soon. Instead, you could keep a running tally of the amount of consecutive numbers you've seen so far, and reset it when you come across a number that breaks the streak.
def straightCheck(playerHand):
playerHand.sort()
tally = 1
for i in range(len(playerHand)-1):
if playerHand[i] != playerHand [i+1] - 1:
tally = 0
tally += 1
if tally >= 5:
return True
return False
Upvotes: 2