Guest User

Untitled

a guest
Jun 17th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.46 KB | None | 0 0
  1. import groovy.swing.SwingBuilder
  2. import java.awt.BorderLayout as BL
  3. import java.awt.Color
  4. import java.awt.Graphics
  5. import java.awt.Rectangle
  6. import javax.swing.*
  7.  
  8. /**
  9. * 世界の定義
  10. */
  11. class World {
  12. def size
  13. def cellMap = [:]
  14. static String BR = System.getProperty('line.separator')
  15.  
  16. World(initCells) {
  17. def cellList = initCells.readLines()*.trim()
  18. int width = cellList*.size().max()
  19. cellList = cellList.collect{ it.padRight(width, '0') }
  20. cellList.eachWithIndex { r, ridx ->
  21. r.eachWithIndex{ c, cidx ->
  22. this.cellMap[[ridx, cidx]] = new Cell(
  23. alive: c.toInteger(),
  24. pos : [ridx, cidx],
  25. world: this
  26. )
  27. }
  28. }
  29. this.size = [
  30. this.cellMap.keySet().collect{ it.first() }.max() + 1,
  31. this.cellMap.keySet().collect{ it.last() }.max() + 1
  32. ]
  33. }
  34.  
  35. def getNextGen() {
  36. def nextGenCellMap = [:]
  37. cellMap.each { nextGenCellMap[it.key] = it.value.next() }
  38. new World(toBinaryString(nextGenCellMap))
  39. }
  40.  
  41. def getCells(keys) {
  42. keys.collect{ this.cellMap[it] }
  43. }
  44.  
  45. @Override String toString() {
  46. def res = '' << ''
  47. size.first().times { r ->
  48. size.last().times { c ->
  49. res << cellMap[[r, c]]
  50. }
  51. res << BR
  52. }
  53. res
  54. }
  55.  
  56. String toBinaryString(map) {
  57. def res = '' << ''
  58. size.first().times { r ->
  59. size.last().times { c ->
  60. res << map[[r, c]].toBinaryString()
  61. }
  62. res << BR
  63. }
  64. res
  65. }
  66. }
  67.  
  68. /**
  69. * セルの定義
  70. */
  71. class Cell {
  72. boolean alive
  73. def pos
  74. World world
  75.  
  76. Cell next() {
  77. def neighbers = world.getCells(neighberPoses())
  78. def deadOrAlive = {
  79. if (this.alive) {
  80. switch (neighbers.findAll{ it.alive }.size()) {
  81. case {it in [2, 3]} : return true
  82. case {it <= 1} : return false
  83. case {it >= 4} : return false
  84. }
  85. } else {
  86. neighbers.findAll{ it.alive }.size() == 3
  87. }
  88. }
  89. new Cell(
  90. alive: deadOrAlive(),
  91. pos : this.pos,
  92. world: this.world
  93. )
  94. }
  95.  
  96. def neighberPoses() {
  97. directions()
  98. .collect{ [ it.first() + pos.first(), it.last() + pos.last() ] }
  99. .collect{ [ it.first() % world.size.first(), it.last() % world.size.last() ] }
  100. .collect{ [ it.first() < 0 ? world.size.first() - 1 : it.first(),
  101. it.last() < 0 ? world.size.last() - 1 : it.last() ] }
  102. }
  103.  
  104. def directions() {
  105. [
  106. [-1, -1], [-1, 0], [-1, 1],
  107. [ 0, -1], [ 0, 1],
  108. [ 1, -1], [ 1, 0], [ 1, 1]
  109. ]
  110. }
  111.  
  112. @Override String toString() {
  113. alive ? '1' : '0'
  114. }
  115. String toBinaryString() {
  116. alive ? '1' : '0'
  117. }
  118. }
  119.  
  120. /**
  121. * 描画用フレーム
  122. */
  123. class LifeFrame extends JFrame {
  124. static def COLOR_ALIVE = new Color(102, 153, 255)
  125. static def COLOR_DEAD = new Color(102, 204, 255)
  126. static def BOX_SIZE = 25
  127.  
  128. def cells
  129.  
  130. @Override void paint(Graphics g) {
  131. def line = cells.readLines()
  132. line.eachWithIndex{ item, y ->
  133. item.eachWithIndex{ cell, x ->
  134. clear(g, ['x': x, 'y': y])
  135. drawing(g, ['x': x, 'y': y], cell.toBoolean())
  136. }
  137. }
  138. }
  139.  
  140. static int toX(int from) { from * BOX_SIZE + 30 }
  141. static int toY(int from) { from * BOX_SIZE + 30 }
  142.  
  143. static def clear(Graphics g, pos) {
  144. g.color = Color.WHITE
  145. g.fill([toX(pos.x), toY(pos.y), BOX_SIZE, BOX_SIZE] as Rectangle)
  146. g.color = COLOR_DEAD
  147. g.draw([toX(pos.x), toY(pos.y), BOX_SIZE, BOX_SIZE] as Rectangle)
  148. }
  149.  
  150. static def drawing(Graphics g, pos, isAlive) {
  151. g.color = isAlive ? COLOR_ALIVE : COLOR_DEAD
  152. def pen = isAlive ? 'fill' : 'draw'
  153. g."$pen"([toX(pos.x), toY(pos.y), BOX_SIZE, BOX_SIZE] as Rectangle)
  154. }
  155. }
  156.  
  157. //------------------------------------------ここからメイン処理
  158.  
  159. // グライダー
  160. def glider = '''|00000
  161. |00100
  162. |01000
  163. |01110
  164. |00000'''.stripMargin()
  165.  
  166. def shape = new File('shape.txt')
  167. def world = new World(shape.exists() ? shape.text : glider)
  168.  
  169. new SwingBuilder().edt {
  170. def lifeFrame = new LifeFrame()
  171. lifeFrame.cells = world.toString()
  172. frame(lifeFrame,
  173. title : 'Life Game',
  174. size : [300,300],
  175. background : Color.WHITE,
  176. defaultCloseOperation : JFrame.EXIT_ON_CLOSE,
  177. show : true) {
  178. borderLayout()
  179. button(text:'Next Generation',
  180. actionPerformed: {
  181. world = world.getNextGen()
  182. lifeFrame.cells = world.toString()
  183. lifeFrame.repaint()
  184. },
  185. constraints:BL.SOUTH)
  186. }
  187. }
Add Comment
Please, Sign In to add comment