Skip to content

Commit

Permalink
Convert code to using Processing
Browse files Browse the repository at this point in the history
  • Loading branch information
csdodd committed Sep 25, 2019
1 parent c99cde5 commit 1e15df0
Show file tree
Hide file tree
Showing 14 changed files with 237 additions and 162 deletions.
12 changes: 12 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
plugins {
id 'org.jetbrains.kotlin.jvm' version '1.3.50'
}

repositories {
mavenCentral()
}

dependencies {
compile "org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.50"
compile 'org.processing:core:3.3.6'
}
2 changes: 2 additions & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
kotlin.code.style=official
kotlin.import.noCommonSourceSets=true
6 changes: 6 additions & 0 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#Tue Sep 24 11:55:32 CEST 2019
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10-all.zip
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStorePath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
2 changes: 2 additions & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
rootProject.name = 'Knights Tour'

15 changes: 0 additions & 15 deletions src/algorithms/Warnsdorff.kt

This file was deleted.

115 changes: 0 additions & 115 deletions src/grid/Grid.kt

This file was deleted.

20 changes: 0 additions & 20 deletions src/grid/PngCoords.kt

This file was deleted.

8 changes: 0 additions & 8 deletions src/main.kt

This file was deleted.

29 changes: 29 additions & 0 deletions src/main/kotlin/algorithms/Warnsdorff.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package algorithms

import grid.Cell
import grid.Grid

class Warnsdorff(private val grid: Grid) {

fun run(): Cell? {
val startCell = grid.randomCell()
var currentCell = startCell
(0 until grid.size()).forEach { moveCount ->
val groupedNeighbours = currentCell
?.neighbours()
?.groupBy { it.neighbours().size }

val lowestAmountOfNeighbours = groupedNeighbours?.keys?.min()

val nextCell = groupedNeighbours
?.get(lowestAmountOfNeighbours)
?.random()

currentCell?.link(nextCell)
currentCell?.moveCount = moveCount
currentCell = nextCell
}

return startCell
}
}
26 changes: 22 additions & 4 deletions src/grid/Cell.kt → src/main/kotlin/grid/Cell.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ open class Cell(
var eastNorth: Cell? = null
var eastSouth: Cell? = null
var linkedCell: Cell? = null
var moveCount: Int = -1

private var visited: Boolean = false

Expand All @@ -46,17 +47,34 @@ open class Cell(
fun link(nextCell: Cell?) {
visited = true
linkedCell = nextCell
nextCell?.markAsVisited(this)
}

fun getPngCoords(cellSize: Int) : PngCoords {
private fun markAsVisited(cell: Cell) {
neighbours().forEach { neighbour ->
if (neighbour == cell) neighbour.visited = true
}
}

fun getCoords(cellSize: Float) : Coords {
val halfDimen = cellSize / 2
return PngCoords(
return Coords(
column * cellSize,
(column + 1) * cellSize,
row * cellSize,
(row + 1) * cellSize,
(column * cellSize) + halfDimen,
(row * cellSize) + halfDimen
)
}

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other !is Cell)
return false
else {
if (other.row == this.row
&& other.column == this.column)
return true
}
return false
}
}
18 changes: 18 additions & 0 deletions src/main/kotlin/grid/Coords.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package grid

data class Coords(
val x1: Float,
val y1: Float,
val xC: Float,
val yC: Float
)

/*
x1/y1 _______
| |
| |
| xC/yC |
| |
|_______|
*/
62 changes: 62 additions & 0 deletions src/main/kotlin/grid/Grid.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package grid

open class Grid(private val numRows: Int, private val numColumns: Int) {

private val grid by lazy { generateGrid() }

private fun generateGrid(): Array<Array<Cell?>> {
val emptyGrid = prepareGrid()
return configureCells(emptyGrid)
}

open fun prepareGrid(): Array<Array<Cell?>> {
return Array(numRows) {
row -> Array<Cell?>(numColumns) {
column ->
Cell(row, column)
}}
}

open fun configureCells(emptyGrid: Array<Array<Cell?>>): Array<Array<Cell?>> {
emptyGrid.forEachIndexed { row, columns ->
columns.forEachIndexed { column, cell ->
cell?.northWest = emptyGrid[row - 2, column - 1]
cell?.northEast = emptyGrid[row - 2, column + 1]
cell?.southWest = emptyGrid[row + 2, column - 1]
cell?.southEast = emptyGrid[row + 2, column + 1]
cell?.westNorth = emptyGrid[row - 1, column - 2]
cell?.westSouth = emptyGrid[row + 1, column - 2]
cell?.eastNorth = emptyGrid[row - 1, column + 2]
cell?.eastSouth = emptyGrid[row + 1, column + 2]
}
}

return emptyGrid
}

fun randomCell(): Cell? {
val r = (0 until numRows).random()
val c = (0 until numColumns).random()
return this[r, c]
}

fun size() = numRows * numColumns

fun forEachCell(action: (Cell) -> Unit) {
grid.forEach { row ->
row.forEach { cell ->
if (cell != null) {
action(cell)
}
}
}
}

private operator fun get(row: Int, column: Int) = grid[row, column]

private operator fun Array<Array<Cell?>>.get(row: Int, column: Int): Cell? {
if (row !in 0 until numRows) return null
if (column !in 0 until numColumns) return null
return this[row][column]
}
}
5 changes: 5 additions & 0 deletions src/main/kotlin/main.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import processing.core.PApplet

fun main(args: Array<String>) {
PApplet.main("output.KnightsTour")
}
Loading

0 comments on commit 1e15df0

Please sign in to comment.