sashite-ggn 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (69) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +523 -521
  3. data/VERSION.semver +1 -1
  4. data/lib/sashite/ggn.rb +3 -2
  5. data/lib/sashite/ggn/ability.rb +11 -19
  6. data/lib/sashite/ggn/actor.rb +5 -13
  7. data/lib/sashite/ggn/ally.rb +6 -14
  8. data/lib/sashite/ggn/area.rb +6 -14
  9. data/lib/sashite/ggn/attacked.rb +6 -14
  10. data/lib/sashite/ggn/boolean.rb +6 -14
  11. data/lib/sashite/ggn/digit.rb +6 -14
  12. data/lib/sashite/ggn/digit_excluding_zero.rb +6 -14
  13. data/lib/sashite/ggn/direction.rb +6 -14
  14. data/lib/sashite/ggn/gameplay.rb +8 -28
  15. data/lib/sashite/ggn/gameplay_into_base64.rb +7 -14
  16. data/lib/sashite/ggn/integer.rb +6 -14
  17. data/lib/sashite/ggn/last_moved_actor.rb +6 -14
  18. data/lib/sashite/ggn/maximum_magnitude.rb +6 -14
  19. data/lib/sashite/ggn/name.rb +6 -14
  20. data/lib/sashite/ggn/negative_integer.rb +6 -14
  21. data/lib/sashite/ggn/null.rb +7 -9
  22. data/lib/sashite/ggn/object.rb +11 -19
  23. data/lib/sashite/ggn/occupied.rb +12 -20
  24. data/lib/sashite/ggn/pattern.rb +7 -16
  25. data/lib/sashite/ggn/previous_moves_counter.rb +6 -14
  26. data/lib/sashite/ggn/promotable_into_actors.rb +8 -17
  27. data/lib/sashite/ggn/required.rb +6 -14
  28. data/lib/sashite/ggn/self.rb +7 -9
  29. data/lib/sashite/ggn/square.rb +11 -19
  30. data/lib/sashite/ggn/state.rb +9 -17
  31. data/lib/sashite/ggn/subject.rb +11 -19
  32. data/lib/sashite/ggn/unsigned_integer.rb +6 -14
  33. data/lib/sashite/ggn/unsigned_integer_excluding_zero.rb +5 -13
  34. data/lib/sashite/ggn/verb.rb +15 -24
  35. data/lib/sashite/ggn/zero.rb +7 -9
  36. data/sashite-ggn.gemspec +2 -4
  37. data/test/test_ggn.rb +501 -495
  38. data/test/test_ggn_ability.rb +10 -10
  39. data/test/test_ggn_actor.rb +516 -536
  40. data/test/test_ggn_ally.rb +13 -29
  41. data/test/test_ggn_area.rb +9 -67
  42. data/test/test_ggn_attacked.rb +13 -29
  43. data/test/test_ggn_boolean.rb +10 -26
  44. data/test/test_ggn_digit.rb +10 -10
  45. data/test/test_ggn_digit_excluding_zero.rb +10 -11
  46. data/test/test_ggn_direction.rb +10 -10
  47. data/test/test_ggn_gameplay.rb +22 -38
  48. data/test/test_ggn_gameplay_into_base64.rb +511 -501
  49. data/test/test_ggn_integer.rb +19 -21
  50. data/test/test_ggn_last_moved_actor.rb +13 -29
  51. data/test/test_ggn_maximum_magnitude.rb +18 -16
  52. data/test/test_ggn_name.rb +9 -39
  53. data/test/test_ggn_negative_integer.rb +10 -10
  54. data/test/test_ggn_null.rb +10 -10
  55. data/test/test_ggn_object.rb +22 -24
  56. data/test/test_ggn_occupied.rb +40 -64
  57. data/test/test_ggn_pattern.rb +12 -10
  58. data/test/test_ggn_previous_moves_counter.rb +18 -20
  59. data/test/test_ggn_promotable_into_actors.rb +529 -549
  60. data/test/test_ggn_required.rb +10 -26
  61. data/test/test_ggn_self.rb +10 -10
  62. data/test/test_ggn_square.rb +14 -16
  63. data/test/test_ggn_state.rb +15 -19
  64. data/test/test_ggn_subject.rb +17 -19
  65. data/test/test_ggn_unsigned_integer.rb +30 -12
  66. data/test/test_ggn_unsigned_integer_excluding_zero.rb +14 -10
  67. data/test/test_ggn_verb.rb +16 -15
  68. data/test/test_ggn_zero.rb +10 -10
  69. metadata +4 -18
@@ -3,25 +3,17 @@ require_relative 'unsigned_integer_excluding_zero'
3
3
 
4
4
  module Sashite
5
5
  module GGN
6
- class MaximumMagnitude
6
+ module MaximumMagnitude
7
7
  PATTERN = /(#{Null::PATTERN}|#{UnsignedIntegerExcludingZero::PATTERN})/
8
8
 
9
- def self.valid? str
10
- !!str.match("^#{PATTERN}$")
9
+ def self.valid? io
10
+ !!io.match("^#{PATTERN}$")
11
11
  end
12
12
 
13
- def initialize str
14
- raise ArgumentError unless self.class.valid? str
13
+ def self.load io
14
+ raise ArgumentError unless valid? io
15
15
 
16
- @value = (Null.valid?(str) ? Null.instance : UnsignedIntegerExcludingZero.new(str))
17
- end
18
-
19
- def as_json
20
- @value.as_json
21
- end
22
-
23
- def to_s
24
- @value.to_s
16
+ Null.valid?(io) ? Null.load : UnsignedIntegerExcludingZero.load(io)
25
17
  end
26
18
  end
27
19
  end
@@ -1,24 +1,16 @@
1
1
  module Sashite
2
2
  module GGN
3
- class Name
3
+ module Name
4
4
  PATTERN = /(capture|remove|shift)/
5
5
 
6
- def self.valid? str
7
- !!str.match("^#{PATTERN}$")
6
+ def self.valid? io
7
+ !!io.match("^#{PATTERN}$")
8
8
  end
9
9
 
10
- def initialize str
11
- raise ArgumentError unless self.class.valid? str
10
+ def self.load io
11
+ raise ArgumentError unless valid? io
12
12
 
13
- @value = str.to_sym
14
- end
15
-
16
- def as_json
17
- @value
18
- end
19
-
20
- def to_s
21
- @value.to_s
13
+ io.to_sym
22
14
  end
23
15
  end
24
16
  end
@@ -2,25 +2,17 @@ require_relative 'unsigned_integer_excluding_zero'
2
2
 
3
3
  module Sashite
4
4
  module GGN
5
- class NegativeInteger
5
+ module NegativeInteger
6
6
  PATTERN = /-#{UnsignedIntegerExcludingZero::PATTERN}/
7
7
 
8
- def self.valid? str
9
- !!str.match("^#{PATTERN}$")
8
+ def self.valid? io
9
+ !!io.match("^#{PATTERN}$")
10
10
  end
11
11
 
12
- def initialize str
13
- raise ArgumentError unless self.class.valid? str
12
+ def self.load io
13
+ raise ArgumentError unless valid? io
14
14
 
15
- @value = str.to_i
16
- end
17
-
18
- def as_json
19
- @value
20
- end
21
-
22
- def to_s
23
- @value.to_s
15
+ io.to_i
24
16
  end
25
17
  end
26
18
  end
@@ -1,21 +1,19 @@
1
- require 'singleton'
2
-
3
1
  module Sashite
4
2
  module GGN
5
- class Null
6
- include Singleton
7
-
3
+ module Null
8
4
  PATTERN = /_/
9
5
 
10
- def self.valid? str
11
- !!str.match("^#{PATTERN}$")
6
+ def self.valid? io
7
+ !!io.match("^#{PATTERN}$")
12
8
  end
13
9
 
14
- def as_json
10
+ def self.load io = nil
11
+ raise ArgumentError if io && !valid?(io)
12
+
15
13
  nil
16
14
  end
17
15
 
18
- def to_s
16
+ def self.dump
19
17
  '_'
20
18
  end
21
19
  end
@@ -3,34 +3,26 @@ require_relative 'promotable_into_actors'
3
3
 
4
4
  module Sashite
5
5
  module GGN
6
- class Object
6
+ module Object
7
7
  PATTERN = /#{Square::PATTERN}~#{Square::PATTERN}%#{PromotableIntoActors::PATTERN}/
8
8
 
9
- def self.valid? str
10
- !!str.match("^#{PATTERN}$")
9
+ def self.valid? io
10
+ !!io.match("^#{PATTERN}$")
11
11
  end
12
12
 
13
- attr_reader :src_square, :dst_square, :promotable_into_actors
13
+ def self.load io
14
+ raise ArgumentError unless valid? io
14
15
 
15
- def initialize str
16
- raise ArgumentError unless self.class.valid? str
16
+ src_square = Square.load io.split('~').fetch 0
17
+ dst_square = Square.load io.split('~').fetch(1).split('%').fetch 0
18
+ promotable_into_actors = PromotableIntoActors.load io.split('%').fetch 1
17
19
 
18
- @src_square = Square.new str.split('~').fetch 0
19
- @dst_square = Square.new str.split('~').fetch(1).split('%').fetch 0
20
- @promotable_into_actors = PromotableIntoActors.new str.split('%').fetch 1
21
- end
22
-
23
- def as_json
24
20
  {
25
- src_square: @src_square.as_json,
26
- dst_square: @dst_square.as_json,
27
- promotable_into_actors: @promotable_into_actors.as_json
21
+ src_square: src_square,
22
+ dst_square: dst_square,
23
+ promotable_into_actors: promotable_into_actors
28
24
  }
29
25
  end
30
-
31
- def to_s
32
- "#{@src_square}~#{@dst_square}%#{@promotable_into_actors}"
33
- end
34
26
  end
35
27
  end
36
28
  end
@@ -4,34 +4,26 @@ require_relative 'subject'
4
4
 
5
5
  module Sashite
6
6
  module GGN
7
- class Occupied
7
+ module Occupied
8
8
  PATTERN = /(#{Null::PATTERN}|#{Boolean::PATTERN}|#{Subject::PATTERN}|an_ally_actor|an_enemy_actor)/
9
9
 
10
- def self.valid? str
11
- !!str.match("^#{PATTERN}$")
10
+ def self.valid? io
11
+ !!io.match("^#{PATTERN}$")
12
12
  end
13
13
 
14
- def initialize str
15
- raise ArgumentError unless self.class.valid? str
14
+ def self.load io
15
+ raise ArgumentError unless valid? io
16
16
 
17
- @value = if Null.valid? str
18
- Null.instance
19
- elsif Boolean.valid? str
20
- Boolean.new str
21
- elsif Subject.valid? str
22
- Subject.new str
17
+ if Null.valid? io
18
+ Null.load
19
+ elsif Boolean.valid? io
20
+ Boolean.load io
21
+ elsif Subject.valid? io
22
+ Subject.load io
23
23
  else
24
- str.to_sym
24
+ io.to_sym
25
25
  end
26
26
  end
27
-
28
- def as_json
29
- @value.is_a?(Symbol) ? @value : @value.as_json
30
- end
31
-
32
- def to_s
33
- @value.to_s
34
- end
35
27
  end
36
28
  end
37
29
  end
@@ -2,27 +2,18 @@ require_relative 'ability'
2
2
 
3
3
  module Sashite
4
4
  module GGN
5
- class Pattern
5
+ module Pattern
6
6
  PATTERN = /#{Ability::PATTERN}(; #{Ability::PATTERN})*/
7
7
 
8
- def self.valid? str
9
- !!str.match("^#{PATTERN}$")
8
+ def self.valid? io
9
+ io.match("^#{PATTERN}$") &&
10
+ io.split('; ').uniq.join('; ') == io
10
11
  end
11
12
 
12
- attr_reader :abilities
13
+ def self.load io
14
+ raise ArgumentError unless valid? io
13
15
 
14
- def initialize str
15
- raise ArgumentError unless self.class.valid? str
16
-
17
- @abilities = str.split('; ').map { |ability| Ability.new ability }
18
- end
19
-
20
- def as_json
21
- @abilities.map &:as_json
22
- end
23
-
24
- def to_s
25
- @abilities.map(&:to_s).join '; '
16
+ io.split('; ').map { |ability| Ability.load ability }
26
17
  end
27
18
  end
28
19
  end
@@ -3,25 +3,17 @@ require_relative 'unsigned_integer'
3
3
 
4
4
  module Sashite
5
5
  module GGN
6
- class PreviousMovesCounter
6
+ module PreviousMovesCounter
7
7
  PATTERN = /(#{Null::PATTERN}|#{UnsignedInteger::PATTERN})/
8
8
 
9
- def self.valid? str
10
- !!str.match("^#{PATTERN}$")
9
+ def self.valid? io
10
+ !!io.match("^#{PATTERN}$")
11
11
  end
12
12
 
13
- def initialize str
14
- raise ArgumentError unless self.class.valid? str
13
+ def self.load io
14
+ raise ArgumentError unless valid? io
15
15
 
16
- @value = (Null.valid?(str) ? Null.instance : UnsignedInteger.new(str))
17
- end
18
-
19
- def as_json
20
- @value.as_json
21
- end
22
-
23
- def to_s
24
- @value.to_s
16
+ Null.valid?(io) ? Null.load : UnsignedInteger.load(io)
25
17
  end
26
18
  end
27
19
  end
@@ -3,30 +3,21 @@ require_relative 'gameplay_into_base64'
3
3
 
4
4
  module Sashite
5
5
  module GGN
6
- class PromotableIntoActors
6
+ module PromotableIntoActors
7
7
  PATTERN = /(#{GameplayIntoBase64::PATTERN},)*#{Actor::PATTERN}/
8
8
 
9
- def self.valid? str
10
- !!str.match("^#{PATTERN}$")
9
+ def self.valid? io
10
+ io.match("^#{PATTERN}$") &&
11
+ io.split(',').uniq.join(',') == io
11
12
  end
12
13
 
13
- attr_reader :values
14
+ def self.load io
15
+ raise ArgumentError unless valid? io
14
16
 
15
- def initialize str
16
- raise ArgumentError unless self.class.valid? str
17
-
18
- @values = str.split(',').map do |value|
19
- Actor.valid?(value) ? Actor.new(value) : GameplayIntoBase64.new(value)
17
+ io.split(',').map do |value|
18
+ Actor.valid?(value) ? Actor.load(value) : GameplayIntoBase64.load(value)
20
19
  end
21
20
  end
22
-
23
- def as_json
24
- @values.map &:as_json
25
- end
26
-
27
- def to_s
28
- @values.map(&:to_s).join ','
29
- end
30
21
  end
31
22
  end
32
23
  end
@@ -2,25 +2,17 @@ require_relative 'boolean'
2
2
 
3
3
  module Sashite
4
4
  module GGN
5
- class Required
5
+ module Required
6
6
  PATTERN = /#{Boolean::PATTERN}/
7
7
 
8
- def self.valid? str
9
- !!str.match("^#{PATTERN}$")
8
+ def self.valid? io
9
+ !!io.match("^#{PATTERN}$")
10
10
  end
11
11
 
12
- def initialize str
13
- raise ArgumentError unless self.class.valid? str
12
+ def self.load io
13
+ raise ArgumentError unless valid? io
14
14
 
15
- @value = Boolean.new(str)
16
- end
17
-
18
- def as_json
19
- @value.as_json
20
- end
21
-
22
- def to_s
23
- @value.to_s
15
+ Boolean.load io
24
16
  end
25
17
  end
26
18
  end
@@ -1,21 +1,19 @@
1
- require 'singleton'
2
-
3
1
  module Sashite
4
2
  module GGN
5
- class Self
6
- include Singleton
7
-
3
+ module Self
8
4
  PATTERN = /self/
9
5
 
10
- def self.valid? str
11
- !!str.match("^#{PATTERN}$")
6
+ def self.valid? io
7
+ !!io.match("^#{PATTERN}$")
12
8
  end
13
9
 
14
- def as_json
10
+ def self.load io = nil
11
+ raise ArgumentError if io && !valid?(io)
12
+
15
13
  :self
16
14
  end
17
15
 
18
- def to_s
16
+ def self.dump
19
17
  'self'
20
18
  end
21
19
  end
@@ -4,34 +4,26 @@ require_relative 'area'
4
4
 
5
5
  module Sashite
6
6
  module GGN
7
- class Square
7
+ module Square
8
8
  PATTERN = /#{Attacked::PATTERN}@#{Occupied::PATTERN}\+#{Area::PATTERN}/
9
9
 
10
- def self.valid? str
11
- !!str.match("^#{PATTERN}$")
10
+ def self.valid? io
11
+ !!io.match("^#{PATTERN}$")
12
12
  end
13
13
 
14
- attr_reader :attacked, :occupied, :area
14
+ def self.load io
15
+ raise ArgumentError unless valid? io
15
16
 
16
- def initialize str
17
- raise ArgumentError unless self.class.valid? str
17
+ attacked = Attacked.load io.split('@').fetch(0)
18
+ occupied = Occupied.load io.split('@').fetch(1).split('+').fetch(0)
19
+ area = Area.load io.split('+').fetch(1)
18
20
 
19
- @attacked = Attacked.new str.split('@').fetch(0)
20
- @occupied = Occupied.new str.split('@').fetch(1).split('+').fetch(0)
21
- @area = Area.new str.split('+').fetch(1)
22
- end
23
-
24
- def as_json
25
21
  {
26
- :"...attacked?" => @attacked.as_json,
27
- :"...occupied!" => @occupied.as_json,
28
- :"area" => @area.as_json
22
+ :"...attacked?" => attacked,
23
+ :"...occupied!" => occupied,
24
+ :"area" => area
29
25
  }
30
26
  end
31
-
32
- def to_s
33
- "#{@attacked}@#{@occupied}+#{@area}"
34
- end
35
27
  end
36
28
  end
37
29
  end
@@ -3,32 +3,24 @@ require_relative 'previous_moves_counter'
3
3
 
4
4
  module Sashite
5
5
  module GGN
6
- class State
6
+ module State
7
7
  PATTERN = /#{LastMovedActor::PATTERN}&#{PreviousMovesCounter::PATTERN}/
8
8
 
9
- def self.valid? str
10
- !!str.match("^#{PATTERN}$")
9
+ def self.valid? io
10
+ !!io.match("^#{PATTERN}$")
11
11
  end
12
12
 
13
- attr_reader :previous_moves_counter, :last_moved_actor
13
+ def self.load io
14
+ raise ArgumentError unless valid? io
14
15
 
15
- def initialize str
16
- raise ArgumentError unless self.class.valid? str
16
+ last_moved_actor = LastMovedActor.load io.split('&').fetch(0)
17
+ previous_moves_counter = PreviousMovesCounter.load io.split('&').fetch(1)
17
18
 
18
- @last_moved_actor = LastMovedActor.new str.split('&').fetch(0)
19
- @previous_moves_counter = PreviousMovesCounter.new str.split('&').fetch(1)
20
- end
21
-
22
- def as_json
23
19
  {
24
- :"...last_moved_actor?" => @last_moved_actor.as_json,
25
- :"...previous_moves_counter" => @previous_moves_counter.as_json
20
+ :"...last_moved_actor?" => last_moved_actor,
21
+ :"...previous_moves_counter" => previous_moves_counter
26
22
  }
27
23
  end
28
-
29
- def to_s
30
- "#{@last_moved_actor}&#{@previous_moves_counter}"
31
- end
32
24
  end
33
25
  end
34
26
  end