Simple rock, paper, scissors script

repeat
	set player to display dialog "Rock, Paper, Scissors?" buttons {"Rock", "Paper", "Scissors"}
	set player_choice to (button returned of the result) as string
	set cpu to {"Rock", "Paper", "Scissors"}
	set the list_count to the count of cpu
	set pick to random number from 1 to list_count
	set the_result to item pick of cpu as string
	display dialog "You picked " & player_choice & "."
	display dialog "The computer picked " & the_result & "."
	if player_choice is equal to "Rock" and the_result is equal to "Rock" then
		display dialog "Tie Game!"
	else if player_choice is equal to "Rock" and the_result is equal to "Paper" then
		display dialog "You lose!"
	else if player_choice is equal to "Rock" and the_result is equal to "Scissors" then
		display dialog "You win!"
	else if player_choice is equal to "Paper" and the_result is equal to "Paper" then
		display dialog "Tie Game!"
	else if player_choice is equal to "Paper" and the_result is equal to "Scissors" then
		display dialog "You lose!"
	else if player_choice is equal to "Paper" and the_result is equal to "Rock" then
		display dialog "You win!"
	else if player_choice is equal to "Scissors" and the_result is equal to "Scissors" then
		display dialog "Tie Game!"
	else if player_choice is equal to "Scissors" and the_result is equal to "Rock" then
		display dialog "You lose!"
	else if player_choice is equal to "Scissors" and the_result is equal to "Paper" then
		display dialog "You win!"
	end if
	set again_dialog to display dialog "Play Again?" buttons {"Yes", "No"} default button 1
	set play_again to the button returned of again_dialog
	if play_again is not equal to "Yes" then
		exit repeat
	end if
end repeat

I haven’t fully tested it, but this may work:

set choiceList to {"Rock", "Paper", "Scissors"}
tell choiceList to set choiceText to beginning & ", " & middle item & ", or " & end & "?"

repeat
	set playerChoice to button returned of (display dialog choiceText buttons choiceList)
	tell playerChoice to if it is beginning of choiceList then
		set playerIndex to 1
	else if it is middle item of choiceList then
		set playerIndex to 2
	else
		set playerIndex to 3
	end if
	
	set computerChoice to random number from 1 to 3
	
	set resultText to item ((playerIndex - computerChoice + 3) mod 3 + 1) of {"You Tie!", "You Win!", "You Lose!"}
	
	display dialog "Player: " & playerChoice & return & "Computer: " & item computerChoice of choiceList & return & return & resultText
	if button returned of (display dialog "Play Again?" buttons {"Yes", "No"}) is "No" then error number -128
end repeat

How about:

property modeNames : {"rock", "paper", "scissors"}

set myChoice to button returned of (display dialog "Throw down." buttons modeNames)
set cpuChoice to item (random number from 1 to 3) of modeNames

if myChoice = cpuChoice then
	display dialog "It's a draw!"
else
	set i to 1
	repeat with i from 1 to (count of modeNames)
		if myChoice = (item i of modeNames) then
			if i = 3 then
				set ind to 0
			else
				set ind to i
			end if
			set i to 1
			exit repeat
		else
			set i to (i + 1)
		end if
	end repeat
	repeat with i from 1 to (count of modeNames)
		if cpuChoice = (item i of modeNames) then
			if i = (ind + 1) then
				set winner to "The computer wins!"
			else
				set winner to "You beat the computer!"
			end if
			display dialog winner
			exit repeat
		end if
		set i to (i + 1)
	end repeat
end if

Or perhaps this form using mod:

repeat
	set Picks to {"Paper", "Scissors", "Rock", 1, 2, 3}
	set You to button returned of (display dialog "Please choose from Paper, Scissors or Rock?" buttons items 1 thru 3 of Picks)
	repeat with k from 1 to 3
		if You is item k of Picks then set Yn to item (k + 3) of Picks <-- Looking back, the error pointed out in kai's post below is that the 3 here should be a 4!
	end repeat
	set Cn to some item of {1, 2, 3}
	set Comp to item Cn of Picks
	if Cn = Yn then
		display dialog "Tie - you both picked " & You
	else if (Yn mod 3) < (Cn mod 3) then -- this is a ring: paper < scissors < rock < paper, etc.
		display dialog "You lose, " & Comp & " beats " & You
	else
		display dialog "You win, " & You & " beats " & Comp
	end if
	if button returned of (display dialog "Again?" buttons {"No", "Yes"} default button 2) is "No" then exit repeat
end repeat

My first thought was to use some “previous position” math that I picked up (here) a while ago.

set choiceList to {"Rock", "Paper", "Scissors"}
set choiceCount to count result

repeat	
	display dialog "Rock, Paper, Scissors?" buttons choiceList
	set playerChoice to button returned of result
	
	random number from 1 to choiceCount
	set cpuChoice to item (result) of choiceList
	
	if playerChoice = cpuChoice then
		set resultMsg to "Tie game."
	else
		-- Get position of playerChoice in choiceList
		repeat with playerIndex from 1 to choiceCount
			if choiceList's item playerIndex is playerChoice then exit repeat
		end repeat
		
		-- Get "previous" position in choiceList
		get (playerIndex + choiceCount - 2) mod choiceCount + 1
		
		if (item (result) of choiceList) = cpuChoice then
			set resultMsg to "You win!"
		else
			set resultMsg to "You lose!"
		end if
	end if
	
	display dialog "You: " & playerChoice & return & "CPU: " & cpuChoice & return & return & resultMsg buttons {"Cancel", "Play Again"} default button 2
end repeat

I borrowed Bruce’s script (just because it’s the last one) to make the game more familiar.

set choiceList to {"Rock", "Paper", "Scissors"}
set choiceCount to count result

repeat
	
	display dialog "One.." buttons choiceList giving up after 1
	display dialog "Two.." buttons choiceList giving up after 1
	display dialog "Three.." buttons choiceList giving up after 1
	display dialog "SHOOT!" buttons choiceList
	set playerChoice to button returned of result
	
	random number from 1 to choiceCount
	set cpuChoice to item (result) of choiceList
	
	if playerChoice = cpuChoice then
		set resultMsg to "Tie game."
	else
		-- Get position of playerChoice in choiceList
		repeat with playerIndex from 1 to choiceCount
			if choiceList's item playerIndex is playerChoice then exit repeat
		end repeat
		
		-- Get "previous" position in choiceList
		get (playerIndex + choiceCount - 2) mod choiceCount + 1
		
		if (item (result) of choiceList) = cpuChoice then
			set resultMsg to "You win!"
		else
			set resultMsg to "You lose!"
		end if
	end if
	
	display dialog "You: " & playerChoice & return & "CPU: " & cpuChoice & return & return & resultMsg buttons {"Cancel", "Play Again"} default button 2
end repeat

EDIT: My wife says the above should start like so…

set choiceList to {"Rock", "Paper", "Scissors"}
display dialog return buttons {"One.."} with icon 1 giving up after 1
	display dialog return buttons {"Two.."} with icon 1 giving up after 1
	display dialog return buttons {"Three.."} with icon 1 giving up after 1
	
	display dialog "SHOOT!" buttons choiceList with icon 2

…but she says the following is also acceptable.

Or maybe it should start like this:

set choiceList to {"Rock", "Paper", "Scissors"}
say "Wontoothree Shoot!" using "Junior"
display dialog "" buttons choiceList

lol! it’s the official list of different ways to make such a simple game!

Indeed. Some interesting variations on a theme there, guys. :slight_smile:

Just a minor observation, but I’m not sure that works quite as intended, Adam. (Hint for bad losers: always choose “Scissors” in this game.) :wink:

However, the mod function can be used to good effect in this type of situation. (And, having knocked up the following example independently, I now see that it has some similarities to the approach suggested earlier by Qwerty - but I’ll throw it into the pot, anyway…)

set l to {"Paper", "Scissors", "Rock"}
repeat
	say "1 2 3 shoot!" using "Junior"
	set {y, m} to {button returned of (display dialog "Choose your weapon:" buttons l), some item of {1, 2, 3}}
	repeat with i from 1 to 3
		if l's item i is y then exit repeat
	end repeat
	display dialog "You chose: " & y & return & "Mac chose: " & l's item m & return & return & "You " & {"draw", "win", "lose"}'s ¬
		item ((i - m + 3) mod 3 + 1) & "!" buttons {"Play Later", "Play Again"} cancel button 1 default button 2
end repeat

i’ll add to the mix. anyone want to keep score?


set choiceList to {"Rock", "Paper", "Scissors"}
set choiceCount to count result
set yourWins to 0
set cpuWins to 0

repeat
	
	display dialog "One.." buttons choiceList giving up after 1
	display dialog "Two.." buttons choiceList giving up after 1
	display dialog "Three.." buttons choiceList giving up after 1
	display dialog "SHOOT!" buttons choiceList
	set playerChoice to button returned of result
	
	random number from 1 to choiceCount
	set cpuChoice to item (result) of choiceList
	
	if playerChoice = cpuChoice then
		set resultMsg to "Tie game."
	else
		-- Get position of playerChoice in choiceList
		repeat with playerIndex from 1 to choiceCount
			if choiceList's item playerIndex is playerChoice then exit repeat
		end repeat
		
		-- Get "previous" position in choiceList
		get (playerIndex + choiceCount - 2) mod choiceCount + 1
		
		if (item (result) of choiceList) = cpuChoice then
			set resultMsg to "You win!"
			set yourWins to (yourWins + 1)
		else
			set resultMsg to "You lose!"
			set cpuWins to (cpuWins + 1)
		end if
	end if
	
	set theResult to button returned of (display dialog "You: " & playerChoice & return & "CPU: " & cpuChoice & return & return & resultMsg buttons {"Score", "Play Again", "Cancel"} default button 2)
	if theResult is "Score" then
		if yourWins > cpuWins then
			display dialog "Your Wins:  " & yourWins & return & "CPU Wins:  " & cpuWins & return & return & "You are currently winning!" & return & return & "Would you like to play again?" buttons {"Play Again", "Cancel"} default button 1
			say "Huray!" using "Junior"
		else if yourWins < cpuWins then
			display dialog "Your Wins:  " & yourWins & return & "CPU Wins:  " & cpuWins & return & return & "You are currently losing!" & return & return & "Would you like to play again?" buttons {"Play Again", "Cancel"} default button 1
			say "uh oh!" using "Junior"
		else
			display dialog "Your Wins:  " & yourWins & return & "CPU Wins:  " & cpuWins & return & return & "You are currently tied!" & return & return & "Would you like to play again?" buttons {"Play Again", "Cancel"} default button 1
			say "Wontoothree Shoot!" using "Junior"
		end if
	end if
end repeat

ok, one more addition. the game ends at 21. if it ends earlier, a final score is given and a win/loss. and we recursively call the game if the player would like to ‘play again!’:

on run
	myGame()
end run

on myGame()
	set choiceList to {"Rock", "Paper", "Scissors"}
	set choiceCount to count result
	set yourWins to 0
	set cpuWins to 0
	set catchEnd to 0
	
	repeat until (yourWins = 21) or (cpuWins = 21) or (catchEnd = "End")
		
		display dialog "One.." buttons choiceList giving up after 1
		display dialog "Two.." buttons choiceList giving up after 1
		display dialog "Three.." buttons choiceList giving up after 1
		display dialog "SHOOT!" buttons choiceList
		set playerChoice to button returned of result
		
		random number from 1 to choiceCount
		set cpuChoice to item (result) of choiceList
		
		if playerChoice = cpuChoice then
			set resultMsg to "Tie game."
		else
			-- Get position of playerChoice in choiceList
			repeat with playerIndex from 1 to choiceCount
				if choiceList's item playerIndex is playerChoice then exit repeat
			end repeat
			
			-- Get "previous" position in choiceList
			get (playerIndex + choiceCount - 2) mod choiceCount + 1
			
			if (item (result) of choiceList) = cpuChoice then
				set resultMsg to "You win!"
				set yourWins to (yourWins + 1)
			else
				set resultMsg to "You lose!"
				set cpuWins to (cpuWins + 1)
			end if
		end if
		
		set theResult to button returned of (display dialog "You: " & playerChoice & return & "CPU: " & cpuChoice & return & return & resultMsg buttons {"Score", "Play Again", "Cancel"} default button 2)
		if theResult is "Score" then
			if yourWins > cpuWins then
				set catchEnd to button returned of (display dialog "Your Wins:  " & yourWins & return & "CPU Wins:  " & cpuWins & return & return & "You are currently winning!" & return & return & "Would you like to play again?" buttons {"Play Again", "End"} default button 1)
				say "Huray!" using "Junior"
			else if yourWins < cpuWins then
				set catchEnd to button returned of (display dialog "Your Wins:  " & yourWins & return & "CPU Wins:  " & cpuWins & return & return & "You are currently losing!" & return & return & "Would you like to play again?" buttons {"Play Again", "End"} default button 1)
				say "uh oh!" using "Junior"
			else
				set catchEnd to button returned of (display dialog "Your Wins:  " & yourWins & return & "CPU Wins:  " & cpuWins & return & return & "You are currently tied!" & return & return & "Would you like to play again?" buttons {"Play Again", "End"} default button 1)
				say "Wontoothree Shoot!" using "Junior"
			end if
		end if
	end repeat
	if yourWins > cpuWins then
		set keepPlay to button returned of (display dialog "FINAL RESULTS:" & return & return & "Your Wins:  " & yourWins & return & "CPU Wins:  " & cpuWins & return & "You Won!" buttons {"Play Again!", "Cancel"} default button 1)
		say "Huray!" using "Junior"
	else if yourWins < cpuWins then
		set keepPlay to button returned of (display dialog "FINAL RESULTS:" & return & return & "Your Wins:  " & yourWins & return & "CPU Wins:  " & cpuWins & return & "You Lost!" buttons {"Play Again!", "Cancel"} default button 1)
		say "uh, oh!" using "Junior"
	else
		set keepPlay to button returned of (display dialog "FINAL RESULTS:" & return & return & "Your Wins:  " & yourWins & return & "CPU Wins:  " & cpuWins & return & "You Tied!" buttons {"Play Again!", "Cancel"} default button 1)
		say "goodbye!" using "Junior"
	end if
	if keepPlay is "Play Again!" then
		myGame()
	end if
end myGame

lol, actually this is my final code…and thats final!:stuck_out_tongue:

set user_score to 0
set cpu_score to 0
repeat
	set rps_list to {"Rock", "Paper", "Scissors"}
	set the list_count to the count of rps_list
	set pick to random number from 1 to list_count
	set cpu_choice to item pick of rps_list as string
	set rps_dialog to display dialog "Rock, Paper, Scissors?" buttons {"Rock", "Paper", "Scissors"}
	set user_choice to (button returned of rps_dialog) as string
	display dialog "Rock..." giving up after 1
	display dialog "Paper..." giving up after 1
	display dialog "Scissors!" giving up after 1
	if user_choice is equal to "Rock" and cpu_choice is equal to "Rock" then
		display dialog "Player chose: " & user_choice & return & "Computer chose: " & cpu_choice & return & return & "Tie Game"
	else if user_choice is equal to "Rock" and cpu_choice is equal to "Paper" then
		display dialog "Player chose: " & user_choice & return & "Computer chose: " & cpu_choice & return & return & "You lose!"
		set cpu_score to cpu_score + 1
	else if user_choice is equal to "Rock" and cpu_choice is equal to "Scissors" then
		display dialog "Player chose: " & user_choice & return & "Computer chose: " & cpu_choice & return & return & "You win!"
		set user_score to user_score + 1
	else if user_choice is equal to "Paper" and cpu_choice is equal to "Paper" then
		display dialog "Player chose: " & user_choice & return & "Computer chose: " & cpu_choice & return & return & "Tie Game"
	else if user_choice is equal to "Paper" and cpu_choice is equal to "Scissors" then
		display dialog "Player chose: " & user_choice & return & "Computer chose: " & cpu_choice & return & return & "You lose!"
		set cpu_score to cpu_score + 1
	else if user_choice is equal to "Paper" and cpu_choice is equal to "Rock" then
		display dialog "Player chose: " & user_choice & return & "Computer chose: " & cpu_choice & return & return & "You win!"
		set user_score to user_score + 1
	else if user_choice is equal to "Scissors" and cpu_choice is equal to "Scissors" then
		display dialog "Player chose: " & user_choice & return & "Computer chose: " & cpu_choice & return & return & "Tie Game"
	else if user_choice is equal to "Scissors" and cpu_choice is equal to "Rock" then
		display dialog "Player chose: " & user_choice & return & "Computer chose: " & cpu_choice & return & return & "You lose!"
		set cpu_score to cpu_score + 1
	else if user_choice is equal to "Scissors" and cpu_choice is equal to "Paper" then
		display dialog "Player chose: " & user_choice & return & "Computer chose: " & cpu_choice & return & return & "You win!"
		set user_score to user_score + 1
	end if
	set play_again to display dialog "Play again?" buttons {"Yes", "No"}
	set again_choice to button returned of play_again
	if again_choice is not equal to "Yes" then
		if user_score is greater than cpu_score then
			display dialog "Final Score" & return & "You: " & user_score & return & "Computer: " & cpu_score & return & "You are the winner!"
		else if user_score is less than cpu_score then
			display dialog "Final Score" & return & "You: " & user_score & return & "Computer: " & cpu_score & return & "You are the loser!"
		else if user_score is equal to cpu_score then
			display dialog "Final Score" & return & "You: " & user_score & return & "Computer: " & cpu_score & return & "Looks like a tie game!"
			
			exit repeat
		end if
	end if
end repeat

I like yours Kai, but haven’t sorted why mine has a bias for scissors. You spotted it and I can’t - it’s like trying to sort out mistakes in your own document :confused:

Who’s going to make this into an AppleScript Studio project?

lol, I WILL! I have been looking for a new project to start but I never thought of this. Lol, it’s gunna be kinda of funny making an AS Studio app out of this:P Well it definaely won’t be very hard, but thats good for me because I am not that fantasti of a programmer:P

ok,ok i lied last time wasn’t the final version, this is:P, and now I am going to work on the AS Studio version:D

set user_score to 0
set cpu_score to 0
repeat
	set rps_list to {"Rock", "Paper", "Scissors"}
	set the list_count to the count of rps_list
	set pick to random number from 1 to list_count
	set cpu_choice to item pick of rps_list as string
	set rps_dialog to display dialog "Rock, Paper, Scissors?" buttons {"Rock", "Paper", "Scissors"}
	set user_choice to (button returned of rps_dialog) as string
	display dialog "Rock..." giving up after 1
	display dialog "Paper..." giving up after 1
	display dialog "Scissors!" giving up after 1
	if user_choice is equal to cpu_choice then
		display dialog "Player chose: " & user_choice & return & "Computer chose: " & cpu_choice & return & return & "Tie Game"
	else if user_choice is equal to "Rock" and cpu_choice is equal to "Paper" then
		display dialog "Player chose: " & user_choice & return & "Computer chose: " & cpu_choice & return & return & "You lose!"
		set cpu_score to cpu_score + 1
	else if user_choice is equal to "Rock" and cpu_choice is equal to "Scissors" then
		display dialog "Player chose: " & user_choice & return & "Computer chose: " & cpu_choice & return & return & "You win!"
		set user_score to user_score + 1
	else if user_choice is equal to "Paper" and cpu_choice is equal to "Scissors" then
		display dialog "Player chose: " & user_choice & return & "Computer chose: " & cpu_choice & return & return & "You lose!"
		set cpu_score to cpu_score + 1
	else if user_choice is equal to "Paper" and cpu_choice is equal to "Rock" then
		display dialog "Player chose: " & user_choice & return & "Computer chose: " & cpu_choice & return & return & "You win!"
		set user_score to user_score + 1
	else if user_choice is equal to "Scissors" and cpu_choice is equal to "Rock" then
		display dialog "Player chose: " & user_choice & return & "Computer chose: " & cpu_choice & return & return & "You lose!"
		set cpu_score to cpu_score + 1
	else if user_choice is equal to "Scissors" and cpu_choice is equal to "Paper" then
		display dialog "Player chose: " & user_choice & return & "Computer chose: " & cpu_choice & return & return & "You win!"
		set user_score to user_score + 1
	end if
	set play_again to display dialog "Play again?" buttons {"Yes", "No"}
	set again_choice to button returned of play_again
	if again_choice is not equal to "Yes" then
		if user_score is greater than cpu_score then
			display dialog "Final Score" & return & "You: " & user_score & return & "Computer: " & cpu_score & return & "You are the winner!"
			exit repeat
		else if user_score is less than cpu_score then
			display dialog "Final Score" & return & "You: " & user_score & return & "Computer: " & cpu_score & return & "You are the loser!"
			exit repeat
		else if user_score is equal to cpu_score then
			display dialog "Final Score" & return & "You: " & user_score & return & "Computer: " & cpu_score & return & "Looks like a tie game!"
			exit repeat
		end if
	end if
end repeat