Class: TakaTicTacToe::Board
- Inherits:
-
Object
- Object
- TakaTicTacToe::Board
- Defined in:
- lib/taka_tic_tac_toe/board.rb
Constant Summary collapse
- WinComb =
[[0,1,2],[3,4,5],[6,7,8], [0,3,6],[1,4,7],[2,5,8], [0,4,8],[2,4,6]]
Instance Attribute Summary collapse
-
#slots ⇒ Object
Returns the value of attribute slots.
Class Method Summary collapse
Instance Method Summary collapse
- #empty_slots ⇒ Object
- #full? ⇒ Boolean
- #get(index) ⇒ Object
- #has_winner ⇒ Object
-
#initialize ⇒ Board
constructor
A new instance of Board.
- #is_empty? ⇒ Boolean
- #is_filled(index) ⇒ Object
- #is_tie ⇒ Object
- #print_slots ⇒ Object
- #reset ⇒ Object
- #set_move(mark, *index) ⇒ Object
- #to_s ⇒ Object
- #undo_move(index) ⇒ Object
- #unique?(one, two, three) ⇒ Boolean
- #winner ⇒ Object
Constructor Details
#initialize ⇒ Board
Returns a new instance of Board.
9 10 11 |
# File 'lib/taka_tic_tac_toe/board.rb', line 9 def initialize @slots = %w[1 2 3 4 5 6 7 8 9] end |
Instance Attribute Details
#slots ⇒ Object
Returns the value of attribute slots.
3 4 5 |
# File 'lib/taka_tic_tac_toe/board.rb', line 3 def slots @slots end |
Class Method Details
.parse(board) ⇒ Object
17 18 19 20 21 |
# File 'lib/taka_tic_tac_toe/board.rb', line 17 def self.parse(board) parsed_board = self.new parsed_board.slots = board.split('') parsed_board end |
Instance Method Details
#empty_slots ⇒ Object
35 36 37 |
# File 'lib/taka_tic_tac_toe/board.rb', line 35 def empty_slots @slots.select {|x| x.to_i != 0} end |
#full? ⇒ Boolean
27 28 29 |
# File 'lib/taka_tic_tac_toe/board.rb', line 27 def full? (@slots.count {|x| x == 'X'} + @slots.count {|x| x == 'O'}) == 9 end |
#get(index) ⇒ Object
23 24 25 |
# File 'lib/taka_tic_tac_toe/board.rb', line 23 def get(index) @slots[index-1] end |
#has_winner ⇒ Object
61 62 63 64 65 |
# File 'lib/taka_tic_tac_toe/board.rb', line 61 def has_winner has_winner = false WinComb.each {|num, num2, num3| has_winner = true if unique?(num, num2, num3)} has_winner end |
#is_empty? ⇒ Boolean
31 32 33 |
# File 'lib/taka_tic_tac_toe/board.rb', line 31 def is_empty? @slots.uniq.length == 9 end |
#is_filled(index) ⇒ Object
39 40 41 |
# File 'lib/taka_tic_tac_toe/board.rb', line 39 def is_filled(index) @slots[index-1].to_i == 0 end |
#is_tie ⇒ Object
49 50 51 |
# File 'lib/taka_tic_tac_toe/board.rb', line 49 def is_tie full? && !has_winner end |
#print_slots ⇒ Object
57 58 59 |
# File 'lib/taka_tic_tac_toe/board.rb', line 57 def print_slots @slots.each_slice(3) {|line| p line} end |
#reset ⇒ Object
53 54 55 |
# File 'lib/taka_tic_tac_toe/board.rb', line 53 def reset @slots = %w[1 2 3 4 5 6 7 8 9] end |
#set_move(mark, *index) ⇒ Object
43 44 45 46 47 |
# File 'lib/taka_tic_tac_toe/board.rb', line 43 def set_move(mark, *index) index.each do |index| @slots[(index.to_i)-1] = mark end end |
#to_s ⇒ Object
13 14 15 |
# File 'lib/taka_tic_tac_toe/board.rb', line 13 def to_s @slots.join end |
#undo_move(index) ⇒ Object
77 78 79 |
# File 'lib/taka_tic_tac_toe/board.rb', line 77 def undo_move(index) @slots[(index.to_i)-1] = index.to_s end |
#unique?(one, two, three) ⇒ Boolean
67 68 69 |
# File 'lib/taka_tic_tac_toe/board.rb', line 67 def unique?(one, two, three) [@slots[one], @slots[two], @slots[three]].uniq.length == 1 end |
#winner ⇒ Object
71 72 73 74 75 |
# File 'lib/taka_tic_tac_toe/board.rb', line 71 def winner winner = "" WinComb.each {|num, num2, num3| winner = @slots[num] if unique?(num, num2, num3)} winner end |