Class: Board

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(player_1, player_2) ⇒ Board

Returns a new instance of Board.



5
6
7
8
9
10
# File 'lib/board.rb', line 5

def initialize(player_1, player_2)
  @width = 3
  @current_board = squares_with_integers
  @player_1 = player_1
  @player_2 = player_2
end

Instance Attribute Details

#current_boardObject

Returns the value of attribute current_board.



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

def current_board
  @current_board
end

#player_1Object

Returns the value of attribute player_1.



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

def player_1
  @player_1
end

#player_2Object

Returns the value of attribute player_2.



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

def player_2
  @player_2
end

#widthObject

Returns the value of attribute width.



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

def width
  @width
end

Instance Method Details

#board_indicesObject



94
95
96
# File 'lib/board.rb', line 94

def board_indices
  (0...@width**2).to_a
end

#board_open?Boolean

Returns:

  • (Boolean)


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

def board_open?
  number_of_empty_squares >= 1? true : false
end

#diagonal_downObject



114
115
116
117
118
# File 'lib/board.rb', line 114

def diagonal_down
  (0...@width).reduce([]) {|diagonal, index| 
    diagonal << index*(@width + 1)
  }
end

#diagonal_upObject



120
121
122
123
124
# File 'lib/board.rb', line 120

def diagonal_up
  (0...@width).reduce([]) {|diagonal, index| 
    diagonal << (index + 1)*(@width - 1)
  }
end

#display_boardObject



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/board.rb', line 28

def display_board
  if @width == 3
    @current_board.each_slice(@width).
                   map { |a,b,c| " #{a} | #{b} | #{c} \n" }.
                   join("---|---|---\n")
  else
    @current_board.each_slice(@width).
                   map { |a,b,c,d| " #{a} | #{b} | #{c} | #{d} \n" }.
                   join("---|---|---|---\n")      
  end
end

#game_over?Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/board.rb', line 86

def game_over?
  (!board_open? or winner_on_board?) ? true : false
end

#game_won?(marker) ⇒ Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/board.rb', line 90

def game_won?(marker)
  winner_on_board? == marker ? true : false
end

#gather_winning_combinationsObject



126
127
128
129
130
131
132
133
# File 'lib/board.rb', line 126

def gather_winning_combinations
  combos = []
  winning_rows.each {|row| combos << row } and
  combos << diagonal_down and
  combos << diagonal_up and
  winning_columns.each {|column| combos << column}
  combos
end

#generate_column(starting_index) ⇒ Object



102
103
104
105
106
# File 'lib/board.rb', line 102

def generate_column(starting_index)
  (0...@width).reduce([]) {|column, index| 
    column << (index*@width+starting_index)
  }
end

#get_opponentObject



16
17
18
# File 'lib/board.rb', line 16

def get_opponent
  number_of_empty_squares%2 != 0 ? @player_2.marker : @player_1.marker
end

#integer_boardObject



20
21
22
# File 'lib/board.rb', line 20

def integer_board
  (1..@width**2).to_a
end

#list_of_open_squaresObject



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

def list_of_open_squares
  @current_board.select {|square| square_empty?(square.to_i)}
end

#number_of_empty_squaresObject



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

def number_of_empty_squares
  list_of_open_squares.length
end

#number_of_filled_squaresObject



68
69
70
# File 'lib/board.rb', line 68

def number_of_filled_squares
  @current_board.length - list_of_open_squares.length
end

#reset_boardObject



40
41
42
# File 'lib/board.rb', line 40

def reset_board
  @current_board = squares_with_integers
end

#set_square(square, marker) ⇒ Object



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

def set_square(square, marker)
  @current_board[square.to_i - 1] = marker
end

#square_empty?(square) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/board.rb', line 52

def square_empty?(square)
  @current_board[square.to_i - 1] == square.to_s ? true : false
end

#squares_with_integersObject



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

def squares_with_integers
  string_board = integer_board.map {|square| square.to_s}
end

#undo_set_square(square) ⇒ Object



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

def undo_set_square(square)
  set_square(square, square.to_s)
end

#whose_turnObject



12
13
14
# File 'lib/board.rb', line 12

def whose_turn
  number_of_empty_squares%2 == 0 ? @player_2.marker : @player_1.marker
end

#winner_on_board?Boolean

Returns:

  • (Boolean)


72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/board.rb', line 72

def winner_on_board?
  winning_marker = false
  gather_winning_combinations.each do |combo|
    marker = @current_board[combo[0]]
    winner = []
    positions = (0...@width).to_a
    positions.each {|position| winner << @current_board[combo[position]]}
    if winner.all? { |square| square == marker }
      winning_marker = marker
    end
  end
  return winning_marker
end

#winning_columnsObject



108
109
110
111
112
# File 'lib/board.rb', line 108

def winning_columns
  (0...@width).reduce([]) {|set_of_columns, index| 
    set_of_columns << generate_column(index)
  }
end

#winning_rowsObject



98
99
100
# File 'lib/board.rb', line 98

def winning_rows
  board_indices.each_slice(@width).reduce([]) {|rows, slice| rows << slice}
end