Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- note
- description : "Bagels application"
- class
- BAGELS
- create
- execute, set_answer
- feature -- Initialization
- execute
- -- Play bagels.
- local
- validated: BOOLEAN
- user_digits: STRING
- digits: INTEGER
- do
- from
- validated := false
- until
- validated = true
- loop
- io.put_string ("Enter number of digits to generate: ")
- io.read_line
- user_digits := io.last_string
- if
- user_digits.is_integer
- then
- digits := user_digits.to_integer
- if
- digits > 0 and digits <= 9
- then
- validated := true
- elseif
- digits > 9
- then
- io.put_string ("The number you entered is greater than 9.")
- io.put_string ("%NWith that many numbers the game will not work properly.")
- io.put_string ("%NPlease input a smaller value.")
- io.new_line
- else
- io.put_string ("Please input a positive integer.")
- io.new_line
- end
- else
- io.put_string ("Please input a positive integer.")
- io.new_line
- end
- end
- play (digits)
- end
- feature -- Implementation
- play (d: INTEGER)
- -- Generate a number with `d' digits and let the player guess it.
- require
- d_positive: d > 0
- local
- guess_count: INTEGER
- guess: STRING
- exit_guessing: BOOLEAN
- do
- generate_answer (d)
- guess_count := 0
- from
- exit_guessing := false
- until
- exit_guessing = true
- loop
- io.new_line
- io.put_string ("Your guess: ")
- io.read_line
- guess := io.last_string
- if
- guess.is_empty = false and then guess.count = answer.count and then not guess.has ('0')
- then
- guess_count := guess_count + 1
- if
- guess ~ answer
- then
- io.put_string ("Guess ")
- io.put_integer (guess_count)
- io.put_string (":")
- io.new_line
- io.put_string (clue (guess))
- io.new_line
- io.put_string ("Congratulations! You won!")
- exit_guessing := true
- else
- io.put_string ("Guess ")
- io.put_integer (guess_count)
- io.put_string (":")
- io.new_line
- io.put_string (clue (guess))
- end
- else
- io.put_string ("This is an invalid guess.")
- io.put_string ("%NPlease make sure you do not have any '0' in your guess")
- io.put_string ("%Nand that your guess is not longer than the digits you entered")
- io.put_string ("%Nat the beginning. Digits you entered: ")
- io.put_integer (d)
- end
- end
- end
- answer: STRING
- -- Correct answer.
- set_answer (s: STRING)
- -- Set `answer' to `s'.
- require
- s_non_empty: s /= Void and then not s.is_empty
- is_natural: s.is_natural
- no_zeros: not s.has ('0')
- do
- answer := s
- ensure
- answer_set:answer = s
- end
- generate_answer (d: INTEGER)
- -- Generate a number with `d' non-zero digits and store it in `answer'.
- require
- d_positive: d > 0
- local
- random: V_RANDOM
- i: INTEGER
- temp_answer: STRING
- do
- temp_answer := ""
- from
- create random
- i := 0
- until
- i = d
- loop
- temp_answer.append_integer (random.bounded_item (1, 9))
- random.forth
- i := i + 1
- end
- set_answer (temp_answer)
- ensure
- answer_exists: answer /= Void
- correct_length: answer.count = d
- is_natural: answer.is_natural
- no_zeros: not answer.has ('0')
- end
- clue (guess: STRING): STRING
- -- Clue for `guess' with respect to `answer'.
- require
- answer_exists: answer /= Void
- guess_exists: guess /= Void
- no_zeros: not guess.has ('0') -- Custom condition
- same_length: answer.count = guess.count
- local
- i, c, d, fermi_count, pico_count: INTEGER
- temp_clue: STRING
- temp_char: CHARACTER
- do
- c := 0
- d := 1
- fermi_count := 0
- pico_count := 0
- temp_clue := ""
- from
- i := 1
- until
- i = guess.count + 1
- loop
- if
- guess.at (i) = answer.at (i)
- then
- fermi_count := fermi_count + 1
- i := i + 1
- elseif
- answer.has (guess.at (i)) and answer.occurrences (guess.at (i)) >= guess.occurrences (guess.at (i))
- then
- pico_count := pico_count + 1
- i := i + 1
- else
- i := i + 1
- end
- end
- if
- fermi_count = 0 and pico_count = 0
- then
- temp_clue := "Bagels"
- else
- from
- c := 0
- until
- c = fermi_count
- loop
- temp_clue.append ("Fermi ")
- c := c + 1
- end
- from
- c := 0
- until
- c = pico_count
- loop
- temp_clue.append ("Pico ")
- c := c + 1
- end
- end
- Result := temp_clue
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement