gobgems 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: bb29086c782595aa8c66939dfcbfdc7f80bb2402
4
+ data.tar.gz: 18f24cb731be7c545d68176fbcc6a7aae99d47b7
5
+ SHA512:
6
+ metadata.gz: 1625669f34c466ae8c9cd721c3327f3da77d3a0e99537a2ff371ef1e5437dab1b02c8b59ea46dc1fe2d485eac27ac7cf442f9d414135bb90662355470b9af59a
7
+ data.tar.gz: 1d51527eca9d0819f69c7a8597d881eacb6b3291c0b29e2d60cd7f77388346aafe2dcf398891edc0052c5b368bdcc1fb41090534af76a06b1e7afe9cd7627be7
@@ -0,0 +1,5 @@
1
+ class Array
2
+ def init(n=1)
3
+ take size - n
4
+ end
5
+ end
@@ -0,0 +1,105 @@
1
+ module Gobgems
2
+ class OutOfBoardError < RuntimeError
3
+ end
4
+
5
+ module WithMovementOps
6
+ def can_move?(direction)
7
+ within_bounds? next_position(direction)
8
+ end
9
+
10
+ def move(direction)
11
+ __move_to__ next_position(direction)
12
+ end
13
+
14
+ def __move_to__(position)
15
+ raise OutOfBoardError unless within_bounds? position
16
+ @head_position = position
17
+ end
18
+
19
+ private
20
+
21
+ def next_position(direction)
22
+ direction.call(*@head_position)
23
+ end
24
+
25
+ end
26
+
27
+ module WithColorOps
28
+ def push(color, amount=1)
29
+ head_cell[color] += amount
30
+ end
31
+
32
+ def pop(color)
33
+ raise "#{color} Underflow" if head_cell[color] == 0
34
+ head_cell[color] -= 1
35
+ end
36
+
37
+ def count(color)
38
+ head_cell[color]
39
+ end
40
+
41
+ def exist?(color)
42
+ count(color) > 0
43
+ end
44
+ end
45
+
46
+ class Board
47
+ include WithMovementOps
48
+ include WithColorOps
49
+
50
+ attr_reader :cells, :head_position
51
+
52
+ def initialize(cells, position)
53
+ @cells = cells
54
+ @head_position = position
55
+ end
56
+
57
+ def size
58
+ [cells.size, cells[0].size]
59
+ end
60
+
61
+ def ==(other)
62
+ self.class == other.class &&
63
+ self.cells == other.cells &&
64
+ self.head_position == other.head_position
65
+ end
66
+
67
+ def hash
68
+ self.cells.hash ^ self.head_position.hash
69
+ end
70
+
71
+ def self.empty(x, y, position=[0, 0])
72
+ self.new((1..x).map { (1..y).map { empty_cell } }, position)
73
+ end
74
+
75
+ def self.from(cells, position=[0, 0])
76
+ self.new(cells.map { |row| row.map { |cell| empty_cell.merge(cell) } }, position)
77
+ end
78
+
79
+ def __set_cell__(position, cell)
80
+ __cell_at__(position).merge! cell
81
+ end
82
+
83
+ def __cell_at__(position)
84
+ raise OutOfBoardError unless within_bounds? position
85
+ cells[position[0]][position[1]]
86
+ end
87
+
88
+ private
89
+
90
+ def within_bounds?(position)
91
+ (x, y) = size
92
+ position[0] >= 0 && position[1] >= 0 &&
93
+ position[0] <= x && position[1] <= y
94
+ end
95
+
96
+ def head_cell
97
+ __cell_at__(head_position)
98
+ end
99
+
100
+ def self.empty_cell
101
+ {red: 0, black: 0, green: 0, blue: 0}
102
+ end
103
+ end
104
+ end
105
+
@@ -0,0 +1,25 @@
1
+ module Gobgems
2
+ module Color
3
+ def red
4
+ :red
5
+ end
6
+
7
+ def blue
8
+ :blue
9
+ end
10
+
11
+ def green
12
+ :green
13
+ end
14
+
15
+ def black
16
+ :black
17
+ end
18
+
19
+ def all
20
+ [red, green, black, blue]
21
+ end
22
+
23
+ module_function :red, :blue, :green, :black, :all
24
+ end
25
+ end
@@ -0,0 +1,25 @@
1
+ module Gobgems
2
+ module Direction
3
+ def east
4
+ @east ||= -> (x, y) { [x, y+1] }
5
+ end
6
+
7
+ def west
8
+ @west ||= -> (x, y) { [x, y-1] }
9
+ end
10
+
11
+ def north
12
+ @north ||= -> (x, y) { [x-1, y] }
13
+ end
14
+
15
+ def south
16
+ @south ||= -> (x, y) { [x+1, y] }
17
+ end
18
+
19
+ def all
20
+ [east, west, south, north]
21
+ end
22
+
23
+ module_function :north, :south, :east, :west, :all
24
+ end
25
+ end
@@ -0,0 +1,11 @@
1
+ module Gobgems
2
+ class ExecutionContext
3
+ attr_accessor :board
4
+
5
+ def run(program_class)
6
+ program = program_class.new
7
+ program.board = board
8
+ program.main
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,52 @@
1
+ require_relative '../extensions/array.rb'
2
+
3
+
4
+ class MatchData
5
+ def to_position
6
+ [self[1].to_i, self[2].to_i]
7
+ end
8
+ end
9
+
10
+ module Gobgems
11
+ class GbbParser
12
+ COLORS = {'Azul' => :blue, 'Negro' => :black, 'Rojo' => :red, 'Verde' => :green}
13
+
14
+ def from_string(gbb_string)
15
+ board = nil
16
+
17
+ lines = gbb_string.lines.reject { |it| it.start_with? 'GBB', '%%' }
18
+
19
+ /size (\d+) (\d+)/.match(lines[0]) { |match| board = Board.empty(match[1].to_i, match[2].to_i) }
20
+ /head (\d+) (\d+)/.match(lines.last) { |match| board.__move_to__ match.to_position }
21
+
22
+ lines.drop(1).init.each do |cell_line|
23
+ position = get_position_from cell_line
24
+ cell = create_cell_from cell_line
25
+
26
+ board.__set_cell__ position, cell
27
+ end
28
+
29
+ board
30
+ end
31
+
32
+ private
33
+
34
+ def get_position_from(cell_line)
35
+ /cell (\d+) (\d+)/.match(cell_line).to_position
36
+ end
37
+
38
+ def create_cell_from(cell_line)
39
+ cell = {}
40
+
41
+ cell_line.scan(/(Azul|Negro|Rojo|Verde) (\d+)/) do |match|
42
+ cell[to_color(match[0])] = match[1].to_i
43
+ end
44
+
45
+ cell
46
+ end
47
+
48
+ def to_color(gbb_color_string)
49
+ COLORS[gbb_color_string]
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,32 @@
1
+ module Gobgems
2
+ module Program
3
+ attr_accessor :board
4
+
5
+ include Gobgems::Color
6
+ include Gobgems::Direction
7
+
8
+ def move(direction)
9
+ board.move(direction)
10
+ end
11
+
12
+ def push(color)
13
+ board.push(color)
14
+ end
15
+
16
+ def pop(color)
17
+ board.pop(color)
18
+ end
19
+
20
+ def exist?(color)
21
+ board.exists?(color)
22
+ end
23
+
24
+ def can_move?(direction)
25
+ board.can_move?(direction)
26
+ end
27
+
28
+ def count(color)
29
+ board.count(color)
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,3 @@
1
+ module Gobgems
2
+ VERSION = '0.0.1'
3
+ end
data/lib/gobgems.rb ADDED
@@ -0,0 +1,10 @@
1
+ module Gobgems
2
+ end
3
+
4
+ require_relative 'gobgems/direction'
5
+ require_relative 'gobgems/color'
6
+ require_relative 'gobgems/board'
7
+ require_relative 'gobgems/execution_context'
8
+ require_relative 'gobgems/program'
9
+ require_relative 'gobgems/gbb_parser'
10
+ require_relative 'gobgems/version'
metadata ADDED
@@ -0,0 +1,111 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gobgems
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Federico Aloi
8
+ - Franco Bulgarelli
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2015-05-27 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '1.7'
21
+ type: :development
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '1.7'
28
+ - !ruby/object:Gem::Dependency
29
+ name: rake
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '10.0'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '10.0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: rspec
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - "~>"
47
+ - !ruby/object:Gem::Version
48
+ version: '2'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - "~>"
54
+ - !ruby/object:Gem::Version
55
+ version: '2'
56
+ - !ruby/object:Gem::Dependency
57
+ name: codeclimate-test-reporter
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ description: Write Gobstones program within Ruby
71
+ email:
72
+ - federico.aloi@gmail.com
73
+ - flbulgarelli@yahoo.com.ar
74
+ executables: []
75
+ extensions: []
76
+ extra_rdoc_files: []
77
+ files:
78
+ - lib/extensions/array.rb
79
+ - lib/gobgems.rb
80
+ - lib/gobgems/board.rb
81
+ - lib/gobgems/color.rb
82
+ - lib/gobgems/direction.rb
83
+ - lib/gobgems/execution_context.rb
84
+ - lib/gobgems/gbb_parser.rb
85
+ - lib/gobgems/program.rb
86
+ - lib/gobgems/version.rb
87
+ homepage: https://github.com/flbulgarelli/gobgems
88
+ licenses:
89
+ - MIT
90
+ metadata: {}
91
+ post_install_message:
92
+ rdoc_options: []
93
+ require_paths:
94
+ - lib
95
+ required_ruby_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ required_rubygems_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ requirements: []
106
+ rubyforge_project:
107
+ rubygems_version: 2.2.0
108
+ signing_key:
109
+ specification_version: 4
110
+ summary: Pure ruby library that implements a Gobstones-like board and dsl
111
+ test_files: []