separate view files; B93Funge stores Value objects

This commit is contained in:
2018-11-19 14:48:18 -05:00
parent a1e6368b2f
commit cdf0dfee28
11 changed files with 175 additions and 232 deletions

View File

@@ -3,64 +3,48 @@ package befide.befunge.b93
import befide.befunge.core.*
import befide.befunge.state.*
fun <T> List<T>.padEnd(size: Int, factory: (Int) -> (T)): List<T> = this + (this.size until size).map { factory(it) }
class B93Funge : Funge {
override val width = 80
override val height = 25
private var cars = Array(height) { Array(width) { ' '.toLong() } }
val bounds = Vec(width, height)
private var cars = Array(height) { Array(width) { Value(' ') } }
override fun get(vec: Vec): Value {
return Value(cars[vec.y][vec.x])
return cars[vec.y][vec.x]
}
override fun set(vec: Vec, value: Value) {
cars[vec.y][vec.x] = value.value
cars[vec.y][vec.x] = value
}
override fun nextVec(vec: Vec, delta: Vec): Vec {
var x = vec.x + delta.x
var y = vec.y + delta.y
if (x >= width || x < 0) {
x %= width
}
if (x < 0) {
x += width
}
if (y >= height || y < 0) {
y %= height
}
if (y < 0) {
y += height
}
return Vec(x, y)
return (vec + delta) mod bounds
}
override fun setString(data: String) {
val strings = data.split('\n')
for (i in strings.size until height) {
cars[i] = Array(width) { ' '.toLong() }
}
strings.map {
it.toList().map { it.toLong() }
}
.forEachIndexed { index, list ->
if (index > height) {
return
}
cars[index] = (
list.toList() + List(
if (list.size <= width) width - list.size else 0
) {
' '.toLong()
}
)
.subList(0, width)
.toTypedArray()
}
cars = data.split("\n").map {
it.map {
Value(it)
}.padEnd(width) {
Value(' ')
}.toTypedArray()
}.padEnd(height) {
Array(width) {
Value(' ')
}
}.toTypedArray()
}
override fun toString(): String {
return cars.map { it.map { Value(it).asChar ?: '?' }.joinToString("") }.joinToString("\n")
return cars.joinToString("\n") { row ->
row.joinToString("") { value ->
(value.asChar ?: '?').toString()
}
}
}
}

View File

@@ -1,6 +1,13 @@
package befide.befunge.state
infix fun Int.mod(other: Int): Int {
val x = this % other
return if (x >= 0) x else (x + other)
}
data class Vec(val x: Int, val y: Int) {
operator fun plus(other: Vec) = Vec(x + other.x, y + other.y)
operator fun times(c: Int) = Vec(x * c, y * c)
infix fun mod(other: Vec) = Vec(x mod other.x, y mod other.y)
}