Skip to content

Commit

Permalink
Implement first iteration of Terminal AppView and BoardView
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiasheine committed Jan 10, 2018
1 parent 616374e commit ea46164
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 59 deletions.
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)
}
}
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")
}
}
}
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")
}


0 comments on commit ea46164

Please sign in to comment.