Sunday, July 27, 2025

TEXT ADVENTURE GAME(IN JAVA)

 package GAME1;


import java.util.*; //importing for user input


public class GAME2 {

// making static variables to be used in full game

static Scanner s = new Scanner(System.in);

static int playerHp;

static String playerName;

static String playerWeapon;

static Random rc = new Random();

static int monsterHp = rc.nextInt(9, 16);

static int siverRing;


public static void main(String[] args) {

playerSetup();

TownGate(); // Start game at town gate

}


public static void playerSetup() {

playerHp = 10;

playerWeapon = "AXE"; // Remove extra spaces for string matching

System.out.println("YOUR HP IS " + playerHp);

System.out.println("YOUR WEAPON IS " + playerWeapon);

System.out.println("ENTER YOUR NAME PLEASE");

playerName = s.nextLine();

System.out.println("Hello, " + playerName + ". Let's start your adventure!");

}

// starting of game

public static void TownGate() {

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

System.out.println("You are at the town gate.");

System.out.println("A guard is in front of the gate.");

System.out.println("What do you want to do?");

System.out.println("1. Talk to the guard");

System.out.println("2. Attack the guard");

System.out.println("3. Leave");


int choice = s.nextInt();

s.nextLine();


if (choice == 1) {

System.out.println("Guard: Hello there, stranger. So, your name is " + playerName + "?\nSorry, we cannot let a stranger enter our town.");

TownGate();

} else if (choice == 2) {

playerHp -= 1;

System.out.println("Guard: What the hell?! Don't be stupid!");

System.out.println("The guard hits you hard. You lose 1 HP.");

System.out.println("Your HP: " + playerHp);

TownGate();

} else if (choice == 3) {

CrossRoad();

} else {

TownGate();

}

}

//At crossroad

public static void CrossRoad() {

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

System.out.println("You are at a crossroad.");

System.out.println("1. Go north");

System.out.println("2. Go south");

System.out.println("3. Go east");

System.out.println("4. Go west");


int option = s.nextInt();

s.nextLine(); // consume newline


if (option == 1) {

north();

} else if (option == 2) {

south();

} else if (option == 3) {

east();

} else if (option == 4) {

west();

} else {

CrossRoad();

}

}

//going to north and hp retaining mechanism

public static void north() {

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

System.out.println("You find a peaceful river. You rest and drink some water.");

if (playerHp < 10) {

playerHp += 1;

System.out.println("You have recovered 1 HP. Your HP is now " + playerHp);

} else {

System.out.println("Your HP is already full.");

}

System.out.println("1. Go back to the crossroad");

System.out.println("2. Stay here ");

int choice = s.nextInt();

s.nextLine();

if (choice == 1) {

CrossRoad();

} else {

north();

}

}

//go to south and visit town again

public static void south() {

TownGate();

}

//finding new weapon in east

public static void east() {

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

System.out.println("You walked into a forest and found a Long Sword!");

playerWeapon = "Long Sword";

System.out.println("Your new weapon is: " + playerWeapon);

System.out.println("1. Go back to the crossroad");

int choice = s.nextInt();

s.nextLine();

if (choice == 1) {

CrossRoad();

} else {

east();

}

}

//fighting enemy in west

public static void west() {

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

System.out.println("You encounter a goblin!");

System.out.println("1. Fight");

System.out.println("2. Run");


int choice = s.nextInt();

s.nextLine();

if (choice == 1) {

fight();

} else if (choice == 2) {

CrossRoad();

} else {

west();

}

}


public static void fight() {

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

System.out.println("Your HP: " + playerHp);

System.out.println("Goblin HP: " + monsterHp);

System.out.println("1. Attack");

System.out.println("2. Run");


int choice = s.nextInt();

s.nextLine();

if (choice == 1) {

attack();

} else if (choice == 2) {

CrossRoad();

} else {

fight();

}

}

//attack and fighting mechanism

public static void attack() {

int weaponDamage = 0;

if (playerWeapon.equals("AXE")) {

weaponDamage = rc.nextInt(1, 6); // 1-5

} else if (playerWeapon.equals("Long Sword")) {

weaponDamage = rc.nextInt(4, 10); // 4-9

}


System.out.println("You attack the goblin and deal " + weaponDamage + " damage.");

monsterHp -= weaponDamage;


if (monsterHp < 1) {

win();

return;

}


System.out.println("Goblin HP is now: " + monsterHp);


// Monster counterattacks

int monsterDamage = rc.nextInt(1, 5);

playerHp -= monsterDamage;

System.out.println("The goblin attacks you back and deals " + monsterDamage + " damage.");

System.out.println("Your HP is now: " + playerHp);

s.nextLine();


if (playerHp < 1) {

lose();

} else {

fight();

}

}

//on defeating goblin

public static void win() {

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

System.out.println("You defeated the goblin!");

System.out.println("He dropped a ring");

System.out.println("You got a siver ring");

siverRing=1;

System.out.println("1.go south to town");

int choice=s.nextInt();

if(choice==1) {

Town();

}else {

win();

}

monsterHp = rc.nextInt(5, 11); // Reset for another fight if needed

CrossRoad();

}


//on getting defeated by goblin

public static void lose() {

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

System.out.println("You have been defeated by the goblin...");

System.out.println("GAME OVER");

System.exit(0);

}//after defeating goblin and arriving at town

public static void Town() {

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

System.out.println("you go to town and go to guard ");

System.out.println("Would you like to:\n1.show your ring\n2.leave");

int choice=s.nextInt();

if(choice==1) {

TownElder();

}

else if(choice==2) {

CrossRoad();

}else {

Town();

}

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


}


public static void TownElder() {

System.out.println("You show silver ring to guard \n he looks surprised!!!!!!!!!!!!!!!");

System.out.println("He takes you directly to TownELder,te head of town");

System.out.println("TownElder sees your ring and says\n\"this ring is very imprtant for us as it was crafted by our ancestors\nand it has been preserved by us for generations");

System.out.println("Recently,a very powerful goblin stole it from us and we could not get it back");

System.out.println("We would like you to become out town's protector,so say,what was your name again?" +playerName+ " yeah,so "+playerName+" ,would you like to be our protector");

System.out.println("what would you do?\n1.become the protector\n2.leave the town");

int choice=s.nextInt();

if(choice==1) {

ending();

}

else if(choice==2) {

CrossRoad();

}

else {

TownElder();

}

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


}

//ending of game

public static void ending() {

System.out.println("So,from here now you will be given your title according to your choice,choose one");

System.out.println("Choose one from them-:\n1.The Silver Lion of Valor\n2.Knight of the Eternal Oath\n3.Warden of the Broken Sigil\n4.The Crimson Bladebearer\n5.Champion of the Hollow Crown");

String playerTitle="";

int choice=s.nextInt();

switch(choice) {

case 1:playerTitle="The Silver Lion of Valor";

break;

case 2:playerTitle="Knight of the Eternal Oath";

break;

case 3:playerTitle="Warden of the Broken Sigil";

break;

case 4:playerTitle="The Crimson Bladebearer";

break;

case 5:playerTitle="Champion of the Hollow Crown";

break;

default:

System.out.println("Could not decide one,huh!,well I have a name for you,you get the title of Spadidus,meaning\"descending from sparda\" ,the warrior who founded this town ");

playerTitle="Spadidus";

}

System.out.println("So,now you hold the title of "+playerTitle);

System.out.println("congratulations,you won " +playerName+ " with the title of "+playerTitle);

System.out.println("GAME OVER");

System.exit(0);

}

}

No comments:

Post a Comment

list in c#

  using System ; using System . Collections . Generic ; class Program {     static void Main () {         // Write your C# code here  ...