Advertisement
Guest User

Untitled

a guest
Aug 2nd, 2016
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Eiffel 11.25 KB | None | 0 0
  1. note
  2.     description: "Summary description for {JOTTO}."
  3.     author: "Marmara El Masri 213630892 cse_user:marmara    Nicolas Jaramillo 211456639 cse_user: andrew68"
  4.     date: "$Date$"
  5.     revision: "$Revision$"
  6.  
  7. class
  8.     JOTTO_ALGORITHM
  9. create
  10.     make
  11. feature
  12.     LENGTH:INTEGER
  13.     once
  14.         result := 3
  15.     end
  16.     ONE:INTEGER
  17.     once
  18.         result := 1
  19.     end
  20.  
  21.     jotto_data_struct:JOTTO_DATA_STRUCT
  22. feature{ANY}
  23.  
  24.     make(data_struct:JOTTO_DATA_STRUCT)
  25.     do
  26.         --hold reference to jotto_data_struct so that it can add to/view what is it correct_characters, eliminated_characters, and the user guesses map
  27.         jotto_data_struct := data_struct
  28.     end
  29.  
  30.  
  31.     to_char_array(n: STRING): ARRAY[CHARACTER]
  32.     local
  33.         out_array:ARRAY[CHARACTER]
  34.         i:INTEGER
  35.     do
  36.         --converts the string "n" to an array of characters
  37.         --initialize char array
  38.         create out_array.make_filled ('0', 0, n.count - 1)
  39.         --loop over string and add each chracter into out_array
  40.         from
  41.             i := 1
  42.         until
  43.             i > n.count
  44.         loop
  45.             out_array[i-1] := n.at (i)
  46.             i:= i + 1
  47.         end
  48.         result := out_array
  49.     end
  50.  
  51.     --checks how many characters in the string provided by the user are common with the secret string
  52.     common_with_secret_size(a:STRING):INTEGER
  53.     require
  54.         a_proper_size : a.count = LENGTH
  55.     local
  56.         aletters:ARRAY[CHARACTER]
  57.         count, i:INTEGER
  58.     do
  59.         --first step: converts user input to an array of characters so it can compare it with the secret set more easily
  60.         aletters := to_char_array(a)
  61.         count := 0
  62.         --loop over user input's character array representation
  63.         from i := 0
  64.         until i >= aletters.count
  65.         loop
  66.             -- for each character inside the user's input, check if this character is also contained in the secret string (through jotto_data_struct)
  67.             if (jotto_data_struct.secret_contains(aletters[i])) then
  68.                 count := count + 1
  69.             end
  70.             i := i + 1
  71.         end
  72.         --note: if the count = 3, this does not necessarily mean that the user's guess is correct as order could still be incorrect
  73.         --another method takes care of checking order
  74.         result := count
  75.     end
  76.  
  77.     different_character(a: STRING ; b: STRING): LINKED_SET[CHARACTER]
  78.     local
  79.         i,j:INTEGER
  80.         found,exit: BOOLEAN
  81.         different_characters:LINKED_SET[CHARACTER]
  82.         aletters: ARRAY[CHARACTER]
  83.         bletters: ARRAY[CHARACTER]
  84.     do
  85.         --change string to array
  86.         create different_characters.make
  87.         aletters := to_char_array(a)
  88.         bletters := to_char_array(b)
  89.         from
  90.             i := 0
  91.         until
  92.             i >= aletters.count
  93.         loop
  94.             found := false
  95.             exit := false
  96.             from
  97.                 j := 0
  98.             until
  99.                 j >= bletters.count or exit
  100.             loop
  101.  
  102.                 if(aletters[i] ~ bletters[j])
  103.                 then
  104.                     found := true
  105.                     exit := true
  106.                 end
  107.                 j:= j + 1
  108.             end
  109.             if(not found)
  110.             then
  111.                 different_characters.extend(aletters[i])
  112.             end
  113.             i := i + 1
  114.         end
  115.  
  116.         result := different_characters
  117.  
  118.     end
  119.  
  120.     get_safe_characters(a: STRING): LINKED_SET[CHARACTER]
  121.     local
  122.         aletters: ARRAY[CHARACTER]
  123.         safe_characters: LINKED_SET[CHARACTER]
  124.         i:INTEGER
  125.     do
  126.         create safe_characters.make
  127.         aletters := to_char_array(a)
  128.  
  129.         from
  130.             i := 0
  131.         until
  132.             i >= aletters.count
  133.         loop
  134.             if(not jotto_data_struct.eliminated_char_contains(aletters[i]) and not jotto_data_struct.correct_char_contains(aletters[i]))
  135.             then
  136.                 safe_characters.extend(aletters[i])
  137.             end
  138.             i:= i + 1
  139.         end
  140.         result := safe_characters
  141.  
  142.     end
  143.  
  144.  
  145.     in_common_with_set(a: STRING; set: LINKED_SET[CHARACTER]): LINKED_SET[CHARACTER]
  146.     local
  147.         aletters: ARRAY[CHARACTER]
  148.         in_common: LINKED_SET[CHARACTER]
  149.         i:INTEGER
  150.     do
  151.         create in_common.make
  152.         aletters := to_char_array(a)
  153.  
  154.         from
  155.             i := 0
  156.         until
  157.             i >= aletters.count
  158.         loop
  159.             if(set.has (aletters[i]))
  160.             then
  161.                 in_common.extend(aletters[i])
  162.             end
  163.             i:= i + 1
  164.         end
  165.         result := in_common
  166.  
  167.     end
  168.  
  169.     in_common_with_correct(a:STRING):LINKED_SET[CHARACTER]
  170.     local
  171.         in_common: LINKED_SET[CHARACTER]
  172.         i:INTEGER
  173.         aletters : ARRAY[CHARACTER]
  174.     do
  175.         create in_common.make
  176.         aletters := to_char_array(a)
  177.         from i := 0
  178.         until i >= aletters.count
  179.         loop
  180.             if (jotto_data_struct.correct_char_contains (aletters[i])) then
  181.                 in_common.extend (aletters[i])
  182.             end
  183.             i:= i + 1
  184.         end
  185.         result := in_common
  186.     end
  187.  
  188.     in_common_with_eliminated(a:STRING):LINKED_SET[CHARACTER]
  189.     local
  190.         in_common: LINKED_SET[CHARACTER]
  191.         i:INTEGER
  192.         aletters : ARRAY[CHARACTER]
  193.     do
  194.         create in_common.make
  195.         aletters := to_char_array(a)
  196.         from i := 0
  197.         until i >= aletters.count
  198.         loop
  199.             if (jotto_data_struct.eliminated_char_contains (aletters[i])) then
  200.                 in_common.extend (aletters[i])
  201.             end
  202.             i:= i + 1
  203.         end
  204.         result := in_common
  205.     end
  206.  
  207.     not_common_with_set(a: STRING; set: LINKED_SET[CHARACTER]): LINKED_SET[CHARACTER]
  208.     local
  209.         aletters: ARRAY[CHARACTER]
  210.         not_common: LINKED_SET[CHARACTER]
  211.         i:INTEGER
  212.     do
  213.         create not_common.make
  214.         aletters := to_char_array(a)
  215.  
  216.         from
  217.             i := 0
  218.         until
  219.             i >= aletters.count
  220.         loop
  221.             if(not set.has (aletters[i]))
  222.             then
  223.                 not_common.extend(aletters[i])
  224.             end
  225.             i:= i + 1
  226.         end
  227.         result := not_common
  228.  
  229.     end
  230.  
  231.     not_common_with_corr(a:STRING):LINKED_SET[CHARACTER]
  232.     local
  233.         not_in_common: LINKED_SET[CHARACTER]
  234.         i:INTEGER
  235.         aletters : ARRAY[CHARACTER]
  236.     do
  237.         create not_in_common.make
  238.         aletters := to_char_array(a)
  239.         from i := 0
  240.         until i >= aletters.count
  241.         loop
  242.             if (not jotto_data_struct.correct_char_contains (aletters[i])) then
  243.                 not_in_common.extend (aletters[i])
  244.             end
  245.             i:= i + 1
  246.         end
  247.         result := not_in_common
  248.     end
  249.  
  250.     not_common_with_elim(a:STRING):LINKED_SET[CHARACTER]
  251.     local
  252.         not_in_common: LINKED_SET[CHARACTER]
  253.         i:INTEGER
  254.         aletters : ARRAY[CHARACTER]
  255.     do
  256.         create not_in_common.make
  257.         aletters := to_char_array(a)
  258.         from i := 0
  259.         until i >= aletters.count
  260.         loop
  261.             if (not jotto_data_struct.eliminated_char_contains (aletters[i])) then
  262.                 not_in_common.extend (aletters[i])
  263.             end
  264.             i:= i + 1
  265.         end
  266.         result := not_in_common
  267.     end
  268.  
  269.     guess_corr_elim_common(a: STRING): INTEGER
  270.     local
  271.         i,count:INTEGER
  272.         aletters: ARRAY[CHARACTER]
  273.     do
  274.         count :=0
  275.         aletters := to_char_array(a)
  276.         from
  277.             i := 0
  278.         until
  279.             i >= aletters.count
  280.         loop
  281.             if (jotto_data_struct.correct_char_contains (aletters[i]) or jotto_data_struct.eliminated_char_contains (aletters[i]))
  282.             then
  283.                 count := count + 1
  284.             end
  285.             i:= i + 1
  286.         end
  287.         result := count
  288.  
  289.     end
  290.  
  291.     next_line:STRING
  292.     local
  293.         input: STRING
  294.     do
  295.         io.read_line
  296.         input := io.last_string
  297.         result := input
  298.  
  299.     end
  300.  
  301.     check_correct_order(user_guess_array: ARRAY[CHARACTER]):BOOLEAN
  302.     local
  303.         count,j:INTEGER
  304.         output:BOOLEAN
  305.         secret_iter:SET_ITERATOR[CHARACTER]
  306.     do
  307.  
  308.         from
  309.             j:=0
  310.         until
  311.             j >= user_guess_array.count
  312.         loop
  313.             jotto_data_struct.add_to_correct (user_guess_array[j])
  314.             j:= j + 1
  315.  
  316.         end
  317.         secret_iter := jotto_data_struct.secret_iterator
  318.         from j := 1
  319.         until secret_iter.all_done
  320.         loop
  321.             if (secret_iter.next ~ user_guess_array[j - 1]) then
  322.                 count := count + 1
  323.             end
  324.             j := j + 1
  325.         end
  326.  
  327.         if count ~ 3
  328.         then
  329.             output := true
  330.         end
  331.         result := output
  332.     end
  333.  
  334.  
  335.     run_operation(input: STRING):BOOLEAN
  336.     local
  337.  
  338.         prev_guess: STRING
  339.         correct,wrong: CHARACTER
  340.         i, num_correct_char,pre_num_of_hits: INTEGER
  341.         win: BOOLEAN
  342.         user_guess_array: ARRAY[CHARACTER]
  343.         user_guesses_iter:MAP_ITERATOR[STRING, INTEGER]
  344.         in_prev_not_current,in_current_not_prev, current_and_corr_common, current_and_elim_common, safe_for_now_current, safe_for_now, prev_and_corr_common, prev_and_elim_common, not_common:LINKED_SET[CHARACTER]
  345.         secret_iter, eliminated_iter, correct_iter:SET_ITERATOR[CHARACTER]
  346.     do
  347.         create in_prev_not_current.make
  348.         create in_current_not_prev.make
  349.  
  350.         num_correct_char := 0
  351.  
  352.         user_guess_array := to_char_array (input)
  353.         num_correct_char := common_with_secret_size(input)
  354.         if (num_correct_char ~ 3)
  355.         then
  356.             win := check_correct_order(user_guess_array)
  357.             if win
  358.             then
  359.                 print("%NYOU WIN! The secret number is: " + input.out + "%N");
  360.  
  361.             end
  362.         end
  363.  
  364.         if(not win)
  365.         then
  366.             if(num_correct_char ~ 0)
  367.             then
  368.                 from
  369.                     i := 0
  370.                 until
  371.                     i >= user_guess_array.count
  372.                 loop
  373.                     jotto_data_struct.add_to_eliminated (user_guess_array[i])
  374.                     i:= i + 1
  375.                 end
  376.             end
  377.  
  378.             user_guesses_iter := jotto_data_struct.user_guesses_iterator
  379.             from
  380.             until
  381.                 user_guesses_iter.all_done
  382.             loop
  383.                 prev_guess:= user_guesses_iter.next
  384.                 pre_num_of_hits := jotto_data_struct.get_previous_hint(prev_guess)
  385.                 if(num_correct_char ~ pre_num_of_hits + 1)
  386.                 then
  387.                     in_prev_not_current := different_character(prev_guess, input)
  388.                     if(in_prev_not_current.count ~ 1)
  389.                     then
  390.                         in_current_not_prev := different_character(input, prev_guess)
  391.                         correct := in_current_not_prev.i_th (1)
  392.                         wrong := in_prev_not_current.i_th (1)
  393.                         jotto_data_struct.add_to_eliminated (wrong)
  394.                         jotto_data_struct.add_to_correct (correct)
  395.                     end
  396.                 elseif(num_correct_char ~ pre_num_of_hits - 1)
  397.                 then
  398.                     in_prev_not_current := different_character(prev_guess, input)
  399.                     if(in_prev_not_current.count ~ 1)
  400.                     then
  401.                         in_current_not_prev := different_character(input, prev_guess)
  402.                         correct := in_prev_not_current.i_th (1)
  403.                         wrong :=in_current_not_prev.i_th (1)
  404.                         jotto_data_struct.add_to_eliminated (wrong)
  405.                         jotto_data_struct.add_to_correct (correct)
  406.                     end
  407.                 end
  408.             end
  409.             current_and_corr_common := in_common_with_correct(input)
  410.             current_and_elim_common := in_common_with_eliminated(input)
  411.             safe_for_now_current := get_safe_characters(input)
  412.             if (current_and_corr_common.count ~ num_correct_char)
  413.             then
  414.             not_common := not_common_with_corr(input)
  415.             jotto_data_struct.add_all_to_eliminated (not_common)
  416.             end
  417.  
  418.             if (safe_for_now_current.count ~ 1 and current_and_corr_common.count ~ num_correct_char - 1)
  419.             then
  420.                 jotto_data_struct.add_all_to_correct (safe_for_now_current)
  421.             end
  422.  
  423.             if (current_and_elim_common.count ~ LENGTH - num_correct_char) then
  424.                 jotto_data_struct.add_all_to_correct (safe_for_now_current)
  425.             end
  426.  
  427.             user_guesses_iter.reset
  428.             from
  429.  
  430.             until
  431.                 user_guesses_iter.all_done
  432.             loop
  433.                  prev_guess:= user_guesses_iter.next
  434.                  safe_for_now := get_safe_characters(prev_guess)
  435.                  prev_and_corr_common := in_common_with_correct(prev_guess)
  436.                  prev_and_elim_common := in_common_with_eliminated(prev_guess)
  437.                  if (safe_for_now.count ~ 1 and prev_and_corr_common.count ~ jotto_data_struct.get_previous_hint (prev_guess) - 1)
  438.                  then
  439.                     jotto_data_struct.add_all_to_correct (safe_for_now)
  440.  
  441.                 elseif (prev_and_corr_common.count ~ jotto_data_struct.get_previous_hint (prev_guess))
  442.                 then
  443.                     not_common := not_common_with_corr(prev_guess)
  444.                     jotto_data_struct.add_all_to_eliminated (not_common)
  445.                 elseif (safe_for_now.count ~ jotto_data_struct.get_previous_hint (prev_guess) and ((LENGTH - safe_for_now.count) ~ prev_and_elim_common.count))
  446.                 then
  447.                     not_common := not_common_with_elim(prev_guess)
  448.                     jotto_data_struct.add_all_to_correct (not_common)
  449.                 end
  450.             end
  451.  
  452.             end
  453.             jotto_data_struct.add_to_user_guesses (input, num_correct_char)
  454.             result := win
  455.             end
  456.  
  457.     get_data_struct:JOTTO_DATA_STRUCT
  458.     do
  459.         result := jotto_data_struct.deep_twin
  460.     end
  461. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement