Problem with elseif block

Discuss using and improving Lua and the Lua Player specific to the PSP.

Moderators: Shine, Insert_witty_name

Post Reply
dandev
Posts: 7
Joined: Fri Jun 24, 2005 11:08 pm
Location: Canada

Problem with elseif block

Post by dandev »

Hi, I am working on a poker type of game, and I have this section with a list of "elseifs":

Code: Select all

function findBestHand() 

	hand_type = NOTHING
	str = "nothing"

	-- Search in this order

	if isRoyalFlush() == 1 then
		hand_type = ROYAL_FLUSH
		str = "Royal Flush"

	elseif isStraightFlush() == 1 then
		hand_type = STRAIGHT_FLUSH
		str = "Straight Flush"

	elseif isFourOfKind() == 1 then
		hand_type = FOUR_KIND
		str = "Four of a Kind"

	elseif isFullHouse() == 1 then
		hand_type = FULL_HOUSE
		str = "Full House"

	elseif isFlush() == 1 then
		hand_type = FLUSH
		str = "Flush"

	elseif isStraight() == 1 then 
		hand_type = STRAIGHT
		str = "Straight"

	elseif isThreeOfKind() == 1 then
		hand_type = THREE_KIND
		str = "Three of a Kind"

	elseif isTwoPairs() == 1 then
		hand_type = TWO_PAIRS
	        str = "Two Pairs"
		
	elseif isOnePair() == 1 then
		hand_type = ONE_PAIR
		str = "One Pair"

	else
		hand_type = getHighCard()
		str = "High Card: " .. hand_type
	end
	
	y = 25 + card_back:height()
	screen:print(5, y, "You got " .. str, white) 

	screen.waitVblankStart()
	screen:flip()

	return hand_type

end -- findBestHand
I've only finished the functions called isTwoPairs(), isOnePair(), and getHighCard(). The rest are just stubs.

The problem is, when I run this function on my PSP, it appears that whenever I have two pairs in my hand, it prints "You got Two Pairs"; but then, ON TOP of that string, it also overlaps it with "You got One Pair"! So it seems like when the Two Pair condition is met, it still checks the next condition, which is One Pair (which would also be met), and then prints both of them!

When I don't have two pairs, but have one pair instead, it correctly prints the One Pair string only. Likewise, when I don't have a single pair, it prints the High Card string only.

Any idea what I may have done wrong in the two pair case??
dandev
Posts: 7
Joined: Fri Jun 24, 2005 11:08 pm
Location: Canada

Post by dandev »

Oops, sorry.. I figured it out. I forgot to clear some flags, so whenever it looped over to this function again it stopped satisfying the two pair case.

My apologies.. you can delete this post.
Post Reply