kaki-lifegame 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +7 -2
  3. data/lib/kaki-lifegame.rb +122 -30
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4505215e7fb0476113f28838092ded6e0dfa1f3c
4
- data.tar.gz: a1e29db488fae3263e013869104e6227b8844c13
3
+ metadata.gz: d7107f04a0136d027aa9e3decf4645d8e901c8d4
4
+ data.tar.gz: 85357842f26ccc0e68305ce77367468c2a7378df
5
5
  SHA512:
6
- metadata.gz: 4974725fe86c19d0d0448332fc6c6df7689ce2fefbd6b88f17fffc2be27be4aefe36c01f0cffb4c057b67d901d19443fcaf615acd61422089c9cfd1d52c525bc
7
- data.tar.gz: 5a37f46264e8cb2f90ea02060711052ff9914a02f204c7a149bc14a683e948a35b93be13f7ba149d86e54f902139ab11eb0c4dc60f8e1f1a6c7986ff9973e4d8
6
+ metadata.gz: '0792b68d8e37e7977d4b19fa68072b6ab3f6d0383903048093615eb48067a7b3bcf7c7c6d1eb90dee3ada2b4e4619ba0314ca236e2420a039745796014329cc0'
7
+ data.tar.gz: 14a770f488c6aebcaa3d51dce71380bd46d3a29265e7f58d4c3671bf536deb5113e524bdaa92e789ede7ae38a03b2886daa0fd1a98ec4cf8291413c2847fcaa4
data/README.md CHANGED
@@ -18,12 +18,18 @@ $ bundle exec kaki-lifegame
18
18
 
19
19
  ## 機能
20
20
 
21
+ - マウスのクリックでセルを ON/OFF できます。
22
+ - マウスの左ボタンを押しながらドラッグすると連続的にセルを描くことができます。また、右ボタンを押しながらドラッグすると、連続的にセルを消すことができます。
21
23
  - 開始
22
24
  - 実行します。
23
25
  - 停止
24
26
  - 停止します。以下の機能はすべて停止中にのみ有効です。
25
27
  - 1ステップ進める
26
28
  - ステップ実行です。
29
+ - 1ステップ戻る
30
+ - ひとつ前のフィールドに戻ります。戻れるのは過去20ステップまでです。
31
+ - ウェイト
32
+ - ウェイトする時間を変更します。値が小さいほど更新が速くなります。なお、これは停止中でなくとも有効なボタンです。
27
33
  - ばらまく
28
34
  - 適当にセルを散布します。
29
35
  - 格子
@@ -42,7 +48,6 @@ $ bundle exec kaki-lifegame
42
48
  - ファイルに保存したフィールドのデータを読み込んで表示します。
43
49
  - 終了
44
50
  - 終了します。
45
- - マウスのクリックでセルを ON/OFF できます。
46
- - マウスの左ボタンを押しながらドラッグすると連続的にセルを描くことができます。また、右ボタンを押しながらドラッグすると、連続的にセルを消すことができます。
51
+
47
52
 
48
53
 
@@ -9,6 +9,7 @@ module LifeGame
9
9
  clear
10
10
  @step = @stored_step = 0
11
11
  @reserved = copy
12
+ @store = []
12
13
  end
13
14
  attr_reader :step
14
15
 
@@ -26,6 +27,8 @@ module LifeGame
26
27
 
27
28
  def next
28
29
  @before = copy
30
+ @store << @before
31
+ @store.shift if @store.size > 20
29
32
  nxf = new_field
30
33
  each_cell do |x, y|
31
34
  n = alive_cells(x, y)
@@ -77,7 +80,7 @@ module LifeGame
77
80
  end
78
81
 
79
82
  def clear
80
- @field = Array.new(Height + MG * 2) {Array.new(Width + MG * 2, 0)}
83
+ @field = new_field
81
84
  end
82
85
 
83
86
  def load(file_name)
@@ -104,6 +107,12 @@ module LifeGame
104
107
  def step_reset
105
108
  @step = 0
106
109
  end
110
+
111
+ def back
112
+ return if @store.empty?
113
+ @field = @store.pop
114
+ @step -= 1
115
+ end
107
116
  end
108
117
 
109
118
  class FieldArea < Gtk::DrawingArea
@@ -113,6 +122,8 @@ module LifeGame
113
122
  L = CellSize + Space * 2
114
123
  AreaW, AreaH = Field::Width * L, Field::Height * L
115
124
 
125
+ WaitChange = 50
126
+
116
127
  def initialize(field)
117
128
  super()
118
129
  set_size_request(AreaW, AreaH)
@@ -120,46 +131,44 @@ module LifeGame
120
131
  @f = field
121
132
 
122
133
  @colormap = Gdk::Colormap.system
123
- @color = {black: Gdk::Color.new(0, 0, 0), green: Gdk::Color.new(0xc784, 0xffff, 0x52c4),
124
- orange: Gdk::Color.new(0xffff, 0xbb85, 0xf7a),
134
+ @color = {black: Gdk::Color.new(0, 0, 0),
135
+ green: Gdk::Color.new(0xc784, 0xffff, 0x52c4),
136
+ orange: Gdk::Color.new(0xffff, 0xbb85, 0xf7a),
125
137
  grid_color: Gdk::Color.new(0x4b79, 0x4b79, 0x4b79)}
126
- @color.each {|_, v| @colormap.alloc_color(v, false, true)}
138
+ @color.each_value {|v| @colormap.alloc_color(v, false, true)}
127
139
  @cell_color = :green
128
140
 
129
141
  @grid = false
142
+ @wait_time = 500
130
143
 
131
144
  signal_connect("expose_event") do
132
145
  @gc = Gdk::GC.new(window)
133
- set_color(@grid ? :grid_color : :black)
134
- window.draw_rectangle(@gc, true, 0, 0, AreaW, AreaH)
135
- redraw
146
+ background_renewal
136
147
  end
137
148
 
138
149
  @time_goes = false
139
150
  add_events(Gdk::Event::BUTTON_PRESS_MASK | Gdk::Event::BUTTON_MOTION_MASK)
140
151
 
152
+ event_to_xy = ->(e) {[e.x.to_i / L, e.y.to_i / L]}
153
+
141
154
  signal_connect("button_press_event") do |w, e|
142
155
  unless @time_goes
143
- x, y = e.x.to_i / L, e.y.to_i / L
144
- c = @f.get_cell(x, y)
145
- c.zero? ? set_cell(x, y) : reset_cell(x, y)
156
+ x, y = event_to_xy[e]
157
+ @f.get_cell(x, y).zero? ? set_cell(x, y) : reset_cell(x, y)
146
158
  end
147
159
  end
148
160
 
149
161
  signal_connect('motion_notify_event') do |w, e|
150
162
  unless @time_goes
151
- x, y = e.x.to_i / L, e.y.to_i / L
163
+ x, y = event_to_xy[e]
152
164
  set_cell(x, y) if e.state.button1_mask?
153
165
  reset_cell(x, y) if e.state.button3_mask?
154
166
  end
155
167
  end
156
168
 
157
- Gtk.timeout_add(500) do
158
- go_on_one_step if @time_goes
159
- true
160
- end
169
+ set_wait_time
161
170
  end
162
- attr_writer :time_goes, :label
171
+ attr_writer :time_goes, :label1, :label2
163
172
 
164
173
  def set_color(color_name)
165
174
  @gc.rgb_fg_color = @color[color_name]
@@ -203,7 +212,7 @@ module LifeGame
203
212
  end
204
213
 
205
214
  def show_step
206
- @label.set_text("Step : #{@f.step}")
215
+ @label1.set_text("Step : #{@f.step}")
207
216
  end
208
217
 
209
218
  def redraw
@@ -224,9 +233,15 @@ module LifeGame
224
233
  end
225
234
 
226
235
  def scatter
227
- 100.times do
236
+ co1 = co2 = 0
237
+ while co1 < 100 and co2 < 5000
228
238
  x, y = rand(Field::Width), rand(Field::Height)
229
- set_cell(x, y) if @f.get_cell(x, y).zero?
239
+ if @f.get_cell(x, y).zero?
240
+ set_cell(x, y)
241
+ co1 += 1
242
+ else
243
+ co2 += 1
244
+ end
230
245
  end
231
246
  end
232
247
 
@@ -240,17 +255,52 @@ module LifeGame
240
255
  show_step
241
256
  end
242
257
 
243
- def grid
244
- @grid = !@grid
258
+ def background_renewal
245
259
  set_color(@grid ? :grid_color : :black)
246
260
  window.draw_rectangle(@gc, true, 0, 0, AreaW, AreaH)
247
261
  redraw
248
262
  end
249
263
 
264
+ def grid
265
+ @grid = !@grid
266
+ background_renewal
267
+ end
268
+
250
269
  def set_cell_color(color)
251
270
  @cell_color = color
252
271
  redraw
253
272
  end
273
+
274
+ def show_wait_time
275
+ @label2.set_text("#{@wait_time} ミリ秒")
276
+ end
277
+
278
+ def slower
279
+ @wait_time += WaitChange
280
+ set_wait_time
281
+ show_wait_time
282
+ end
283
+
284
+ def faster
285
+ @wait_time -= WaitChange
286
+ @wait_time = 50 if @wait_time < 50
287
+ set_wait_time
288
+ show_wait_time
289
+ end
290
+
291
+ def set_wait_time
292
+ Gtk.timeout_remove(@timer_id) if @timer_id
293
+ @timer_id = Gtk.timeout_add(@wait_time) do
294
+ go_on_one_step if @time_goes
295
+ true
296
+ end
297
+ end
298
+
299
+ def back
300
+ @f.back
301
+ redraw
302
+ show_step
303
+ end
254
304
  end
255
305
 
256
306
  class SideBar < Gtk::VBox
@@ -265,12 +315,14 @@ module LifeGame
265
315
  set_box3
266
316
  set_box4
267
317
  set_box5
318
+ set_box6
268
319
  end
269
320
 
270
321
  def set_box1
271
- start_bt = Gtk::Button.new("開始")
272
- stop_bt = Gtk::Button.new("停止")
273
- step_bt = Gtk::Button.new("1ステップ進める")
322
+ start_bt = Gtk::Button.new("開始")
323
+ stop_bt = Gtk::Button.new("停止")
324
+ step_bt = Gtk::Button.new("1ステップ進める")
325
+ back_bt = Gtk::Button.new("1ステップ戻る")
274
326
 
275
327
  start_bt.signal_connect("clicked") do
276
328
  @edit_mode = false
@@ -286,19 +338,59 @@ module LifeGame
286
338
  @farea.go_on_one_step if @edit_mode
287
339
  end
288
340
 
341
+ back_bt.signal_connect("clicked") do
342
+ @farea.back if @edit_mode
343
+ end
344
+
289
345
  l = Gtk::Label.new("")
290
- @farea.label = l
346
+ @farea.label1 = l
291
347
 
292
348
  box = Gtk::VBox.new
293
349
  pack_start(box, false, false, 0)
294
350
  box.pack_start(start_bt, false, false, 5)
295
351
  box.pack_start(stop_bt , false, false, 5)
296
352
  box.pack_start(step_bt , false, false, 5)
353
+ box.pack_start(back_bt , false, false, 5)
297
354
  box.pack_start(l, false, false, 5)
298
355
  box.pack_start(Gtk::HSeparator.new, false, false, 10)
299
356
  end
300
357
 
301
358
  def set_box2
359
+ arrow1 = Gtk::Arrow.new(Gtk::Arrow::LEFT , Gtk::SHADOW_IN)
360
+ arrow2 = Gtk::Arrow.new(Gtk::Arrow::RIGHT, Gtk::SHADOW_IN)
361
+
362
+ l1 = Gtk::Label.new("ウェイト")
363
+ left_arrow_bt = Gtk::Button.new
364
+ right_arrow_bt = Gtk::Button.new
365
+ l2 = Gtk::Label.new("")
366
+
367
+ @farea.label2 = l2
368
+ @farea.show_wait_time
369
+
370
+ left_arrow_bt.signal_connect("clicked") do
371
+ @farea.slower
372
+ end
373
+
374
+ right_arrow_bt.signal_connect("clicked") do
375
+ @farea.faster
376
+ end
377
+
378
+ left_arrow_bt .add(arrow1)
379
+ right_arrow_bt.add(arrow2)
380
+
381
+ small_box = Gtk::HBox.new
382
+ small_box.pack_start(left_arrow_bt , true, true, 1)
383
+ small_box.pack_start(right_arrow_bt, true, true, 1)
384
+
385
+ box = Gtk::VBox.new
386
+ pack_start(box, false, false, 0)
387
+ box.pack_start(l1 , false, false, 5)
388
+ box.pack_start(small_box, false, false, 5)
389
+ box.pack_start(l2 , false, false, 5)
390
+ box.pack_start(Gtk::HSeparator.new, false, false, 10)
391
+ end
392
+
393
+ def set_box3
302
394
  scatter_bt = Gtk::Button.new("ばらまく")
303
395
  grid_bt = Gtk::Button.new("格子")
304
396
  green_bt = Gtk::Button.new("緑色")
@@ -338,7 +430,7 @@ module LifeGame
338
430
  box.pack_start(Gtk::HSeparator.new, false, false, 10)
339
431
  end
340
432
 
341
- def set_box3
433
+ def set_box4
342
434
  preserve_bt = Gtk::Button.new("一時保存")
343
435
  restore_bt = Gtk::Button.new("復帰")
344
436
 
@@ -353,11 +445,11 @@ module LifeGame
353
445
  box = Gtk::VBox.new
354
446
  pack_start(box, false, false, 0)
355
447
  box.pack_start(preserve_bt, false, false, 5)
356
- box.pack_start(restore_bt, false, false, 5)
448
+ box.pack_start(restore_bt , false, false, 5)
357
449
  box.pack_start(Gtk::HSeparator.new, false, false, 10)
358
450
  end
359
451
 
360
- def set_box4
452
+ def set_box5
361
453
  save_bt = Gtk::Button.new("ファイルに保存")
362
454
  load_bt = Gtk::Button.new("ファイルの読み込み")
363
455
 
@@ -382,7 +474,7 @@ module LifeGame
382
474
  box.pack_start(Gtk::HSeparator.new, false, false, 10)
383
475
  end
384
476
 
385
- def set_box5
477
+ def set_box6
386
478
  close_bt = Gtk::Button.new("終了")
387
479
 
388
480
  close_bt.signal_connect("clicked") do
@@ -399,7 +491,7 @@ module LifeGame
399
491
  dialog = Gtk::FileChooserDialog.new(title,
400
492
  nil, mode, nil,
401
493
  [Gtk::Stock::CANCEL, Gtk::Dialog::RESPONSE_CANCEL],
402
- [Gtk::Stock::OPEN, Gtk::Dialog::RESPONSE_ACCEPT])
494
+ [Gtk::Stock::OPEN , Gtk::Dialog::RESPONSE_ACCEPT])
403
495
  if dialog.run == Gtk::Dialog::RESPONSE_ACCEPT
404
496
  file_name = dialog.filename
405
497
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kaki-lifegame
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - obelisk
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-04-09 00:00:00.000000000 Z
11
+ date: 2018-04-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: gtk2