Skip to content

Commit d3e985f

Browse files
authored
Merge pull request #348 from novoda/gol/update_terminal_client
[Gol] Update Terminal Client
2 parents 60c1d8c + c69c51b commit d3e985f

File tree

6 files changed

+119
-48
lines changed

6 files changed

+119
-48
lines changed

game-of-life-multiplatform/common/src/main/kotlin/com/novoda/gol/data/SimulationBoardEntity.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ data class SimulationBoardEntity(private val cellMatrix: CellMatrix) : BoardEnti
88
val seeds = this.cellMatrix.getSeeds().toMutableList()
99
seeds.addAll(patternEntity.getSeeds())
1010

11-
val cellMatrix = ListBasedMatrix(width = 50, height = 50, seeds = seeds)
11+
val cellMatrix = ListBasedMatrix(width = cellMatrix.getWidth(), height = cellMatrix.getHeight(), seeds = seeds)
1212
return SimulationBoardEntity(cellMatrix)
1313
}
1414

game-of-life-multiplatform/game-of-life-jvm/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ apply plugin: 'kotlin-platform-jvm'
22

33
dependencies {
44
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
5+
compile group: 'io.reactivex.rxjava2', name: 'rxjava', version: '2.1.1'
56
expectedBy project(":common")
67
testCompile "junit:junit:4.12"
78
testCompile "org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version"
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.novoda.gol
2+
3+
import io.reactivex.Flowable
4+
import io.reactivex.disposables.Disposable
5+
import java.util.concurrent.TimeUnit
6+
7+
actual class GameLoop actual constructor() {
8+
9+
private var gameLoop: Disposable? = null
10+
11+
actual var onTick: () -> Unit = {}
12+
13+
actual fun startWith(intervalMs: Int) {
14+
gameLoop = Flowable.interval(intervalMs.toLong(), TimeUnit.MILLISECONDS).subscribe {
15+
onTick()
16+
}
17+
}
18+
19+
actual fun stop() {
20+
gameLoop?.dispose()
21+
gameLoop = null
22+
23+
}
24+
25+
actual fun isLooping() = gameLoop != null
26+
27+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.novoda.gol.presentation
2+
3+
import com.novoda.gol.patterns.Glider
4+
import com.novoda.gol.patterns.PatternEntity
5+
import io.reactivex.Observable
6+
import io.reactivex.schedulers.Schedulers
7+
import java.io.BufferedReader
8+
import java.io.InputStreamReader
9+
10+
class AppTerminalView : AppView {
11+
12+
override var onControlButtonClicked: () -> Unit = {}
13+
14+
override var onPatternSelected: (pattern: PatternEntity) -> Unit = {}
15+
16+
private val presenter = AppPresenter()
17+
18+
fun onCreate() {
19+
presenter.bind(this)
20+
21+
Observable
22+
.fromCallable {
23+
readString()
24+
}
25+
.filter { it == "yes" }
26+
.subscribeOn(Schedulers.io())
27+
.subscribe {
28+
onPatternSelected(Glider.create())
29+
onControlButtonClicked()
30+
}
31+
}
32+
33+
override fun renderControlButtonLabel(controlButtonLabel: String) {
34+
print("$controlButtonLabel\n")
35+
}
36+
37+
private fun readString(): String {
38+
val bufferedReader = BufferedReader(InputStreamReader(System.`in`))
39+
return bufferedReader.readLine().toString()
40+
}
41+
42+
override fun renderPatternSelectionVisibility(visibility: Boolean) {
43+
//TODO: pattern selection is ignored in first iteration
44+
}
45+
46+
override fun renderBoard(boardViewState: BoardViewState) {
47+
BoardTerminalView().onCreate(boardViewState)
48+
}
49+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.novoda.gol.presentation
2+
3+
import com.novoda.gol.data.BoardEntity
4+
import com.novoda.gol.data.PositionEntity
5+
import com.novoda.gol.patterns.PatternEntity
6+
7+
class BoardTerminalView : BoardView {
8+
9+
override var onPatternSelected: (pattern: PatternEntity) -> Unit = {}
10+
override var onCellClicked: (position: PositionEntity) -> Unit = {}
11+
override var onStartSimulationClicked: () -> Unit = {}
12+
override var onStopSimulationClicked: () -> Unit = {}
13+
14+
private var presenter = BoardPresenter(width = 20, height = 20)
15+
16+
fun onCreate(boardViewState: BoardViewState) {
17+
presenter.bind(this)
18+
if (boardViewState.selectedPattern != null) {
19+
onPatternSelected(boardViewState.selectedPattern!!)
20+
onStartSimulationClicked()
21+
}
22+
}
23+
24+
override fun renderBoard(boardEntity: BoardEntity) {
25+
for (y in 0 until boardEntity.getHeight()) {
26+
for (x in 0 until boardEntity.getWidth()) {
27+
val cellAtPosition = boardEntity.cellAtPosition(x, y)
28+
if (cellAtPosition.isAlive) {
29+
print("X")
30+
} else {
31+
print(" ")
32+
}
33+
34+
}
35+
print("\n")
36+
}
37+
}
38+
}
Lines changed: 3 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,14 @@
11
package com.novoda.gol.terminal
22

3-
import com.novoda.gol.data.BoardEntity
4-
import com.novoda.gol.data.ListBasedMatrix
5-
import com.novoda.gol.data.PositionEntity
6-
import com.novoda.gol.data.SimulationBoardEntity
7-
import java.io.BufferedReader
8-
import java.io.InputStreamReader
9-
10-
const val EXIT = "exit"
3+
import com.novoda.gol.presentation.AppTerminalView
114

125
fun main(args: Array<String>) {
6+
AppTerminalView().onCreate()
137

14-
var keepLooping = true
15-
val br = BufferedReader(InputStreamReader(System.`in`))
16-
17-
print("enter width: ")
18-
val width = br.readLine().toInt()
19-
20-
print("enter height: ")
21-
val height = br.readLine().toInt()
22-
23-
val cellMatrix = ListBasedMatrix(width = width, height = height, seeds = listOf(PositionEntity(2, 1), PositionEntity(2, 2), PositionEntity(2, 3)))
24-
25-
var boardEntity: BoardEntity = SimulationBoardEntity(cellMatrix)
26-
8+
while (true){
279

28-
while (keepLooping) {
29-
val input = br.readLine()
30-
keepLooping = input != EXIT
31-
32-
render(boardEntity)
33-
34-
if (keepLooping) {
35-
boardEntity = boardEntity.nextIteration()
36-
}
3710
}
3811
}
3912

40-
fun render(boardEntity: BoardEntity) {
41-
for (y in 0 until boardEntity.getHeight()) {
42-
for (x in 0 until boardEntity.getWidth()) {
43-
val cellAtPosition = boardEntity.cellAtPosition(x, y)
44-
if (cellAtPosition.isAlive) {
45-
print("X")
46-
} else {
47-
print(" ")
48-
}
49-
50-
}
51-
print("\n")
52-
}
53-
54-
print("exit: Stop Simulation\n")
55-
print("next: Next Iteration\n")
56-
}
5713

5814

0 commit comments

Comments
 (0)