0% found this document useful (0 votes)
36 views3 pages

CSC Ass

This document contains the code for a text-based adventure game. It initializes the game by randomly generating the size of the room and locations of the adventurer, grid bug, gold, and hole. It then prints the room and gets player input for moving the adventurer. The main game loop continuously prints the room, gets input, moves entities, and checks for winning/losing conditions until the game ends.

Uploaded by

Anjola Adeyemi
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)
36 views3 pages

CSC Ass

This document contains the code for a text-based adventure game. It initializes the game by randomly generating the size of the room and locations of the adventurer, grid bug, gold, and hole. It then prints the room and gets player input for moving the adventurer. The main game loop continuously prints the room, gets input, moves entities, and checks for winning/losing conditions until the game ends.

Uploaded by

Anjola Adeyemi
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 App {

static int width;


static int height;
static int adventurerX;
static int adventurerY;
static int gridBugX;
static int gridBugY;
static int goldX;
static int goldY;
static int holeX;
static int holeY;
static int steps;
static int inputs;

public static void main(String[] args) {


initialize();
printRoom();
gameLoop();
}

// Part 1: Game Initialization


public static void initialize() {
// Randomize the width and height of the room
width = 5 + new Random().nextInt(10);
height = 5 + new Random().nextInt(10);

// Randomize the starting location for each entity


do {
adventurerX = new Random().nextInt(width);
adventurerY = new Random().nextInt(height);
gridBugX = new Random().nextInt(width);
gridBugY = new Random().nextInt(height);
goldX = new Random().nextInt(width);
goldY = new Random().nextInt(height);
holeX = new Random().nextInt(width);
holeY = new Random().nextInt(height);
} while ((adventurerX == gridBugX && adventurerY == gridBugY) ||
(adventurerX == goldX && adventurerY == goldY) || (goldX == holeX && goldY ==
holeY));
}

// Part 2: Printing the Room


public static void printRoom() {
// Print top wall
System.out.print("#".repeat(width + 2));
System.out.println();

for (int y = 0; y < height; y++) {


System.out.print("#");
for (int x = 0; x < width; x++) {
if (x == adventurerX && y == adventurerY) {
System.out.print("@");
} else if (x == gridBugX && y == gridBugY) {
System.out.print("x");
} else if (x == goldX && y == goldY) {
System.out.print("$");
} else if (x == holeX && y == holeY) {
System.out.print("^");
} else {
System.out.print(".");
}
}
System.out.println("#");
}

// Print bottom wall


System.out.print("#".repeat(width + 2));
System.out.println();
}

// Part 3: Getting Player Input


public static void getPlayerInput() {
try (Scanner scanner = new Scanner(System.in)) {
System.out.print("Enter direction and number of steps (e.g. up 3): ");
if (scanner.hasNextLine()) {
String input = scanner.nextLine();
String[] parts = input.split(" ");
if (parts.length != 2) {
System.out.println("Invalid input");
return;
}
String direction = parts[0];
switch (direction) {
case "up":
moveAdventurer(0, -steps);
break;
case "down":
moveAdventurer(0, steps);
break;
case "left":
moveAdventurer(-steps, 0);
break;
case "right":
moveAdventurer(steps, 0);
break;
default:
System.out.println("Invalid direction");
break;
}
}else {
System.out.println("No input found");
}
} catch (NumberFormatException e) {
e.printStackTrace();
}
inputs++;
}

private static void moveAdventurer(int i, int j) {


}

// Part 4: The Game Logic


public static void gameLoop() {
while (true) {
printRoom();
getPlayerInput();
moveGridBug();
if (adventurerX == gridBugX && adventurerY == gridBugY) {
System.out.println("You were caught by the bug! Game over!");
break;
} else if (adventurerX == goldX && adventurerY == goldY) {
System.out.println("You picked up the gold!");
goldX = -1;
goldY = -1;
} else if (adventurerX == holeX && adventurerY == holeY && goldX == -1
&& goldY == -1) {
System.out.println("You escaped with the gold! You win!");
break;
}
steps++;
System.out.println("Steps taken: " + steps);
System.out.println("Inputs: " + inputs);
System.out.println("Press Enter to continue...");
try (Scanner scanner = new Scanner(System.in)) {
if (scanner.hasNextLine()) {
scanner.nextLine();
}
}

}
}

private static void moveGridBug() {


}

You might also like