-
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.
Implement first iteration of Terminal AppView and BoardView
- Loading branch information
1 parent
616374e
commit ea46164
Showing
3 changed files
with
84 additions
and
59 deletions.
There are no files selected for viewing
52 changes: 46 additions & 6 deletions
52
...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 |
---|---|---|
@@ -1,9 +1,49 @@ | ||
package com.novoda.gol.presentation | ||
|
||
/** | ||
* User: tobiasheine | ||
* Date: 10.01.18 | ||
* Time: 16:31 | ||
*/ | ||
class AppTerminalView { | ||
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 ignored in first iteration | ||
} | ||
|
||
override fun renderBoard(boardViewState: BoardViewState) { | ||
BoardTerminalView().onCreate(boardViewState) | ||
} | ||
} |
41 changes: 35 additions & 6 deletions
41
...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 |
---|---|---|
@@ -1,9 +1,38 @@ | ||
package com.novoda.gol.presentation | ||
|
||
/** | ||
* User: tobiasheine | ||
* Date: 10.01.18 | ||
* Time: 16:42 | ||
*/ | ||
class BoardTerminalView { | ||
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") | ||
} | ||
|
||
|