slack_game 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b832f686474b7cbb6d76598301f784be79857623
4
- data.tar.gz: 0396d9bca5e3405f750d34f2fa63dc44891a06dd
3
+ metadata.gz: e9ca92e8dec26d82482e34d8c25ae15655b2efc8
4
+ data.tar.gz: fa710eaa80fa6863d2868d60f1c5b5858036c7f0
5
5
  SHA512:
6
- metadata.gz: 244887aae4a178155ad508371e85d67d30cf63141969f679a755c22cb3c3b3b56d74753da570a290fc1098d92c868bf9b53423ba40e61b37b8a821ad3dd8ff3e
7
- data.tar.gz: 813db0f650369c3f5f31722a00c4ead8b7dc84bd8b82b74d584236a9239cf39ae06290589f926de41c5b40e5233a483a76b32ef9b08885b1a9b72511ff2e6751
6
+ metadata.gz: 5beb8b5b3a780646b797bfab90bb05af6e2a11e6f4f95dc49a530b102b88b5e703d4d1f8df5d9a1c10de612256970f0c0eca2e8a9e00d3e488b49f40c1994365
7
+ data.tar.gz: d6ef909422cba057e3768e1f62577e8e1df9caf3a5dc2a26f40b091445809a705a874d8d7b4acda2b38e9893975a999e34daf1a0f2d86513a6297f7c20473de4
@@ -4,12 +4,14 @@ module SlackGame
4
4
  attr_accessor :matrix
5
5
 
6
6
  def self.inherited(subclass)
7
+ subclass.class_variable_set(:@@dot, {})
8
+
7
9
  def subclass.dot(id, emoji)
8
- dot_map[id] = emoji
10
+ self.dot_map[id] = emoji
9
11
  end
10
12
 
11
13
  def subclass.dot_map
12
- @@dot ||= {}
14
+ self.class_variable_get(:@@dot)
13
15
  end
14
16
  end
15
17
 
@@ -20,11 +22,11 @@ module SlackGame
20
22
  end
21
23
 
22
24
  def draw
23
- decoded_matrix = encode(matrix)
25
+ encoded_matrix = encode(matrix)
24
26
  result = if last_update
25
- @slack.chat_update(ts: last_update, channel: channel, text: decoded_matrix)
27
+ @slack.chat_update(ts: last_update, channel: channel, text: encoded_matrix)
26
28
  else
27
- @slack.chat_postMessage(channel: channel, text: decoded_matrix, as_user: true)
29
+ @slack.chat_postMessage(channel: channel, text: encoded_matrix, as_user: true)
28
30
  end
29
31
  @last_update = result['ok'] ? result['ts'] : raise
30
32
  end
@@ -32,15 +34,15 @@ module SlackGame
32
34
  private
33
35
 
34
36
  def encode(matrix)
35
- decoded = matrix.map { |l| line_decode(l) }
36
- decoded.join("\n")
37
+ encoded = matrix.map { |l| line_encode(l) }
38
+ encoded.join("\n")
37
39
  end
38
40
 
39
- def line_decode(line)
40
- decoded_line = line.map do |t|
41
+ def line_encode(line)
42
+ encoded_line = line.map do |t|
41
43
  dot_emoji(t)
42
44
  end
43
- decoded_line.join
45
+ encoded_line.join
44
46
  end
45
47
 
46
48
  def dot_emoji(id)
@@ -2,12 +2,14 @@ module SlackGame
2
2
  class Controller
3
3
 
4
4
  def self.inherited(subclass)
5
+ subclass.class_variable_set(:@@parsers, [])
6
+
5
7
  def subclass.command(command, pattern)
6
8
  parsers << InputParser.new(command, pattern)
7
9
  end
8
10
 
9
11
  def subclass.parsers
10
- @@parsers ||= []
12
+ self.class_variable_get(:@@parsers)
11
13
  end
12
14
  end
13
15
 
@@ -1,6 +1,7 @@
1
1
  module SlackGame
2
2
  module Game
3
3
  require 'slack_game/game/demo'
4
+ require 'slack_game/game/lifegame'
4
5
  end
5
6
  end
6
7
 
@@ -0,0 +1,90 @@
1
+ require 'slack_game/game/lifegame/canvas'
2
+
3
+ module SlackGame
4
+ module Game
5
+ class Lifegame
6
+ def initialize(channel, size = 10)
7
+ @canvas = Canvas.new(channel, size, size)
8
+ @field = Field.new(size)
9
+ @canvas.matrix = @field.to_display
10
+ @canvas.draw
11
+ end
12
+
13
+ def main_loop
14
+ loop{
15
+ begin
16
+ update
17
+ rescue => e
18
+ puts "GAME OVER #{e.message}"
19
+ break
20
+ end
21
+ }
22
+ end
23
+
24
+ def update
25
+ @canvas.matrix = @field.next.to_display
26
+ @canvas.draw
27
+ end
28
+
29
+ class Field
30
+ attr_reader :field
31
+ def initialize(size)
32
+ @size = size
33
+ @field = Array.new(size) { Array.new(size) { rand < 0.3 ? Cell.new(true) : Cell.new(false) } }
34
+ end
35
+
36
+ def next
37
+ next_field = Array.new(@size) { Array.new(@size) }
38
+ @field.each_with_index do |line, outer_index|
39
+ line.each_with_index do |cell, inner_index|
40
+ next_field[outer_index][inner_index] = cell.next_gen(arround_cells(outer_index, inner_index))
41
+ end
42
+ end
43
+ @field = next_field
44
+ self
45
+ end
46
+
47
+ def to_display
48
+ @field.map do |l|
49
+ l.map do |d|
50
+ d.alive? ? Canvas::ARIVE : Canvas::DEAD
51
+ end
52
+ end
53
+ end
54
+
55
+ private
56
+
57
+ def arround_cells(row, col)
58
+ cells = []
59
+ [row-1, row, row+1].each do |n|
60
+ [col-1, col, col+1].each do |m|
61
+ if (n!=row || m!=col) && 0 <= n && n < @size && 0 <= m && m < @size
62
+ cells << @field[n][m]
63
+ end
64
+ end
65
+ end
66
+ cells
67
+ end
68
+ end
69
+
70
+ class Cell
71
+ def initialize(alive)
72
+ @alive = !! alive
73
+ end
74
+
75
+ def alive?
76
+ !! @alive
77
+ end
78
+
79
+ def next_gen(arround_cells)
80
+ alives = arround_cells.select(&:alive?).size
81
+ if alive?
82
+ (alives <= 1 || 4 <= alives) ? Cell.new(false) : Cell.new(true)
83
+ else
84
+ alives == 3 ? Cell.new(true) : Cell.new(false)
85
+ end
86
+ end
87
+ end
88
+ end
89
+ end
90
+ end
@@ -0,0 +1,13 @@
1
+ module SlackGame
2
+ module Game
3
+ class Lifegame
4
+ class Canvas < ::SlackGame::Canvas
5
+ ARIVE = 0
6
+ DEAD = 1
7
+
8
+ dot ARIVE, ENV['LIFE_ALIVE']
9
+ dot DEAD, ':spacer:'
10
+ end
11
+ end
12
+ end
13
+ end
File without changes
@@ -1,3 +1,3 @@
1
1
  module SlackGame
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: slack_game
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kinoshita.Yasuhiro
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-11-08 00:00:00.000000000 Z
11
+ date: 2015-12-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: slack-api
@@ -116,6 +116,9 @@ files:
116
116
  - lib/slack_game/game/demo.rb
117
117
  - lib/slack_game/game/demo/canvas.rb
118
118
  - lib/slack_game/game/demo/controller.rb
119
+ - lib/slack_game/game/lifegame.rb
120
+ - lib/slack_game/game/lifegame/canvas.rb
121
+ - lib/slack_game/game/lifegame/controller.rb
119
122
  - lib/slack_game/game/tetris.rb
120
123
  - lib/slack_game/game/tetris/canvas.rb
121
124
  - lib/slack_game/game/tetris/controller.rb