Class: JustGo::Point

Inherits:
Object
  • Object
show all
Defined in:
lib/just_go/point.rb

Overview

Point

A Point on a go board

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id:, x:, y:, stone:, territory_id: nil) ⇒ Point

Returns a new instance of Point.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/just_go/point.rb', line 9

def initialize(id: , x: , y: , stone: , territory_id: nil)
  @id = id
  @x = x
  @y = y
  @stone = case stone
    when JustGo::Stone
      stone
    when Hash
      JustGo::Stone.new(**stone)
    when nil 
      stone
    else
      raise ArgumentError, "stone must be Stone, Hash or nil"
    end
  @territory_id = territory_id
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



26
27
28
# File 'lib/just_go/point.rb', line 26

def id
  @id
end

#stoneObject (readonly)

Returns the value of attribute stone.



29
30
31
# File 'lib/just_go/point.rb', line 29

def stone
  @stone
end

#territory_idObject (readonly)

Returns the value of attribute territory_id.



30
31
32
# File 'lib/just_go/point.rb', line 30

def territory_id
  @territory_id
end

#xObject (readonly)

Returns the value of attribute x.



27
28
29
# File 'lib/just_go/point.rb', line 27

def x
  @x
end

#yObject (readonly)

Returns the value of attribute y.



28
29
30
# File 'lib/just_go/point.rb', line 28

def y
  @y
end

Instance Method Details

#==(other) ⇒ Object



43
44
45
# File 'lib/just_go/point.rb', line 43

def ==(other)
  self.id == other.id
end

#add_to_territory(t_id) ⇒ Object



75
76
77
# File 'lib/just_go/point.rb', line 75

def add_to_territory(t_id)
  @territory_id = t_id
end

#as_jsonObject



32
33
34
35
36
37
38
39
40
41
# File 'lib/just_go/point.rb', line 32

def as_json
  _stone = stone ? stone.as_json : nil
  {
    id: id,
    x: x,
    y: y,
    stone: _stone,
    territory_id: territory_id
  }
end

#capture_stoneObject



71
72
73
# File 'lib/just_go/point.rb', line 71

def capture_stone
  @stone = nil
end

#clear_territoryObject



79
80
81
# File 'lib/just_go/point.rb', line 79

def clear_territory
  @territory_id = nil
end

#occupied?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/just_go/point.rb', line 47

def occupied?
  !stone.nil?
end

#occupied_by?(player_number) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/just_go/point.rb', line 55

def occupied_by?(player_number)
  !stone.nil? && stone.player_number == player_number
end

#occupied_by_opponent?(player_number) ⇒ Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/just_go/point.rb', line 59

def occupied_by_opponent?(player_number)
  !stone.nil? && stone.player_number != player_number
end

#place(s) ⇒ Object



67
68
69
# File 'lib/just_go/point.rb', line 67

def place(s)
  @stone = s
end

#unmarked?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/just_go/point.rb', line 63

def unmarked?
  territory_id.nil?
end

#unoccupied?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/just_go/point.rb', line 51

def unoccupied?
  stone.nil?
end