Class: RubyGo::Board

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

Constant Summary collapse

COLORS =
{ black: 'x', white: 'o', empty: '_' }.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(size) ⇒ Board

Returns a new instance of Board.



7
8
9
10
# File 'lib/ruby-go/board.rb', line 7

def initialize(size)
  @internal_board = Array.new(size) { Array.new(size) { Liberty.new } }
  @size = size
end

Instance Attribute Details

#internal_board=(value) ⇒ Object

Sets the attribute internal_board

Parameters:

  • value

    the value to set the attribute internal_board to.



5
6
7
# File 'lib/ruby-go/board.rb', line 5

def internal_board=(value)
  @internal_board = value
end

#sizeObject

Returns the value of attribute size.



5
6
7
# File 'lib/ruby-go/board.rb', line 5

def size
  @size
end

Instance Method Details

#around(x, y) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/ruby-go/board.rb', line 22

def around(x, y)
  intersections = []

  intersections << at(x-1, y) unless x == 0
  intersections << at(x+1, y) unless x == (internal_board.length - 1)
  intersections << at(x, y-1) unless y == 0
  intersections << at(x, y+1) unless y == (internal_board.length - 1)
  intersections
end

#at(x, y) ⇒ Object



18
19
20
# File 'lib/ruby-go/board.rb', line 18

def at(x, y)
  internal_board[y][x]
end

#empty?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/ruby-go/board.rb', line 14

def empty?
  internal_board.flatten.all?(&:empty?)
end

#group_of(stone, stones = []) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/ruby-go/board.rb', line 57

def group_of(stone, stones = [])
  return stones if stones.include?(stone)

  stones << stone

  around(*stone.to_coord).each do |intersection|
    next if intersection.empty?

    group_of(intersection, stones) if intersection.color == stone.color
  end

  stones
end

#liberties(stone) ⇒ Object



47
48
49
50
51
52
53
54
55
# File 'lib/ruby-go/board.rb', line 47

def liberties(stone)
  libs = []

  group_of(stone).each do |stn|
    libs += around(*stn.to_coord).select(&:empty?)
  end

  libs.uniq.length
end

#place(stone) ⇒ Object

Board shouldn’t care about game rules



41
42
43
44
45
# File 'lib/ruby-go/board.rb', line 41

def place(stone)
  x, y = stone.to_coord

  internal_board[y][x] = stone
end

#remove(stone) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/ruby-go/board.rb', line 32

def remove(stone)
  return if stone.empty?

  x, y = stone.to_coord

  internal_board[y][x] = Liberty.new
end

#to_sObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/ruby-go/board.rb', line 71

def to_s
  out = ""
  
  if size < 11
    out << "\s\s\s#{(0..size - 1).to_a.join(' ')}\n" 
  else
    out << "\s\s\s#{(0..10).to_a.join(' ')}" 
    out << "#{(11..size - 1).to_a.join('')}\n"
  end

  internal_board.each_with_index do |row, i|
    i = "\s#{i}" if i < 10
    out << "#{i}\s#{(row.collect {|stn| stn.to_s}).join(' ')}\n"
  end

  out
end