Tic Tac Toe Game AI

This Tic Tac Toe game has a player vs player mode and a computer vs player mode were the computer always goes first in the top left corner. The computer can only ever be tied or win. In the player vs player mode it doesn’t recognize a winning move until one move after a winning move :confused: I need to get this fixed. I’m hoping that I can get a easy and medium level for the computer vs player mode any ways here is the code enjoy and if you find an easier way of scripting this please let me know :slight_smile: because it’s a bit long

repeat
	
	script pvp_
		set a1 to "1"
		set a2 to "2"
		set a3 to "3"
		set a4 to "4"
		set a5 to "5"
		set a6 to "6"
		set a7 to "7"
		set a8 to "8"
		set a9 to "9"
		
		set s1 to "0"
		set s2 to "0"
		set s3 to "0"
		set s4 to "0"
		set s5 to "0"
		set s6 to "0"
		set s7 to "0"
		set s8 to "0"
		set s9 to "0"
		
		set player1 to "X"
		set player2 to "O"
		
		repeat
			
			
			set move_1 to text returned of (display dialog a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9 & "
Where would you like to go?" default answer "")
			
			
			
			if move_1 is equal to "1" then
				if s1 is equal to "0" then
					set a1 to player1
					set s1 to "1"
				else
					display dialog "That space has already been taken." with icon 0
				end if
			end if
			
			
			if move_1 is equal to "2" then
				if s2 is equal to "0" then
					set a2 to player1
					set s2 to "1"
				else
					display dialog "That space has already been taken." with icon 0
				end if
			end if
			
			if move_1 is equal to "3" then
				if s3 is equal to "0" then
					set a3 to player1
					set s3 to "1"
				else
					display dialog "That space has already been taken." with icon 0
				end if
			end if
			
			if move_1 is equal to "4" then
				if s4 is equal to "0" then
					set a4 to player1
					set s4 to "1"
				else
					display dialog "That space has already been taken." with icon 0
				end if
			end if
			
			if move_1 is equal to "5" then
				if s5 is equal to "0" then
					set a5 to player1
					set s5 to "1"
				else
					display dialog "That space has already been taken." with icon 0
				end if
			end if
			
			if move_1 is equal to "6" then
				if s6 is equal to "0" then
					set a6 to player1
					set s6 to "1"
				else
					display dialog "That space has already been taken." with icon 0
				end if
			end if
			
			if move_1 is equal to "7" then
				if s7 is equal to "0" then
					set a7 to player1
					set s7 to "1"
				else
					display dialog "That space has already been taken." with icon 0
				end if
			end if
			
			if move_1 is equal to "8" then
				if s8 is equal to "0" then
					set a8 to player1
					set s8 to "1"
				else
					display dialog "That space has already been taken." with icon 0
				end if
			end if
			
			if move_1 is equal to "9" then
				if s9 is equal to "0" then
					set a9 to player1
					set s9 to "1"
				else
					display dialog "That space has already been taken." with icon 0
				end if
			end if
			--------------------------------------------------------------	
			set move_2 to text returned of (display dialog a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9 & "
Where would you like to go?" default answer "")
			
			if move_2 is equal to "1" then
				if s1 is equal to "0" then
					set a1 to player2
					set s1 to "1"
				else
					display dialog "That space has already been taken." with icon 0
				end if
			end if
			
			if move_2 is equal to "2" then
				if s2 is equal to "0" then
					set a2 to player2
					set s2 to "1"
				else
					display dialog "That space has already been taken." with icon 0
				end if
			end if
			
			if move_2 is equal to "3" then
				if s3 is equal to "0" then
					set a3 to player2
					set s3 to "1"
				else
					display dialog "That space has already been taken." with icon 0
				end if
			end if
			
			if move_2 is equal to "4" then
				if s4 is equal to "0" then
					set a4 to player2
					set s4 to "1"
				else
					display dialog "That space has already been taken." with icon 0
				end if
			end if
			
			if move_2 is equal to "5" then
				if s5 is equal to "0" then
					set a5 to player2
					set s5 to "1"
				else
					display dialog "That space has already been taken." with icon 0
				end if
			end if
			
			if move_2 is equal to "6" then
				if s6 is equal to "0" then
					set a6 to player2
					set s6 to "1"
				else
					display dialog "That space has already been taken." with icon 0
				end if
			end if
			
			if move_2 is equal to "7" then
				if s7 is equal to "0" then
					set a7 to player2
					set s7 to "1"
				else
					display dialog "That space has already been taken." with icon 0
				end if
			end if
			
			if move_2 is equal to "8" then
				if s8 is equal to "0" then
					set a8 to player2
					set s8 to "1"
				else
					display dialog "That space has already been taken." with icon 0
				end if
			end if
			
			if move_2 is equal to "9" then
				if s9 is equal to "0" then
					set a9 to player2
					set s9 to "1"
				else
					display dialog "That space has already been taken." with icon 0
				end if
			end if
			
			if s1 & s2 & s3 & s4 & s5 & s6 & s7 & s8 & s9 is equal to ("111111111") then
				display dialog "Cats Game!"
				exit repeat
			end if
			
			if a1 & a2 & a3 contains ("XXX") then
				display dialog player1 & " Wins!"
				exit repeat
			end if
			
			if a4 & a5 & a6 contains ("XXX") then
				display dialog player1 & " Wins!"
				exit repeat
			end if
			
			if a7 & a8 & a9 contains ("XXX") then
				display dialog player1 & " Wins!"
				exit repeat
			end if
			
			if a1 & a4 & a7 contains ("XXX") then
				display dialog player1 & " Wins!"
				exit repeat
			end if
			
			if a2 & a5 & a8 contains ("XXX") then
				display dialog player1 & " Wins!"
				exit repeat
			end if
			
			if a3 & a6 & a9 contains ("XXX") then
				display dialog player1 & " Wins!"
				exit repeat
			end if
			
			if a1 & a5 & a9 contains ("XXX") then
				display dialog player1 & " Wins!"
				exit repeat
			end if
			
			if a3 & a5 & a7 contains ("XXX") then
				display dialog player1 & " Wins!"
				exit repeat
			end if
			------------------------------------------------------------
			if a1 & a2 & a3 contains ("OOO") then
				display dialog player2 & " Wins!"
				exit repeat
			end if
			
			if a4 & a5 & a6 contains ("OOO") then
				display dialog player2 & " Wins!"
				exit repeat
			end if
			
			if a7 & a8 & a9 contains ("OOO") then
				display dialog player2 & " Wins!"
				exit repeat
			end if
			
			if a1 & a4 & a7 contains ("OOO") then
				display dialog player2 & " Wins!"
				exit repeat
			end if
			
			if a2 & a5 & a8 contains ("OOO") then
				display dialog player2 & " Wins!"
				exit repeat
			end if
			
			if a3 & a6 & a9 contains ("OOO") then
				display dialog player2 & " Wins!"
				exit repeat
			end if
			
			if a1 & a5 & a9 contains ("OOO") then
				display dialog player2 & " Wins!"
				exit repeat
			end if
			
			if a3 & a5 & a7 contains ("OOO") then
				display dialog player2 & " Wins!"
				exit repeat
			end if
			
		end repeat
	end script
	
	script pvc_
		set a1 to "1"
		
		set a2 to "2"
		
		set a3 to "3"
		
		set a4 to "4"
		
		set a5 to "5"
		
		set a6 to "6"
		
		set a7 to "7"
		
		set a8 to "8"
		
		set a9 to "9"
		
		
		set player1 to "X"
		
		set player2 to "O"
		
		
		set a1 to player1
		
		
		
		
		
		
		
		set move_1 to text returned of (display dialog "Where would you like to go?

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9 default answer "")
		
		
		if move_1 is equal to "1" then
			
			display dialog "That space is taken already!" with icon 0
			
		end if
		
		
		if move_1 is equal to "2" then
			
			run script x_1_2
			
		end if
		
		
		if move_1 is equal to "3" then
			
			run script x_1_3
			
		end if
		
		
		if move_1 is equal to "4" then
			
			run script x_1_4
			
		end if
		
		
		if move_1 is equal to "5" then
			
			run script x_1_5
			
		end if
		
		
		if move_1 is equal to "6" then
			
			run script x_1_6
			
		end if
		
		
		if move_1 is equal to "7" then
			
			run script x_1_7
			
		end if
		
		
		if move_1 is equal to "8" then
			
			run script x_1_8
			
		end if
		
		
		if move_1 is equal to "9" then
			
			run script x_1_9
			
		end if
		
		
		
		script x_1_2
			
			set a1 to "1"
			
			set a2 to "2"
			
			set a3 to "3"
			
			set a4 to "4"
			
			set a5 to "5"
			
			set a6 to "6"
			
			set a7 to "7"
			
			set a8 to "8"
			
			set a9 to "9"
			
			set player1 to "X"
			
			set player2 to "O"
			
			set a1 to player1
			
			set a2 to player2
			
			set a5 to player1
			
			set move_2 to text returned of (display dialog "Where would you like to go?

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9 default answer "")
			
			if move_2 is equal to "1" then
				
				display dialog "That space is taken already!" with icon 0
				
			end if
			
			if move_2 is equal to "2" then
				
				display dialog "That space is taken already!" with icon 0
				
			end if
			
			if move_2 is equal to "5" then
				
				display dialog "That space is taken already!" with icon 0
				
			end if
			
			if move_2 is equal to "3" then
				
				set a9 to player1
				
				set a3 to player2
				
				display dialog "I won

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9
				
			end if
			
			if move_2 is equal to "4" then
				
				set a9 to player1
				
				set a4 to player2
				
				display dialog "I won

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9
				
			end if
			
			if move_2 is equal to "6" then
				
				set a9 to player1
				
				set a6 to player2
				
				display dialog "I won

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9
				
			end if
			
			if move_2 is equal to "7" then
				
				set a9 to player1
				
				set a7 to player2
				
				display dialog "I won

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9
				
			end if
			
			if move_2 is equal to "8" then
				
				set a9 to player1
				
				set a8 to player2
				
				display dialog "I won

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9
				
			end if
			
			if move_2 is equal to "9" then
				
				set a9 to player2
				
				set a7 to player1
				
				set move_3 to text returned of (display dialog "Where would you like to go?

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9 default answer "")
				
				if move_3 is equal to "1" then
					
					display dialog "That space is taken already!" with icon 0
					
				end if
				
				if move_3 is equal to "2" then
					
					display dialog "That space is taken already!" with icon 0
					
				end if
				
				if move_3 is equal to "5" then
					
					display dialog "That space is taken already!" with icon 0
					
				end if
				
				if move_3 is equal to "7" then
					
					display dialog "That space is taken already!" with icon 0
					
				end if
				
				if move_3 is equal to "9" then
					
					display dialog "That space is taken already!" with icon 0
					
				end if
				
				if move_3 is equal to "3" then
					
					set a4 to player1
					
					set a3 to player2
					
					display dialog "I won

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9
					
				end if
				
				if move_3 is equal to "4" then
					
					set a3 to player1
					
					set a4 to player2
					
					display dialog "I won

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9
					
				end if
				
				if move_3 is equal to "6" then
					
					set a4 to player1
					
					set a6 to player2
					
					display dialog "I won

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9
					
				end if
				
				if move_3 is equal to "8" then
					
					set a3 to player1
					
					set a8 to player2
					
					display dialog "I won

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9
					
				end if
				
			end if
			
		end script
		
		------------------------->1
		
		
		script x_1_3
			
			set a1 to "1"
			
			set a2 to "2"
			
			set a3 to "3"
			
			set a4 to "4"
			
			set a5 to "5"
			
			set a6 to "6"
			
			set a7 to "7"
			
			set a8 to "8"
			
			set a9 to "9"
			
			set player1 to "X"
			
			set player2 to "O"
			
			set a1 to player1
			
			set a3 to player2
			
			set a9 to player1
			
			set move_2 to text returned of (display dialog "Where would you like to go?

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9 default answer "")
			
			if move_2 is equal to "1" then
				
				display dialog "That space is taken already!" with icon 0
				
			end if
			
			if move_2 is equal to "3" then
				
				display dialog "That space is taken already!" with icon 0
				
			end if
			
			if move_2 is equal to "9" then
				
				display dialog "That space is taken already!" with icon 0
				
			end if
			
			if move_2 is equal to "2" then
				
				set a5 to player1
				
				set a2 to player2
				
				display dialog "I won

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9
				
			end if
			
			if move_2 is equal to "4" then
				
				set a5 to player1
				
				set a4 to player2
				
				display dialog "I won

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9
				
			end if
			
			if move_2 is equal to "5" then
				
				set a5 to player2
				
				set a7 to player1
				
				set move_3 to text returned of (display dialog "Where would you like to go?

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9 default answer "")
				
				if move_3 is equal to "1" then
					
					display dialog "That space is taken already!" with icon 0
					
				end if
				
				if move_3 is equal to "3" then
					
					display dialog "That space is taken already!" with icon 0
					
				end if
				
				if move_3 is equal to "9" then
					
					display dialog "That space is taken already!" with icon 0
					
				end if
				
				if move_3 is equal to "5" then
					
					display dialog "That space is taken already!" with icon 0
					
				end if
				
				if move_3 is equal to "7" then
					
					display dialog "That space is taken already!" with icon 0
					
				end if
				
				if move_3 is equal to "2" then
					
					set a4 to player1
					
					set a2 to player2
					
					display dialog "I won

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9
					
				end if
				
				if move_3 is equal to "6" then
					
					set a8 to player1
					
					set a6 to player2
					
					display dialog "I won

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9
					
				end if
				
				if move_3 is equal to "4" then
					
					set a8 to player1
					
					set a4 to player2
					
					display dialog "I won

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9
					
				end if
				
				if move_3 is equal to "8" then
					
					set a4 to player1
					
					set a8 to player2
					
					display dialog "I won

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9
					
				end if
				
			end if
			
			if move_2 is equal to "6" then
				
				set a5 to player1
				
				set a6 to player2
				
				display dialog "I won

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9
				
			end if
			
			if move_2 is equal to "7" then
				
				set a5 to player1
				
				set a7 to player2
				
				display dialog "I won

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9
				
			end if
			
			if move_2 is equal to "8" then
				
				set a5 to player1
				
				set a8 to player2
				
				display dialog "I won

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9
				
			end if
			
		end script
		
		-------------------------->2
		
		
		script x_1_4
			
			set a1 to "1"
			
			set a2 to "2"
			
			set a3 to "3"
			
			set a4 to "4"
			
			set a5 to "5"
			
			set a6 to "6"
			
			set a7 to "7"
			
			set a8 to "8"
			
			set a9 to "9"
			
			set player1 to "X"
			
			set player2 to "O"
			
			set a1 to player1
			
			set a4 to player2
			
			set a5 to player1
			
			set move_2 to text returned of (display dialog "Where would you like to go?

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9 default answer "")
			
			if move_2 is equal to "1" then
				
				display dialog "That space is taken already!" with icon 0
				
			end if
			
			if move_2 is equal to "4" then
				
				display dialog "That space is taken already!" with icon 0
				
			end if
			
			if move_2 is equal to "5" then
				
				display dialog "That space is taken already!" with icon 0
				
			end if
			
			if move_2 is equal to "2" then
				
				set a9 to player1
				
				set a2 to player2
				
				display dialog "I won

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9
				
			end if
			
			if move_2 is equal to "3" then
				
				set a9 to player1
				
				set a3 to player2
				
				display dialog "I won

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9
				
			end if
			
			if move_2 is equal to "6" then
				
				set a9 to player1
				
				set a6 to player2
				
				display dialog "I won

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9
				
			end if
			
			if move_2 is equal to "7" then
				
				set a9 to player1
				
				set a7 to player2
				
				display dialog "I won

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9
				
			end if
			
			if move_2 is equal to "8" then
				
				set a9 to player1
				
				set a8 to player2
				
				display dialog "I won

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9
				
			end if
			
			if move_2 is equal to "9" then
				
				set a9 to player2
				
				set a3 to player1
				
				set move_3 to text returned of (display dialog "Where would you like to go?

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9 default answer "")
				
				if move_3 is equal to "1" then
					
					display dialog "That space is taken already!" with icon 0
					
				end if
				
				if move_3 is equal to "4" then
					
					display dialog "That space is taken already!" with icon 0
					
				end if
				
				if move_3 is equal to "5" then
					
					display dialog "That space is taken already!" with icon 0
					
				end if
				
				if move_3 is equal to "3" then
					
					display dialog "That space is taken already!" with icon 0
					
				end if
				
				if move_3 is equal to "9" then
					
					display dialog "That space is taken already!" with icon 0
					
				end if
				
				if move_3 is equal to "6" then
					
					set a7 to player1
					
					set a6 to player2
					
					display dialog "I won

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9
					
				end if
				
				if move_3 is equal to "7" then
					
					set a2 to player1
					
					set a7 to player2
					
					display dialog "I won

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9
					
				end if
				
				if move_3 is equal to "8" then
					
					set a2 to player1
					
					set a8 to player2
					
					display dialog "I won

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9
					
				end if
				
				if move_2 is equal to "2" then
					
					set a7 to player1
					
					set a2 to player2
					
					display dialog "I won

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9
					
				end if
				
			end if
			
		end script
		
		------------------------->3
		
		script x_1_5
			
			set a1 to "1"
			
			set a2 to "2"
			
			set a3 to "3"
			
			set a4 to "4"
			
			set a5 to "5"
			
			set a6 to "6"
			
			set a7 to "7"
			
			set a8 to "8"
			
			set a9 to "9"
			
			set player1 to "X"
			
			set player2 to "O"
			
			set a1 to player1
			
			set a5 to player2
			
			set a9 to player1
			
			set move_2 to text returned of (display dialog "Where would you like to go?

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9 default answer "")
			
			if move_2 is equal to "1" then
				
				display dialog "That space is taken already!" with icon 0
				
			end if
			
			if move_2 is equal to "5" then
				
				display dialog "That space is taken already!" with icon 0
				
			end if
			
			if move_2 is equal to "9" then
				
				display dialog "That space is taken already!" with icon 0
				
			end if
			
			if move_2 is equal to "3" then
				
				run script x_1_5_a_a
				
			end if
			
			if move_2 is equal to "7" then
				
				run script x_1_5_a_b
				
			end if
			
			if move_2 is equal to "2" then
				
				run script x_1_5_b_a
				
			end if
			
			if move_2 is equal to "4" then
				
				run script x_1_5_b_b
				
			end if
			
			if move_2 is equal to "6" then
				
				run script x_1_5_b_c
				
			end if
			
			if move_2 is equal to "8" then
				
				run script x_1_5_b_d
				
			end if
			
			script x_1_5_a_a
				
				set a1 to "1"
				
				set a2 to "2"
				
				set a3 to "3"
				
				set a4 to "4"
				
				set a5 to "5"
				
				set a6 to "6"
				
				set a7 to "7"
				
				set a8 to "8"
				
				set a9 to "9"
				
				set player1 to "X"
				
				set player2 to "O"
				
				set a1 to player1
				
				set a5 to player2
				
				set a9 to player1
				
				set a3 to player2 --------------------these two change
				
				set a7 to player1 --------------------these two change
				
				set move_3 to text returned of (display dialog "Where would you like to go?

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9 default answer "")
				
				if move_3 is equal to "1" then
					
					display dialog "That space is taken already!" with icon 0
					
				end if
				
				if move_3 is equal to "5" then
					
					display dialog "That space is taken already!" with icon 0
					
				end if
				
				if move_3 is equal to "9" then
					
					display dialog "That space is taken already!" with icon 0
					
				end if
				
				--------------------------------next two change
				
				if move_3 is equal to "3" then
					
					display dialog "That space is taken already!" with icon 0
					
				end if
				
				if move_3 is equal to "7" then
					
					display dialog "That space is taken already!" with icon 0
					
				end if
				
				if move_3 is equal to "6" then
					
					set a4 to player1
					
					set a6 to player2
					
					display dialog "I won

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9
					
				end if
				
				if move_3 is equal to "4" then
					
					set a8 to player1
					
					set a4 to player2
					
					display dialog "I won

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9
					
				end if
				
				if move_3 is equal to "8" then
					
					set a4 to player1
					
					set a8 to player2
					
					display dialog "I won

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9
					
				end if
				
				if move_3 is equal to "2" then
					
					set a8 to player1
					
					set a2 to player2
					
					display dialog "I won

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9
					
				end if
				
			end script
			
			script x_1_5_a_b
				
				set a1 to "1"
				
				set a2 to "2"
				
				set a3 to "3"
				
				set a4 to "4"
				
				set a5 to "5"
				
				set a6 to "6"
				
				set a7 to "7"
				
				set a8 to "8"
				
				set a9 to "9"
				
				set player1 to "X"
				
				set player2 to "O"
				
				set a1 to player1
				
				set a5 to player2
				
				set a9 to player1
				
				set a7 to player2 --------------------these two change
				
				set a3 to player1 --------------------these two change
				
				set move_3 to text returned of (display dialog "Where would you like to go?

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9 default answer "")
				
				if move_3 is equal to "1" then
					
					display dialog "That space is taken already!" with icon 0
					
				end if
				
				if move_3 is equal to "5" then
					
					display dialog "That space is taken already!" with icon 0
					
				end if
				
				if move_3 is equal to "9" then
					
					display dialog "That space is taken already!" with icon 0
					
				end if
				
				--------------------------------next two change
				
				if move_3 is equal to "7" then
					
					display dialog "That space is taken already!" with icon 0
					
				end if
				
				if move_3 is equal to "3" then
					
					display dialog "That space is taken already!" with icon 0
					
				end if
				
				if move_3 is equal to "6" then
					
					set a2 to player1
					
					set a6 to player2
					
					display dialog "I won

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9
					
				end if
				
				if move_3 is equal to "4" then
					
					set a6 to player1
					
					set a4 to player2
					
					display dialog "I won

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9
					
				end if
				
				if move_3 is equal to "8" then
					
					set a2 to player1
					
					set a8 to player2
					
					display dialog "I won

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9
					
				end if
				
				if move_3 is equal to "2" then
					
					set a6 to player1
					
					set a2 to player2
					
					display dialog "I won

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9
					
				end if
				
			end script
			
			script x_1_5_b_a
				
				set a1 to "1"
				
				set a2 to "2"
				
				set a3 to "3"
				
				set a4 to "4"
				
				set a5 to "5"
				
				set a6 to "6"
				
				set a7 to "7"
				
				set a8 to "8"
				
				set a9 to "9"
				
				set player1 to "X"
				
				set player2 to "O"
				
				set a1 to player1
				
				set a5 to player2
				
				set a9 to player1
				
				set a2 to player2 --------------------these two change
				
				set a8 to player1 --------------------these two change
				
				set move_3 to text returned of (display dialog "Where would you like to go?

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9 default answer "")
				
				if move_3 is equal to "1" then
					
					display dialog "That space is taken already!" with icon 0
					
				end if
				
				if move_3 is equal to "5" then
					
					display dialog "That space is taken already!" with icon 0
					
				end if
				
				if move_3 is equal to "9" then
					
					display dialog "That space is taken already!" with icon 0
					
				end if
				
				--------------------------------next two change
				
				if move_3 is equal to "2" then
					
					display dialog "That space is taken already!" with icon 0
					
				end if
				
				if move_3 is equal to "8" then
					
					display dialog "That space is taken already!" with icon 0
					
				end if
				
				if move_3 is equal to "6" then
					
					set a7 to player1
					
					set a6 to player2
					
					display dialog "I won

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9
					
				end if
				
				if move_3 is equal to "4" then
					
					set a7 to player1
					
					set a4 to player2
					
					display dialog "I won

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9
					
				end if
				
				if move_3 is equal to "3" then
					
					set a7 to player1
					
					set a3 to player2
					
					display dialog "I won

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9
					
				end if
				
				if move_3 is equal to "7" then
					
					set a7 to player2
					
					set a3 to player1
					
					set move_4 to text returned of (display dialog "Where would you like to go?

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9 default answer "")
					
				end if
				
				if move_4 is equal to "1" then
					
					display dialog "That space is taken already!" with icon 0
					
				end if
				
				if move_4 is equal to "5" then
					
					display dialog "That space is taken already!" with icon 0
					
				end if
				
				if move_4 is equal to "9" then
					
					display dialog "That space is taken already!" with icon 0
					
				end if
				
				--------------------------------next two change
				
				if move_4 is equal to "2" then
					
					display dialog "That space is taken already!" with icon 0
					
				end if
				
				if move_4 is equal to "8" then
					
					display dialog "That space is taken already!" with icon 0
					
				end if
				
				if move_4 is equal to "7" then
					
					display dialog "That space is taken already!" with icon 0
					
				end if
				
				if move_4 is equal to "3" then
					
					display dialog "That space is taken already!" with icon 0
					
				end if
				
				if move_4 is equal to "4" then
					
					set a6 to player1
					
					set a4 to player2
					
					display dialog "I won

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9
					
				end if
				
				if move_4 is equal to "6" then
					
					set a4 to player1
					
					set a6 to player2
					
					display dialog "We Tied

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9
					
				end if
				
			end script
			
			script x_1_5_b_b
				
				set a1 to "1"
				
				set a2 to "2"
				
				set a3 to "3"
				
				set a4 to "4"
				
				set a5 to "5"
				
				set a6 to "6"
				
				set a7 to "7"
				
				set a8 to "8"
				
				set a9 to "9"
				
				set player1 to "X"
				
				set player2 to "O"
				
				set a1 to player1
				
				set a5 to player2
				
				set a9 to player1
				
				set a4 to player2 --------------------these two change
				
				set a6 to player1 --------------------these two change
				
				set move_3 to text returned of (display dialog "Where would you like to go?

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9 default answer "")
				
				if move_3 is equal to "1" then
					
					display dialog "That space is taken already!" with icon 0
					
				end if
				
				if move_3 is equal to "5" then
					
					display dialog "That space is taken already!" with icon 0
					
				end if
				
				if move_3 is equal to "9" then
					
					display dialog "That space is taken already!" with icon 0
					
				end if
				
				--------------------------------next two change
				
				if move_3 is equal to "4" then
					
					display dialog "That space is taken already!" with icon 0
					
				end if
				
				if move_3 is equal to "6" then
					
					display dialog "That space is taken already!" with icon 0
					
				end if
				
				if move_3 is equal to "2" then
					
					set a3 to player1
					
					set a2 to player2
					
					display dialog "I won

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9
					
				end if
				
				if move_3 is equal to "7" then
					
					set a3 to player1
					
					set a7 to player2
					
					display dialog "I won

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9
					
				end if
				
				if move_3 is equal to "8" then
					
					set a3 to player1
					
					set a8 to player2
					
					display dialog "I won

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9
					
				end if
				
				if move_3 is equal to "3" then
					
					set a3 to player2
					
					set a7 to player1
					
					set move_4 to text returned of (display dialog "Where would you like to go?

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9 default answer "")
					
				end if
				
				if move_4 is equal to "1" then
					
					display dialog "That space is taken already!" with icon 0
					
				end if
				
				if move_4 is equal to "5" then
					
					display dialog "That space is taken already!" with icon 0
					
				end if
				
				if move_4 is equal to "9" then
					
					display dialog "That space is taken already!" with icon 0
					
				end if
				
				--------------------------------next two change
				
				if move_4 is equal to "4" then
					
					display dialog "That space is taken already!" with icon 0
					
				end if
				
				if move_4 is equal to "6" then
					
					display dialog "That space is taken already!" with icon 0
					
				end if
				
				if move_4 is equal to "3" then
					
					display dialog "That space is taken already!" with icon 0
					
				end if
				
				if move_4 is equal to "7" then
					
					display dialog "That space is taken already!" with icon 0
					
				end if
				
				if move_4 is equal to "2" then
					
					set a8 to player1
					
					set a2 to player2
					
					display dialog "I won

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9
					
				end if
				
				if move_4 is equal to "8" then
					
					set a8 to player2
					
					set a2 to player1
					
					display dialog "We Tied

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9
					
				end if
				
			end script
			
			script x_1_5_b_c
				
				set a1 to "1"
				
				set a2 to "2"
				
				set a3 to "3"
				
				set a4 to "4"
				
				set a5 to "5"
				
				set a6 to "6"
				
				set a7 to "7"
				
				set a8 to "8"
				
				set a9 to "9"
				
				set player1 to "X"
				
				set player2 to "O"
				
				set a1 to player1
				
				set a5 to player2
				
				set a9 to player1
				
				set a6 to player2 --------------------these two change
				
				set a4 to player1 --------------------these two change
				
				set move_3 to text returned of (display dialog "Where would you like to go?

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9 default answer "")
				
				if move_3 is equal to "1" then
					
					display dialog "That space is taken already!" with icon 0
					
				end if
				
				if move_3 is equal to "5" then
					
					display dialog "That space is taken already!" with icon 0
					
				end if
				
				if move_3 is equal to "9" then
					
					display dialog "That space is taken already!" with icon 0
					
				end if
				
				--------------------------------next two change
				
				if move_3 is equal to "6" then
					
					display dialog "That space is taken already!" with icon 0
					
				end if
				
				if move_3 is equal to "4" then
					
					display dialog "That space is taken already!" with icon 0
					
				end if
				
				if move_3 is equal to "2" then
					
					set a7 to player1
					
					set a2 to player2
					
					display dialog "I won

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9
					
				end if
				
				if move_3 is equal to "3" then
					
					set a7 to player1
					
					set a3 to player2
					
					display dialog "I won

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9
					
				end if
				
				if move_3 is equal to "8" then
					
					set a7 to player1
					
					set a8 to player2
					
					display dialog "I won

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9
					
				end if
				
				if move_3 is equal to "7" then
					
					set a7 to player2
					
					set a3 to player1
					
					set move_4 to text returned of (display dialog "Where would you like to go?

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9 default answer "")
					
				end if
				
				if move_4 is equal to "1" then
					
					display dialog "That space is taken already!" with icon 0
					
				end if
				
				if move_4 is equal to "5" then
					
					display dialog "That space is taken already!" with icon 0
					
				end if
				
				if move_4 is equal to "9" then
					
					display dialog "That space is taken already!" with icon 0
					
				end if
				
				--------------------------------next two change
				
				if move_4 is equal to "4" then
					
					display dialog "That space is taken already!" with icon 0
					
				end if
				
				if move_4 is equal to "6" then
					
					display dialog "That space is taken already!" with icon 0
					
				end if
				
				if move_4 is equal to "7" then
					
					display dialog "That space is taken already!" with icon 0
					
				end if
				
				if move_4 is equal to "3" then
					
					display dialog "That space is taken already!" with icon 0
					
				end if
				
				if move_4 is equal to "2" then
					
					set a8 to player1
					
					set a2 to player2
					
					display dialog "We Tied

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9
					
				end if
				
				if move_4 is equal to "8" then
					
					set a2 to player1
					
					set a8 to player2
					
					display dialog "I won

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9
					
				end if
				
			end script
			
			script x_1_5_b_d
				
				set a1 to "1"
				
				set a2 to "2"
				
				set a3 to "3"
				
				set a4 to "4"
				
				set a5 to "5"
				
				set a6 to "6"
				
				set a7 to "7"
				
				set a8 to "8"
				
				set a9 to "9"
				
				set player1 to "X"
				
				set player2 to "O"
				
				set a1 to player1
				
				set a5 to player2
				
				set a9 to player1
				
				set a8 to player2 --------------------these two change
				
				set a2 to player1 --------------------these two change
				
				set move_3 to text returned of (display dialog "Where would you like to go?

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9 default answer "")
				
				if move_3 is equal to "1" then
					
					display dialog "That space is taken already!" with icon 0
					
				end if
				
				if move_3 is equal to "5" then
					
					display dialog "That space is taken already!" with icon 0
					
				end if
				
				if move_3 is equal to "9" then
					
					display dialog "That space is taken already!" with icon 0
					
				end if
				
				--------------------------------next two change
				
				if move_3 is equal to "8" then
					
					display dialog "That space is taken already!" with icon 0
					
				end if
				
				if move_3 is equal to "2" then
					
					display dialog "That space is taken already!" with icon 0
					
				end if
				
				if move_3 is equal to "6" then
					
					set a3 to player1
					
					set a6 to player2
					
					display dialog "I won

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9
					
				end if
				
				if move_3 is equal to "4" then
					
					set a3 to player1
					
					set a4 to player2
					
					display dialog "I won

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9
					
				end if
				
				if move_3 is equal to "7" then
					
					set a3 to player1
					
					set a7 to player2
					
					display dialog "I won

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9
					
				end if
				
				if move_3 is equal to "3" then
					
					set a3 to player2
					
					set a7 to player1
					
					set move_4 to text returned of (display dialog "Where would you like to go?

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9 default answer "")
					
				end if
				
				if move_4 is equal to "1" then
					
					display dialog "That space is taken already!" with icon 0
					
				end if
				
				if move_4 is equal to "5" then
					
					display dialog "That space is taken already!" with icon 0
					
				end if
				
				if move_4 is equal to "9" then
					
					display dialog "That space is taken already!" with icon 0
					
				end if
				
				--------------------------------next two change
				
				if move_4 is equal to "8" then
					
					display dialog "That space is taken already!" with icon 0
					
				end if
				
				if move_4 is equal to "2" then
					
					display dialog "That space is taken already!" with icon 0
					
				end if
				
				if move_4 is equal to "3" then
					
					display dialog "That space is taken already!" with icon 0
					
				end if
				
				if move_4 is equal to "7" then
					
					display dialog "That space is taken already!" with icon 0
					
				end if
				
				if move_4 is equal to "6" then
					
					set a4 to player1
					
					set a6 to player2
					
					display dialog "I won

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9
					
				end if
				
				if move_4 is equal to "4" then
					
					set a6 to player1
					
					set a4 to player2
					
					display dialog "We Tied

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9
					
				end if
				
			end script
			
		end script
		
		
		--------------------->4
		
		
		script x_1_6
			
			set a1 to "1"
			
			set a2 to "2"
			
			set a3 to "3"
			
			set a4 to "4"
			
			set a5 to "5"
			
			set a6 to "6"
			
			set a7 to "7"
			
			set a8 to "8"
			
			set a9 to "9"
			
			set player1 to "X"
			
			set player2 to "O"
			
			set a1 to player1
			
			set a6 to player2
			
			set a5 to player1
			
			set move_2 to text returned of (display dialog "Where would you like to go?

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9 default answer "")
			
			if move_2 is equal to "1" then
				
				display dialog "That space is taken already!" with icon 0
				
			end if
			
			if move_2 is equal to "5" then
				
				display dialog "That space is taken already!" with icon 0
				
			end if
			
			if move_2 is equal to "6" then
				
				display dialog "That space is taken already!" with icon 0
				
			end if
			
			if move_2 is equal to "2" then
				
				set a9 to player1
				
				set a2 to player2
				
				display dialog "I won

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9
				
			end if
			
			if move_2 is equal to "3" then
				
				set a9 to player1
				
				set a3 to player2
				
				display dialog "I won

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9
				
			end if
			
			if move_2 is equal to "4" then
				
				set a9 to player1
				
				set a4 to player2
				
				display dialog "I won

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9
				
			end if
			
			if move_2 is equal to "7" then
				
				set a9 to player1
				
				set a7 to player2
				
				display dialog "I won

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9
				
			end if
			
			if move_2 is equal to "8" then
				
				set a9 to player1
				
				set a8 to player2
				
				display dialog "I won

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9
				
			end if
			
			if move_2 is equal to "9" then
				
				set a9 to player2
				
				set a3 to player1
				
				set move_3 to text returned of (display dialog "Where would you like to go?

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9 default answer "")
				
				if move_3 is equal to "1" then
					
					display dialog "That space is taken already!" with icon 0
					
				end if
				
				if move_3 is equal to "5" then
					
					display dialog "That space is taken already!" with icon 0
					
				end if
				
				if move_3 is equal to "6" then
					
					display dialog "That space is taken already!" with icon 0
					
				end if
				
				if move_3 is equal to "3" then
					
					display dialog "That space is taken already!" with icon 0
					
				end if
				
				if move_3 is equal to "9" then
					
					display dialog "That space is taken already!" with icon 0
					
				end if
				
				if move_3 is equal to "4" then
					
					set a7 to player1
					
					set a4 to player2
					
					display dialog "I won

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9
					
				end if
				
				if move_3 is equal to "7" then
					
					set a2 to player1
					
					set a7 to player2
					
					display dialog "I won

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9
					
				end if
				
				if move_3 is equal to "8" then
					
					set a2 to player1
					
					set a8 to player2
					
					display dialog "I won

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9
					
				end if
				
				if move_3 is equal to "2" then
					
					set a7 to player1
					
					set a2 to player2
					
					display dialog "I won

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9
					
				end if
				
			end if
			
		end script
		
		---------------------->5
		
		
		script x_1_7
			
			set a1 to "1"
			
			set a2 to "2"
			
			set a3 to "3"
			
			set a4 to "4"
			
			set a5 to "5"
			
			set a6 to "6"
			
			set a7 to "7"
			
			set a8 to "8"
			
			set a9 to "9"
			
			set player1 to "X"
			
			set player2 to "O"
			
			set a1 to player1
			
			set a7 to player2
			
			set a3 to player1
			
			set move_2 to text returned of (display dialog "Where would you like to go?

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9 default answer "")
			
			if move_2 is equal to "1" then
				
				display dialog "That space is taken already!" with icon 0
				
			end if
			
			if move_2 is equal to "3" then
				
				display dialog "That space is taken already!" with icon 0
				
			end if
			
			if move_2 is equal to "7" then
				
				display dialog "That space is taken already!" with icon 0
				
			end if
			
			if move_2 is equal to "6" then
				
				set a2 to player1
				
				set a6 to player2
				
				display dialog "I won

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9
				
			end if
			
			if move_2 is equal to "5" then
				
				set a2 to player1
				
				set a5 to player2
				
				display dialog "I won

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9
				
			end if
			
			if move_2 is equal to "4" then
				
				set a2 to player1
				
				set a4 to player2
				
				display dialog "I won

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9
				
			end if
			
			if move_2 is equal to "8" then
				
				set a2 to player1
				
				set a8 to player2
				
				display dialog "I won

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9
				
			end if
			
			if move_2 is equal to "9" then
				
				set a2 to player1
				
				set a9 to player2
				
				display dialog "I won

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9
				
			end if
			
			if move_2 is equal to "2" then
				
				set a2 to player2
				
				set a9 to player1
				
				set move_3 to text returned of (display dialog "Where would you like to go?

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9 default answer "")
				
				if move_3 is equal to "1" then
					
					display dialog "That space is taken already!" with icon 0
					
				end if
				
				if move_3 is equal to "3" then
					
					display dialog "That space is taken already!" with icon 0
					
				end if
				
				if move_3 is equal to "7" then
					
					display dialog "That space is taken already!" with icon 0
					
				end if
				
				if move_3 is equal to "2" then
					
					display dialog "That space is taken already!" with icon 0
					
				end if
				
				if move_3 is equal to "9" then
					
					display dialog "That space is taken already!" with icon 0
					
				end if
				
				if move_3 is equal to "6" then
					
					set a5 to player1
					
					set a6 to player2
					
					display dialog "I won

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9
					
				end if
				
				if move_3 is equal to "5" then
					
					set a6 to player1
					
					set a5 to player2
					
					display dialog "I won

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9
					
				end if
				
				if move_3 is equal to "4" then
					
					set a5 to player1
					
					set a4 to player2
					
					display dialog "I won

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9
					
				end if
				
				if move_3 is equal to "8" then
					
					set a6 to player1
					
					set a8 to player2
					
					display dialog "I won

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9
					
				end if
				
			end if
			
		end script
		
		-------------------->6
		
		
		script x_1_8
			
			set a1 to "1"
			
			set a2 to "2"
			
			set a3 to "3"
			
			set a4 to "4"
			
			set a5 to "5"
			
			set a6 to "6"
			
			set a7 to "7"
			
			set a8 to "8"
			
			set a9 to "9"
			
			set player1 to "X"
			
			set player2 to "O"
			
			set a1 to player1
			
			set a8 to player2
			
			set a5 to player1
			
			set move_2 to text returned of (display dialog "Where would you like to go?

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9 default answer "")
			
			if move_2 is equal to "1" then
				
				display dialog "That space is taken already!" with icon 0
				
			end if
			
			if move_2 is equal to "8" then
				
				display dialog "That space is taken already!" with icon 0
				
			end if
			
			if move_2 is equal to "5" then
				
				display dialog "That space is taken already!" with icon 0
				
			end if
			
			if move_2 is equal to "2" then
				
				set a9 to player1
				
				set a2 to player2
				
				display dialog "I won

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9
				
			end if
			
			if move_2 is equal to "3" then
				
				set a9 to player1
				
				set a3 to player2
				
				display dialog "I won

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9
				
			end if
			
			if move_2 is equal to "4" then
				
				set a9 to player1
				
				set a4 to player2
				
				display dialog "I won

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9
				
			end if
			
			if move_2 is equal to "6" then
				
				set a9 to player1
				
				set a6 to player2
				
				display dialog "I won

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9
				
			end if
			
			if move_2 is equal to "7" then
				
				set a9 to player1
				
				set a7 to player2
				
				display dialog "I won

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9
				
			end if
			
			if move_2 is equal to "9" then
				
				set a9 to player2
				
				set a7 to player1
				
				set move_3 to text returned of (display dialog "Where would you like to go?

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9 default answer "")
				
				if move_3 is equal to "1" then
					
					display dialog "That space is taken already!" with icon 0
					
				end if
				
				if move_3 is equal to "8" then
					
					display dialog "That space is taken already!" with icon 0
					
				end if
				
				if move_3 is equal to "5" then
					
					display dialog "That space is taken already!" with icon 0
					
				end if
				
				if move_3 is equal to "9" then
					
					display dialog "That space is taken already!" with icon 0
					
				end if
				
				if move_3 is equal to "7" then
					
					display dialog "That space is taken already!" with icon 0
					
				end if
				
				if move_3 is equal to "3" then
					
					set a4 to player1
					
					set a3 to player2
					
					display dialog "I won

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9
					
				end if
				
				if move_3 is equal to "4" then
					
					set a3 to player1
					
					set a4 to player2
					
					display dialog "I won

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9
					
				end if
				
				if move_3 is equal to "6" then
					
					set a4 to player1
					
					set a6 to player2
					
					display dialog "I won

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9
					
				end if
				
				if move_3 is equal to "2" then
					
					set a3 to player1
					
					set a2 to player2
					
					display dialog "I won

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9
					
				end if
				
			end if
			
		end script
		
		------------------------>7
		
		script x_1_9
			
			set a1 to "1"
			
			set a2 to "2"
			
			set a3 to "3"
			
			set a4 to "4"
			
			set a5 to "5"
			
			set a6 to "6"
			
			set a7 to "7"
			
			set a8 to "8"
			
			set a9 to "9"
			
			set player1 to "X"
			
			set player2 to "O"
			
			set a1 to player1
			
			set a9 to player2
			
			set a7 to player1
			
			set move_2 to text returned of (display dialog "Where would you like to go?

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9 default answer "")
			
			if move_2 is equal to "1" then
				
				display dialog "That space is taken already!" with icon 0
				
			end if
			
			if move_2 is equal to "9" then
				
				display dialog "That space is taken already!" with icon 0
				
			end if
			
			if move_2 is equal to "7" then
				
				display dialog "That space is taken already!" with icon 0
				
			end if
			
			if move_2 is equal to "2" then
				
				set a4 to player1
				
				set a2 to player2
				
				display dialog "I won

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9
				
			end if
			
			if move_2 is equal to "3" then
				
				set a4 to player1
				
				set a3 to player2
				
				display dialog "I won

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9
				
			end if
			
			if move_2 is equal to "5" then
				
				set a4 to player1
				
				set a5 to player2
				
				display dialog "I won

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9
				
			end if
			
			if move_2 is equal to "6" then
				
				set a4 to player1
				
				set a6 to player2
				
				display dialog "I won

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9
				
			end if
			
			if move_2 is equal to "8" then
				
				set a4 to player1
				
				set a8 to player2
				
				display dialog "I won

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9
				
			end if
			
			if move_2 is equal to "4" then
				
				set a4 to player2
				
				set a3 to player1
				
				set move_3 to text returned of (display dialog "Where would you like to go?

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9 default answer "")
				
				if move_3 is equal to "1" then
					
					display dialog "That space is taken already!" with icon 0
					
				end if
				
				if move_3 is equal to "9" then
					
					display dialog "That space is taken already!" with icon 0
					
				end if
				
				if move_3 is equal to "7" then
					
					display dialog "That space is taken already!" with icon 0
					
				end if
				
				if move_3 is equal to "4" then
					
					display dialog "That space is taken already!" with icon 0
					
				end if
				
				if move_3 is equal to "3" then
					
					display dialog "That space is taken already!" with icon 0
					
				end if
				
				if move_3 is equal to "2" then
					
					set a5 to player1
					
					set a2 to player2
					
					display dialog "I won

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9
					
				end if
				
				if move_3 is equal to "5" then
					
					set a2 to player1
					
					set a5 to player2
					
					display dialog "I won

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9
					
				end if
				
				if move_3 is equal to "6" then
					
					set a5 to player1
					
					set a6 to player2
					
					display dialog "I won

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9
					
				end if
				
				if move_3 is equal to "8" then
					
					set a2 to player1
					
					set a8 to player2
					
					display dialog "I won

" & a1 & a2 & a3 & "
" & a4 & a5 & a6 & "
" & a7 & a8 & a9
					
				end if
				
			end if
			
		end script
		
		----------------------------->8 end :)
	end script
	
	
	
	
	display dialog "What type of game would you like to play" buttons {"Player v.s. Player", "Player v.s. Computer", "Cancel"} default button 1
	
	if the button returned of the result is "Player v.s. Player" then
		run script pvp_
	else if the button returned of the result is "Player v.s. Computer" then
		
		run script pvc_
		
	else
		exit repeat
	end if
end repeat

Model: Mac OS X 10.5.8
AppleScript: 2.2.1
Browser: Firefox 3.6.13
Operating System: Mac OS X (10.7)

wow that is amazing
sum comments on how the code works whud be nice so if sum one wants to make sum thing simmer they can understand the code.

how long did it take you to Finnish?

Thanks for contributing, but if there was ever a script in need of handlers and lists, this is a perfect example. You could reduce the length of this script very substantially that way. Use handlers instead of repeating instructions and use lists to deal with position-related choices.

Here’s a reworking of willis00788’s script with handlers, lists, records, and comments! The player vs player winning move recognition delay is also fixed.

main()

-- The main loop.
on main()
	repeat
		set gameChoice to button returned of (display dialog "What type of game would you like to play?" buttons {"Player v.s. Player", "Player v.s. Computer", "Cancel"} default button 1 cancel button 3)
		
		set a to {"1", "2", "3", "4", "5", "6", "7", "8", "9"} -- Nine-square grid for display.
		
		set player1 to "X"
		set player2 to "O"
		
		if (gameChoice is "Player v.s. Player") then
			pvp_(a, player1, player2)
		else -- if (gameChoice is "Player v.s. Computer") then			
			pvc_(a, player1, player2)
		end if
	end repeat
end main

-- The Player v.s. Player game.
on pvp_(a, player1, player2)
	set thePlayers to {player1, player2}
	
	repeat with s from 1 to 9
		-- Get a move from the current player and update the a and s lists.
		getMove(a, item ((s - 1) mod 2 + 1) of thePlayers)
		
		-- Check for a result. If none, continue.
		set theWinner to testForWinner(a)
		if (theWinner is missing value) then
			if (s is 9) then display dialog "Cats Game!"
		else
			display dialog theWinner & " Wins!" & return & return & getGridText(a)
			exit repeat
		end if
	end repeat
end pvp_

-- The Player v.s. Computer game.
on pvc_(a, player1, player2)
	-- The computer's always player1 and it always makes the first move: to square 1.
	set item 1 of a to player1
	
	-- Get a move from the human player.
	set move_1 to getMove(a, player2)
	if (move_1 is 5) then
		x_1_5(a, player1, player2)
	else
		x_1_generic(a, player1, player2, move_1)
	end if
end pvc_

-- The Player v.s. Computer continuation when the human's first move's to square 5.
on x_1_5(a, player1, player2)
	-- The computer puts a cross in square 9.
	set item 9 of a to player1
	
	-- There are then two possible courses, depending on whether the human chooses one of the remaining odd-numbered squares (3 or 7) or one of the even-numbered ones.
	set move_2 to getMove(a, player2)
	if (move_2 mod 2 is 1) then
		x_1_5_a(a, player1, player2, move_2) -- 3 or 7.
	else
		x_1_5_b(a, player1, player2, move_2) -- 2, 4, 6, or 8.
	end if
end x_1_5

-- The Player v.s. Computer endgame when the human's played square 5 and then square 3 or 7.
on x_1_5_a(a, player1, player2, move_2)
	-- Use the set of possibilities resulting from the human's second move.
	set gamePlan to item (move_2 div 3) of ¬
		{{compMove_3:7, responses_3a:{6, 8}, computerWin_1:4, computerWin_2:8}, ¬
			{compMove_3:3, responses_3a:{6, 8}, computerWin_1:2, computerWin_2:6}}
	
	-- The computer makes its next move .
	set item (gamePlan's compMove_3) of a to player1
	
	-- . then wins on the next move according to how the human plays.
	set move_3 to getMove(a, player2)
	if (move_3 is in gamePlan's responses_3a) then
		computerWin(a, gamePlan's computerWin_1, player1)
	else
		computerWin(a, gamePlan's computerWin_2, player1)
	end if
end x_1_5_a

-- The Player v.s. Computer endgame when the human's played square 5 and then an even-numbered square.
on x_1_5_b(a, player1, player2, move_2)
	-- Use the set of possibilities resulting from the human's second move.
	set gamePlan to item (move_2 div 2) of ¬
		{{compMove_3:8, responses_3a:{3, 4, 6}, computerWin_1:7, compMove_4:3, response_4a:4, computerWin_2:6, tie:4}, ¬
			{compMove_3:6, responses_3a:{2, 7, 8}, computerWin_1:3, compMove_4:7, response_4a:2, computerWin_2:8, tie:2}, ¬
			{compMove_3:4, responses_3a:{2, 3, 8}, computerWin_1:7, compMove_4:3, response_4a:8, computerWin_2:2, tie:8}, ¬
			{compMove_3:2, responses_3a:{4, 6, 7}, computerWin_1:3, compMove_4:7, response_4a:6, computerWin_2:4, tie:6}}
	
	-- The computer makes its next move .
	set item (gamePlan's compMove_3) of a to player1
	
	-- . then either wins in 1 or wins or draws in 2.
	set move_3 to getMove(a, player2)
	if (move_3 is in gamePlan's responses_3a) then
		computerWin(a, gamePlan's computerWin_1, player1)
	else
		set item (gamePlan's compMove_4) of a to player1
		
		set move_4 to getMove(a, player2)
		if (move_4 is gamePlan's response_4a) then
			computerWin(a, gamePlan's computerWin_2, player1)
		else
			tie(a, gamePlan's tie, player1)
		end if
	end if
end x_1_5_b

-- The Player v.s. Computer continuation when the human's first move's NOT to square 5.
on x_1_generic(a, player1, player2, move_1)
	-- Use the set of possibilities resulting from the human's first move.
	set gamePlan to item move_1 of {missing value, ¬
		{compMove_2:5, best_2:9, compMove_3:7, responses_3a:{3, 6}, compWin_1:9, compWin_2a:4, compWin_2b:3}, ¬
		{compMove_2:9, best_2:5, compMove_3:7, responses_3a:{2, 8}, compWin_1:5, compWin_2a:4, compWin_2b:8}, ¬
		{compMove_2:5, best_2:9, compMove_3:3, responses_3a:{2, 6}, compWin_1:9, compWin_2a:7, compWin_2b:2}, ¬
		missing value, ¬
		{compMove_2:5, best_2:9, compMove_3:3, responses_3a:{2, 4}, compWin_1:9, compWin_2a:7, compWin_2b:2}, ¬
		{compMove_2:3, best_2:2, compMove_3:9, responses_3a:{4, 6}, compWin_1:2, compWin_2a:5, compWin_2b:6}, ¬
		{compMove_2:5, best_2:9, compMove_3:7, responses_3a:{3, 6}, compWin_1:9, compWin_2a:4, compWin_2b:3}, ¬
		{compMove_2:7, best_2:4, compMove_3:3, responses_3a:{2, 6}, compWin_1:4, compWin_2a:5, compWin_2b:2}}
	
	-- The computer places its second cross according to the data set used.
	set item (gamePlan's compMove_2) of a to player1
	
	set move_2 to getMove(a, player2)
	if (move_2 is gamePlan's best_2) then
		-- If the human now makes the best possible response, the computer makes the appropriate move and wins on the next move whatever happens.
		set item (gamePlan's compMove_3) of a to player1
		
		set move_3 to getMove(a, player2)
		if (move_3 is in gamePlan's responses_3a) then
			computerWin(a, gamePlan's compWin_2a, player1)
		else
			computerWin(a, gamePlan's compWin_2b, player1)
		end if
	else
		-- Otherwise the computer makes a winning move immediately.
		computerWin(a, gamePlan's compWin_1, player1)
	end if
end x_1_generic

-- Get a move for the given player, check its validity, and update the a and s lists when satisfied.
on getMove(a, player)
	set msg to getGridText(a)
	
	set valid to false
	repeat until (valid)
		try
			set move_ to (text returned of (display dialog player & "'s turn. Where would you like to go?" & (return & return & msg) default answer ""))
			if (move_ is "") then set move_ to "\"\""
			set move_ to move_ as integer
			if (item move_ of a is in "OX") then
				display dialog "That space has already been taken." with icon stop
			else
				set item move_ of a to player
				set valid to true
			end if
		on error number errNum
			if (errNum is -128) then
				error number -128
			else
				display dialog (move_ as text) & " is not a valid square number." with icon stop
			end if
		end try
	end repeat
	
	return move_ -- Needed in "Player v.s Computer" game.
end getMove

-- Format the current grid list as text for display.
on getGridText(a)
	set astid to AppleScript's text item delimiters
	set AppleScript's text item delimiters to ""
	set txt to ((items 1 thru 3 of a) as text) & return & items 4 thru 6 of a & return & items 7 thru 9 of a
	set AppleScript's text item delimiters to astid
	
	return txt
end getGridText

-- Check for winning lines in the grid and return the lucky player, if any.
-- Winning lines are equal values in squares 1-2-3, 1-4-7, 1-5-9, 2-5-8, 3-5-6, 3-6-9, 4-5-6, or 7-8-9.
on testForWinner(a)
	tell beginning of a to if ((it is item 2 of a and it is item 3 of a) or (it is item 4 of a and it is item 7 of a) or (it is item 5 of a and it is item 9 of a)) then return it
	tell item 2 of a to if (it is item 5 of a and it is item 8 of a) then return it
	tell item 3 of a to if ((it is item 5 of a and it is item 7 of a) or (it is item 6 of a and it is item 9 of a)) then return it
	tell item 4 of a to if (it is item 5 of a and it is item 6 of a) then return it
	tell item 7 of a to if (it is item 8 of a and it is item 9 of a) then return it
	
	return missing value
end testForWinner

-- Perform a winning computer move and claim victory.
on computerWin(a, move_, player1)
	set item move_ of a to player1
	display dialog ("I won." & return & return & getGridText(a))
end computerWin

-- Perform a tying computer move and show the result.
on tie(a, move_, player1)
	set item move_ of a to player1
	display dialog ("We tied." & return & return & getGridText(a))
end tie

Good stuff, Nigel – you’re a patient man. Needs one tiny tweak: if you’re playing person to person, the “Where would you like to go” dialog doesn’t indicate whose turn it is.

Fixed, though the cue now appears in both games.

I may try to lose the ‘s’ list later on.

Edit: OK. ‘s’ now survives only as a loop variable in the Player vs Player game. It serves its former (list) function of indicating when the grid’s full and now doubles as the root from which the current player’s calculated. :slight_smile:

Here’s another version where, in the Player vs Computer game, the computer bases its moves on the current state of the board and follows a general strategy rather than a set game plan. If faced with two or more equally good moves, it choses one at random. The human player can choose whether to be X or O and whether or not to go first. If the computer goes first, it makes its first move entirely at random. This should all make the game slightly less boring. :rolleyes:

main()

-- The main loop.
on main()
	repeat
		set gameChoice to button returned of (display dialog "What type of game would you like to play?" buttons {"Cancel", "Player v.s. Player", "Player v.s. Computer"} default button 3 cancel button 1)
		
		set a to {1, 2, 3, 4, 5, 6, 7, 8, 9} -- Nine-square grid for display.
		
		set player1 to "X"
		set player2 to "O"
		
		if (gameChoice is "Player v.s. Player") then
			pvp_(a, player1, player2)
		else -- if (gameChoice is "Player v.s. Computer") then			
			pvc_(a, player1, player2)
		end if
	end repeat
end main

-- The Player v.s. Player game.
on pvp_(a, player1, player2)
	set thePlayers to {player1, player2}
	
	repeat with s from 1 to 9
		-- Get a move from the current player and update the grid list.
		getMove(a, item ((s - 1) mod 2 + 1) of thePlayers)
		
		-- Check for a win or a draw. If none, continue.
		set theWinner to testForWinner(a)
		if (theWinner is missing value) then
			if (s is 9) then display dialog "Cats Game!"
		else
			display dialog theWinner & " Wins!" & getGridText(a)
			exit repeat
		end if
	end repeat
end pvp_

-- The Player v.s. Computer game.
on pvc_(a, cross, naught)
	-- Get the player's preferences for this game.
	set computerIsX to (button returned of (display dialog "Do you want to play " & cross & " or " & naught & "? ." buttons {"Cancel", cross, naught} default button 3 cancel button 1) is naught)
	set {|computer|, human} to item ((computerIsX as integer) + 1) of {{naught, cross}, {cross, naught}}
	set computerFirst to (button returned of (display dialog ". and do you want to go first or second?" buttons {"Cancel", "First", "Second"} default button 3 cancel button 1) is "Second")
	
	repeat with s from 1 to 9
		-- The computer and human play odd- or even-numbered moves as appropriate. If the computer goes first, it choses its first move at random.
		if ((computerFirst) and (s is 1)) then
			set item (some item of a) of a to |computer|
		else if ((computerFirst) as integer = (s mod 2)) then
			computerMove(a, |computer|, human)
		else
			getMove(a, human)
		end if
		
		-- Check for a win or a draw. If none, continue.
		set theWinner to testForWinner(a)
		if (theWinner is missing value) then
			if (s is 9) then display dialog "It's a draw!" & getGridText(a)
		else
			if (theWinner is |computer|) then
				display dialog "I win!" & getGridText(a)
			else
				display dialog "You win!" & getGridText(a)
			end if
			exit repeat
		end if
	end repeat
end pvc_


-- Get a move for the given human player, check its validity, and update the grid list when satisfied.
on getMove(a, player)
	set gridText to getGridText(a)
	
	set valid to false
	repeat until (valid)
		try
			set move_ to (text returned of (display dialog player & "'s turn. Where would you like to go?" & gridText default answer ""))
			if (move_ is "") then set move_ to "\"\"" -- Hack to get "" to error and display properly.
			set move_ to move_ as integer
			if (a contains move_) then
				set item move_ of a to player
				set valid to true
			else
				display dialog "That space has already been taken." with icon stop
			end if
		on error number errNum
			if (errNum is -128) then
				error number -128
			else
				display dialog (move_ as text) & " is not a valid square number." with icon stop
			end if
		end try
	end repeat
	
	return move_ -- Needed in "Player v.s Computer" game.
end getMove

-- Format the current state of the grid list as text for display.
on getGridText(a)
	set astid to AppleScript's text item delimiters
	set AppleScript's text item delimiters to ""
	tell a to set txt to return & return & (items 1 thru 3) & return & (items 4 thru 6) & return & (items 7 thru 9)
	set AppleScript's text item delimiters to astid
	
	return txt
end getGridText

-- Check for winning lines in the grid and return the lucky player, if any.
-- Winning lines are equal values in squares 1-2-3, 1-4-7, 1-5-9, 2-5-8, 3-5-6, 3-6-9, 4-5-6, or 7-8-9.
on testForWinner(a)
	tell beginning of a to if (((it is item 2 of a) and (it is item 3 of a)) or ((it is item 4 of a) and (it is item 7 of a)) or ((it is item 5 of a) and (it is item 9 of a))) then return it
	tell item 2 of a to if ((it is item 5 of a) and (it is item 8 of a)) then return it
	tell item 3 of a to if (((it is item 5 of a) and (it is item 7 of a)) or ((it is item 6 of a) and (it is item 9 of a))) then return it
	tell item 4 of a to if ((it is item 5 of a) and (it is item 6 of a)) then return it
	tell item 7 of a to if ((it is item 8 of a) and (it is item 9 of a)) then return it
	
	return missing value
end testForWinner

-- Player vs Computer: get a move from the computer.
on computerMove(a, |computer|, human)
	-- Try these strategy priorities in order until one of them returns 'true'.
	((win(a, |computer|, human)) or ¬
		(blockWin(a, |computer|)) or ¬
		(fork(a, |computer|)) or ¬
		(blockFork(a, |computer|, human)) or ¬
		(centre(a, |computer|)) or ¬
		(oppositeCorner(a, |computer|, human)) or ¬
		(emptyCorner(a, |computer|)) or ¬
		(emptySide(a, |computer|)))
end computerMove

-- Player vs Computer. Strategy priority 1: play a winning computer move, if any.
on win(a, |computer|, human)
	return spotWin(a, |computer|, human)
end win

-- Player vs Computer. Strategy priority 2: block a winning human move, if any.
on blockWin(a, |computer|)
	return spotWin(a, |computer|, |computer|)
end blockWin

-- Player vs Computer: identify all possible winning moves for the player which isn't the passed "opponent" and, if any, place the computer's mark in one of them.
on spotWin(a, |computer|, opponent)
	set possibilities to {}
	set {a1, a2, a3, a4, a5, a6, a7, a8, a9} to a
	set winLines to {{a1, a2, a3}, {a1, a4, a7}, {a1, a5, a9}, {a2, a5, a8}, {a3, a5, a7}, {a3, a6, a9}, {a4, a5, a6}, {a7, a8, a9}}
	
	repeat with thisLine in winLines
		-- Ignore lines containing the opponent's mark. Two equal values in any of the others are the non-opponent's marks and the third value is the number of the winning square.
		if (thisLine does not contain opponent) then
			set {v1, v2, v3} to thisLine
			if (v1 = v2) then
				set end of possibilities to v3
			else if (v1 = v3) then
				set end of possibilities to v2
			else if (v2 = v3) then
				set end of possibilities to v1
			end if
		end if
	end repeat
	
	return playRandom(a, possibilities, |computer|)
end spotWin

-- Player vs Computer: play a computer move chosen randomly from the possibilities gleaned by the calling handler.
on playRandom(a, possibilities, |computer|)
	if (possibilities is {}) then
		return false
	else
		set item (some item of possibilities) of a to |computer|
		return true
	end if
end playRandom

-- Player vs Computer. Strategy priority 3: create a fork situation for the computer, if possible.
on fork(a, |computer|)
	return spotFork(a, |computer|, |computer|)
end fork

-- Player vs Computer. Strategy priority 4: prevent a fork situation for the human, if necessary.
on blockFork(a, |computer|, human)
	return spotFork(a, human, |computer|)
end blockFork

-- Player vs Computer: identify all possible fork-creating moves for the passed "fork player" and play the computer's mark in one of them chosen at random.
on spotFork(a, |computer|, forkPlayer)
	set {a1, a2, a3, a6, a9, a8, a7, a4} to {1, 2, 3, 6, 9, 8, 7, 4} -- The grid's corner and side indices in clockwise order.
	set possibilities to {}
	
	-- Starting with squares 1 and 2, repeat with each corner/side pair in turn, clockwise.
	repeat 4 times
		if (item a1 of a is forkPlayer) then
			-- The player for which we're checking fork possibilities has a mark in this corner. Test which fork-creation moves are possible given the current combination of free spaces and the player's other marks.
			if ((a contains a2) and (a2 is not in possibilities) and ¬
				(item 5 of a is forkPlayer) and (a contains a3) and (a contains a8)) then set end of possibilities to a2
			if ((a contains a3) and (a3 is not in possibilities) and (a contains a2) and ¬
				(((item 5 of a is forkPlayer) and (a contains a7)) or ¬
					((item a6 of a is forkPlayer) and (a contains a9)) or ¬
					((item a9 of a is forkPlayer) and (a contains a6)))) then set end of possibilities to a3
			if ((a contains a4) and (a4 is not in possibilities) and (a contains a7) and ¬
				(((item 5 of a is forkPlayer) and (a contains a6)) or ¬
					((item a6 of a is forkPlayer) and (a contains 5)))) then set end of possibilities to a4
			if ((a contains 5) and (5 is not in possibilities) and (a contains a9) and ¬
				(((item a2 of a is forkPlayer) and (a contains a8)) or ¬
					((item a3 of a is forkPlayer) and (a contains a7)) or ¬
					((item a4 of a is forkPlayer) and (a contains a6)))) then set end of possibilities to 5
			if ((a contains a7) and (a7 is not in possibilities) and (a contains a4) and ¬
				((item a3 of a is forkPlayer) or ¬
					((item 5 of a is forkPlayer) and (a contains a3)) or ¬
					((item a8 of a is forkPlayer) and (a contains a9)))) then set end of possibilities to a7
			if ((a contains a9) and (a9 is not in possibilities) and (a contains 5) and ¬
				(((item a3 of a is forkPlayer) and (a contains a6)) or ¬
					((item a6 of a is forkPlayer) and (a contains a3)) or ¬
					((item a8 of a is forkPlayer) and (a contains a7)))) then set end of possibilities to a9
		end if
		
		if (item a2 of a is forkPlayer) then
			-- The player has a mark in the middle of this side. Test the fork-creation moves in this case.
			if ((a contains 5) and (5 is not in possibilities) and (item a6 of a is forkPlayer) and (a contains a4) and (a contains a8)) then set end of possibilities to 5
			if ((item 5 of a is forkPlayer) and (a contains a1) and (a contains a3)) then
				if ((a1 is not in possibilities) and (a contains a9)) then set end of possibilities to a1
				if ((a3 is not in possibilities) and (a contains a7)) then set end of possibilities to a3
			end if
		end if
		
		-- Rotate the indices clockwise to make them relative to the next corner and side.
		set {a1, a2, a3, a6, a9, a8, a7, a4} to {a3, a6, a9, a8, a7, a4, a1, a2}
	end repeat
	
	return playRandom(a, possibilities, |computer|)
end spotFork

-- Player vs Computer. Strategy priority 5: play the computer in the centre position, if possible.
on centre(a, |computer|)
	set success to (a contains 5)
	if (success) then set item 5 of a to |computer|
	return success
end centre

-- Player vs Computer. Strategy priority 6: play the computer in a corner opposite to the human, if possible.
on oppositeCorner(a, |computer|, human)
	repeat with i from 1 to 3 by 2
		set j to 10 - i
		set ai to item i of a
		set aj to item j of a
		if ((ai is human) and (aj is j)) then
			set item j of a to |computer|
			return true
		else if ((aj is human) and (ai is i)) then
			set item i of a to |computer|
			return true
		end if
	end repeat
	
	return false
end oppositeCorner

-- Player vs Computer. Strategy priority 7: play the computer in any free corner, if possible, unless it also occupies the centre and the human has two opposite corners.
on emptyCorner(a, |computer|)
	if ((item 5 of a is |computer|) and ((a contains 1 and a contains 9) is not (a contains 3 and a contains 7))) then return false
	return oneFrom(a, {1, 3, 7, 9}, |computer|)
end emptyCorner

-- Player vs Computer. Strategy priority 8: play the computer on any free side, if possible.
on emptySide(a, |computer|)
	return oneFrom(a, {2, 4, 6, 8}, |computer|)
end emptySide

-- Player vs Computer: find out which of the squares indexed in 'options' are free and play the computer in one of them at random.
on oneFrom(a, options, |computer|)
	set possibilities to {}
	repeat with i from 1 to (count options)
		set ai to item i of options
		if (a contains ai) then set end of possibilities to ai
	end repeat
	
	return playRandom(a, possibilities, |computer|)
end oneFrom

This is another variant (which can be improved). Too bad that AppleScript lacks bitwise operations and therefore I simulated it (last handler BWAND(n1, n2) in the code). There are two difficulties and you can choose if you want to start or not.

CPU starts on normal: CPU starts with a random pick
CPU starts on hard: CPU always starts in the upper left corner (which makes you only able to make it a draw)
Players starts on normal: CPU is easy to beat
Player starts on Hard: it check if you start in the corner or not

The script works but isn’t finished as Nigel’s but is shows you how you can save code working bitwise. Programming languages that supports bitwise operations will also be a lot quicker.


property OMask : missing value
property XMask : missing value
property winningNumbers : {7, 56, 73, 84, 146, 273, 292, 448}
property difficulty : missing value

repeat
	set OMask to 0
	set XMask to 0
	
	if button returned of (display dialog "Who should start?" buttons {"I shoud", "CPU"}) = "CPU" then set OMask to npcGet()
	set difficulty to button returned of (display dialog "Please choose your difficulty" buttons {"Hard", "Normal"})
	
	repeat
		set XMask to XMask + 2 ^ (nGet() - 1)
		if winnerForMask(XMask) or OMask + XMask = 511 then exit repeat
		set OMask to npcGet()
		if winnerForMask(OMask) or OMask + XMask = 511 then exit repeat
	end repeat
	
	if winnerForMask(OMask) then
		set msg to "CPU Wins!"
	else if winnerForMask(XMask) then
		set msg to "You WON!!!"
	else
		set msg to "It's a draw"
	end if
	
	display dialog msg & return & return & drawGrid() & return & return & "Do you want to play again?"
end repeat

on nGet()
	set theMessage to "It's your turn Player 1, please fill in the number for X" & return & return & drawGrid()
	repeat
		set value to text returned of (display dialog theMessage default answer "")
		if (offset of value in "123456789") is not 0 then
			if not positionIsUsed(value as integer) then exit repeat
		end if
	end repeat
	return value as integer
end nGet

on npcGet()
	--first get the free positions 
	set freeSpots to {}
	repeat with s from 1 to 9
		if not positionIsUsed(s) then set end of freeSpots to 2 ^ (s - 1)
	end repeat
	--second check if 1 move can make the CPU win
	repeat with spot in freeSpots
		if winnerForMask(OMask + spot) then return OMask + spot
	end repeat
	
	if difficulty is "Hard" and OMask is 0 then
		if XMask = 1 or XMask = 4 then return 2
		if XMask = 64 or XMask = 256 then return 128
	end if
	--third check if a user can make make it win (defensive) place it on position
	repeat with spot in freeSpots
		if winnerForMask(XMask + spot) then return OMask + spot
	end repeat
	
	--fourth check if CPU can win in two moves
	repeat with spot1 in freeSpots
		repeat with spot2 in freeSpots
			if winnerForMask(OMask + spot1 + spot2) then return OMask + spot2
		end repeat
	end repeat
	--fifth check if player can win in two moves
	repeat with spot1 in freeSpots
		repeat with spot2 in reverse of freeSpots
			if winnerForMask(XMask + spot1 + spot2) then return OMask + spot1
		end repeat
	end repeat
	--at last pick a random spot
	if XMask + OMask = 0 and difficulty = "Hard" then return 1
	
	return OMask + (some item of freeSpots)
end npcGet

on winnerForMask(mask)
	repeat with winLine in winningNumbers
		if BWAND(winLine, mask) = contents of winLine then return true
	end repeat
	return false
end winnerForMask

on drawGrid()
	set grid to ""
	repeat with o from 0 to 8
		if BWAND(OMask, 2 ^ o) = 2 ^ o then
			set grid to grid & "O"
		else if BWAND(XMask, 2 ^ o) = 2 ^ o then
			set grid to grid & "X"
		else
			set grid to grid & o + 1
		end if
		if o is in {2, 5} then set grid to grid & return
	end repeat
	return grid
end drawGrid

on positionIsUsed(pos)
	return BWAND(OMask + XMask, 2 ^ (pos - 1)) = 2 ^ (pos - 1)
end positionIsUsed

on BWAND(n1, n2)
	set theResult to 0
	repeat with o from 0 to 8
		if (n1 mod 2) = 1 and (n2 mod 2) = 1 then set theResult to theResult + 2 ^ o
		set {n1, n2} to {n1 div 2, n2 div 2}
	end repeat
	return theResult as integer
end BWAND

EDIT: removed unused functions

Nice one, DJ! :cool:

I haven’t fully analysed it yet, but I understand the bitwise principle and can see that your ‘winningNumbers’ are binary equivalents of my ‘winLines’ .

Here’s my own attempt at a 9-bit bitwise-AND handler:

on BWAND(n1, n2)
	repeat 9 times
		set n1 to (n1 + (n1 mod 2) * (n2 mod 2) * 512) div 2
		set n2 to n2 div 2
	end repeat
	return n1
end BWAND

Correct! I’m not saying mine or yours is better, my purpose was just to show the differences in code with the original post.

Great!!! haven’s seen that one ever. Mine could be improved by summing the values to get rid of the if-statement but yours did get rid of the extra variable as well:cool:.

Thanks. :slight_smile: I think mine can lose one of the 'mod 2’s:

on BWAND(n1, n2)
	repeat 9 times
		set n1 to (n1 + (n1 * n2 mod 2 * 512)) div 2
		set n2 to n2 div 2
	end repeat
	return n1
end BWAND

Great, greater, greatest, I don’t think it can be improved in any way. Only a single line of lists but that’s just an optical performance. I haven’t had the time till today to look at this tiny project again. I love the way you did this binary operator but my question is how short will an NOT, XOR and OR will be?

Thanks to your example I came up with this:

on BWNOT(n)
	repeat 9 times
		set n to (n + ((not n mod 2 as boolean) as integer) * 512) div 2
	end repeat
	return n
end BWNOT

on BWOR(n1, n2)
	repeat 9 times
		if n1 mod 2 as boolean or n2 mod 2 as boolean then set n1 to n1 + 512
		set {n1, n2} to {n1 div 2, n2 div 2}
	end repeat
	return n1
end BWOR

I came up with these 16-bit efforts:

on bwAND(n1, n2)
	repeat 16 times
		set n1 to (n1 + n1 * n2 mod 2 * 65536) div 2
		set n2 to n2 div 2
	end repeat
	return n1
end bwAND

on bwNAND(n1, n2)
	repeat 16 times
		set n1 to (n1 + (n1 * n2 + 1) mod 2 * 65536) div 2
		set n2 to n2 div 2
	end repeat
	return n1
end bwNAND

on bwOR(n1, n2)
	repeat 16 times
		set n1 to (n1 + (n1 mod 2 + n2 mod 2 + 1) div 2 * 65536) div 2
		set n2 to n2 div 2
	end repeat
	return n1
end bwOR

on bwNOR(n1, n2)
	repeat 16 times
		set n1 to (n1 + ((n1 mod 2 + n2 mod 2 + 1) div 2 + 1) mod 2 * 65536) div 2
		set n2 to n2 div 2
	end repeat
	return n1
end bwNOR

on bwXOR(n1, n2)
	repeat 16 times
		set n1 to (n1 + (n1 + n2) mod 2 * 65536) div 2
		set n2 to n2 div 2
	end repeat
	return n1
end bwXOR

on bwNXOR(n1, n2)
	repeat 16 times
		set n1 to (n1 + (n1 + n2 + 1) mod 2 * 65536) div 2
		set n2 to n2 div 2
	end repeat
	return n1
end bwNXOR

on bwNOT(n)
	return 65535 - n
end bwNOT

set {a, b} to {14, 7}
{bwAND(a, b), bwNAND(a, b), bwOR(a, b), bwNOR(a, b), bwXOR(a, b), bwNXOR(a, b), bwNOT(a)}

Thanks NIgel :smiley: