Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import groovy.swing.SwingBuilder
- import java.awt.BorderLayout as BL
- import java.awt.Color
- import java.awt.Graphics
- import java.awt.Rectangle
- import javax.swing.*
- /**
- * 世界の定義
- */
- class World {
- def size
- def cellMap = [:]
- static String BR = System.getProperty('line.separator')
- World(initCells) {
- def cellList = initCells.readLines()*.trim()
- int width = cellList*.size().max()
- cellList = cellList.collect{ it.padRight(width, '0') }
- cellList.eachWithIndex { r, ridx ->
- r.eachWithIndex{ c, cidx ->
- this.cellMap[[ridx, cidx]] = new Cell(
- alive: c.toInteger(),
- pos : [ridx, cidx],
- world: this
- )
- }
- }
- this.size = [
- this.cellMap.keySet().collect{ it.first() }.max() + 1,
- this.cellMap.keySet().collect{ it.last() }.max() + 1
- ]
- }
- def getNextGen() {
- def nextGenCellMap = [:]
- cellMap.each { nextGenCellMap[it.key] = it.value.next() }
- new World(toBinaryString(nextGenCellMap))
- }
- def getCells(keys) {
- keys.collect{ this.cellMap[it] }
- }
- @Override String toString() {
- def res = '' << ''
- size.first().times { r ->
- size.last().times { c ->
- res << cellMap[[r, c]]
- }
- res << BR
- }
- res
- }
- String toBinaryString(map) {
- def res = '' << ''
- size.first().times { r ->
- size.last().times { c ->
- res << map[[r, c]].toBinaryString()
- }
- res << BR
- }
- res
- }
- }
- /**
- * セルの定義
- */
- class Cell {
- boolean alive
- def pos
- World world
- Cell next() {
- def neighbers = world.getCells(neighberPoses())
- def deadOrAlive = {
- if (this.alive) {
- switch (neighbers.findAll{ it.alive }.size()) {
- case {it in [2, 3]} : return true
- case {it <= 1} : return false
- case {it >= 4} : return false
- }
- } else {
- neighbers.findAll{ it.alive }.size() == 3
- }
- }
- new Cell(
- alive: deadOrAlive(),
- pos : this.pos,
- world: this.world
- )
- }
- def neighberPoses() {
- directions()
- .collect{ [ it.first() + pos.first(), it.last() + pos.last() ] }
- .collect{ [ it.first() % world.size.first(), it.last() % world.size.last() ] }
- .collect{ [ it.first() < 0 ? world.size.first() - 1 : it.first(),
- it.last() < 0 ? world.size.last() - 1 : it.last() ] }
- }
- def directions() {
- [
- [-1, -1], [-1, 0], [-1, 1],
- [ 0, -1], [ 0, 1],
- [ 1, -1], [ 1, 0], [ 1, 1]
- ]
- }
- @Override String toString() {
- alive ? '1' : '0'
- }
- String toBinaryString() {
- alive ? '1' : '0'
- }
- }
- /**
- * 描画用フレーム
- */
- class LifeFrame extends JFrame {
- static def COLOR_ALIVE = new Color(102, 153, 255)
- static def COLOR_DEAD = new Color(102, 204, 255)
- static def BOX_SIZE = 25
- def cells
- @Override void paint(Graphics g) {
- def line = cells.readLines()
- line.eachWithIndex{ item, y ->
- item.eachWithIndex{ cell, x ->
- clear(g, ['x': x, 'y': y])
- drawing(g, ['x': x, 'y': y], cell.toBoolean())
- }
- }
- }
- static int toX(int from) { from * BOX_SIZE + 30 }
- static int toY(int from) { from * BOX_SIZE + 30 }
- static def clear(Graphics g, pos) {
- g.color = Color.WHITE
- g.fill([toX(pos.x), toY(pos.y), BOX_SIZE, BOX_SIZE] as Rectangle)
- g.color = COLOR_DEAD
- g.draw([toX(pos.x), toY(pos.y), BOX_SIZE, BOX_SIZE] as Rectangle)
- }
- static def drawing(Graphics g, pos, isAlive) {
- g.color = isAlive ? COLOR_ALIVE : COLOR_DEAD
- def pen = isAlive ? 'fill' : 'draw'
- g."$pen"([toX(pos.x), toY(pos.y), BOX_SIZE, BOX_SIZE] as Rectangle)
- }
- }
- //------------------------------------------ここからメイン処理
- // グライダー
- def glider = '''|00000
- |00100
- |01000
- |01110
- |00000'''.stripMargin()
- def shape = new File('shape.txt')
- def world = new World(shape.exists() ? shape.text : glider)
- new SwingBuilder().edt {
- def lifeFrame = new LifeFrame()
- lifeFrame.cells = world.toString()
- frame(lifeFrame,
- title : 'Life Game',
- size : [300,300],
- background : Color.WHITE,
- defaultCloseOperation : JFrame.EXIT_ON_CLOSE,
- show : true) {
- borderLayout()
- button(text:'Next Generation',
- actionPerformed: {
- world = world.getNextGen()
- lifeFrame.cells = world.toString()
- lifeFrame.repaint()
- },
- constraints:BL.SOUTH)
- }
- }
Add Comment
Please, Sign In to add comment