Bulls and Cows


Task

Create a four digit random number from the digits   1   to   9,   without duplication.

The program should:

   ask for guesses to this number
   reject guesses that are malformed
   print the score for the guess


The score is computed as:

 The player wins if the guess is the same as the randomly chosen number, and the program ends.
 A score of one bull is accumulated for each digit in the guess that equals the corresponding digit in the randomly chosen initial number.
 A score of one cow is accumulated for each digit in the guess that also appears in the randomly chosen number, but in the wrong position.


Related tasks

   Bulls and cows/Player
   Guess the number
   Guess the number/With Feedback
   Mastermind


repeat forever
	repeat forever
		put random(1111,9999) into num
		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 repeat
	set description to "Guess the 4 digit number" & newline & "- zero's excluded" & newline & "- each digit is unique" & newline & newline & "Receive 1 Bull for each digit that equals the corresponding digit in the random number." & newline & newline & "Receive 1 Cow for each digit that appears in the wrong position." & newline
	repeat forever
		repeat forever
			Ask "Guess the number" title "Bulls & Cows" message description
			put it into guess
			if number of characters in guess is equal to 4
				exit repeat
			else if guess is ""
				Answer "" with "Play" or "Quit" title "Quit Bulls & Cows?"
				put it into myAnswer
				if myAnswer is "Quit"
					exit all
				end if
			end if
		end repeat
		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
							insert character the counter of guess into score.cows.values
						end if	
					end if
				end if
			end if
		end repeat
		set showScores to "Your score is:" & newline & newline & "Bulls:" && score.bulls.qty & newline & newline & "Cows:" && score.cows.qty
		if guess is not equal to num
			Answer showScores with "Guess Again" or "Quit" title "Score"
			put it into myAnswer
			if myAnswer is "Quit"
				exit all
			end if
		else
			set winShowScores to showScores & newline & newline & "Your Guess:" && guess & newline & "Random Number:" && num & newline
			Answer winShowScores with "Play Again" or "Quit" title "You Win!"
			put it into myAnswer
			if myAnswer is "Quit"
				exit all
			end if
			exit repeat
		end if
	end repeat
end repeat