Class: TicTacToe::Board
- Inherits:
-
Object
- Object
- TicTacToe::Board
- Defined in:
- lib/ttt/board.rb
Instance Attribute Summary collapse
-
#solutions ⇒ Object
Returns the value of attribute solutions.
-
#spaces ⇒ Object
Returns the value of attribute spaces.
Class Method Summary collapse
Instance Method Summary collapse
- #empty_spaces ⇒ Object
- #full_board? ⇒ Boolean
- #get(space) ⇒ Object
- #has_winner? ⇒ Boolean
-
#initialize ⇒ Board
constructor
A new instance of Board.
- #is_board_empty? ⇒ Boolean
- #is_solution_found?(spaces) ⇒ Boolean
- #is_space_taken?(space) ⇒ Boolean
- #place_move(piece, *indices) ⇒ Object
- #tied_game? ⇒ Boolean
- #to_s ⇒ Object
- #translate_board_to_string ⇒ Object
- #undo_move(space) ⇒ Object
- #winner ⇒ Object
- #winning_solutions ⇒ Object
Constructor Details
#initialize ⇒ Board
Returns a new instance of Board.
5 6 7 8 |
# File 'lib/ttt/board.rb', line 5 def initialize @spaces = %w[1 2 3 4 5 6 7 8 9] winning_solutions end |
Instance Attribute Details
#solutions ⇒ Object
Returns the value of attribute solutions.
3 4 5 |
# File 'lib/ttt/board.rb', line 3 def solutions @solutions end |
#spaces ⇒ Object
Returns the value of attribute spaces.
3 4 5 |
# File 'lib/ttt/board.rb', line 3 def spaces @spaces end |
Class Method Details
.parse(board) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/ttt/board.rb', line 10 def self.parse(board) new_board_for_translation = self.new translated_board_spaces = [] new_spaces_for_translation = board.split('') new_spaces_for_translation.each_index do |index| if new_spaces_for_translation[index] == '_' || new_spaces_for_translation[index].to_i != 0 translated_board_spaces << (index + 1).to_s else translated_board_spaces << new_spaces_for_translation[index] end end new_board_for_translation.spaces = translated_board_spaces new_board_for_translation end |
Instance Method Details
#empty_spaces ⇒ Object
77 78 79 |
# File 'lib/ttt/board.rb', line 77 def empty_spaces @spaces.select { |x| x.to_i != 0} end |
#full_board? ⇒ Boolean
51 52 53 |
# File 'lib/ttt/board.rb', line 51 def full_board? (@spaces.count { |x| x == 'X' } + @spaces.count { |x| x == 'O' }) == 9 end |
#get(space) ⇒ Object
29 30 31 |
# File 'lib/ttt/board.rb', line 29 def get(space) @spaces[(space.to_i)-1] end |
#has_winner? ⇒ Boolean
59 60 61 |
# File 'lib/ttt/board.rb', line 59 def has_winner? @solutions.find { |sol| is_solution_found?(sol)} end |
#is_board_empty? ⇒ Boolean
55 56 57 |
# File 'lib/ttt/board.rb', line 55 def is_board_empty? !@spaces.include?("X") && !@spaces.include?("O") end |
#is_solution_found?(spaces) ⇒ Boolean
63 64 65 |
# File 'lib/ttt/board.rb', line 63 def is_solution_found?(spaces) spaces.map { |s| @spaces[s-1]}.uniq.length == 1 end |
#is_space_taken?(space) ⇒ Boolean
43 44 45 |
# File 'lib/ttt/board.rb', line 43 def is_space_taken?(space) @spaces[(space.to_i) - 1].to_i == 0 end |
#place_move(piece, *indices) ⇒ Object
33 34 35 36 37 |
# File 'lib/ttt/board.rb', line 33 def place_move(piece, *indices) indices.each do |space| @spaces[(space.to_i)-1] = piece end end |
#tied_game? ⇒ Boolean
47 48 49 |
# File 'lib/ttt/board.rb', line 47 def tied_game? full_board? && !has_winner? end |
#to_s ⇒ Object
25 26 27 |
# File 'lib/ttt/board.rb', line 25 def to_s @spaces.join end |
#translate_board_to_string ⇒ Object
67 68 69 70 71 72 73 74 75 |
# File 'lib/ttt/board.rb', line 67 def translate_board_to_string @spaces.reduce("") do |blank_spaces, spaces| if !spaces.to_i.zero? blank_spaces << '_' else blank_spaces << spaces end end end |
#undo_move(space) ⇒ Object
39 40 41 |
# File 'lib/ttt/board.rb', line 39 def undo_move(space) @spaces[(space.to_i)-1] = space.to_s end |
#winner ⇒ Object
87 88 89 90 91 |
# File 'lib/ttt/board.rb', line 87 def winner winner = "" @solutions.each { |sol| winner = @spaces[sol[0]-1] if is_solution_found?(sol)} winner end |
#winning_solutions ⇒ Object
81 82 83 84 85 |
# File 'lib/ttt/board.rb', line 81 def winning_solutions @solutions = [[1,2,3], [4,5,6], [7,8,9], [1,4,7], [2,5,8], [3,6,9], [1,5,9], [3,5,7]] end |