-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgame_logic.rb
48 lines (41 loc) · 1.1 KB
/
game_logic.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# frozen_string_literal: true
# game logic for human_solver and computer_solver
module GameLogic
def compare(master, guess)
temp_master = master.clone
temp_guess = guess.clone
@exact_number = exact_matches(temp_master, temp_guess)
@same_number = right_numbers(temp_master, temp_guess)
@total_number = exact_number + same_number
end
def exact_matches(master, guess)
exact = 0
master.each_with_index do |item, index|
next unless item == guess[index]
exact += 1
master[index] = '*'
guess[index] = '*'
end
exact
end
def right_numbers(master, guess)
same = 0
guess.each_index do |index|
next unless guess[index] != '*' && master.include?(guess[index])
same += 1
remove = master.find_index(guess[index])
master[remove] = '?'
guess[index] = '?'
end
same
end
def solved?(master, guess)
master == guess
end
def repeat_game
puts game_message('repeat_prompt')
replay = gets.chomp
puts game_message('thanks') if replay.downcase != 'y'
Game.new.play if replay.downcase == 'y'
end
end