Class: EightyOne::Piece

Inherits:
Object
  • Object
show all
Includes:
Helper
Defined in:
lib/eighty-one/piece.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helper

#assert

Constructor Details

#initialize(turn) ⇒ Piece

Returns a new instance of Piece.



18
19
20
21
22
# File 'lib/eighty-one/piece.rb', line 18

def initialize(turn)
  assert(turn == :sente || turn == :gote)
  @turn = turn
  @promoted = false
end

Instance Attribute Details

#turnObject (readonly)

Returns the value of attribute turn.



16
17
18
# File 'lib/eighty-one/piece.rb', line 16

def turn
  @turn
end

Class Method Details

.decode(code) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/eighty-one/piece.rb', line 67

def self.decode(code)
  turn = code[0] == ?+ ? :sente : :gote
  symbol = code[1, 2].to_sym
  piece_class = Pieces::ALL.find{|p| p.forward.symbol == symbol }
  if piece_class
    piece_class.new(turn)
  else
    piece_class = ALL.find{|p| p.backword.symbol == symbol }
    if piece_class
      piece_class.new(turn).promote
    end
  end
end

.new_class(forward, backword) ⇒ Object



5
6
7
8
9
10
11
12
13
14
# File 'lib/eighty-one/piece.rb', line 5

def self.new_class(forward, backword)
  Class.new(self) do |c|
    c.instance_eval do
      define_method(:forward) { forward }
      define_singleton_method(:forward) { forward }
      define_method(:backward) { backward }
      define_singleton_method(:backward) { backward }
    end
  end
end

Instance Method Details

#encodeObject



59
60
61
# File 'lib/eighty-one/piece.rb', line 59

def encode
  (@turn == :sente ? ?+ : ?-) + face.symbol.to_s
end

#faceObject



24
25
26
# File 'lib/eighty-one/piece.rb', line 24

def face
  @promoted ? backward : forward
end

#forward?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/eighty-one/piece.rb', line 43

def forward?
  !@promoted
end

#gote?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/eighty-one/piece.rb', line 55

def gote?
  @turn == :gote
end

#oppositeObject



47
48
49
# File 'lib/eighty-one/piece.rb', line 47

def opposite
  @turn.sente? ? :gote : :sente
end

#promoteObject



34
35
36
37
# File 'lib/eighty-one/piece.rb', line 34

def promote
  @promoted = true
  self
end

#promoted?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/eighty-one/piece.rb', line 39

def promoted?
  @promoted
end

#reset(turn) ⇒ Object



28
29
30
31
32
# File 'lib/eighty-one/piece.rb', line 28

def reset(turn)
  @turn = turn
  @promote = false
  self
end

#sente?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/eighty-one/piece.rb', line 51

def sente?
  @turn == :sente
end

#to_sObject



63
64
65
# File 'lib/eighty-one/piece.rb', line 63

def to_s
  self.encode
end