Class: Board
- Inherits:
-
Object
- Object
- Board
- Defined in:
- lib/mastermind_cli/board.rb
Constant Summary collapse
- COLORS =
%w(r b g y p o)
Instance Attribute Summary collapse
-
#win_code ⇒ Object
readonly
Returns the value of attribute win_code.
Instance Method Summary collapse
- #add_move(move, row) ⇒ Object
- #check_feedbacks(move) ⇒ Object
- #create_code ⇒ Object
- #full?(turn) ⇒ Boolean
-
#initialize ⇒ Board
constructor
A new instance of Board.
- #red_feedbacks(move) ⇒ Object
- #render ⇒ Object
- #setup ⇒ Object
- #white_feedbacks(move) ⇒ Object
- #winning_combination?(turn) ⇒ Boolean
Constructor Details
#initialize ⇒ Board
Returns a new instance of Board.
5 6 7 8 9 |
# File 'lib/mastermind_cli/board.rb', line 5 def initialize @board = [] setup create_code end |
Instance Attribute Details
#win_code ⇒ Object (readonly)
Returns the value of attribute win_code.
4 5 6 |
# File 'lib/mastermind_cli/board.rb', line 4 def win_code @win_code end |
Instance Method Details
#add_move(move, row) ⇒ Object
39 40 41 42 |
# File 'lib/mastermind_cli/board.rb', line 39 def add_move(move, row) check_feedbacks(move) @board[row] = move, @feedback end |
#check_feedbacks(move) ⇒ Object
63 64 65 66 67 68 69 70 71 |
# File 'lib/mastermind_cli/board.rb', line 63 def check_feedbacks(move) @feedback = [] @hash = {} red_feedbacks(move) white_feedbacks(move) @hash.each do |key, val| @feedback << val end end |
#create_code ⇒ Object
17 18 19 |
# File 'lib/mastermind_cli/board.rb', line 17 def create_code @win_code = %w(r b g y) end |
#full?(turn) ⇒ Boolean
30 31 32 |
# File 'lib/mastermind_cli/board.rb', line 30 def full?(turn) turn > 11 end |
#red_feedbacks(move) ⇒ Object
44 45 46 47 48 49 50 |
# File 'lib/mastermind_cli/board.rb', line 44 def red_feedbacks(move) 4.times do |idx| if move[idx] == @win_code[idx] @hash[idx] = "R" end end end |
#render ⇒ Object
21 22 23 24 25 26 27 28 |
# File 'lib/mastermind_cli/board.rb', line 21 def render puts "~"*40 (1..12).each do |row| puts "#{@board[-row][0]}#{@board[-row][1]}" puts "" end puts "~"*40 end |
#setup ⇒ Object
11 12 13 14 15 |
# File 'lib/mastermind_cli/board.rb', line 11 def setup 12.times do |row| @board[row] = %w(_)*4, [] end end |
#white_feedbacks(move) ⇒ Object
53 54 55 56 57 58 59 60 61 |
# File 'lib/mastermind_cli/board.rb', line 53 def white_feedbacks(move) move.each do |color| 4.times do |idx| if color == @win_code[idx] @hash[idx] = "W" unless @hash.has_key?(idx) end end end end |
#winning_combination?(turn) ⇒ Boolean
34 35 36 37 |
# File 'lib/mastermind_cli/board.rb', line 34 def winning_combination?(turn) row = turn - 1 @board[row][0] == @win_code end |