Advertisement
PhilDrummer

Assignment_6 02

Oct 26th, 2014
2,745
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Eiffel 4.52 KB | None | 0 0
  1. note
  2.     description : "Bagels application"
  3.  
  4. class
  5.     BAGELS
  6.  
  7. create
  8.     execute, set_answer
  9.  
  10. feature -- Initialization
  11.     execute
  12.             -- Play bagels.
  13.         local
  14.             validated: BOOLEAN
  15.             user_digits: STRING
  16.             digits: INTEGER
  17.         do
  18.             from
  19.                 validated := false
  20.             until
  21.                 validated = true
  22.             loop
  23.                 io.put_string ("Enter number of digits to generate: ")
  24.                 io.read_line
  25.                 user_digits := io.last_string
  26.                 if
  27.                     user_digits.is_integer
  28.                 then
  29.                     digits := user_digits.to_integer
  30.                     if
  31.                         digits > 0 and digits <= 9
  32.                     then
  33.                         validated := true
  34.                     elseif
  35.                         digits > 9
  36.                     then
  37.                         io.put_string ("The number you entered is greater than 9.")
  38.                         io.put_string ("%NWith that many numbers the game will not work properly.")
  39.                         io.put_string ("%NPlease input a smaller value.")
  40.                         io.new_line
  41.                     else
  42.                         io.put_string ("Please input a positive integer.")
  43.                         io.new_line
  44.                     end
  45.                 else
  46.                     io.put_string ("Please input a positive integer.")
  47.                     io.new_line
  48.                 end
  49.             end
  50.             play (digits)
  51.         end
  52.  
  53. feature -- Implementation
  54.  
  55.     play (d: INTEGER)
  56.             -- Generate a number with `d' digits and let the player guess it.
  57.         require
  58.             d_positive: d > 0
  59.         local
  60.             guess_count: INTEGER
  61.             guess: STRING
  62.             exit_guessing: BOOLEAN
  63.         do
  64.             generate_answer (d)
  65.             guess_count := 0
  66.             from
  67.                 exit_guessing := false
  68.             until
  69.                 exit_guessing = true
  70.             loop
  71.                 io.new_line
  72.                 io.put_string ("Your guess: ")
  73.                 io.read_line
  74.                 guess := io.last_string
  75.                 if
  76.                     guess.is_empty = false and then guess.count = answer.count and then not guess.has ('0')
  77.                 then
  78.                     guess_count := guess_count + 1
  79.                     if
  80.                         guess ~ answer
  81.                     then
  82.                         io.put_string ("Guess ")
  83.                         io.put_integer (guess_count)
  84.                         io.put_string (":")
  85.                         io.new_line
  86.                         io.put_string (clue (guess))
  87.                         io.new_line
  88.                         io.put_string ("Congratulations! You won!")
  89.                         exit_guessing := true
  90.                     else
  91.                         io.put_string ("Guess ")
  92.                         io.put_integer (guess_count)
  93.                         io.put_string (":")
  94.                         io.new_line
  95.                         io.put_string (clue (guess))
  96.                     end
  97.                 else
  98.                     io.put_string ("This is an invalid guess.")
  99.                     io.put_string ("%NPlease make sure you do not have any '0' in your guess")
  100.                     io.put_string ("%Nand that your guess is not longer than the digits you entered")
  101.                     io.put_string ("%Nat the beginning. Digits you entered: ")
  102.                     io.put_integer (d)
  103.                 end
  104.             end
  105.         end
  106.  
  107.  
  108.     answer: STRING
  109.             -- Correct answer.     
  110.  
  111.     set_answer (s: STRING)
  112.             -- Set `answer' to `s'.    
  113.         require
  114.             s_non_empty: s /= Void and then not s.is_empty
  115.             is_natural: s.is_natural
  116.             no_zeros: not s.has ('0')
  117.         do
  118.             answer := s
  119.         ensure
  120.             answer_set:answer = s
  121.         end
  122.  
  123.     generate_answer (d: INTEGER)
  124.             -- Generate a number with `d' non-zero digits and store it in `answer'.
  125.         require
  126.             d_positive: d > 0
  127.         local
  128.             random: V_RANDOM
  129.             i: INTEGER
  130.             temp_answer: STRING
  131.         do
  132.             temp_answer := ""
  133.             from
  134.                 create random
  135.                 i := 0
  136.             until
  137.                 i = d
  138.             loop
  139.                 temp_answer.append_integer (random.bounded_item (1, 9))
  140.                 random.forth
  141.                 i := i + 1
  142.             end
  143.             set_answer (temp_answer)
  144.         ensure
  145.             answer_exists: answer /= Void
  146.             correct_length: answer.count = d
  147.             is_natural: answer.is_natural
  148.             no_zeros: not answer.has ('0')
  149.         end
  150.  
  151.  
  152.     clue (guess: STRING): STRING
  153.             -- Clue for `guess' with respect to `answer'.
  154.         require
  155.             answer_exists: answer /= Void
  156.             guess_exists: guess /= Void
  157.             no_zeros: not guess.has ('0') -- Custom condition
  158.             same_length: answer.count = guess.count
  159.         local
  160.             i, c, d, fermi_count, pico_count: INTEGER
  161.             temp_clue: STRING
  162.             temp_char: CHARACTER
  163.         do
  164.             c := 0
  165.             d := 1
  166.             fermi_count := 0
  167.             pico_count := 0
  168.             temp_clue := ""
  169.  
  170.             from
  171.                 i := 1
  172.             until
  173.                 i = guess.count + 1
  174.             loop
  175.                 if
  176.                     guess.at (i) = answer.at (i)
  177.                 then
  178.                     fermi_count := fermi_count + 1
  179.                     i := i + 1
  180.                 elseif
  181.                     answer.has (guess.at (i)) and answer.occurrences (guess.at (i)) >= guess.occurrences (guess.at (i))
  182.                 then
  183.                     pico_count := pico_count + 1
  184.                     i := i + 1
  185.                 else
  186.                     i := i + 1
  187.                 end
  188.             end
  189.  
  190.             if
  191.                 fermi_count = 0 and pico_count = 0
  192.             then
  193.                 temp_clue := "Bagels"
  194.             else
  195.                 from
  196.                     c := 0
  197.                 until
  198.                     c = fermi_count
  199.                 loop
  200.                     temp_clue.append ("Fermi ")
  201.                     c := c + 1
  202.                 end
  203.  
  204.                 from
  205.                     c := 0
  206.                 until
  207.                     c = pico_count
  208.                 loop
  209.                     temp_clue.append ("Pico ")
  210.                     c := c + 1
  211.                 end
  212.             end
  213.             Result := temp_clue
  214.         end
  215. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement