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.
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.
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.
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.
Download as TXT, PDF, TXT or read online from Scribd
Download as txt, pdf, or txt
You are on page 1of 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("#"); }