Tools: VSCode | Source Code: GitHub
The "Underwater Exploration" program is designed to simulate an adventure through three different ocean zones: the Sunlit Zone, Twilight Zone, and Midnight Zone. Players navigate through these zones, attempting to collect treasure while encountering various hazards that affect their health. The objective of the game is to collect five treasures without losing all health points, which start at three.
The game begins by asking the player if they want to dive into the ocean. If the player agrees, the program uses the `navigate()` method to randomly select one of the three ocean zones. Each zone has unique characteristics, such as depth, average temperature, and main marine species, which are displayed to the player. In addition, a warning is issued regarding the danger level of the zone, with the Midnight Zone being the most dangerous.
Once the player is in an ocean zone, they are given the choice to attempt to collect treasure. The outcome of this attempt is determined randomly. If the player is successful, they collect a treasure from a predefined list, and their treasure point count increases by one. If the attempt is unsuccessful, the player encounters a hazard, such as a jellyfish sting or a squid attack, which decreases their health points by one. These hazards are also randomly selected from a hazard list.
The game ends if the player's health points reach zero, causing them to lose the game. If this happens, their score is saved, and they are given the option to restart the game with their health and treasure points reset. Alternatively, if the player successfully collects five treasures, they are able to safely return to the surface, ending the game with victory.
The program introduces several important Java concepts. Random selection is implemented using `Math.random()`, which is used to determine which ocean zone the player navigates to and whether they collect treasure or face a hazard. This adds unpredictability to the game, enhancing the player's experience. User input is handled using the `Scanner` class, allowing the player to make decisions throughout the game, such as whether to dive, navigate, or attempt to collect treasure.
The game also makes use of `ArrayList` to store treasures, hazards, and player scores. The dynamic nature of `ArrayList` allows for flexible storage and access to game elements, such as treasure descriptions or hazards. Specific items in the list are accessed using the `get()` method, and new elements are added using the `add()` method. The program’s flow is controlled using conditional logic and loops. `If-else` statements are used to evaluate the player's decisions and game conditions, while nested loops ensure the game continues to prompt the player for choices as long as their health is above zero and their treasure count is less than five.
Managing health and treasure is central to the game. The player’s health is reduced by one point whenever they encounter a hazard, but a method ensures that health doesn’t drop below zero. The player’s treasure count increases as they collect treasures, and the game keeps track of both of these variables to determine when the game ends—either by death or by success. This project involved learning how to handle real-time user input, randomness, conditional logic, and working with arrays and lists in Java, all of which are important concepts for creating interactive programs.
import java.util.Scanner;
import java.util.ArrayList;
public class Underwater {
private ArrayList<Integer> oceansList = new ArrayList<>();
private String name;
private int depth;
private double temperature;
private String species;
public void ocean(String name, int depth, double temperature, String species){
this.name = name;
this.depth = depth;
this.temperature = temperature;
this.species = species;
oceansList.add(1);
oceansList.add(2);
oceansList.add(3);
}
public void oceanInfo(){
System.out.println("Zone Name: " + name);
System.out.println("Depth: " + depth + " meters");
System.out.println("Average Temperature: " + temperature + " °C");
System.out.println("Main Marine Species: " + species);
}
public void navigate(Underwater sunlitZone, Underwater twilightZone, Underwater midnightZone) {
int navigationNumber = (int)((Math.random() * 3) + 1);
Underwater selectedZone;
if (navigationNumber == 1) {
selectedZone = sunlitZone;
System.out.println("Navigating to Sunlit Zone");
selectedZone.oceanInfo();
}
else if (navigationNumber == 2) {
selectedZone = twilightZone;
System.out.println("Navigating to Twilight Zone");
selectedZone.oceanInfo();
}
else {
selectedZone = midnightZone;
System.out.println("Navigating to Midnight Zone");
selectedZone.oceanInfo();
}
if (selectedZone == sunlitZone) {
System.out.println("Be careful! Sunlit Zone might be dangerous!");
} else if (selectedZone == twilightZone) {
System.out.println("Be careful! Twilight Zone is dangerous!");
} else if (selectedZone == midnightZone) {
System.out.println("Be careful! Midnight Zone is very dangerous!");
}
}
public static void main(String[] args) {
boolean isOcean = true;
int treasurePoint = 0;
int healthPoint = 3;
ArrayList<String> treasureList = new ArrayList<>();
treasureList.add("You found a Treasure Chest. + 1 Treasure Collected");
treasureList.add("You found a Black Pearl. + 1 Treasure Collected");
treasureList.add("You found a Golden Watch. + 1 Treasure Collected");
treasureList.add("You found a Golden Bracelet. + 1 Treasure Collected");
treasureList.add("You found a Diamond Ring. + 1 Treasure Collected");
ArrayList<String> hazardList = new ArrayList<>();
hazardList.add("Watch Out! There is a shark attack! - 1 Health Point");
hazardList.add("Watch Out! There is a jellyfish sting! - 1 Health Point");
hazardList.add("Watch Out! There is a thunderstorm! - 1 Health Point");
hazardList.add("Watch Out! There is a dangerous current! - 1 Health Point");
hazardList.add("Watch Out! There is a poisonous fish! - 1 Health Point");
while (isOcean && healthPoint > 0) {
Scanner input = new Scanner(System.in);
System.out.print("Enter ocean zone (Sunlit, Twilight, Midnight): ");
String zoneChoice = input.nextLine();
Underwater sunlitZone = new Underwater();
Underwater twilightZone = new Underwater();
Underwater midnightZone = new Underwater();
sunlitZone.ocean("Sunlit Zone", 200, 24.0, "Coral Reef");
twilightZone.ocean("Twilight Zone", 1000, 5.0, "Squid");
midnightZone.ocean("Midnight Zone", 4000, 2.0, "Giant Squid");
if (zoneChoice.equals("Sunlit")) {
sunlitZone.navigate(sunlitZone, twilightZone, midnightZone);
}
else if (zoneChoice.equals("Twilight")) {
twilightZone.navigate(sunlitZone, twilightZone, midnightZone);
}
else if (zoneChoice.equals("Midnight")) {
midnightZone.navigate(sunlitZone, twilightZone, midnightZone);
} else {
System.out.println("Invalid choice, try again.");
continue;
}
int randomEvent = (int)(Math.random() * 2);
if (randomEvent == 0) {
System.out.println(hazardList.get((int)(Math.random() * hazardList.size())));
healthPoint--;
}
else {
System.out.println(treasureList.get((int)(Math.random() * treasureList.size())));
treasurePoint++;
}
System.out.println("Treasure Points: " + treasurePoint);
System.out.println("Health Points: " + healthPoint);
if (healthPoint <= 0) {
System.out.println("You have no health left! Game Over!");
isOcean = false;
}
else {
System.out.print("Do you want to continue? (yes/no): ");
String continueChoice = input.nextLine();
if (!continueChoice.equalsIgnoreCase("yes")) {
isOcean = false;
}
}
}
}
}