import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.List;
public class Game {
private Room currentRoom;
private Scanner scanner;
public static void main(String[] args) {
Game game = new Game();
game.start();
}
public Game() {
setupGame();
scanner = new Scanner(System.in);
}
private void setupGame() {
// Create rooms and objects
Room hall = new Room("Grand Hall");
Room dyingRoom = new Room("Dying Room");
Mirror mirror = new Mirror();
HauntedVase hauntedVase = new HauntedVase();
hall.addObject(mirror);
dyingRoom.addObject(hauntedVase);
// Link rooms - for simplicity, let's just go back and forth
hall.setAdjacentRoom(dyingRoom);
dyingRoom.setAdjacentRoom(hall);
currentRoom = hall; // Starting room
}
public void start() {
System.out.println("Welcome to the Mysterious Mansion!");
boolean playing = true;
while (playing) {
System.out.println("\nYou are in the " + currentRoom.getName());
System.out.println("Objects in the room:");
List<GameObject> objects = currentRoom.getObjects();
for (int i = 0; i < objects.size(); i++) {
System.out.println((i + 1) + ": " + objects.get(i).getDescription());
}
System.out.println((objects.size() + 1) + ": Move to the adjacent room");
System.out.println((objects.size() + 2) + ": Exit game");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();
if (choice > 0 && choice <= objects.size()) {
inspectObject(objects.get(choice - 1));
} else if (choice == objects.size() + 1) {
currentRoom = currentRoom.getAdjacentRoom();
} else if (choice == objects.size() + 2) {
playing = false;
System.out.println("Exiting Mysterious Mansion. Goodbye!");
} else {
System.out.println("Invalid choice. Try again.");
}
}
}
private void inspectObject(GameObject object) {
Class<?> cls = object.getClass();
System.out.println("Inspecting: " + object.getDescription());
Field[] fields = cls.getDeclaredFields();
for (Field field : fields) {
field.setAccessible(true);
try {
System.out.println("Field: " + field.getName() + ", Value: " + field.get(object));
} catch (IllegalAccessException e) {
System.out.println("Could not access field: " + field.getName());
}
}
Method[] methods = cls.getDeclaredMethods();
for (Method method : methods) {
method.setAccessible(true);
if (method.getParameterCount() == 0) {
try {
System.out.println("Invoking method: " + method.getName());
method.invoke(object);
} catch (Exception e) {
System.out.println("Could not invoke method: " + method.getName());
}
}
}
}
class Room {
private String name;
private List<GameObject> objects;
private Room adjacentRoom; // Added for room navigation
public Room(String name) {
this.name = name;
this.objects = new ArrayList<>();
}
public void addObject(GameObject object) {
objects.add(object);
}
public List<GameObject> getObjects() {
return objects;
}
public String getName() {
return name;
}
public void setAdjacentRoom(Room room) {
this.adjacentRoom = room;
}
public Room getAdjacentRoom() {
return adjacentRoom;
}
}
class GameObject {
private String description;
public GameObject(String description) {
this.description = description;
}
public String getDescription() {
return description;
}
}
class Mirror extends GameObject {
private String secretMessage = "The key is in the vase!";
public Mirror() {
super("An ancient mirror with mysterious carvings.");
}
private void revealSecret() {
System.out.println(secretMessage);