Yahtzee game

Hi everyone,

I had some time to kill and thought i’d try my hand at a variation of a dice-rolling game. I have played it a bit, and thought I might share it. I have some additional plans for it – adding the ability to have multiple players, and I think I will also use a plain-text TextEdit window to make a scoresheet that the script will then update. If someone else has ideas or suggestions I’d love to hear them. Also let me know if you have any fun playing it =)



set rollagain to 1
repeat until rollagain is 0
	set Scoring_Options to {"1. Ones", "2. Twos", "3. Threes", "4. Fours", "5. Fives", "6. Sixes", "A. Three of a Kind", "B. Four of a Kind", "C. Full House", "D. Straight", "E. Chance", "Z. YAHTZEE"}
	set Current_Score to {"-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-"}
	
	
	set dicerolls to {1, 2, 3, 4, 5, 6}
	
	--main game loop
	repeat with current_round from 1 to 12
		set straight_flag to 0
		set five_of_a_kind_flag to 0
		set four_of_a_kind_flag to 0
		set three_of_a_kind_flag to 0
		set two_of_a_kind_flag to 0
		set check_for_three to 0
		set fullhouse_flag to 0
		
		--roll the dice!
		set thedice to {}
		repeat 5 times
			set thedice to thedice & some item of dicerolls
		end repeat
		
		set current_reroll_count to 0
		repeat while current_reroll_count is not 2
			set current_reroll_count to current_reroll_count + 1
			set DieRerollList to {}
			set currdie to 0
			repeat with currdice in items of thedice
				set currdie to 1 + currdie
				set DieRerollList to DieRerollList & ("Die " & (ASCII character (currdie + 48)) & ": [" & (currdice as text) & "]")
			end repeat
			set RerollList to choose from list DieRerollList with prompt "Round #" & (current_round as text) & ": Roll #" & (current_reroll_count as text) & ": Please select which dice to reroll. Use COMMAND for multiple selections" OK button name "Roll em!" cancel button name "KEEP ALL" with multiple selections allowed
			if RerollList is not false then
				repeat with i in items of RerollList
					set current_reroll to (character 5 of i) as number
					set item current_reroll of thedice to some item of dicerolls
				end repeat
			else
				set current_reroll_count to 2
			end if
		end repeat
		
		--now figure out scoring possibilities!
		--first check for three/four of a kind, full house, yahtzee
		repeat with i from 1 to 6
			set current_multiple_count to 0
			repeat with currdice in items of thedice
				if i is (currdice as integer) then
					
					set current_multiple_count to current_multiple_count + 1
					--display dialog "checking " & i & " - total count: " & current_multiple_count
				end if
			end repeat
			if current_multiple_count is 2 then
				set two_of_a_kind_flag to 1
			end if
			if current_multiple_count is 3 then
				set three_of_a_kind_flag to 1
			end if
			if current_multiple_count is 4 then
				set four_of_a_kind_flag to 1
			end if
			if current_multiple_count is 5 then
				set five_of_a_kind_flag to 1
			end if
		end repeat
		if two_of_a_kind_flag is 1 and three_of_a_kind_flag is 1 then
			set fullhouse_flag to 1
		end if
		
		--now check to see if we have a straight
		set low to 1
		set high to 5
		repeat 2 times
			set running_count to 0
			repeat with i from low to high
				if (i as integer) is in thedice then
					set running_count to running_count + 1
				end if
			end repeat
			if running_count is 5 then
				set straight_flag to 1
			end if
			set low to 1 + low
			set high to 1 + high
		end repeat
		
		--now assign scores
		set acceptable_score to 0
		repeat until acceptable_score is 1
			
			--set up scoreboard
			set score_output to {}
			repeat with i from 1 to 12
				set score_output to score_output & ((item i of Scoring_Options & ": " & ((item i of Current_Score) as text)) as text)
			end repeat
			set sorted_die_rolls to BubbleSort(thedice)
			set die_roll_output to "You rolled: "
			repeat with i from 1 to 5
				if i is not 5 then
					set die_roll_output to die_roll_output & ((item i of sorted_die_rolls) as text) & ","
				else
					set die_roll_output to die_roll_output & ((item i of sorted_die_rolls) as text)
				end if
			end repeat
			
			set pick_score to (choose from list score_output with prompt "Round #" & (current_round as text) & ": " & die_roll_output & ". Please choose how to score your roll!") as text
			
			
			--number-based score
			try
				if ((character 1 of pick_score) as integer) is greater than 0 and ((character 1 of pick_score) as integer) is less than 7 then
					set scoring_number to (character 1 of pick_score) as integer
					set current_scoring_number to 0
					repeat with i in items of thedice
						if (i as integer) is equal to scoring_number then
							set current_scoring_number to current_scoring_number + scoring_number
						end if
					end repeat
				end if
				if item scoring_number of Current_Score is "-" then
					set item scoring_number of Current_Score to current_scoring_number
					set acceptable_score to 1
				end if
			end try
			
			--case-by-case score
			if (character 1 of pick_score) is "A" then
				if three_of_a_kind_flag is 1 then
					if item 7 of Current_Score is "-" then
						set item 7 of Current_Score to 15
						set acceptable_score to 1
					end if
				end if
				if three_of_a_kind_flag is 0 and item 7 of Current_Score is "-" then
					set double_check to button returned of (display dialog "Do you really want to zero out this score?" buttons {"Yes", "No"})
					if double_check is "Yes" then
						set item 7 of Current_Score to 0
						set acceptable_score to 1
					end if
				end if
			end if
			
			if (character 1 of pick_score) is "B" then
				if four_of_a_kind_flag is 1 then
					if item 8 of Current_Score is "-" then
						set item 8 of Current_Score to 25
						set acceptable_score to 1
					end if
				end if
				if four_of_a_kind_flag is 0 and item 8 of Current_Score is "-" then
					set double_check to button returned of (display dialog "Do you really want to zero out this score?" buttons {"Yes", "No"})
					if double_check is "Yes" then
						set item 8 of Current_Score to 0
						set acceptable_score to 1
					end if
				end if
			end if
			
			if (character 1 of pick_score) is "C" then
				if fullhouse_flag is 1 then
					if item 9 of Current_Score is "-" then
						set item 9 of Current_Score to 20
						set acceptable_score to 1
					end if
				end if
				if fullhouse_flag is 0 and item 9 of Current_Score is "-" then
					set double_check to button returned of (display dialog "Do you really want to zero out this score?" buttons {"Yes", "No"})
					if double_check is "Yes" then
						set item 9 of Current_Score to 0
						set acceptable_score to 1
					end if
				end if
			end if
			
			if (character 1 of pick_score) is "D" then
				if straight_flag is 1 then
					if item 10 of Current_Score is "-" then
						set item 10 of Current_Score to 30
						set acceptable_score to 1
					end if
				end if
				if straight_flag is 0 and item 10 of Current_Score is "-" then
					set double_check to button returned of (display dialog "Do you really want to zero out this score?" buttons {"Yes", "No"})
					if double_check is "Yes" then
						set item 10 of Current_Score to 0
						set acceptable_score to 1
					end if
				end if
			end if
			
			if (character 1 of pick_score) is "E" then
				if item 11 of Current_Score is "-" then
					set running_total to 0
					repeat with i in items of thedice
						set running_total to running_total + (i as integer)
					end repeat
				end if
				set item 11 of Current_Score to running_total
				set acceptable_score to 1
			end if
			
			
			
			if (character 1 of pick_score) is "Z" then
				if five_of_a_kind_flag is 1 then
					if item 12 of Current_Score is "-" then
						set item 12 of Current_Score to 50
						set acceptable_score to 1
					end if
				end if
				if five_of_a_kind_flag is 0 and item 12 of Current_Score is "-" then
					set double_check to button returned of (display dialog "Do you really want to zero out this score?" buttons {"Yes", "No"})
					if double_check is "Yes" then
						set item 12 of Current_Score to 0
						set acceptable_score to 1
					end if
				end if
			end if
			
			
		end repeat
		
		--end of main game loop
	end repeat
	
	--calculate final score
	set final_score to 0
	--check for bonus
	set bonus_score to 0
	set bonus_text to ""
	repeat with i from 1 to 6
		set bonus_score to bonus_score + ((item i of Current_Score) as integer)
	end repeat
	if bonus_score is greater than 62 then
		set final_score to 35
		set bonus_text to "Congratulations, you also earned the 35-point bonus."
	end if
	
	
	repeat with i in items of Current_Score
		set final_score to final_score + (i as integer)
	end repeat
	display dialog "Game Over! " & bonus_text & " Final score: " & final_score
	
	set playagain to button returned of (display dialog "Would you like to play again?" buttons {"yes", "no"})
	if playagain is "no" then
		set rollagain to 0
	end if
end repeat



on BubbleSort(theList)
	if class of theList is list then
		set theSize to length of theList
		repeat with i from 1 to theSize
			repeat with j from 2 to (theSize - i + 1)
				if ((item (j - 1) of theList) > (item j of theList)) then
					set temp to (item (j - 1) of theList)
					set (item (j - 1) of theList) to (item j of theList)
					set (item j of theList) to temp
				end if
			end repeat
		end repeat
		return theList
	else
		return false
	end if
end BubbleSort


I suggest you make changes so that the user can exit whenever he wants by pressing cancel button. Currently pressing cancel keeps popping the dialog box and I found no way to exit it other than quitting Script Editor.

edit:
maybe you want us to save it as an application

And when you’ve made the changes Chris suggests, you should post this to Code Exchange; AppleScript | OS X is really for questions.