Skip to content

Instantly share code, notes, and snippets.

@jeffxchu
Created November 29, 2023 02:22
Show Gist options
  • Save jeffxchu/e25c0383e68d2eaefc8678f200ad79b6 to your computer and use it in GitHub Desktop.
Save jeffxchu/e25c0383e68d2eaefc8678f200ad79b6 to your computer and use it in GitHub Desktop.
Drop Bomb game
import kotlin.random.Random
fun main() {
val gridSize = 10
val numEnemies = 15
val maxBombs = 20
var bombsUsed = 0
var enemiesHit = 0
val board = Array(gridSize) {
Array(gridSize) { false } // False indicates no enemy
}
placeEnemies(board, numEnemies)
while (bombsUsed < maxBombs && enemiesHit < numEnemies) {
println("Enter bomb coordinates (row and column, 0-${gridSize - 1}): ")
val (row, col) = readln().split(' ').map(String::toInt)
val hit = dropBomb(board, row, col)
bombsUsed++
if (hit) {
enemiesHit++
println("Hit! Enemies hit: $enemiesHit")
} else {
println("Miss!")
}
if (enemiesHit == numEnemies) {
println("You win! All enemies have been eliminated.")
break
}
}
if (enemiesHit < numEnemies) {
println("Game over. You used all your bombs. Enemies left: ${numEnemies - enemiesHit}")
}
}
fun placeEnemies(board: Array<Array<Boolean>>, numEnemies: Int) {
var enemiesPlaced = 0
while (enemiesPlaced < numEnemies) {
val row = Random.nextInt(board.size)
val col = Random.nextInt(board.size)
if (!board[row][col]) {
board[row][col] = true
enemiesPlaced++
}
}
}
fun dropBomb(board: Array<Array<Boolean>>, row: Int, col: Int): Boolean {
if (board[row][col]) {
board[row][col] = false // Remove the enemy
return true
}
return false
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment