Class: TicTacToe::Board

Inherits:
Object
  • Object
show all
Defined in:
lib/ttt/board.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBoard

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

#solutionsObject

Returns the value of attribute solutions.



3
4
5
# File 'lib/ttt/board.rb', line 3

def solutions
  @solutions
end

#spacesObject

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)
  parsed_board = self.new
  parse_board = []
  new_board = board.split('')
  new_board.each_index do |space|
    if new_board[space] == '_' || new_board[space].to_i != 0
      parse_board << (space + 1).to_s
    else
      parse_board << new_board[space]
    end
  end
  parsed_board.spaces = parse_board
  parsed_board
end

Instance Method Details

#column_winObject



87
88
89
90
91
92
93
# File 'lib/ttt/board.rb', line 87

def column_win
  3.times do |check|
    ((check+1)..(9)).step(3).each_slice(3) do |nums|
      @solutions.push(nums)
    end
  end
end

#diagonal_winObject



95
96
97
98
99
100
101
102
# File 'lib/ttt/board.rb', line 95

def diagonal_win
  (1..(9)).step(4).each_slice(3) do |nums|
    @solutions.push(nums)
  end
  ((3)..(9)-1).step(2).each_slice(3) do |nums|
    @solutions.push(nums)
  end
end

#empty_spacesObject



79
80
81
# File 'lib/ttt/board.rb', line 79

def empty_spaces
  @spaces.select { |x| x.to_i != 0}
end

#full_board?Boolean

Returns:

  • (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-1]
end

#has_winner?Boolean

Returns:

  • (Boolean)


59
60
61
62
63
# File 'lib/ttt/board.rb', line 59

def has_winner?
  has_winner = false
  @solutions.each { |sol| has_winner = true if unique?(sol)}
  has_winner
end

#is_board_empty?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/ttt/board.rb', line 55

def is_board_empty?
  !@spaces.include?("X") && !@spaces.include?("O")
end

#is_taken?(space) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/ttt/board.rb', line 43

def is_taken?(space)
  @spaces[(space.to_i) - 1].to_i == 0
end

#make_blankObject



69
70
71
72
73
74
75
76
77
# File 'lib/ttt/board.rb', line 69

def make_blank
  @spaces.reduce("") do |blank_spaces, spaces|
    if !spaces.to_i.zero?
      blank_spaces << '_'
    else
      blank_spaces << spaces
    end
  end
end

#place_move(piece, *space) ⇒ Object



33
34
35
36
37
# File 'lib/ttt/board.rb', line 33

def place_move(piece, *space)
  space.each do |space|
    @spaces[(space.to_i)-1] = piece
  end
end

#row_winObject



83
84
85
# File 'lib/ttt/board.rb', line 83

def row_win
  (@spaces.map { |space| space.to_i }).each_slice(3) { |nums| @solutions.push(nums)}
end

#tied_game?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/ttt/board.rb', line 47

def tied_game?
  full_board? && !has_winner?
end

#to_sObject



25
26
27
# File 'lib/ttt/board.rb', line 25

def to_s
  @spaces.join
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

#unique?(spaces) ⇒ Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/ttt/board.rb', line 65

def unique?(spaces)
  spaces.map { |s| @spaces[s-1]}.uniq.length == 1 
end

#winnerObject



111
112
113
114
115
# File 'lib/ttt/board.rb', line 111

def winner
  winner = ""
  @solutions.each { |sol| winner = @spaces[sol[0]-1] if unique?(sol)}
  winner
end

#winning_solutionsObject



104
105
106
107
108
109
# File 'lib/ttt/board.rb', line 104

def winning_solutions
  @solutions = []
  row_win
  column_win
  diagonal_win
end