Commit 82105a5f authored by Adrian Kosmaczewski's avatar Adrian Kosmaczewski

Upgraded Kotlin project to Android Studio 4.1

parent 9a02c1b0
......@@ -3,18 +3,9 @@
<JetCodeStyleSettings>
<option name="PACKAGES_TO_USE_STAR_IMPORTS">
<value>
<package name="java.util" alias="false" withSubpackages="false" />
<package name="kotlinx.android.synthetic" alias="false" withSubpackages="true" />
<package name="io.ktor" alias="false" withSubpackages="true" />
</value>
</option>
<option name="PACKAGES_IMPORT_LAYOUT">
<value>
<package name="" alias="false" withSubpackages="true" />
<package name="java" alias="false" withSubpackages="true" />
<package name="javax" alias="false" withSubpackages="true" />
<package name="kotlin" alias="false" withSubpackages="true" />
<package name="" alias="true" withSubpackages="true" />
<package name="java.util" withSubpackages="false" static="false" />
<package name="kotlinx.android.synthetic" withSubpackages="true" static="false" />
<package name="io.ktor" withSubpackages="true" static="false" />
</value>
</option>
<option name="CODE_STYLE_DEFAULTS" value="KOTLIN_OFFICIAL" />
......
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
<bytecodeTargetLevel target="1.8" />
</component>
</project>
\ No newline at end of file
......@@ -14,6 +14,7 @@
</set>
</option>
<option name="resolveModulePerSourceSet" value="false" />
<option name="useQualifiedModuleNames" value="true" />
</GradleProjectSettings>
</option>
</component>
......
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/build/classes" />
</component>
<component name="ProjectType">
......
......@@ -2,8 +2,8 @@
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/Conway.iml" filepath="$PROJECT_DIR$/Conway.iml" group="Conway" />
<module fileurl="file://$PROJECT_DIR$/app/app.iml" filepath="$PROJECT_DIR$/app/app.iml" group="Conway/app" />
<module fileurl="file://$PROJECT_DIR$/Conway.iml" filepath="$PROJECT_DIR$/Conway.iml" />
<module fileurl="file://$PROJECT_DIR$/.idea/modules/app/Conway.app.iml" filepath="$PROJECT_DIR$/.idea/modules/app/Conway.app.iml" />
</modules>
</component>
</project>
\ No newline at end of file
......@@ -2,16 +2,16 @@ package training.akosma.conway
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
private lateinit var world: World
private lateinit var worldView: WorldView
private val handler = Handler()
private val handler = Handler(Looper.getMainLooper())
private var generation: Int = 1
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
worldView = WorldView(this)
val alive = mutableListOf<Coord>()
alive.addAll(World.blinker(Coord(0, 1)))
......@@ -21,19 +21,18 @@ class MainActivity : AppCompatActivity() {
alive.addAll(World.block(Coord(18, 3)))
alive.addAll(World.tub(Coord(6, 1)))
world = World(30, alive)
worldView.world = world
worldView = WorldView(this)
worldView.world = World(30, alive)
title = "Conway – Generation ${generation}"
setContentView(worldView)
evolve()
}
private fun evolve() {
val runnable = Runnable {
val next = world.evolve()
world = next
worldView.world = next
generation += 1
title = "Conway – Generation ${generation}"
worldView.world = worldView.world.evolve()
evolve()
}
handler.postDelayed(runnable, 500)
......
package training.akosma.conway
class World(val size: Int, private val aliveCells: List<Coord>?) {
class World(val size: Int, private val aliveCells: List<Coord>) {
val cells = mutableMapOf<Coord, Cell>()
init {
for (a in 0..(size - 1)) {
(0..(size - 1))
.map { Coord(a, it) }
.forEach {
if (aliveCells != null && aliveCells.contains(it)) {
cells[it] = Cell.Alive
} else {
cells[it] = Cell.Dead
}
for (a in 0 until size) {
(0 until size)
.map { Coord(a, it) }
.forEach {
if (aliveCells.contains(it)) {
cells[it] = Cell.Alive
} else {
cells[it] = Cell.Dead
}
}
}
}
fun evolve(): World {
val alive = mutableListOf<Coord>()
for ((coord, cell) in cells) {
var count = 0
(-1..1).map { a ->
count += (-1..1)
.map { Coord(coord.x + a, coord.y + it) }
.filter { it != coord }
.map { cells[it] }
.count { it == Cell.Alive }
.map { Coord(coord.x + a, coord.y + it) }
.filter { it != coord }
.map { cells[it] }
.count { it == Cell.Alive }
}
when (cell) {
......@@ -43,37 +42,34 @@ class World(val size: Int, private val aliveCells: List<Coord>?) {
}
}
}
return World(size, alive)
}
override fun toString(): String {
var str = ""
for (a in 0..(size - 1)) {
val str = StringBuffer()
for (a in 0 until size) {
if (a == 0) {
// First line with coordinates
str += " "
for (b in 0..(size - 1)) {
str += String.format("%1$3s|", b)
str.append(" ")
for (b in 0 until size) {
str.append(String.format("%1$3s|", b))
}
str += "\n"
str.append("\n")
}
str += String.format("%1$3s|", a)
str += (0..(size - 1))
.map { Coord(it, a) }
.map { cells[it]?.tableDescription() }
.reduce { acc, s -> acc + s }
str += "\n"
str.append(String.format("%1$3s|", a))
str.append((0 until size)
.map { Coord(it, a) }
.map { cells[it]?.tableDescription() }
.reduce { acc, s -> acc + s })
str.append("\n")
}
return str
return str.toString()
}
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other is World) {
return cells == other.cells
return cells == other.cells && size == other.size
}
return false
}
......@@ -85,50 +81,50 @@ class World(val size: Int, private val aliveCells: List<Coord>?) {
companion object {
fun blinker(coord: Coord): List<Coord> {
return listOf(
Coord(coord.x, coord.y),
Coord(coord.x + 1, coord.y),
Coord(coord.x + 2, coord.y)
Coord(coord.x, coord.y),
Coord(coord.x + 1, coord.y),
Coord(coord.x + 2, coord.y)
)
}
fun beacon(coord: Coord): List<Coord> {
return listOf(
Coord(coord.x, coord.y),
Coord(coord.x + 1, coord.y),
Coord(coord.x, coord.y + 1),
Coord(coord.x + 1, coord.y + 1),
Coord(coord.x + 2, coord.y + 2),
Coord(coord.x + 3, coord.y + 2),
Coord(coord.x + 2, coord.y + 3),
Coord(coord.x + 3, coord.y + 3)
Coord(coord.x, coord.y),
Coord(coord.x + 1, coord.y),
Coord(coord.x, coord.y + 1),
Coord(coord.x + 1, coord.y + 1),
Coord(coord.x + 2, coord.y + 2),
Coord(coord.x + 3, coord.y + 2),
Coord(coord.x + 2, coord.y + 3),
Coord(coord.x + 3, coord.y + 3)
)
}
fun glider(coord: Coord): List<Coord> {
return listOf(
Coord(coord.x + 2, coord.y + 2),
Coord(coord.x + 1, coord.y + 2),
Coord(coord.x, coord.y + 2),
Coord(coord.x + 2, coord.y + 1),
Coord(coord.x + 1, coord.y)
Coord(coord.x + 2, coord.y + 2),
Coord(coord.x + 1, coord.y + 2),
Coord(coord.x, coord.y + 2),
Coord(coord.x + 2, coord.y + 1),
Coord(coord.x + 1, coord.y)
)
}
fun block(coord: Coord): List<Coord> {
return listOf(
Coord(coord.x, coord.y),
Coord(coord.x + 1, coord.y),
Coord(coord.x, coord.y + 1),
Coord(coord.x + 1, coord.y + 1)
Coord(coord.x, coord.y),
Coord(coord.x + 1, coord.y),
Coord(coord.x, coord.y + 1),
Coord(coord.x + 1, coord.y + 1)
)
}
fun tub(coord: Coord): List<Coord> {
return listOf(
Coord(coord.x + 1, coord.y),
Coord(coord.x, coord.y + 1),
Coord(coord.x + 2, coord.y + 1),
Coord(coord.x + 1, coord.y + 2)
Coord(coord.x + 1, coord.y),
Coord(coord.x, coord.y + 1),
Coord(coord.x + 2, coord.y + 1),
Coord(coord.x + 1, coord.y + 2)
)
}
}
......
......@@ -5,7 +5,7 @@ import android.graphics.*
import android.view.View
class WorldView(context: Context) : View(context) {
private var mWorld: World? = null
private lateinit var mWorld: World
private var mRectangles = mutableMapOf<Coord, Rect>()
private var mPaths = mutableListOf<Path>()
......@@ -31,7 +31,7 @@ class WorldView(context: Context) : View(context) {
}
}
var world: World?
var world: World
get() = mWorld
set(newWorld) {
mWorld = newWorld
......@@ -41,39 +41,37 @@ class WorldView(context: Context) : View(context) {
override fun onDraw(canvas: Canvas) {
super.onDraw(canvas)
if (mWorld != null) {
val world = mWorld!!
val size = world.size
val contentWidth = width
val contentHeight = height
val world = mWorld
val size = world.size
val contentWidth = width
val contentHeight = height
val horizontalStep = contentWidth / size
val verticalStep = contentHeight / size
val horizontalStep = contentWidth / size
val verticalStep = contentHeight / size
if (mRectangles.count() == 0) {
createRectangles(horizontalStep, verticalStep)
}
for ((coord, cell) in world.cells) {
if (cell == Cell.Alive) {
val rect = mRectangles[coord]
if (rect != null) {
canvas.drawRect(rect, cellPaint)
canvas.drawRect(rect, cellStroke)
}
if (mRectangles.count() == 0) {
createRectangles(horizontalStep, verticalStep)
}
for ((coord, cell) in world.cells) {
if (cell == Cell.Alive) {
val rect = mRectangles[coord]
if (rect != null) {
canvas.drawRect(rect, cellPaint)
canvas.drawRect(rect, cellStroke)
}
}
}
if (mPaths.count() == 0) {
createGrid(horizontalStep, verticalStep, contentWidth, contentHeight)
}
for (path in mPaths) {
canvas.drawPath(path, linePaint)
}
if (mPaths.count() == 0) {
createGrid(horizontalStep, verticalStep, contentWidth, contentHeight)
}
for (path in mPaths) {
canvas.drawPath(path, linePaint)
}
}
private fun createRectangles(horizontalStep: Int, verticalStep: Int) {
for ((coord, _) in mWorld!!.cells) {
for ((coord, _) in mWorld.cells) {
val x = coord.x * horizontalStep
val y = coord.y * verticalStep
val rect = Rect(x, y, x + horizontalStep, y + verticalStep)
......@@ -83,7 +81,7 @@ class WorldView(context: Context) : View(context) {
private fun createGrid(horizontalStep: Int, verticalStep: Int, contentWidth: Int, contentHeight: Int) {
// Vertical lines
for (i in 1..(mWorld!!.size - 1)) {
for (i in 1 until mWorld.size) {
val vertical = Path()
val distance = i.toFloat()
vertical.moveTo(horizontalStep * distance, 0f)
......@@ -92,7 +90,7 @@ class WorldView(context: Context) : View(context) {
}
// Horizontal lines
for (j in 1..(mWorld!!.size - 1)) {
for (j in 1 until mWorld.size) {
val horizontal = Path()
val distance = j.toFloat()
horizontal.moveTo(0f, verticalStep * distance)
......
......@@ -8,7 +8,7 @@ buildscript {
}
dependencies {
classpath 'com.android.tools.build:gradle:4.0.1'
classpath 'com.android.tools.build:gradle:4.1.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
// NOTE: Do not place your application dependencies here; they belong
......
#Sat Oct 03 13:43:29 CEST 2020
#Wed Oct 14 16:53:15 CEST 2020
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.1.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-bin.zip
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment