-
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
Showing
24 changed files
with
1,882 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
21 changes: 21 additions & 0 deletions
21
Kotlin for Java Developers/Week 4/.idea/codeStyles/Project.xml
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
5 changes: 5 additions & 0 deletions
5
Kotlin for Java Developers/Week 4/.idea/codeStyles/codeStyleConfig.xml
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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 @@ | ||
q3GRK8FuEeit4A6YDvCSVA |
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 @@ | ||
rv5mL |
41 changes: 41 additions & 0 deletions
41
Kotlin for Java Developers/Week 4/Board/Task/src/board/Board.kt
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,41 @@ | ||
package board | ||
|
||
data class Cell(val i: Int, val j: Int) { | ||
override fun toString()= "($i, $j)" | ||
} | ||
|
||
enum class Direction { | ||
UP, DOWN, RIGHT, LEFT; | ||
|
||
fun reversed() = when (this) { | ||
UP -> DOWN | ||
DOWN -> UP | ||
RIGHT -> LEFT | ||
LEFT -> RIGHT | ||
} | ||
} | ||
|
||
interface SquareBoard { | ||
val width: Int | ||
|
||
fun getCellOrNull(i: Int, j: Int): Cell? | ||
fun getCell(i: Int, j: Int): Cell | ||
|
||
fun getAllCells(): Collection<Cell> | ||
|
||
fun getRow(i: Int, jRange: IntProgression): List<Cell> | ||
fun getColumn(iRange: IntProgression, j: Int): List<Cell> | ||
|
||
fun Cell.getNeighbour(direction: Direction): Cell? | ||
} | ||
|
||
interface GameBoard<T> : SquareBoard { | ||
|
||
operator fun get(cell: Cell): T? | ||
operator fun set(cell: Cell, value: T?) | ||
|
||
fun filter(predicate: (T?) -> Boolean): Collection<Cell> | ||
fun find(predicate: (T?) -> Boolean): Cell? | ||
fun any(predicate: (T?) -> Boolean): Boolean | ||
fun all(predicate: (T?) -> Boolean): Boolean | ||
} |
69 changes: 69 additions & 0 deletions
69
Kotlin for Java Developers/Week 4/Board/Task/src/board/BoardImpl.kt
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,69 @@ | ||
package board | ||
|
||
fun IntProgression.applyBounds(bound: Int): IntProgression = | ||
if (last > bound) | ||
first..bound | ||
else if (first > bound) | ||
bound..last | ||
else this | ||
|
||
fun createSquareBoard(width: Int): SquareBoard = SquareBoardImpl(width) | ||
|
||
open class SquareBoardImpl(final override val width: Int) : SquareBoard { | ||
|
||
private val cells = (1..width).flatMap { i -> | ||
(1..width).map { j -> | ||
Cell(i, j) | ||
} | ||
} | ||
|
||
override fun getCellOrNull(i: Int, j: Int): Cell? = | ||
getAllCells().firstOrNull { it == Cell(i, j) } | ||
|
||
override fun getCell(i: Int, j: Int): Cell = | ||
getCellOrNull(i, j) ?: throw IllegalArgumentException("Cell not found for i:$i, j:$j") | ||
|
||
override fun getAllCells(): Collection<Cell> = cells | ||
|
||
override fun getRow(i: Int, jRange: IntProgression): List<Cell> = | ||
jRange.applyBounds(width).map { j -> getCell(i, j) } | ||
|
||
|
||
override fun getColumn(iRange: IntProgression, j: Int): List<Cell> = | ||
iRange.applyBounds(width).map { i -> getCell(i, j) } | ||
|
||
override fun Cell.getNeighbour(direction: Direction): Cell? = when (direction) { | ||
Direction.UP -> getCellOrNull(this.i - 1, j) | ||
Direction.LEFT -> getCellOrNull(this.i, j - 1) | ||
Direction.DOWN -> getCellOrNull(this.i + 1, j) | ||
Direction.RIGHT -> getCellOrNull(this.i, j + 1) | ||
} | ||
} | ||
|
||
fun <T> createGameBoard(width: Int): GameBoard<T> = GameBoardImpl(width) | ||
|
||
class GameBoardImpl<T>(width: Int) : SquareBoardImpl(width), GameBoard<T> { | ||
|
||
private val board = getAllCells() | ||
.map { it to null } | ||
.toMap<Cell, T?>() | ||
.toMutableMap() | ||
|
||
override operator fun get(cell: Cell): T? = board[cell] | ||
|
||
override operator fun set(cell: Cell, value: T?) { | ||
board[cell] = value | ||
} | ||
|
||
override fun filter(predicate: (T?) -> Boolean): Collection<Cell> = | ||
board.entries.filter { predicate(it.value) }.map { it.key } | ||
|
||
override fun find(predicate: (T?) -> Boolean): Cell? = | ||
board.entries.find { predicate(it.value) }?.key | ||
|
||
override fun any(predicate: (T?) -> Boolean): Boolean = | ||
board.entries.any { predicate(it.value) } | ||
|
||
override fun all(predicate: (T?) -> Boolean): Boolean = | ||
board.entries.all { predicate(it.value) } | ||
} |
Oops, something went wrong.