Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- note
- description: "Summary description for {JOTTO}."
- author: "Marmara El Masri 213630892 cse_user:marmara Nicolas Jaramillo 211456639 cse_user: andrew68"
- date: "$Date$"
- revision: "$Revision$"
- class
- JOTTO_ALGORITHM
- create
- make
- feature
- LENGTH:INTEGER
- once
- result := 3
- end
- ONE:INTEGER
- once
- result := 1
- end
- jotto_data_struct:JOTTO_DATA_STRUCT
- feature{ANY}
- make(data_struct:JOTTO_DATA_STRUCT)
- do
- --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
- jotto_data_struct := data_struct
- end
- to_char_array(n: STRING): ARRAY[CHARACTER]
- local
- out_array:ARRAY[CHARACTER]
- i:INTEGER
- do
- --converts the string "n" to an array of characters
- --initialize char array
- create out_array.make_filled ('0', 0, n.count - 1)
- --loop over string and add each chracter into out_array
- from
- i := 1
- until
- i > n.count
- loop
- out_array[i-1] := n.at (i)
- i:= i + 1
- end
- result := out_array
- end
- --checks how many characters in the string provided by the user are common with the secret string
- common_with_secret_size(a:STRING):INTEGER
- require
- a_proper_size : a.count = LENGTH
- local
- aletters:ARRAY[CHARACTER]
- count, i:INTEGER
- do
- --first step: converts user input to an array of characters so it can compare it with the secret set more easily
- aletters := to_char_array(a)
- count := 0
- --loop over user input's character array representation
- from i := 0
- until i >= aletters.count
- loop
- -- for each character inside the user's input, check if this character is also contained in the secret string (through jotto_data_struct)
- if (jotto_data_struct.secret_contains(aletters[i])) then
- count := count + 1
- end
- i := i + 1
- end
- --note: if the count = 3, this does not necessarily mean that the user's guess is correct as order could still be incorrect
- --another method takes care of checking order
- result := count
- end
- different_character(a: STRING ; b: STRING): LINKED_SET[CHARACTER]
- local
- i,j:INTEGER
- found,exit: BOOLEAN
- different_characters:LINKED_SET[CHARACTER]
- aletters: ARRAY[CHARACTER]
- bletters: ARRAY[CHARACTER]
- do
- --change string to array
- create different_characters.make
- aletters := to_char_array(a)
- bletters := to_char_array(b)
- from
- i := 0
- until
- i >= aletters.count
- loop
- found := false
- exit := false
- from
- j := 0
- until
- j >= bletters.count or exit
- loop
- if(aletters[i] ~ bletters[j])
- then
- found := true
- exit := true
- end
- j:= j + 1
- end
- if(not found)
- then
- different_characters.extend(aletters[i])
- end
- i := i + 1
- end
- result := different_characters
- end
- get_safe_characters(a: STRING): LINKED_SET[CHARACTER]
- local
- aletters: ARRAY[CHARACTER]
- safe_characters: LINKED_SET[CHARACTER]
- i:INTEGER
- do
- create safe_characters.make
- aletters := to_char_array(a)
- from
- i := 0
- until
- i >= aletters.count
- loop
- if(not jotto_data_struct.eliminated_char_contains(aletters[i]) and not jotto_data_struct.correct_char_contains(aletters[i]))
- then
- safe_characters.extend(aletters[i])
- end
- i:= i + 1
- end
- result := safe_characters
- end
- in_common_with_set(a: STRING; set: LINKED_SET[CHARACTER]): LINKED_SET[CHARACTER]
- local
- aletters: ARRAY[CHARACTER]
- in_common: LINKED_SET[CHARACTER]
- i:INTEGER
- do
- create in_common.make
- aletters := to_char_array(a)
- from
- i := 0
- until
- i >= aletters.count
- loop
- if(set.has (aletters[i]))
- then
- in_common.extend(aletters[i])
- end
- i:= i + 1
- end
- result := in_common
- end
- in_common_with_correct(a:STRING):LINKED_SET[CHARACTER]
- local
- in_common: LINKED_SET[CHARACTER]
- i:INTEGER
- aletters : ARRAY[CHARACTER]
- do
- create in_common.make
- aletters := to_char_array(a)
- from i := 0
- until i >= aletters.count
- loop
- if (jotto_data_struct.correct_char_contains (aletters[i])) then
- in_common.extend (aletters[i])
- end
- i:= i + 1
- end
- result := in_common
- end
- in_common_with_eliminated(a:STRING):LINKED_SET[CHARACTER]
- local
- in_common: LINKED_SET[CHARACTER]
- i:INTEGER
- aletters : ARRAY[CHARACTER]
- do
- create in_common.make
- aletters := to_char_array(a)
- from i := 0
- until i >= aletters.count
- loop
- if (jotto_data_struct.eliminated_char_contains (aletters[i])) then
- in_common.extend (aletters[i])
- end
- i:= i + 1
- end
- result := in_common
- end
- not_common_with_set(a: STRING; set: LINKED_SET[CHARACTER]): LINKED_SET[CHARACTER]
- local
- aletters: ARRAY[CHARACTER]
- not_common: LINKED_SET[CHARACTER]
- i:INTEGER
- do
- create not_common.make
- aletters := to_char_array(a)
- from
- i := 0
- until
- i >= aletters.count
- loop
- if(not set.has (aletters[i]))
- then
- not_common.extend(aletters[i])
- end
- i:= i + 1
- end
- result := not_common
- end
- not_common_with_corr(a:STRING):LINKED_SET[CHARACTER]
- local
- not_in_common: LINKED_SET[CHARACTER]
- i:INTEGER
- aletters : ARRAY[CHARACTER]
- do
- create not_in_common.make
- aletters := to_char_array(a)
- from i := 0
- until i >= aletters.count
- loop
- if (not jotto_data_struct.correct_char_contains (aletters[i])) then
- not_in_common.extend (aletters[i])
- end
- i:= i + 1
- end
- result := not_in_common
- end
- not_common_with_elim(a:STRING):LINKED_SET[CHARACTER]
- local
- not_in_common: LINKED_SET[CHARACTER]
- i:INTEGER
- aletters : ARRAY[CHARACTER]
- do
- create not_in_common.make
- aletters := to_char_array(a)
- from i := 0
- until i >= aletters.count
- loop
- if (not jotto_data_struct.eliminated_char_contains (aletters[i])) then
- not_in_common.extend (aletters[i])
- end
- i:= i + 1
- end
- result := not_in_common
- end
- guess_corr_elim_common(a: STRING): INTEGER
- local
- i,count:INTEGER
- aletters: ARRAY[CHARACTER]
- do
- count :=0
- aletters := to_char_array(a)
- from
- i := 0
- until
- i >= aletters.count
- loop
- if (jotto_data_struct.correct_char_contains (aletters[i]) or jotto_data_struct.eliminated_char_contains (aletters[i]))
- then
- count := count + 1
- end
- i:= i + 1
- end
- result := count
- end
- next_line:STRING
- local
- input: STRING
- do
- io.read_line
- input := io.last_string
- result := input
- end
- check_correct_order(user_guess_array: ARRAY[CHARACTER]):BOOLEAN
- local
- count,j:INTEGER
- output:BOOLEAN
- secret_iter:SET_ITERATOR[CHARACTER]
- do
- from
- j:=0
- until
- j >= user_guess_array.count
- loop
- jotto_data_struct.add_to_correct (user_guess_array[j])
- j:= j + 1
- end
- secret_iter := jotto_data_struct.secret_iterator
- from j := 1
- until secret_iter.all_done
- loop
- if (secret_iter.next ~ user_guess_array[j - 1]) then
- count := count + 1
- end
- j := j + 1
- end
- if count ~ 3
- then
- output := true
- end
- result := output
- end
- run_operation(input: STRING):BOOLEAN
- local
- prev_guess: STRING
- correct,wrong: CHARACTER
- i, num_correct_char,pre_num_of_hits: INTEGER
- win: BOOLEAN
- user_guess_array: ARRAY[CHARACTER]
- user_guesses_iter:MAP_ITERATOR[STRING, INTEGER]
- 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]
- secret_iter, eliminated_iter, correct_iter:SET_ITERATOR[CHARACTER]
- do
- create in_prev_not_current.make
- create in_current_not_prev.make
- num_correct_char := 0
- user_guess_array := to_char_array (input)
- num_correct_char := common_with_secret_size(input)
- if (num_correct_char ~ 3)
- then
- win := check_correct_order(user_guess_array)
- if win
- then
- print("%NYOU WIN! The secret number is: " + input.out + "%N");
- end
- end
- if(not win)
- then
- if(num_correct_char ~ 0)
- then
- from
- i := 0
- until
- i >= user_guess_array.count
- loop
- jotto_data_struct.add_to_eliminated (user_guess_array[i])
- i:= i + 1
- end
- end
- user_guesses_iter := jotto_data_struct.user_guesses_iterator
- from
- until
- user_guesses_iter.all_done
- loop
- prev_guess:= user_guesses_iter.next
- pre_num_of_hits := jotto_data_struct.get_previous_hint(prev_guess)
- if(num_correct_char ~ pre_num_of_hits + 1)
- then
- in_prev_not_current := different_character(prev_guess, input)
- if(in_prev_not_current.count ~ 1)
- then
- in_current_not_prev := different_character(input, prev_guess)
- correct := in_current_not_prev.i_th (1)
- wrong := in_prev_not_current.i_th (1)
- jotto_data_struct.add_to_eliminated (wrong)
- jotto_data_struct.add_to_correct (correct)
- end
- elseif(num_correct_char ~ pre_num_of_hits - 1)
- then
- in_prev_not_current := different_character(prev_guess, input)
- if(in_prev_not_current.count ~ 1)
- then
- in_current_not_prev := different_character(input, prev_guess)
- correct := in_prev_not_current.i_th (1)
- wrong :=in_current_not_prev.i_th (1)
- jotto_data_struct.add_to_eliminated (wrong)
- jotto_data_struct.add_to_correct (correct)
- end
- end
- end
- current_and_corr_common := in_common_with_correct(input)
- current_and_elim_common := in_common_with_eliminated(input)
- safe_for_now_current := get_safe_characters(input)
- if (current_and_corr_common.count ~ num_correct_char)
- then
- not_common := not_common_with_corr(input)
- jotto_data_struct.add_all_to_eliminated (not_common)
- end
- if (safe_for_now_current.count ~ 1 and current_and_corr_common.count ~ num_correct_char - 1)
- then
- jotto_data_struct.add_all_to_correct (safe_for_now_current)
- end
- if (current_and_elim_common.count ~ LENGTH - num_correct_char) then
- jotto_data_struct.add_all_to_correct (safe_for_now_current)
- end
- user_guesses_iter.reset
- from
- until
- user_guesses_iter.all_done
- loop
- prev_guess:= user_guesses_iter.next
- safe_for_now := get_safe_characters(prev_guess)
- prev_and_corr_common := in_common_with_correct(prev_guess)
- prev_and_elim_common := in_common_with_eliminated(prev_guess)
- if (safe_for_now.count ~ 1 and prev_and_corr_common.count ~ jotto_data_struct.get_previous_hint (prev_guess) - 1)
- then
- jotto_data_struct.add_all_to_correct (safe_for_now)
- elseif (prev_and_corr_common.count ~ jotto_data_struct.get_previous_hint (prev_guess))
- then
- not_common := not_common_with_corr(prev_guess)
- jotto_data_struct.add_all_to_eliminated (not_common)
- elseif (safe_for_now.count ~ jotto_data_struct.get_previous_hint (prev_guess) and ((LENGTH - safe_for_now.count) ~ prev_and_elim_common.count))
- then
- not_common := not_common_with_elim(prev_guess)
- jotto_data_struct.add_all_to_correct (not_common)
- end
- end
- end
- jotto_data_struct.add_to_user_guesses (input, num_correct_char)
- result := win
- end
- get_data_struct:JOTTO_DATA_STRUCT
- do
- result := jotto_data_struct.deep_twin
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement