|
| 1 | +package org.learningconcurrency |
| 2 | +package exercises |
| 3 | +package ch5 |
| 4 | + |
| 5 | +import javafx.scene.layout.HBox |
| 6 | + |
| 7 | +import scalafx.Includes._ |
| 8 | +import scalafx.application.JFXApp |
| 9 | +import scalafx.application.JFXApp.PrimaryStage |
| 10 | +import scalafx.geometry.Insets |
| 11 | +import scalafx.scene.Scene |
| 12 | +import scalafx.scene.control._ |
| 13 | +import scalafx.scene.layout.{Pane, VBox} |
| 14 | +import scalafx.scene.paint.Color |
| 15 | +import scalafx.scene.shape.Rectangle |
| 16 | + |
| 17 | +object Ex4 extends JFXApp { |
| 18 | + |
| 19 | + /** |
| 20 | + * Implement a program that simulates a cellular automaton in parallel. |
| 21 | + */ |
| 22 | + |
| 23 | + val maxX = 40 |
| 24 | + val maxY = 40 |
| 25 | + |
| 26 | + case class Cell(state: Boolean, index: Int, neighbors: Seq[Int]) |
| 27 | + |
| 28 | + def index(i: Int, j: Int) = i + j * maxX |
| 29 | + |
| 30 | + def findNeighbors(x: Int, y: Int) = { |
| 31 | + for { |
| 32 | + i <- x - 1 to x + 1 |
| 33 | + j <- y - 1 to y + 1 |
| 34 | + if i > -1 && i < maxX && j > -1 && j < maxY && ((i != x) || (j != x)) |
| 35 | + } yield index(i, j) |
| 36 | + } |
| 37 | + |
| 38 | + def checkInitialState(x: Int, y: Int) = (x == maxX / 2 - 1 || x == maxX / 2 + 1) && y == maxY / 2 |
| 39 | + |
| 40 | + def initialize: IndexedSeq[Cell] = { |
| 41 | + ( |
| 42 | + for { |
| 43 | + x <- 0 until maxX |
| 44 | + y <- 0 until maxY |
| 45 | + } yield (x, y) |
| 46 | + ).map { |
| 47 | + case (x, y) => Cell( |
| 48 | + state = checkInitialState(x,y), |
| 49 | + index = index(x, y), |
| 50 | + neighbors = findNeighbors(x, y) |
| 51 | + ) |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + def countNeighbors(cell: Cell, cells: Seq[Cell]) = |
| 56 | + cell.neighbors.count((i) => cells(i).state) |
| 57 | + |
| 58 | + def defState(countNeighbors: Int) = { |
| 59 | + (countNeighbors == 2) || (countNeighbors == 3) |
| 60 | + } |
| 61 | + |
| 62 | + def next(cells: Seq[Cell]) = { |
| 63 | + cells.par.map( |
| 64 | + (cell) => cell.copy(state = defState(countNeighbors(cell, cells)))).toIndexedSeq |
| 65 | + } |
| 66 | + |
| 67 | + //Test App |
| 68 | + |
| 69 | + var cells = List(initialize) |
| 70 | + |
| 71 | + val cellAreaWidth = 400 |
| 72 | + val cellAreaHeight = 400 |
| 73 | + |
| 74 | + val rectWidth = cellAreaWidth / maxX |
| 75 | + val rectHeight = cellAreaHeight / maxY |
| 76 | + |
| 77 | + def buildRectangles = ( |
| 78 | + for { |
| 79 | + y <- 0 until maxY |
| 80 | + x <- 0 until maxX |
| 81 | + } yield (x, y)). |
| 82 | + map { |
| 83 | + case (posX, posY) => new Rectangle() { |
| 84 | + x = posX * rectWidth |
| 85 | + y = posY * rectHeight |
| 86 | + width = rectWidth |
| 87 | + height = rectHeight |
| 88 | + fill = Color.GRAY |
| 89 | + } |
| 90 | + }.toArray |
| 91 | + |
| 92 | + |
| 93 | + val rectangles = buildRectangles |
| 94 | + |
| 95 | + val cellsPane = new Pane { |
| 96 | + maxWidth = cellAreaWidth |
| 97 | + maxHeight = cellAreaHeight |
| 98 | + } |
| 99 | + rectangles.foreach(cellsPane.children.add(_)) |
| 100 | + |
| 101 | + def drawCells = cells.head.foreach((c) => rectangles(c.index).setFill(if (c.state) Color.RED else Color.WHITE)) |
| 102 | + |
| 103 | + val nextButton = new Button() { |
| 104 | + text = "=>" |
| 105 | + onAction = handle { |
| 106 | + cells = next(cells.head) :: cells |
| 107 | + drawCells |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + val prevButton = new Button() { |
| 112 | + text = "<=" |
| 113 | + onAction = handle { |
| 114 | + cells = cells match { |
| 115 | + case h :: Nil => cells |
| 116 | + case h :: t => t |
| 117 | + } |
| 118 | + drawCells |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + val hbox = new HBox() |
| 123 | + hbox.children.add(prevButton) |
| 124 | + hbox.children.add(nextButton) |
| 125 | + |
| 126 | + |
| 127 | + val vbox = new VBox { |
| 128 | + padding = Insets(20) |
| 129 | + spacing = 10 |
| 130 | + } |
| 131 | + |
| 132 | + vbox.children.add(hbox) |
| 133 | + vbox.children.add(cellsPane) |
| 134 | + |
| 135 | + stage = new PrimaryStage { |
| 136 | + title.value = "Ch5 Ex4" |
| 137 | + scene = new Scene { |
| 138 | + content = vbox |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + stage.sizeToScene() |
| 143 | + |
| 144 | + drawCells |
| 145 | + |
| 146 | +} |
0 commit comments