Skip to content

Commit

Permalink
Add week 4
Browse files Browse the repository at this point in the history
  • Loading branch information
danfimov committed May 14, 2021
1 parent 26de353 commit 6eef010
Show file tree
Hide file tree
Showing 24 changed files with 1,882 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Kotlin for Java Developers/Week 4/.idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions 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.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions Kotlin for Java Developers/Week 4/.idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Kotlin for Java Developers/Week 4/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Kotlin for Java Developers/Week 4/Board/Task/assignmentKey
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
q3GRK8FuEeit4A6YDvCSVA
1 change: 1 addition & 0 deletions Kotlin for Java Developers/Week 4/Board/Task/partId
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rv5mL
41 changes: 41 additions & 0 deletions Kotlin for Java Developers/Week 4/Board/Task/src/board/Board.kt
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
}
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) }
}
Loading

0 comments on commit 6eef010

Please sign in to comment.