-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathhuman_solver.rb
71 lines (57 loc) · 1.46 KB
/
human_solver.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# frozen_string_literal: true
require './game_logic'
require './display'
require './text_content'
# class for code_breaker game option
class HumanSolver
attr_reader :computer_code, :guess, :exact_number, :same_number
include GameLogic
include Display
include TextContent
def initialize
random_numbers = [rand(1..6), rand(1..6), rand(1..6), rand(1..6)]
@computer_code = random_numbers.map(&:to_s)
end
def player_turns
puts turn_message('breaker_start')
turn_order
human_game_over(computer_code, guess)
end
def turn_order
turn = 1
while turn <= 12
turn_messages(turn)
@guess = player_input.split(//)
turn += 1
break if guess[0].downcase == 'q'
show_code(guess)
break if solved?(computer_code, guess)
turn_outcome
end
end
def turn_messages(turn)
puts turn_message('guess_prompt', turn)
puts warning_message('last_turn') if turn == 12
end
def player_input
input = gets.chomp
return input if input.match(/^[1-6]{4}$/)
return input if input.downcase == 'q'
puts warning_message('turn_error')
player_input
end
def turn_outcome
compare(computer_code, guess)
show_clues(exact_number, same_number)
end
def human_game_over(master, guess)
if solved?(master, guess)
puts game_message('human_won')
else
puts warning_message('game_over')
puts game_message('display_code')
show_code(master)
end
repeat_game
end
end