Share
Explore

Assignment: Star Trek Space Battle Simulator

This document is subject to copyright and other intellectual property rights.
Modification, distribution or reposting of this document is prohibited.

Submission Requirements:
1. Code Submission:
● Submit a file called A2FirstName.kt for example: A2Kevin.kt
● The file should contain all the necessary classes and main() function for the application.
2. Code Output
● Submit a screenshot displaying the output of the program’s main() function
Academic Integrity:
● This is an individual assessment
● You are permitted to refer to the Internet for Kotlin syntax. However, following tutorials, homework help websites, or using full/partial solutions from the internet is not permitted
● You are not permitted to share code/references with other learners, or discuss
solutions/approaches.

Problem Description.
Object oriented programming is commonly used to do empirical investigation of real world business domains by creating simulations.
This simulation will model basic space combat between Federation and Romulan ships.
Note: It is not a grading rubric requirement, but - It would be quite helpful for you to get started by sketching out some UML diagrams:
Class Interaction to visualize COMPOSITIONAL Relationships. This includes shared field composition and inheritance.
Object Interaction Diagrams: How are objects communicating via METHOD CALLS : parameters being passed, and return values.

classDiagram

class Weapon {
<<interface>>
+power: Int
+fire(): Int
}

class Shield {
<<interface>>
+power: Int
+absorb(damage: Int): Int
}

class Spaceship {
<<interface>>
+name: String
+weapon: Weapon
+shield: Shield
+isDestroyed: Boolean
+takeDamage(damage: Int)
}

class BattleSimulator {
<<interface>>
+simulate(federation1: Spaceship, federation2: Spaceship, romulan1: Spaceship, romulan2: Spaceship)
}

class BattleReporter {
<<interface>>
+reportBattle(federation1: Spaceship, federation2: Spaceship, romulan1: Spaceship, romulan2: Spaceship)
}

class Phaser {
+power: Int
+fire(): Int
}

class DeflectorShield {
+power: Int
+absorb(damage: Int): Int
}

class AbstractSpaceship {
<<abstract>>
-hullStrength: Int
+name: String
+weapon: Weapon
+shield: Shield
+isDestroyed: Boolean
+takeDamage(damage: Int)
}
class ConstitutionClass {
+ConstitutionClass(name: String, weaponPower: Int, shieldPower: Int)
}

class BirdOfPrey {
+BirdOfPrey(name: String, weaponPower: Int, shieldPower: Int)
}

class SpaceBattleSimulator {
+simulate(federation1: Spaceship, federation2: Spaceship, romulan1: Spaceship, romulan2: Spaceship)
}

class BattleReport {
+reportBattle(federation1: Spaceship, federation2: Spaceship, romulan1: Spaceship, romulan2: Spaceship)
}

Weapon <|.. Phaser

Shield <|.. DeflectorShield

Spaceship <|.. AbstractSpaceship
AbstractSpaceship <|-- ConstitutionClass
AbstractSpaceship <|-- BirdOfPrey

BattleSimulator <|.. SpaceBattleSimulator

BattleReporter <|.. BattleReport

Spaceship *-- Weapon
Spaceship *-- Shield

AbstractSpaceship *-- Phaser

AbstractSpaceship *-- DeflectorShield

SpaceBattleSimulator ..> Spaceship

BattleReport ..> Spaceship
This UML diagram shows:
Interfaces (Weapon, Shield, Spaceship, BattleSimulator, BattleReporter) at the top.
Concrete implementations of Weapon (Phaser) and Shield (DeflectorShield).
AbstractSpaceship as an abstract class implementing Spaceship.
ConstitutionClass and BirdOfPrey extending AbstractSpaceship.
SpaceBattleSimulator implementing BattleSimulator.
BattleReport implementing BattleReporter.
Composition relationships (Spaceship has a Weapon and a Shield).
Dependencies (SpaceBattleSimulator and BattleReport depend on Spaceship).
This diagram illustrates the structure of the system, showing how the different components relate to each other through inheritance, implementation, and composition.


Starter Code:
interface Weapon {
var power: Int
fun fire(): Int
}

interface Shield {
var power: Int
fun absorb(damage: Int): Int
}

interface Spaceship {
val name: String
val weapon: Weapon
val shield: Shield
var isDestroyed: Boolean
fun takeDamage(damage: Int)
}

interface BattleSimulator {
fun simulate(federation1: Spaceship, federation2: Spaceship, romulan1: Spaceship, romulan2: Spaceship)
}

interface BattleReporter {
fun reportBattle(federation1: Spaceship, federation2: Spaceship, romulan1: Spaceship, romulan2: Spaceship)
}

class Phaser(override var power: Int) : Weapon {
override fun fire(): Int {
val damage = maxOf(power / 10, 1)
power -= damage
return damage
}
}

class DeflectorShield(override var power: Int) : Shield {
override fun absorb(damage: Int): Int {
val absorbed = minOf(power, damage)
power -= absorbed
return damage - absorbed
}
}

abstract class AbstractSpaceship(
override val name: String,
weaponPower: Int,
shieldPower: Int
) : Spaceship {
override val weapon = Phaser(weaponPower)
override val shield = DeflectorShield(shieldPower)
override var isDestroyed = false
private var hullStrength = 100

override fun takeDamage(damage: Int) {
val remainingDamage = shield.absorb(damage)
hullStrength -= remainingDamage
if (hullStrength <= 0) {
isDestroyed = true
}
}
}

class SpaceBattleSimulator : BattleSimulator {
override fun simulate(federation1: Spaceship, federation2: Spaceship, romulan1: Spaceship, romulan2: Spaceship) {
// Simulate 5 rounds of battle
repeat(5) {
// Federation ships attack Romulan ships
if (!federation1.isDestroyed && !romulan1.isDestroyed) {
romulan1.takeDamage(federation1.weapon.fire())
}
if (!federation2.isDestroyed && !romulan2.isDestroyed) {
romulan2.takeDamage(federation2.weapon.fire())
}

// Romulan ships attack Federation ships
if (!romulan1.isDestroyed && !federation1.isDestroyed) {
federation1.takeDamage(romulan1.weapon.fire())
}
if (!romulan2.isDestroyed && !federation2.isDestroyed) {
federation2.takeDamage(romulan2.weapon.fire())
}
}
}
}

class BattleReport : BattleReporter {
override fun reportBattle(federation1: Spaceship, federation2: Spaceship, romulan1: Spaceship, romulan2: Spaceship) {
println("Battle Report:")
printShipStatus("Federation", federation1)
printShipStatus("Federation", federation2)
printShipStatus("Romulan", romulan1)
printShipStatus("Romulan", romulan2)

val fedSurvivors = listOf(federation1, federation2).count { !it.isDestroyed }
val romSurvivors = listOf(romulan1, romulan2).count { !it.isDestroyed }

println("\nBattle Outcome:")
when {
fedSurvivors > romSurvivors -> println("Federation Victory!")
romSurvivors > fedSurvivors -> println("Romulan Victory!")
else -> println("It's a draw!")
}
}

private fun printShipStatus(fleetName: String, ship: Spaceship) {
val status = if (ship.isDestroyed) "Destroyed" else "Operational"
println("$fleetName ${ship.name}: $status (Shield: ${ship.shield.power}, Weapon: ${ship.weapon.power})")
}
}

fun main() {
// Create the 2 Federation and the 2 Romulan Ships

// Run the simulation
// Create the battle results report.
}

Sample Output:
Battle Report: Federation USS Enterprise: Operational (Shield: 63, Weapon: 60) Federation USS Reliant: Operational (Shield: 57, Weapon: 54) Romulan IKS Korinar: Operational (Shield: 55, Weapon: 58) Romulan IKS Talon: Operational (Shield: 49, Weapon: 52)
Battle Outcome: It's a draw!
Process finished with exit code 0

Requirements:

Implement two ship classes:
a. ConstitutionClass representing Federation ships b. BirdOfPrey representing Romulan ships Both should extend AbstractSpaceship.
The SpaceBattleSimulator class:
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.