-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
csdodd
committed
Sep 25, 2019
1 parent
c99cde5
commit 1e15df0
Showing
14 changed files
with
237 additions
and
162 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -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' | ||
} |
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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
kotlin.code.style=official | ||
kotlin.import.noCommonSourceSets=true |
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 |
---|---|---|
@@ -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 |
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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
rootProject.name = 'Knights Tour' | ||
|
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -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 | ||
} | ||
} |
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
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 |
---|---|---|
@@ -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 | | ||
| | | ||
|_______| | ||
*/ |
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 |
---|---|---|
@@ -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] | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import processing.core.PApplet | ||
|
||
fun main(args: Array<String>) { | ||
PApplet.main("output.KnightsTour") | ||
} |
Oops, something went wrong.