-
Notifications
You must be signed in to change notification settings - Fork 129
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #348 from novoda/gol/update_terminal_client
[Gol] Update Terminal Client
- Loading branch information
Showing
6 changed files
with
119 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
game-of-life-multiplatform/game-of-life-jvm/src/main/kotlin/com/novoda/gol/GameLoop.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.novoda.gol | ||
|
||
import io.reactivex.Flowable | ||
import io.reactivex.disposables.Disposable | ||
import java.util.concurrent.TimeUnit | ||
|
||
actual class GameLoop actual constructor() { | ||
|
||
private var gameLoop: Disposable? = null | ||
|
||
actual var onTick: () -> Unit = {} | ||
|
||
actual fun startWith(intervalMs: Int) { | ||
gameLoop = Flowable.interval(intervalMs.toLong(), TimeUnit.MILLISECONDS).subscribe { | ||
onTick() | ||
} | ||
} | ||
|
||
actual fun stop() { | ||
gameLoop?.dispose() | ||
gameLoop = null | ||
|
||
} | ||
|
||
actual fun isLooping() = gameLoop != null | ||
|
||
} |
49 changes: 49 additions & 0 deletions
49
...iplatform/game-of-life-jvm/src/main/kotlin/com/novoda/gol/presentation/AppTerminalView.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package com.novoda.gol.presentation | ||
|
||
import com.novoda.gol.patterns.Glider | ||
import com.novoda.gol.patterns.PatternEntity | ||
import io.reactivex.Observable | ||
import io.reactivex.schedulers.Schedulers | ||
import java.io.BufferedReader | ||
import java.io.InputStreamReader | ||
|
||
class AppTerminalView : AppView { | ||
|
||
override var onControlButtonClicked: () -> Unit = {} | ||
|
||
override var onPatternSelected: (pattern: PatternEntity) -> Unit = {} | ||
|
||
private val presenter = AppPresenter() | ||
|
||
fun onCreate() { | ||
presenter.bind(this) | ||
|
||
Observable | ||
.fromCallable { | ||
readString() | ||
} | ||
.filter { it == "yes" } | ||
.subscribeOn(Schedulers.io()) | ||
.subscribe { | ||
onPatternSelected(Glider.create()) | ||
onControlButtonClicked() | ||
} | ||
} | ||
|
||
override fun renderControlButtonLabel(controlButtonLabel: String) { | ||
print("$controlButtonLabel\n") | ||
} | ||
|
||
private fun readString(): String { | ||
val bufferedReader = BufferedReader(InputStreamReader(System.`in`)) | ||
return bufferedReader.readLine().toString() | ||
} | ||
|
||
override fun renderPatternSelectionVisibility(visibility: Boolean) { | ||
//TODO: pattern selection is ignored in first iteration | ||
} | ||
|
||
override fun renderBoard(boardViewState: BoardViewState) { | ||
BoardTerminalView().onCreate(boardViewState) | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
...latform/game-of-life-jvm/src/main/kotlin/com/novoda/gol/presentation/BoardTerminalView.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.novoda.gol.presentation | ||
|
||
import com.novoda.gol.data.BoardEntity | ||
import com.novoda.gol.data.PositionEntity | ||
import com.novoda.gol.patterns.PatternEntity | ||
|
||
class BoardTerminalView : BoardView { | ||
|
||
override var onPatternSelected: (pattern: PatternEntity) -> Unit = {} | ||
override var onCellClicked: (position: PositionEntity) -> Unit = {} | ||
override var onStartSimulationClicked: () -> Unit = {} | ||
override var onStopSimulationClicked: () -> Unit = {} | ||
|
||
private var presenter = BoardPresenter(width = 20, height = 20) | ||
|
||
fun onCreate(boardViewState: BoardViewState) { | ||
presenter.bind(this) | ||
if (boardViewState.selectedPattern != null) { | ||
onPatternSelected(boardViewState.selectedPattern!!) | ||
onStartSimulationClicked() | ||
} | ||
} | ||
|
||
override fun renderBoard(boardEntity: BoardEntity) { | ||
for (y in 0 until boardEntity.getHeight()) { | ||
for (x in 0 until boardEntity.getWidth()) { | ||
val cellAtPosition = boardEntity.cellAtPosition(x, y) | ||
if (cellAtPosition.isAlive) { | ||
print("X") | ||
} else { | ||
print(" ") | ||
} | ||
|
||
} | ||
print("\n") | ||
} | ||
} | ||
} |
50 changes: 3 additions & 47 deletions
50
game-of-life-multiplatform/game-of-life-jvm/src/main/kotlin/com/novoda/gol/terminal/Main.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,14 @@ | ||
package com.novoda.gol.terminal | ||
|
||
import com.novoda.gol.data.BoardEntity | ||
import com.novoda.gol.data.ListBasedMatrix | ||
import com.novoda.gol.data.PositionEntity | ||
import com.novoda.gol.data.SimulationBoardEntity | ||
import java.io.BufferedReader | ||
import java.io.InputStreamReader | ||
|
||
const val EXIT = "exit" | ||
import com.novoda.gol.presentation.AppTerminalView | ||
|
||
fun main(args: Array<String>) { | ||
AppTerminalView().onCreate() | ||
|
||
var keepLooping = true | ||
val br = BufferedReader(InputStreamReader(System.`in`)) | ||
|
||
print("enter width: ") | ||
val width = br.readLine().toInt() | ||
|
||
print("enter height: ") | ||
val height = br.readLine().toInt() | ||
|
||
val cellMatrix = ListBasedMatrix(width = width, height = height, seeds = listOf(PositionEntity(2, 1), PositionEntity(2, 2), PositionEntity(2, 3))) | ||
|
||
var boardEntity: BoardEntity = SimulationBoardEntity(cellMatrix) | ||
|
||
while (true){ | ||
|
||
while (keepLooping) { | ||
val input = br.readLine() | ||
keepLooping = input != EXIT | ||
|
||
render(boardEntity) | ||
|
||
if (keepLooping) { | ||
boardEntity = boardEntity.nextIteration() | ||
} | ||
} | ||
} | ||
|
||
fun render(boardEntity: BoardEntity) { | ||
for (y in 0 until boardEntity.getHeight()) { | ||
for (x in 0 until boardEntity.getWidth()) { | ||
val cellAtPosition = boardEntity.cellAtPosition(x, y) | ||
if (cellAtPosition.isAlive) { | ||
print("X") | ||
} else { | ||
print(" ") | ||
} | ||
|
||
} | ||
print("\n") | ||
} | ||
|
||
print("exit: Stop Simulation\n") | ||
print("next: Next Iteration\n") | ||
} | ||
|
||
|