Kotlin - Completed Minesweeper and All Your Base
This commit is contained in:
parent
568e69fe4d
commit
14a276add1
2 changed files with 118 additions and 0 deletions
64
kotlin/all-your-base/src/main/kotlin/BaseConverter.kt
Normal file
64
kotlin/all-your-base/src/main/kotlin/BaseConverter.kt
Normal file
|
@ -0,0 +1,64 @@
|
|||
class BaseConverter(val base: Int, val valueList: IntArray){
|
||||
init {
|
||||
require(base > 1) {"Bases must be at least 2."}
|
||||
require(valueList.isNotEmpty()) {"You must supply at least one digit."}
|
||||
require(valueList.all { it < base }) {"All digits must be strictly less than the base."}
|
||||
require(valueList.all { it > -1 }) {"Digits may not be negative."}
|
||||
require(when (valueList.size){
|
||||
1 -> true
|
||||
else -> valueList.first() != 0
|
||||
}) {"Digits may not contain leading zeros."}
|
||||
}
|
||||
val baseTen: Int by lazy {
|
||||
var n = valueList.size
|
||||
val b = base
|
||||
var s = 0.toDouble()
|
||||
for (i in valueList){
|
||||
n--
|
||||
s += i * Math.pow(b.toDouble(), n.toDouble())
|
||||
}
|
||||
s.toInt()
|
||||
}
|
||||
|
||||
fun convertToBase(newBase: Int): IntArray{
|
||||
require(newBase > 1) {"Bases must be at least 2."}
|
||||
var n = baseTen
|
||||
val m = mutableListOf<Int>()
|
||||
val b = newBase
|
||||
|
||||
if (n == 0){
|
||||
return intArrayOf(0)
|
||||
}
|
||||
|
||||
while (n != 0){
|
||||
val d = n/b
|
||||
val r = n.rem(b)
|
||||
m.add(0, r.toInt())
|
||||
n = d
|
||||
}
|
||||
|
||||
return m.toIntArray()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
|
||||
1. Let n be the number of digits in the number. For example, 104 has 3 digits, so n=3.
|
||||
2. Let b be the base of the number. For example, 104 is decimal so b = 10.
|
||||
3. Let s be a running total, initially 0.
|
||||
4. For each digit in the number, working left to right do:
|
||||
Subtract 1 from n.
|
||||
Multiply the digit times bn and add it to s.
|
||||
5. When your done with all the digits in the number, its decimal value will be s
|
||||
*/
|
||||
|
||||
/*
|
||||
Let n be the decimal number.
|
||||
Let m be the number, initially empty, that we are converting to. We'll be composing it right to left.
|
||||
Let b be the base of the number we are converting to.
|
||||
Repeat until n becomes 0
|
||||
Divide n by b, letting the result be d and the remainder be r.
|
||||
Write the remainder, r, as the leftmost digit of b.
|
||||
Let d be the new value of n.
|
||||
*/
|
54
kotlin/minesweeper/src/main/kotlin/MinesweeperBoard.kt
Normal file
54
kotlin/minesweeper/src/main/kotlin/MinesweeperBoard.kt
Normal file
|
@ -0,0 +1,54 @@
|
|||
class MinesweeperBoard(val inputBoard: List<String>) {
|
||||
val board by lazy {
|
||||
val board: MutableList<Cell> = mutableListOf()
|
||||
for (i in 0..inputBoard.size - 1){
|
||||
for (j in 0..inputBoard[i].length - 1){
|
||||
if (inputBoard[i][j] == '*'){
|
||||
board.add(Cell(x = i, y = j, mine = true, neighbors = 0))
|
||||
}else {
|
||||
var neighbors: Int = 0
|
||||
for (xOffset in -1..1) {
|
||||
for (yOffset in -1..1) {
|
||||
if ((xOffset + i >= 0)
|
||||
and (xOffset + i < inputBoard.size)
|
||||
and (yOffset + j >= 0)
|
||||
and (yOffset + j < inputBoard[i].length)
|
||||
) {
|
||||
if (inputBoard[i + xOffset][j + yOffset] == '*') {
|
||||
neighbors += 1
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
board.add(Cell(x = i, y = j, mine = false, neighbors = neighbors))
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
board
|
||||
}
|
||||
|
||||
fun withNumbers(): List<String>{
|
||||
val returnList = mutableListOf<String>()
|
||||
for (i in 0..inputBoard.size - 1){
|
||||
val stringBuilder: StringBuilder = StringBuilder()
|
||||
for (j in 0..inputBoard[i].length - 1){
|
||||
stringBuilder.append(board[(i*inputBoard[i].length)+j])
|
||||
}
|
||||
returnList.add(String(stringBuilder))
|
||||
}
|
||||
|
||||
return returnList
|
||||
}
|
||||
}
|
||||
|
||||
data class Cell(val x: Int, val y: Int, val mine: Boolean, val neighbors: Int){
|
||||
override fun toString(): String {
|
||||
if (mine){
|
||||
return "*"
|
||||
} else if (neighbors == 0) {
|
||||
return " "
|
||||
} else {
|
||||
return neighbors.toString()
|
||||
} }
|
||||
}
|
Loading…
Add table
Reference in a new issue