You are on page 1of 5

import java.util.

Random;
import java.util.Scanner;

public class lol {

// Declare variables
static int width;
static int height;
static int[][] room;
static int playerX;
static int playerY;
static int bugX;
static int bugY;
static int goldX;
static int goldY;
static int holeX;
static int holeY;
static boolean hasGold;

static int steps;


static int inputs;

public static void main(String[] args) {

// Initialize the game


initialize();

// Game loop
while(true) {

// Print the room


printRoom();

// Get user input


String move = getInput();
inputs++;

// Process the move


processMove(move);

// Check game conditions


if(checkConditions()) {
break;
}

steps++;
}

// Print the final message


printFinalMessage();

// Initialize the game


public static void initialize() {
// Generate the room
generateRoom();

// Set initial positions


playerX = randomPosition();
playerY = randomPosition();

bugX = randomPosition();
bugY = randomPosition();

goldX = randomPosition();
goldY = randomPosition();

holeX = randomPosition();
holeY = randomPosition();

hasGold = false;

steps = 0;
inputs = 0;
}

// Get user input


public static String getInput() {
try (Scanner in = new Scanner(System.in)) {
System.out.print("Next move (dir steps): ");
return in.nextLine();
}
}

// Process the player's move


public static void processMove(String input) {
// Validate input
String[] parts = input.split(" ");
if(parts.length != 2) {
System.out.println("Invalid input");
return;
}

String dir = parts[0];


int spaces = Integer.parseInt(parts[1]);

if(spaces < 1) {
System.out.println("Steps must be 1 or greater");
return;
}

// Update player position


switch(dir) {
case "left": playerX -= spaces; break;
case "right": playerX += spaces; break;
case "up": playerY -= spaces; break;
case "down": playerY += spaces; break;
default:
System.out.println("Invalid direction");
return;
}

// Keep player in bounds


playerX = Math.max(0, Math.min(playerX, width-1));
playerY = Math.max(0, Math.min(playerY, height-1));

// Update bug position


int[] dirs = {-1, 0, 1};
int dx = dirs[new Random().nextInt(3)];
int dy = dirs[new Random().nextInt(3)];

bugX += dx;
bugY += dy;

// Keep bug in bounds


bugX = Math.max(0, Math.min(bugX, width-1));
bugY = Math.max(0, Math.min(bugY, height-1));

// Update gold position if the player has it


if(hasGold) {
goldX = playerX;
goldY = playerY;
hasGold = false;
}
}

// Check game conditions


public static boolean checkConditions() {
// Did the player win or lose?
if(playerX == bugX && playerY == bugY) {
return true;
}
if(playerX == holeX && playerY == holeY && hasGold) {
return true;
}
return false;
}

// Generate the room


public static void generateRoom() {
// Set random size
width = 5 + new Random().nextInt(10);
height = 5 + new Random().nextInt(10);

// Initialize 2D array
room = new int[height][width];

// Add walls
for(int i = 0; i < width; i++){
room[0][i] = 1;
room[height-1][i] = 1;
}

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


room[i][0] = 1;
room[i][width-1] = 1;
}

// Fill with empty floor tiles


for(int r = 1; r < height-1; r++){
for(int c = 1; c < width-1; c++){
room[r][c] = 0;
}
}
}
// Print the room
public static void printRoom() {
// Print top wall
System.out.print("=".repeat(width*2+1));
System.out.println();

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


System.out.print("|");

for(int x = 0; x < width; x++) {


if(x == playerX && y == playerY) {
System.out.print("@");
}
else if(x == bugX && y == bugY) {
System.out.print("X");
}
else if(x == goldX && y == goldY) {
System.out.print("$");
}
else if(x == holeX && y == holeY) {
System.out.print("O");
}
else if(room[y][x] == 1) {
System.out.print("#"); // Wall
}
else {
System.out.print(" "); // Empty floor
}
}

System.out.println("|");
}

// Print bottom wall


System.out.print("=".repeat(width*2+1));
System.out.println();
}

// Print the final message


public static void printFinalMessage() {
System.out.println();

if(playerX == bugX && playerY == bugY) {


System.out.println("You were caught by the bug! Game over!");
}
else if(playerX == holeX && playerY == holeY && hasGold) {
System.out.println("You escaped with the gold! You win!");
}
else {
System.out.println("Out of steps... Game over!");
}

System.out.println();
System.out.println("GAME STATS:");
System.out.println("Inputs: " + inputs);
System.out.println("Steps Taken: " + steps);
}

// Additional helper method to get a random position


public static int randomPosition() {
return new Random().nextInt(width);
}
}

You might also like