0% found this document useful (0 votes)
91 views

Code

This document contains the code for a text-based Java game where the player battles randomly generated enemies in a dungeon. The player can attack enemies, use health potions, or run away. If the player's health drops to 0 or they choose to exit the dungeon, the game ends. The code initializes variables for the player's health, damage, and inventory as well as enemies' health and damage. It uses random numbers to determine outcomes and contains the game loop logic.

Uploaded by

Moodyy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
91 views

Code

This document contains the code for a text-based Java game where the player battles randomly generated enemies in a dungeon. The player can attack enemies, use health potions, or run away. If the player's health drops to 0 or they choose to exit the dungeon, the game ends. The code initializes variables for the player's health, damage, and inventory as well as enemies' health and damage. It uses random numbers to determine outcomes and contains the game loop logic.

Uploaded by

Moodyy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

import java.util.

Random;
import java.util.Scanner;

public class Main {


public static final String ANSI_RESET = "\u001B[0m";
public static final String ANSI_RED = "\u001b[31;1m";
public static final String ANSI_YELLOW = "\u001b[33;1m";

public static void main(String[] args) {

// system objects
Scanner in = new Scanner(System.in);
Random rand = new Random();

// game variables
String[] enemies = { "Skelton", "Zombie", "Warrior", "Assassin" };
int maxEnemyHealth = 75;
int enemyAttackDamage = 25;

// player variables
int health = 100;
int attackDamage = 50;
int numHealthPotions = 3;
int healthPotionHealAmount = 30;
int healthPotionDropChance = 50; // percentage

boolean running = true;


System.out.println("Welcome to the Dungeons!");
System.out.println("This game was designed by :");
System.out.println("20CS083-Jiya Shah");
System.out.println("20CS098-Kanak Tripathi");
System.out.println("20CS101-Shelin Vankawala");

GAME: while (running) {

System.out.println("--------------------------------------------------");

int enemyHealth = rand.nextInt(maxEnemyHealth);


String enemy = enemies[rand.nextInt(enemies.length)];
System.out.println("\t# " + enemy + " appeared : #\n");

while (enemyHealth > 0) {


System.out.println(ANSI_RED + "\tYour HP : " + health + "\t" +
ANSI_RESET);
System.out.println(ANSI_RED + "\t" + enemy + "'s HP : " +
enemyHealth + "\t" + ANSI_RESET);
System.out.println("\n\tWhat would you like to do?");
System.out.println("\t1. Attack");
System.out.println("\t2. Drink health potion");
System.out.println("\t3. Run!");
String input = in.nextLine();

// different actions based on user input


if (input.equals("1")) {
int damageDealt = rand.nextInt(attackDamage);
int damageTaken = rand.nextInt(enemyAttackDamage);
enemyHealth -= damageDealt;
health -= damageTaken;
System.out.println("\t> You strike the " + enemy + " for " +
damageDealt + " damage. ");
System.out.println("\t> You recieve " + damageTaken + " in
retaliation!");
if (health < 1) {
System.out.println("\t> You have taken too much damage, you
rae too weak to go on!");
break;
}
} else if (input.equals("2")) {

// calculation logic for number of health potions


if (numHealthPotions > 0) {
health += healthPotionHealAmount;
numHealthPotions--;
System.out.println("\t> You drink a health potion, healing
yourself for "
+ healthPotionHealAmount + "\n\t> You now have " +
health + " HP" + "\n\t You have "
+ numHealthPotions + " health potions left.\n");
} else {
System.out.println(" \t> You have health potions left!
Defeat your enemies for a chance");
}

} else if (input.equals("3")) {

System.out.println("\t You run away from the " + enemy + "!");


continue GAME;
} else {
System.out.println("\tInvalid command!");
}
}

//condition for the game to end


if (health < 1) {
System.out.println(ANSI_YELLOW + "You limp out of the dungeon, weak
from battle" + ANSI_RESET);
break;
}
System.out.println("--------------------------------------------");
System.out.println(" # " + enemy + " was defeated! # ");
System.out.println(" # You have " + health + " HP left. #");
if (rand.nextInt(100) < healthPotionDropChance) {
numHealthPotions++;
System.out.println(" # The " + enemy + " dropped a health potion! #
");
System.out.println(" # You may have " + numHealthPotions + " health
potion(s). #");
}

System.out.println("--------------------------------------------------");
System.out.println("What would you like to do now?");
System.out.println("1. Continue fighting");
System.out.println("2. Exit dungeon");
String input = in.nextLine();
while (!input.equals("1") && !input.equals("2")) {
System.out.println("Invalid command!");
input = in.nextLine();
}
if (input.equals("1")) {
System.out.println("You continue on your adventure!");
} else if (input.equals("2")) {
System.out.println(ANSI_YELLOW + "You exit the dungeon, successful
from your adventure!" + ANSI_RESET);
break;
}
}
}
}

You might also like