Class: TictactoeJ8th::Board
- Inherits:
-
Object
- Object
- TictactoeJ8th::Board
- Defined in:
- lib/tictactoe_j8th/board.rb
Constant Summary collapse
- BOARD_SIZE =
9
- BOARD_LINES =
[ [0, 1, 2], [3, 4, 5], [6, 7, 8], [0, 3, 6], [1, 4, 7], [2, 5, 8], [0, 4, 8], [2, 4, 6] ]
Class Method Summary collapse
-
.from_s(string) ⇒ Object
Public Class Methods.
Instance Method Summary collapse
- #[](spot) ⇒ Object
- #create_copy ⇒ Object
- #empty? ⇒ Boolean
- #full? ⇒ Boolean
-
#initialize ⇒ Board
constructor
A new instance of Board.
- #lines ⇒ Object
- #place(item, position) ⇒ Object
- #set_board(array) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize ⇒ Board
Returns a new instance of Board.
17 18 19 |
# File 'lib/tictactoe_j8th/board.rb', line 17 def initialize @board = Array.new(BOARD_SIZE) end |
Class Method Details
.from_s(string) ⇒ Object
Public Class Methods
71 72 73 74 75 76 77 |
# File 'lib/tictactoe_j8th/board.rb', line 71 def self.from_s(string) board = Board.new (0..BOARD_SIZE-1).each do |i| board.place(string[i].to_sym, i) unless string[i] == 'E' end board end |
Instance Method Details
#[](spot) ⇒ Object
34 35 36 |
# File 'lib/tictactoe_j8th/board.rb', line 34 def [](spot) board[spot] end |
#create_copy ⇒ Object
54 55 56 57 58 |
# File 'lib/tictactoe_j8th/board.rb', line 54 def create_copy copy = Board.new copy.set_board(@board.dup) copy end |
#empty? ⇒ Boolean
21 22 23 |
# File 'lib/tictactoe_j8th/board.rb', line 21 def empty? board.all? { |value| value.nil? } end |
#full? ⇒ Boolean
29 30 31 32 |
# File 'lib/tictactoe_j8th/board.rb', line 29 def full? return false if board.include? nil true end |
#lines ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/tictactoe_j8th/board.rb', line 38 def lines array = [] BOARD_LINES.each do |line| hash = {} line.each do |i| hash[i] = board[i] end array.push(hash) end array end |
#place(item, position) ⇒ Object
25 26 27 |
# File 'lib/tictactoe_j8th/board.rb', line 25 def place(item, position) board[position] = item if board[position].nil? end |
#set_board(array) ⇒ Object
50 51 52 |
# File 'lib/tictactoe_j8th/board.rb', line 50 def set_board(array) @board = array end |
#to_s ⇒ Object
60 61 62 63 64 65 66 |
# File 'lib/tictactoe_j8th/board.rb', line 60 def to_s string = '' board.each do |spot| string += spot.nil? ? 'E' : spot.to_s end string end |