Class: Board

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

Overview

Here I go again

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(size) ⇒ Board

Returns a new instance of Board.



8
9
10
11
# File 'lib/tic_cat_toe/board.rb', line 8

def initialize(size)
  @size = size
  @slots = build_tic_tac_toe(size)
end

Instance Attribute Details

#sizeObject (readonly)

Returns the value of attribute size.



6
7
8
# File 'lib/tic_cat_toe/board.rb', line 6

def size
  @size
end

#slotsObject

Returns the value of attribute slots.



5
6
7
# File 'lib/tic_cat_toe/board.rb', line 5

def slots
  @slots
end

Instance Method Details

#available_slotsObject



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

def available_slots
  @slots.compact.length.equal?(@size**2) ? true : false
end

#build_tic_tac_toe(size) ⇒ Object



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

def build_tic_tac_toe(size)
  size >= 3 ? Array.new(size**2) : Array.new(3)
end

#check_board(mark) ⇒ Object



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

def check_board(mark)
  vertical(mark) || horizontal(mark) || diagonals(mark) ? true : false
end

#check_slot(row, col) ⇒ Object



30
31
32
33
34
# File 'lib/tic_cat_toe/board.rb', line 30

def check_slot(row, col)
  # @slots[@size * row + col] != 'X' && @slots[@size * row + col] != 'O' ? true : false
  # @slots[@size * row + col].match(/^[X|O]$/) ? false : true
  @slots[@size * row + col].nil? ? true : false
end

#diagonals(mark) ⇒ Object



81
82
83
# File 'lib/tic_cat_toe/board.rb', line 81

def diagonals(mark)
  negative_line(mark) || positive_line(mark) ? true : false
end

#horizontal(mark) ⇒ Object



52
53
54
55
56
57
58
59
60
61
# File 'lib/tic_cat_toe/board.rb', line 52

def horizontal(mark)
  @size.times do |row|
    matches_count = 0
    @size.times do |index|
      matches_count += 1 if /^#{mark}$/.match(@slots[@size * row + index])
      return true if matches_count.equal?(@size)
    end
  end
  false
end

#negative_line(mark) ⇒ Object



63
64
65
66
67
68
69
70
# File 'lib/tic_cat_toe/board.rb', line 63

def negative_line(mark)
  matches_count = 0
  @size.times do |index|
    matches_count += 1 if /^#{mark}$/.match(@slots[index * (@size + 1)])
    return true if matches_count.equal?(@size)
  end
  false
end

#positive_line(mark) ⇒ Object



72
73
74
75
76
77
78
79
# File 'lib/tic_cat_toe/board.rb', line 72

def positive_line(mark)
  matches_count = 0
  @size.times do |index|
    matches_count += 1 if /^#{mark}$/.match(@slots[(index + 1) * (@size - 1)])
    return true if matches_count.equal?(@size)
  end
  false
end

#put_mark(row, col, mark) ⇒ Object



36
37
38
39
# File 'lib/tic_cat_toe/board.rb', line 36

def put_mark(row, col, mark)
  @slots[@size * row + col] = mark
  check_board(mark)
end

#to_printObject



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/tic_cat_toe/board.rb', line 17

def to_print
  @size.times do |index|
    @size.times do |jndex|
      case @slots[@size * index + jndex]
      when 'X' then print "\e[1;31m[#{index},#{jndex} X ]\e[0m"
      when 'O' then print "\e[1;36m[#{index},#{jndex} O ]\e[0m"
      else print "[#{index},#{jndex}   ]"
      end
    end
    puts
  end
end

#vertical(mark) ⇒ Object



41
42
43
44
45
46
47
48
49
50
# File 'lib/tic_cat_toe/board.rb', line 41

def vertical(mark)
  @size.times do |col|
    matches_count = 0
    @size.times do |index|
      matches_count += 1 if /^#{mark}$/.match(@slots[@size * index + col])
      return true if matches_count.equal?(@size)
    end
  end
  false
end