Class: Board
- Inherits:
-
Object
- Object
- Board
- Defined in:
- lib/board.rb
Instance Attribute Summary collapse
-
#cells ⇒ Object
Returns the value of attribute cells.
-
#input ⇒ Object
Returns the value of attribute input.
Instance Method Summary collapse
- #display ⇒ Object
- #full? ⇒ Boolean
-
#initialize ⇒ Board
constructor
A new instance of Board.
- #position(input) ⇒ Object
- #reset! ⇒ Object
- #taken?(input) ⇒ Boolean
- #turn_count ⇒ Object
- #update(position, player) ⇒ Object
- #valid_move?(input) ⇒ Boolean
Constructor Details
#initialize ⇒ Board
Returns a new instance of Board.
6 7 8 |
# File 'lib/board.rb', line 6 def initialize @cells = Array.new(9, " ") end |
Instance Attribute Details
#cells ⇒ Object
Returns the value of attribute cells.
3 4 5 |
# File 'lib/board.rb', line 3 def cells @cells end |
#input ⇒ Object
Returns the value of attribute input.
4 5 6 |
# File 'lib/board.rb', line 4 def input @input end |
Instance Method Details
#display ⇒ Object
19 20 21 22 23 24 25 |
# File 'lib/board.rb', line 19 def display puts " #{@cells[0]} | #{@cells[1]} | #{@cells[2]} " puts "-----------" puts " #{@cells[3]} | #{@cells[4]} | #{@cells[5]} " puts "-----------" puts " #{@cells[6]} | #{@cells[7]} | #{@cells[8]} " end |
#full? ⇒ Boolean
32 33 34 |
# File 'lib/board.rb', line 32 def full? @cells.include?(" " || "" || nil) ? false : true end |
#position(input) ⇒ Object
27 28 29 30 |
# File 'lib/board.rb', line 27 def position(input) index = (input.to_i - 1) @cells[index] end |
#reset! ⇒ Object
10 11 12 |
# File 'lib/board.rb', line 10 def reset! @cells = Array.new(9, " ") end |
#taken?(input) ⇒ Boolean
43 44 45 46 47 48 49 50 51 |
# File 'lib/board.rb', line 43 def taken?(input) if position(input) == "X" true elsif position(input) == "O" true else false end end |
#turn_count ⇒ Object
36 37 38 39 40 41 |
# File 'lib/board.rb', line 36 def turn_count x_turns = @cells.count("X") o_turns = @cells.count("O") turn_count = x_turns + o_turns return turn_count end |
#update(position, player) ⇒ Object
61 62 63 64 |
# File 'lib/board.rb', line 61 def update(position, player) index = (position.to_i - 1) @cells[index] = player.token end |
#valid_move?(input) ⇒ Boolean
53 54 55 56 57 58 59 |
# File 'lib/board.rb', line 53 def valid_move?(input) if (input.to_i.between?(1,9) == true) && (taken?(input.to_i) == false) return true else return false end end |