Class: Board
- Inherits:
-
Object
- Object
- Board
- Defined in:
- lib/board.rb
Instance Attribute Summary collapse
-
#current_board ⇒ Object
Returns the value of attribute current_board.
-
#player_1 ⇒ Object
Returns the value of attribute player_1.
-
#player_2 ⇒ Object
Returns the value of attribute player_2.
-
#width ⇒ Object
Returns the value of attribute width.
Instance Method Summary collapse
- #board_indices ⇒ Object
- #board_open? ⇒ Boolean
- #diagonal_down ⇒ Object
- #diagonal_up ⇒ Object
- #display_board ⇒ Object
- #game_over? ⇒ Boolean
- #game_won?(marker) ⇒ Boolean
- #gather_winning_combinations ⇒ Object
- #generate_column(starting_index) ⇒ Object
- #get_opponent ⇒ Object
-
#initialize(player_1, player_2) ⇒ Board
constructor
A new instance of Board.
- #integer_board ⇒ Object
- #list_of_open_squares ⇒ Object
- #number_of_empty_squares ⇒ Object
- #number_of_filled_squares ⇒ Object
- #reset_board ⇒ Object
- #set_square(square, marker) ⇒ Object
- #square_empty?(square) ⇒ Boolean
- #squares_with_integers ⇒ Object
- #undo_set_square(square) ⇒ Object
- #whose_turn ⇒ Object
- #winner_on_board? ⇒ Boolean
- #winning_columns ⇒ Object
- #winning_rows ⇒ Object
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_board ⇒ Object
Returns the value of attribute current_board.
3 4 5 |
# File 'lib/board.rb', line 3 def current_board @current_board end |
#player_1 ⇒ Object
Returns the value of attribute player_1.
3 4 5 |
# File 'lib/board.rb', line 3 def player_1 @player_1 end |
#player_2 ⇒ Object
Returns the value of attribute player_2.
3 4 5 |
# File 'lib/board.rb', line 3 def player_2 @player_2 end |
#width ⇒ Object
Returns the value of attribute width.
3 4 5 |
# File 'lib/board.rb', line 3 def width @width end |
Instance Method Details
#board_indices ⇒ Object
94 95 96 |
# File 'lib/board.rb', line 94 def board_indices (0...@width**2).to_a end |
#board_open? ⇒ Boolean
64 65 66 |
# File 'lib/board.rb', line 64 def board_open? number_of_empty_squares >= 1? true : false end |
#diagonal_down ⇒ Object
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_up ⇒ Object
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_board ⇒ Object
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
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
90 91 92 |
# File 'lib/board.rb', line 90 def game_won?(marker) winner_on_board? == marker ? true : false end |
#gather_winning_combinations ⇒ Object
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_opponent ⇒ Object
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_board ⇒ Object
20 21 22 |
# File 'lib/board.rb', line 20 def integer_board (1..@width**2).to_a end |
#list_of_open_squares ⇒ Object
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_squares ⇒ Object
60 61 62 |
# File 'lib/board.rb', line 60 def number_of_empty_squares list_of_open_squares.length end |
#number_of_filled_squares ⇒ Object
68 69 70 |
# File 'lib/board.rb', line 68 def number_of_filled_squares @current_board.length - list_of_open_squares.length end |
#reset_board ⇒ Object
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
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_integers ⇒ Object
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_turn ⇒ Object
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
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_columns ⇒ Object
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_rows ⇒ Object
98 99 100 |
# File 'lib/board.rb', line 98 def winning_rows board_indices.each_slice(@width).reduce([]) {|rows, slice| rows << slice} end |