EC_BullsandCows
Works with: Euphoria version 4.0.3, 4.0.0 RC1 and later
Bulls and Cows is an old game played with pencil and paper that was later implemented on computer. The task is for the program to create a four digit random number from the digits 0 to 9, without duplication. The program should ask for guesses to this number, reject guesses that are malformed, then 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.
include std/sequence.e include std/console.e atom bulls = 0, cows = 0 sequence bulls_string, cows_string sequence goal_num computer's secret number digits sequence goal_check marked as bull/cow indexes sequence guess_num player's guess digits sequence guess_check marked as bull/cow indexes integer tries = 0 track number of tries to guess the number sequence tries_string  for printing singular or plural of 'try' sequence numbers numbers = shuffle("0123456789") goal_num = numbers[1..4] procedure getInputAndProcess(integer stage = 1) stage = 1 sets default value for the parameter if it isn't specified goal_check = "0000" set these to unscanned (0) since the scanning will start over. guess_check = "0000" these too, or they will contain old marks tries += 1 bulls = 0 cows = 0 if stage <= 1 then if this process was run for the first time or with no parameters, then print instructions puts(1,"The program has thought of a four digit number using only digits 0 to 9.\n") puts(1,"Type your guess and press enter.\n") end if while 1 do do this loop until we get a valid guess then exit guess_num = gets(0)  filter out all characters except "0123456789" guess_num = filter(guess_num, "in", {'0', '9'}, "[]") if length(guess_num) != 4 then if the input string is not exactly 4 digits, the input won't be used. puts(1,"\nYou didn't type 4 digits.\n") puts(1,"Try typing a new 4 digit number with only numbers 0 through 9.\n") else exit end if end while if compare(goal_num, guess_num) = 0 then bulls = 4 else check for bulls for i = 1 to 4 do if goal_num[i] = guess_num[i] then goal_check[i] = '1' guess_check[i] = '1' bulls += 1 end if end for check for cows, but not slots marked as bulls or cows already. goal_num += 0 guess_num += 0 for i = 1 to 4 do loop through each guessed digit if guess_check[i] = '1' then if the guessed digit we're comparing right now has been marked as bull or cow already continue skip to the next guess digit without comparing this guess digit to the other goal digits end if for j = 1 to 4 do go through each goal digit, comparing the first guessed digit, and then the other guessed digits 2 through 4 if goal_check[j] = '1' then if the goal digit we're comparing to right now has been marked as a bull or cow already continue skip to the next goal digit end if if guess_num[i] = goal_num[j] then if the guessed digit is the same as the goal one, it won't be a bull, so it's a cow cows += 1 score one more cow goal_check[j] = '1' mark this digit as a found cow in the sequence stores '0's or '1's as flags exit skip to the next guess digit, so that this digit won't try to check for matches(cow) with other goal digits end if end for this guess digit was compared to one goal digit , try comparing this guess digit with the next goal digit end for this guess digit was compared with all goal digits, compare the next guess digit to all the goal digits end if if bulls = 1 then uses singular noun when there is score of 1, else plural bulls_string = "bull" else bulls_string = "bulls" end if if cows = 1 then the same kind of thing as above block cows_string = "cow" else cows_string = "cows" end if end procedure run the procedure getInputAndProcess(1) while 1 do if bulls < 4 then if less than 4 bulls were found, the player hasn't won printf(1, "\nGuess #%d : You guessed %s. You found %d %s, %d %s. Type new guess.\n", {tries, guess_num, bulls, bulls_string, cows, cows_string} ) getInputAndProcess(2) else else they have won and the procedure ends if tries = 1 then  decide whether to use singular or plural of 'try' tries_string = "try" else tries_string = "tries" end if printf(1, "\nThe number was %s. You guessed %s in %d %s.\n", {goal_num, goal_num, tries, tries_string}) any_key()wait for keypress before closing console window. exit end if end while
Output :
The program has thought of a four digit number using only digits 0 to 9. Type your guess and press enter. 12345 You didn't type 4 digits. Try typing a new 4 digit number with only numbers 0 through 9. abcd You didn't type 4 digits. Try typing a new 4 digit number with only numbers 0 through 9. 012 You didn't type 4 digits. Try typing a new 4 digit number with only numbers 0 through 9. 0123 Guess #1 : You guessed 0123. You found 0 bulls, 2 cows. Type new guess. 1234 Guess #2 : You guessed 1234. You found 0 bulls, 3 cows. Type new guess. 5642 Guess #3 : You guessed 5642. You found 1 bull, 1 cow. Type new guess. 2347 Guess #4 : You guessed 2347. You found 1 bull, 2 cows. Type new guess. 4382 Guess #5 : You guessed 4382. You found 3 bulls, 0 cows. Type new guess. 4392 The number was 4392. You guessed 4392 in 6 tries. Press Any Key to continue...
This page has a chart to allow you to solve any number in a minimum amount of tries.
Not Categorized, Please Help
