Share
Explore

JavaScript Lab Test: Space Battle

Due February 16, W23
For this Lab Test, you will construct a JavaScript Game.
Save as StudentName-LabTest-SpaceBattle.docx
What to hand in:
Name:
Student ID: GITHUB URL:

Use Star Fleet Standard Document and Communication Encryption Protocols. All communications with Star Fleet are to be tagged Priority Ultra Violet.

Here are the requirements for your Game:
It is the Earth Year 2154.
The fledging Unified Federation of Planets has had a disastrous first contact with a Warlike alien race called the Romulans, and a war broke out.
A picture containing person, person, indoor

Description automatically generated
You had been tasked by the Federation High Command to create Battle Simulation Software. Working under the orders of Admiral Hikaru Sulu, you will create a war game simulation to provide tactical inputs to the War Planning Office on how to best deploy the Federation’s Resources.
Hikuru Sulu was promoted to Captain upon the promotion of James Kirk to Admiral and Mr. Spock to Vulcan High Ambassador to the Federation. Prior to that, he was a crucial fighter for  during the , where he helped end the .
Following the Methods you have developed in the first Lab Activity {Lions and Tigers}, you will create Objects representing 40 Federation and 40 Romulan Star Ships.
Earth Starship battle armory specifications:
30 Photon Torpedoes with an explosive yield of 150 Mega Joules each.
12 Phaser Banks capable of delivering 100 Mega Joules of Energy between charges, with a 20 minute interval required between use for recharging.
4 Shield Generators: Bow, Stern, Port and Starboard. Each Shield Generator is capable of withstanding 140 Mega Joules before collapsing. Their maintenance crew will require between 10 and 20 minutes to reset. (Your simulation will use random number generators to set this time).
Romulan starship battle armory specifications:
50 Quantum Torpedoes with an explosive yield of 100 Mega Joules each.
12 Phased Pulse Generators capable of delivering 60 Mega Joules of Energy between charges, with a 12 minute interval required between use for recharging. (For the sake of this Simulation, we are normalizing the enemy’s measurement units to Federation equivalents.).
4 Shield Generators: Bow, Stern, Port and Starboard. Each Shield Generator is capable of withstanding 200 Mega Joules before collapsing. Their maintenance crew will require between 10 and 20 Earth Time minutes to reset. (Your simulation will use random number generators to set this time)..
Page Break
You will create a class to run the simulation by randomly engaging 2 ships at a time.
The will fire at each other until one or the other is destroyed.
A ship is destroyed by the first attack that takes place after 2 of its shields have collapsed.
The battle continues until all of one side’s ships have been destroyed.
As we cannot control what the Romulans do, the output of your Simulation will be taken under advisement by the Federation High Command as to how to reassign armory logistics to Federation Ships.
Do your work well. A million years of life on Earth will come to an End if the Romulans win this war.
e Break

Starter Code:

This Lab Test will be graded on a score of 100 points:
Grading Rubic
The Project is handed in one time in the correct format.
10 points
1
The program runs and reports its output in a concise and effective manner
25 points
2
Good object-oriented design is presented. The components of the Requirement are elegantly mapped to Objects
25 points
3
Good programming style including Comments are demonstrated.
25 points
4
Data is well encapsulated in discrete variables. Behaviors are well-encapsulated in discrete methods
15 points
5
There are no rows in this table

Program Specifications

A starship will be modeled by a JavaScript Class from which you will make OBJECTs.
Recommended: Use a Factory Pattern to make your StarShips
You have 2 opposing Forces:

The Battle Field
Name
Column 2
Column 3
Notes
1
Romulans
Federation
Open
2

Num Torpedos

10
10
Open
3
Energy per Torpedo
20 MJ
20 MJ
Open
4
1 phaser bank
200 MJ
200 MJ
Open
5
Shield Energy
1000 MJ
1000 MJ
Open
There are no rows in this table

image.png

Example of how to create a class in JavaScript using constructors:

// Define a class using a constructor function

class Person {

constructor(firstName, lastName, age) {

this.firstName = firstName;

this.lastName = lastName;

this.age = age;

}


// Define a method on the class

sayHello() {

console.log(`Hello, my name is ${this.firstName} ${this.lastName}.`);

}

}


// Create an object instance of the class

const john = new Person('John', 'Doe', 30);


// Access properties on the object instance

console.log(john.firstName); // Output: 'John'


// Call methods on the object instance

john.sayHello(); // Output: 'Hello, my name is John Doe.'


In the example above, we define a class called Person using a constructor function that takes in firstName, lastName, and age as arguments. We also define a method called sayHello on the class.
We then create an object instance of the class called john using the new keyword and pass in values for the constructor arguments. We can then access properties and call methods on the object instance.

What you need to do:
Set up a GIT HUB REPO: Private with an invite to ComputationalKnowledge@gmail.om
Create 10 Ships on each side and put those objects into an Array (GIT COMMIT)
Make 2 Arrays to manage your BattleFormations (The ships of the Romulan and Federation Fleets).
Write the Battle management Algorithm:
While LOOP
One side acquires an Enemy Target: Fires at it. If the Attack Energy > Defender’s shield strenght: Defender is destroyed. Else : gets cycled back into their BattleFormation Array
Continue until only side is remaining with at least one ship.


implement the attack method of class Battle
class Battle {
constructor(player1, player2) {
this.player1 = player1;
this.player2 = player2;
}

attack(attacker, defender) {
// Calculate the damage
const damage = Math.floor(Math.random() * attacker.attackPower);

// Apply the damage to the defender's health
defender.health -= damage;

// Log the attack
console.log(`${attacker.name} attacked ${defender.name} for ${damage} damage.`);

// Check if the defender is still alive
if (defender.health <= 0) {
console.log(`${defender.name} has been defeated!`);
}
}

start() {
// Loop until one of the players is defeated
while (this.player1.health > 0 && this.player2.health > 0) {
// Player 1 attacks Player 2
this.attack(this.player1, this.player2);

// Check if Player 2 is still alive
if (this.player2.health > 0) {
// Player 2 attacks Player 1
this.attack(this.player2, this.player1);
}
}

// Log the winner
if (this.player1.health > 0) {
console.log(`${this.player1.name} wins!`);
} else {
console.log(`${this.player2.name} wins!`);
}
}
}

class Player {
constructor(name, health, attackPower) {
this.name = name;
this.health = health;
this.attackPower = attackPower;
}
}

const player1 = new Player('Player 1', 100, 10);
const player2 = new Player('Player 2', 100, 10);
const battle = new Battle(player1, player2);
battle.start();

In the updated code, we define a Battle class and an attack method that takes in an attacker and a defender. The attack method calculates the damage based on the attacker's attack power, applies the damage to the defender's health, and logs the attack. If the defender's health drops to 0 or below, the method logs that the defender has been defeated.
We then update the start method to use the attack method to simulate the battle. The start method loops until one of the players is defeated, with each player taking turns attacking the other.
We also define a Player class to create player instances with a name, health, and attackPower. Finally, we create two player instances, create a battle instance, and start the battle by calling the start method.
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.