Class: Board
- Inherits:
-
Object
- Object
- Board
- Defined in:
- lib/nick_tac_toe/board.rb
Instance Attribute Summary collapse
-
#cells ⇒ Object
Returns the value of attribute cells.
-
#player_one ⇒ Object
Returns the value of attribute player_one.
-
#player_two ⇒ Object
Returns the value of attribute player_two.
Instance Method Summary collapse
- #blocking_group_for(team) ⇒ Object
- #center ⇒ Object
- #columns ⇒ Object
- #copy ⇒ Object
- #diagonals ⇒ Object
- #empty_at_center? ⇒ Boolean
- #empty_spot_in_group(group) ⇒ Object
- #first_empty_cell ⇒ Object
- #get_cell(position) ⇒ Object
-
#initialize(player_one, player_two) ⇒ Board
constructor
A new instance of Board.
- #opponent_for(team) ⇒ Object
- #print ⇒ Object
- #remaining_moves ⇒ Object
- #rows ⇒ Object
- #set_cell(position, team) ⇒ Object
- #valid_move?(position) ⇒ Boolean
- #winning_group_for(team) ⇒ Object
Constructor Details
#initialize(player_one, player_two) ⇒ Board
Returns a new instance of Board.
5 6 7 8 9 |
# File 'lib/nick_tac_toe/board.rb', line 5 def initialize(player_one, player_two) @cells = [empty_spot]*9 @player_one = player_one @player_two = player_two end |
Instance Attribute Details
#cells ⇒ Object
Returns the value of attribute cells.
3 4 5 |
# File 'lib/nick_tac_toe/board.rb', line 3 def cells @cells end |
#player_one ⇒ Object
Returns the value of attribute player_one.
3 4 5 |
# File 'lib/nick_tac_toe/board.rb', line 3 def player_one @player_one end |
#player_two ⇒ Object
Returns the value of attribute player_two.
3 4 5 |
# File 'lib/nick_tac_toe/board.rb', line 3 def player_two @player_two end |
Instance Method Details
#blocking_group_for(team) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/nick_tac_toe/board.rb', line 70 def blocking_group_for(team) 3.times do |i| # rows return [3*i, 3*i + 1, 3*i + 2] if rows[i].sort == [empty_spot, opponent_for(team), opponent_for(team)] end 3.times do |i| # columns return [i, i + 3, i + 6] if columns[i].sort == [empty_spot, opponent_for(team), opponent_for(team)] end return [0, 4, 8] if diagonals[0].sort == [empty_spot, opponent_for(team), opponent_for(team)] return [2, 4, 6] if diagonals[1].sort == [empty_spot, opponent_for(team), opponent_for(team)] return nil end |
#center ⇒ Object
96 97 98 |
# File 'lib/nick_tac_toe/board.rb', line 96 def center 4 end |
#columns ⇒ Object
45 46 47 48 49 50 51 |
# File 'lib/nick_tac_toe/board.rb', line 45 def columns [ [@cells[0], @cells[3], @cells[6]], [@cells[1], @cells[4], @cells[7]], [@cells[2], @cells[5], @cells[8]] ] end |
#copy ⇒ Object
126 127 128 129 130 131 132 |
# File 'lib/nick_tac_toe/board.rb', line 126 def copy new_board = self.dup new_board.cells = @cells.dup # new_board.player_one = @player_one.dup # new_board.player_one = @player_two.dup return new_board end |
#diagonals ⇒ Object
53 54 55 56 57 58 |
# File 'lib/nick_tac_toe/board.rb', line 53 def diagonals [ [@cells[0], @cells[4], @cells[8]], [@cells[2], @cells[4], @cells[6]], ] end |
#empty_at_center? ⇒ Boolean
100 101 102 |
# File 'lib/nick_tac_toe/board.rb', line 100 def empty_at_center? get_cell(4) == empty_spot end |
#empty_spot_in_group(group) ⇒ Object
85 86 87 88 89 90 |
# File 'lib/nick_tac_toe/board.rb', line 85 def empty_spot_in_group(group) empty_spots = group.select do |position| get_cell(position) == empty_spot end return empty_spots.first end |
#first_empty_cell ⇒ Object
104 105 106 107 108 |
# File 'lib/nick_tac_toe/board.rb', line 104 def first_empty_cell @cells.each_with_index do |cell, index| return index if cell == empty_spot end end |
#get_cell(position) ⇒ Object
33 34 35 |
# File 'lib/nick_tac_toe/board.rb', line 33 def get_cell position @cells[position] end |
#opponent_for(team) ⇒ Object
110 111 112 113 114 115 116 |
# File 'lib/nick_tac_toe/board.rb', line 110 def opponent_for(team) if team == @player_one return @player_two else return @player_one end end |
#print ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/nick_tac_toe/board.rb', line 11 def print list = [] cells.each_with_index do |marker, index| if marker == empty_spot list << index+1 else list << marker end end list.map! do |m| case m when 'x' "\e[1;36mx\e[0m" when 'o' "\e[1;33mo\e[0m" else "\e[1;30m#{m}\e[0m" end end puts "\n#{list[0]} | #{list[1]} | #{list[2]}\n----------\n#{list[3]} | #{list[4]} | #{list[5]}\n----------\n#{list[6]} | #{list[7]} | #{list[8]}\n " end |
#remaining_moves ⇒ Object
118 119 120 121 122 123 124 |
# File 'lib/nick_tac_toe/board.rb', line 118 def remaining_moves count = [] 9.times do |index| count << index if @cells[index] == empty_spot end return count end |
#rows ⇒ Object
41 42 43 |
# File 'lib/nick_tac_toe/board.rb', line 41 def rows [@cells[0..2], @cells[3..5], @cells[6..8]] end |
#set_cell(position, team) ⇒ Object
37 38 39 |
# File 'lib/nick_tac_toe/board.rb', line 37 def set_cell position, team @cells[position] = team end |
#valid_move?(position) ⇒ Boolean
60 61 62 63 64 65 66 67 68 |
# File 'lib/nick_tac_toe/board.rb', line 60 def valid_move?(position) if [0,1,2,3,4,5,6,7,8].include?(position.to_i-1) if get_cell(position-1) == empty_spot return true end else return false end end |
#winning_group_for(team) ⇒ Object
92 93 94 |
# File 'lib/nick_tac_toe/board.rb', line 92 def winning_group_for(team) blocking_group_for(opponent_for(team)) end |