-- Copyright (C) 2001 Gerard Whyte -- Created: 17/11/01 -- Last Modified: 29/11/01 class MASTERMIND creation make feature {NONE} code : ARRAY[INTEGER] guess : ARRAY[INTEGER] solved : BOOLEAN quit : BOOLEAN needed : INTEGER make is -- The main procedure of the program. local i : INTEGER answer : STRING do from setcode !!guess.make( 0, 3 ) solved := false needed := 0 until solved or quit loop setguess evaluate end -- convert code into a string from i := code.lower !!answer.make( 4 ) until i > code.upper loop answer.insert( ( code.item( i ) + 48 ).to_character, i ) i := i + 1 end io.print( "Code was: " + answer + "%N" ) io.print( "Number of guesses: " + needed.to_string + "%N%N" ) end -- make feature setcode is -- Randomly sets a code within the range of 1111 to 6666 which the user -- must guess. local i : INTEGER rand : MIN_STAND seed : BASIC_TIME do -- Uses a Random Number Generator Library supplied with -- SmallEiffel to set the code. Takes the product of numerous -- details of the time when the random number generator is being -- seeded as its seed. from seed.update !!rand.with_seed( seed.second + 1 * seed.minute + 1 * seed.hour + 1 * seed.day * seed.month * seed.year * seed.year_day ) !!code.make( 0, 3 ) i := code.lower until i > code.upper loop rand.next code.put( rand.last_integer( 6 ), i ) i := i + 1 end end -- setcode setguess is -- Takes the following inputs from the console input: q or 1, 2, 3, 4, 5 -- and 6. -- Stores the valid guess entered, which must be within the range of -- 1111 to 6666. local i : INTEGER do needed := needed + 1 io.print( "Enter Guess (or q to quit): " ) io.read_line if ( io.last_string.item( 1 ) = 'q' ) then quit := true else from i := guess.lower until i > guess.upper loop guess.put( ( io.last_string.item( i + 1 ).to_integer ) - 48, i ) i := i + 1 end end end -- setguess evaluate is -- Evaluates the current guess and prints the correct number of cows and -- bulls or congratulates the user on getting the correct code and then -- prints the number of guesses needed. local i, x, cows, bulls : INTEGER found : ARRAY[BOOLEAN] do cows := 0 bulls := 0 !!found.make( 0, 3 ) found.set_all_with( false ) if code.is_equal( guess ) then solved := true io.print( "%N%T%T%TWell done, you cracked the code!%N" ) else from i := guess.lower until i > guess.upper or quit loop -- Only continue if the code contains the number -- at i of the guess if code.has( guess.item( i ) ) then -- if the number at i is in the same -- place in both code and guess, then it -- is a bull -- if a number has already been found as -- a cow then decrease cows if code.item( i ) = guess.item( i ) then bulls := bulls + 1 if found.item( i ) then cows := cows - 1 end found.put( true, i ) -- if the number at i is not in the same -- place then it must satisfy the -- following reqirements to be -- considered a cow 1) numbers must be -- the same, 2) not in the same -- position in both guess and code, and -- 3) the number in the code must not -- have been found before else from x := code.lower until x > code.upper loop if guess.item( i ) = code.item( x ) and not( i = x ) and not( found.item( x ) ) then cows := cows + 1 found.put( true, x ) end x := x + 1 end end end i := i + 1 end if not( quit ) then io.print( "Cows: " + cows.to_string + "%N" ) io.print( "Bulls: " + bulls.to_string + "%N" ) end end io.print( "%N" ) end -- evaluate end -- class MASTERMIND