-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboard.rb
83 lines (72 loc) · 2.76 KB
/
board.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
72
73
74
75
76
77
78
79
80
81
82
83
require_relative 'feedback'
require_relative 'code'
require_relative 'computer'
require_relative 'player'
require_relative 'colorize'
# This class will create the board w/ nested array
# Also display the board for user to see while playing
class Board
attr_accessor :solution, :guesses, :feedback
@code_colors = %w[red yellow blue cyan green magenta]
include Colorization
class << self
attr_accessor :code_colors
end
def initialize
@solution = solution
@guesses = Array.new(12, Code.new('white', 'white', 'white', 'white'))
@feedback = Array.new(12, Feedback.new('black', 'black', 'black', 'black'))
# create an array for the board
end
def display_board
# display the current board status
puts "\n ____________________ + reflect the guesses"
puts " [ ] * reflect the feedback"
@guesses.each_with_index do |guess, index|
if index > 8
puts " #{index + 1} | #{colorize_display(guess, true)} || #{colorize_display(@feedback[index], false)} |"
else
puts " #{index + 1} | #{colorize_display(guess, true)} || #{colorize_display(@feedback[index], false)} |"
end
end
puts " [____________________]"
end
def update_board(turn)
check_guess(turn)
display_board
puts "\n #{@correct_pegs} peg(s) in the correct position and correct color." if @correct_pegs > 0
puts " #{@wrong_position_pegs} peg(s) in the correct color BUT wrong position." if @wrong_position_pegs > 0
puts "\n None of the colors matched those from the secret code. Try again." if @correct_pegs < 1 && @wrong_position_pegs < 1
end
def check_guess(turn)
# create a copy for wrong position peg comparison
@current_guess = Array.new(@guesses[turn].colors)
@temporary_solution = Array.new(@solution)
check_correct_pegs(turn)
check_wrong_pegs
pegs = []
update_feedback_pegs(pegs)
@feedback[turn] = Feedback.new(pegs[0], pegs[1], pegs[2], pegs[3])
end
def check_correct_pegs(turn)
@correct_pegs = 0
# set the values that are correct as nil to be compacted in check_wrong_pegs
@guesses[turn].colors.each_with_index do |color, index|
next unless @solution[index] == color
@current_guess[index] = nil
@temporary_solution[index] = nil
@correct_pegs += 1
end
end
def check_wrong_pegs
# remove the nil values so that the remainder can be checked for color matches
@current_guess = @current_guess.compact
@temporary_solution = @temporary_solution.compact
@wrong_position_pegs = @temporary_solution.intersection(@current_guess).length
end
def update_feedback_pegs(pegs)
@correct_pegs.times { pegs << 'red' }
@wrong_position_pegs.times { pegs << 'white' }
pegs << 'black' until pegs.length == 4
end
end