Task

Write a player of the Bulls and Cows game, rather than a scorer. The player should give intermediate answers that respect the scores to previous attempts.

One method is to generate a list of all possible numbers that could be the answer, then to prune the list by keeping only those numbers that would give an equivalent score to how your last guess was scored. Your next guess can be any number from the pruned list.

Either you guess correctly or run out of numbers to guess, which indicates a problem with the scoring.



Related tasks

   Bulls and cows
   Guess the number
   Guess the number/With Feedback (Player)


set the remoteWorkInterval to .001 -- optional
repeat forever
	repeat forever
		set description to "Enter a 4 digit number" & newline & "- zero's excluded" & newline & "- each digit should be unique" & newline
		Ask "Enter a Number" title "Bulls & Cows (Player)" message description
		put it into num
		if num is ""
			Answer "" with "Play" or "Quit" title "Quit Bulls & Cows (Player)?"
			if it is "Quit"
				exit all
			end if
		end if
		set startTime to now
		if number of characters in num is 4
			if character 1 of num is not equal to character 2 of num
				if character 1 of num is not equal to character 3 of num
					if character 1 of num is not equal to character 4 of num
						if character 2 of num is not equal to character 3 of num
							if character 2 of num is not equal to character 4 of num
								if character 3 of num is not equal to character 4 of num
									if num does not contain 0
										exit repeat
									end if
								end if
							end if
						end if
					end if
				end if
			end if
		end if
	end repeat
	set guess to 1111
	set lastBullQty to 0
	set digitLocation to 1
	set cowVals to empty
	repeat forever
		set score to {
			bulls: {
				qty: 0,
				values: {}
			},
			cows: {
				qty: 0,
				values: {}
			}
		}
		repeat the number of characters in num times
			if character the counter of guess is equal to character the counter of num
				add 1 to score.bulls.qty
				insert character the counter of guess into score.bulls.values
			else
				if num contains character the counter of guess
					if character the counter of guess is not equal to character the counter of num
						if score.bulls.values does not contain character the counter of guess and score.cows.values does not contain character the counter of guess
							add 1 to score.cows.qty
							if cowVals.(character the counter of guess) is empty
								set cowVals.(character the counter of guess) to false
							end if
						end if
					end if
				end if
			end if
		end repeat
		if guess is equal to num
			put now - startTime into elapsedTime
			set displayMessage to "Your Number:" && num & newline & "Guessed Number:" && guess & newline & newline & "Time Elapsed:" && elapsedTime.seconds && seconds
			Answer displayMessage with "Play Again" or "Quit" title "Done!"
			if it is "Quit"
				exit all
			end if
			exit repeat
		else
			if the counter is greater than 1
				if score.bulls.qty is not lastBullQty
					if score.bulls.qty is greater than lastBullQty
						if digitLocation is not 4 -- move on to the next digit
							add 1 to digitLocation
						else
							set digitLocation to 1
						end if
						repeat for each (key,value) in cowVals
							if score.bulls.values contains key
								set cowVals.(key) to "bull"
							else
								set cowVals.(key) to false
							end if
						end repeat			
					else
						subtract 1 from character digitLocation of guess -- stay on current digit
					end if
					set lastBullQty to score.bulls.qty -- save bull qty
				else -- score.bulls.qty = lastBullQty
					set cow_guessed to false
					if cowVals is not empty
						repeat for each (key,value) in cowVals
							if value is false
								set cow_guessed to true
								set cowVals.(key) to true
								if digitLocation is not 1
									set character digitLocation of guess to key
									exit repeat
								else
									add 1 to character digitLocation of guess
									exit repeat
								end if
							end if
						end repeat
						if cow_guessed is false
							if character digitLocation of guess is greater than 9
								set character digitLocation of guess to 0 -- reset the current digit
							end if
							add 1 to character digitLocation of guess -- increment the current digit
						end if
					else
						add 1 to character digitLocation of guess -- increment the current digit
					end if
				end if
			end if
		end if
	end repeat
end repeat