A simulation of the famous battleship board game

Overview

battleships

A simulation of the famous battleship board game

A quick intro to the Kotlin syntax

Function/Method syntax

fun hello(param1: String): String
 ^   ^     ^       ^        ^- return type
 |   |     |       |- parameter type
 |   |     |- parameter name
 |   |- function name
 |- function keyword

Variables

Kotlin is always strictly typed, but it's not necessary to add the type declaration to every element.

var foo = "bar" // a mutable variable of type String (the type is automatically inferred by the compiler)
var foo: String = "bar" // same as above, but ": String" is obsolete

val foo = "bar" // an immutable variable of type String (the type is automatically inferred by the compiler)
foo = "hello" // not possible, as immutable variables cannot be changed after initialization

var foo: String? = null // by default, variables in Kotlin cannot be null, unless suffixed with ?

Control flows

Kotlin makes use of functional and oo principles.

val list = listOf("A","B","C")
val c = list.find { it == "C" } // c is of type :String?, as find could return null

var ships: List<Ship?> = listOf(null, Ship("Curiser"))
println(ships[0]?.name) // prints null, due to the ? safecall
println(ships[0]!!.name) // throws an NPE, due to !! not-null assertion operator
println(ships[0].name) // does not compile, because list elements can be nullable
println(ships[1].name) // prints "Cruiser"

var ship: Ship?
[..] // more code
ship?.let { // same logic as with "if ship != null"
  // do something useful
}

Structures

The code makes some use of Pairs/Tuples. In Kotlin, it's very easy to create Pairs:

val foo = 0 to "Zero" // -> same as val foo = Pair<Int, String>(0, "Zero")

// with such, it's also straight forward to create lists:
val myList = listOf(
    0 to "Zero",
    1 to "One",
    2 to "Two",
    ...
)
You might also like...
Experimental multiplayer game
Experimental multiplayer game

Enjoy retro games? Enjoy multiplayer games? Ever wanted to play one retro game against a different game in real time?

In-game editor for item NBT

Building Gradle - Dependency Management The GradleWrapper in included in this project. On Windows: gradlew.bat clean build On MacOS/Linux: ./gradlew

Basic template to create a game using minigdx

MiniGDX Game Template Create your first game using miniGDX by clicking the "Use this Template" button above. The game will be configured for: the JVM

 Android Kotlin: Matching Kitties: A Game Inspired by Cats
Android Kotlin: Matching Kitties: A Game Inspired by Cats

Android Kotlin: Matching Kitties: A Game Inspired by Cats A kotlin based Android memory game Screenshots | | | | | | | | | Viewing the App You can clo

a bitcoin key collision game for android

BitteryApp BitteryApp is an opensource bitcoin key collision game for Android. How to Build BitteryApp source code build in chromium building environm

Flappy Bird game
Flappy Bird game

Flappy Bird by Compose 📜 Description Flappy Bird game built with Jetpack Compose. 💡 Motivation and Context All UI built with Jeptack Compose Images

The second iteration of the Xlite game.

Xlite 2.0 Xlite is a modular kotlin based RSPS. The goal of Xlite 2.0 is to provide the community with a stable and powerful sandbox to experiment wit

The Xlite Game Server.

Xlite 2.0 Xlite is a modular kotlin based RSPS. The goal of Xlite 2.0 is to provide the community with a stable and powerful sandbox to experiment wit

A simple Snake game implemented using Compose for Desktop
A simple Snake game implemented using Compose for Desktop

A simple Snake game implemented using Compose for Desktop

Owner
Daniel Bunte
Save the environment - write smart code! Work: @Accenture
Daniel Bunte
This is an application that is about an X / O game. You can enter the names of the game, and there is also a screen for those who win and there is a button to continue playing and the game determines the result of each player

Game-X-O This is an application that is about an X / O game. You can enter the names of the game, and there is also a screen for those who win and the

Mohamed Rafat 2 Aug 20, 2022
Game made with Korge (Kotlin Multiplatform game engine)

MolesAttack Kotlin Multiplatform Game Play Html/js: https://feliperce.github.io/MolesAttack-Distribution/ Jar: https://feliperce.github.io/MolesAttack

Felipe Rodrigues 10 May 30, 2022
Our maze game is an 2d-animation game developed using android studio.

Our maze game is an 2d-animation game developed using android studio. The game consists of a ball and a board with a hole in the center of it. We are using accelerometer as controller to guide ball towards the hole. T

Suraj Devgan 6 Nov 29, 2022
An easy open source Android Native Game FrameWork.

JustWeEngine - Android Game FrameWork An easy open source Android Native Game FrameWork. Engine Flow Chart How To Use? Import Engine's module as Libra

JustWe 767 Dec 8, 2022
Desktop/Android/HTML5/iOS Java game development framework

Cross-platform Game Development Framework libGDX is a cross-platform Java game development framework based on OpenGL (ES) that works on Windows, Linux

libgdx 20.9k Jan 8, 2023
Cocos2d-x is a suite of open-source, cross-platform, game-development tools used by millions of developers all over the world.

cocos2d-x Win32 Others cocos2d-x is a multi-platform framework for building 2d games, interactive books, demos and other graphical applications. It is

cocos2d 16.7k Dec 31, 2022
Free Android 2D OpenGL Game Engine

AndEngine Donations While developing AndEngine was a lot of fun, it also also consumed many(!) months of my life. It actually continues to cost me a s

Nicolas Gramlich 3.2k Jan 5, 2023
A cross-platform Java game Engine (Framework) , support JavaFX / Android / IOS / HTML5 / Linux / MAC / Windows

Loon Game Engine (Java Game Framework) EN / KR Free Game Resources Links Download Loon Game Engine Only Android-studio Template : androidstudio-templa

cping 502 Jan 4, 2023
Cocos2d-x is a suite of open-source, cross-platform, game-development tools used by millions of developers all over the world.

cocos2d-x Win32 Others cocos2d-x is a multi-platform framework for building 2d games, interactive books, demos and other graphical applications. It is

cocos2d 16.7k Jan 7, 2023
QRGame - storage a game inside a qr code

QRGame Can we storage a game inside a qr code? Yes! At least in theory. That was the question that started that entire project. QRGame is an Android p

Aislan Tavares 10 Mar 13, 2022