about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorelioat <elioat@tilde.institute>2023-12-28 20:20:52 -0500
committerelioat <elioat@tilde.institute>2023-12-28 20:20:52 -0500
commit42ec1a26d1e2c671d0f64aaf14a5025910d95c9c (patch)
treefc834a310710d6dc5eec2b84b62fd78510af7310
parent37b9b5ad35f2d49f02914b4df82c2432883d6412 (diff)
downloadtour-42ec1a26d1e2c671d0f64aaf14a5025910d95c9c.tar.gz
*
-rw-r--r--js/text/game.js433
1 files changed, 433 insertions, 0 deletions
diff --git a/js/text/game.js b/js/text/game.js
new file mode 100644
index 0000000..668b76d
--- /dev/null
+++ b/js/text/game.js
@@ -0,0 +1,433 @@
+const readline = require('readline');
+// Define a room function that represents a room in the game
+// A room has a name, description, and exits to other rooms
+// A room can be locked or unlocked
+// A room can contain items
+// A room can contain a monster to talk with or fight
+function createRoom(name, description, exits, locked, items, monster) {
+  return {
+    name,
+    description,
+    exits,
+    locked,
+    items,
+    monster
+  };
+}
+
+// Define a monster function that represents a monster in the game
+// A monster has a name, description, and a list of items it can drop
+// A monster can be talked to or fought
+function createMonster(name, description, items) {
+  return {
+    name,
+    description,
+    items
+  };
+}
+
+// Define an item function that represents an item in the game
+// An item has a name, description, and a value
+// An item can be picked up and dropped
+function createItem(name, description, value) {
+  return {
+    name,
+    description,
+    value
+  };
+}
+
+// Define a player function that represents the player in the game
+// A player has a name, description, and a list of items
+// A player can pick up and drop items
+// A player can talk to monsters
+// A player can fight monsters
+// A player has a current location
+// A player has a history of all past locations where they've been
+// A player can move to a different room
+function createPlayer(name, description, items, location, history) {
+  return {
+    name,
+    description,
+    items,
+    location,
+    history
+  };
+}
+
+// Define a game function that represents the game
+// A game has a player and a list of rooms
+// A game can be started
+// A game can be saved
+// A game can be loaded
+// A game can be ended
+function createGame(player, rooms) {
+  return {
+    player,
+    rooms
+  };
+}
+
+// Define a function that starts the game
+function startGame() {
+  // Create the game
+  const game = createGame(
+    // Create the player
+    createPlayer(
+      "Player 1",
+      "This is you",
+      [],
+      "room1",
+      []
+    ),
+    // Create the rooms
+    [
+      createRoom(
+        "room1",
+        "This is room 1",
+        {
+          north: "room2"
+        },
+        false,
+        [],
+        null
+      ),
+      createRoom(
+        "room2",
+        "This is room 2",
+        {
+          south: "room1"
+        },
+        false,
+        [],
+        null
+      )
+    ]
+  );
+
+  // Start the game loop
+  gameLoop(game);
+}
+
+// Define a function that loops the game
+// Define a function that displays the current location
+function displayLocation(game) {
+  // Get the current location
+  const location = game.player.location;
+
+  // Get the current room
+  const room = game.rooms.find(room => room.name === location);
+
+  // Display the current room
+  console.log(room.description);
+}
+
+// Define a function that displays the current inventory
+function displayInventory(game) {
+  // Get the current inventory
+  const inventory = game.player.items;
+
+  // Display the current inventory
+  console.log(`You are carrying: ${inventory.join(", ")}`);
+}
+
+// Define a function that displays the current history
+function displayHistory(game) {
+  // Get the current history
+  const history = game.player.history;
+
+  // Display the current history
+  console.log(`History: ${history.join(", ")}`);
+}
+
+// Define a function that processes the user input
+function processInput(game, input) {
+  // Process the user input here
+  // ...
+}
+
+// Define a function that loops the game
+function gameLoop(game) {
+  // Display the current location
+  displayLocation(game);
+
+  // Display the current inventory
+  displayInventory(game);
+
+  // Display the current history
+  displayHistory(game);
+
+  // Create a readline interface for reading user input
+  const rl = readline.createInterface({
+    input: process.stdin,
+    output: process.stdout
+  });
+
+  // Prompt the user for input
+  rl.question("What would you like to do? ", (input) => {
+    // Close the readline interface
+    rl.close();
+
+    // Process the user input
+    processInput(game, input);
+  });
+}
+
+// Start the game
+startGame();
+
+// Define a function that displays the current location
+function displayLocation(game) {
+  // Get the current location
+  const location = game.player.location;
+
+  // Get the current room
+  const room = game.rooms.find(room => room.name === location);
+
+  // Display the current room
+  console.log(room.description);
+}
+
+// Define a function that displays the current inventory
+function displayInventory(game) {
+  // Get the current inventory
+  const inventory = game.player.items;
+
+  // Display the current inventory
+  console.log(`You are carrying: ${inventory.join(", ")}`);
+}
+
+// Define a function that displays the current history
+function displayHistory(game) {
+  // Get the current history
+  const history = game.player.history;
+
+  // Display the current history
+  console.log(`You have been to: ${history.join(", ")}`);
+}
+
+// Define a function that processes the user input
+function processInput(game, input) {
+  // Split the input into a command and a target
+  const [command, target] = input.split(" ");
+
+  // Process the command
+  switch (command) {
+    case "go":
+      // Process the go command
+      processGoCommand(game, target);
+      break;
+    case "get":
+      // Process the get command
+      processGetCommand(game, target);
+      break;
+    case "drop":
+      // Process the drop command
+      processDropCommand(game, target);
+      break;
+    case "talk":
+      // Process the talk command
+      processTalkCommand(game, target);
+      break;
+    case "fight":
+      // Process the fight command
+      processFightCommand(game, target);
+      break;
+    case "save":
+      // Process the save command
+      processSaveCommand(game);
+      break;
+    case "load":
+      // Process the load command
+      processLoadCommand(game);
+      break;
+    case "quit":
+      // Process the quit command
+      processQuitCommand();
+      break;
+    default:
+      // Process the unknown command
+      processUnknownCommand(game);
+      break;
+  }
+}
+
+
+// Define a function that processes the go command
+function processGoCommand(game, target) {
+  // Get the current location
+  const location = game.player.location;
+
+  // Get the current room
+  const room = game.rooms.find(room => room.name === location);
+
+  // Check if the target is a valid exit
+  if (room.exits[target]) {
+    // Update the player's location
+    game.player.location = room.exits[target];
+
+    // Update the player's history
+    game.player.history.push(room.exits[target]);
+
+    // Display the current location
+    displayLocation(game);
+  } else {
+    // Display an error message
+    console.log("You can't go that way!");
+  }
+
+  // Loop the game
+  gameLoop(game);
+}
+
+// Define a function that processes the get command
+function processGetCommand(game, target) {
+  // Get the current location
+  const location = game.player.location;
+
+  // Get the current room
+  const room = game.rooms.find(room => room.name === location);
+
+  // Check if the target is a valid item
+  if (room.items.includes(target)) {
+    // Remove the item from the room
+    room.items.splice(room.items.indexOf(target), 1);
+
+    // Add the item to the player's inventory
+    game.player.items.push(target);
+
+    // Display the current inventory
+    displayInventory(game);
+  } else {
+    // Display an error message
+    console.log("You can't get that!");
+  }
+
+  // Loop the game
+  gameLoop(game);
+}
+
+// Define a function that processes the drop command
+function processDropCommand(game, target) {
+  // Get the current location
+  const location = game.player.location;
+
+  // Get the current room
+  const room = game.rooms.find(room => room.name === location);
+
+  // Check if the target is a valid item
+  if (game.player.items.includes(target)) {
+    // Remove the item from the player's inventory
+    game.player.items.splice(game.player.items.indexOf(target), 1);
+
+    // Add the item to the room
+    room.items.push(target);
+
+    // Display the current inventory
+    displayInventory(game);
+  } else {
+    // Display an error message
+    console.log("You can't drop that!");
+  }
+
+  // Loop the game
+  gameLoop(game);
+}
+
+// Define a function that processes the talk command
+function processTalkCommand(game, target) {
+  // Get the current location
+  const location = game.player.location;
+
+  // Get the current room
+  const room = game.rooms.find(room => room.name === location);
+
+  // Check if the room has a monster
+  if (room.monster) {
+    // Display the monster's description
+    console.log(room.monster.description);
+  } else {
+    // Display an error message
+    console.log("There is no one to talk to!");
+  }
+
+  // Loop the game
+  gameLoop(game);
+}
+
+// Define a function that processes the fight command
+function processFightCommand(game, target) {
+  // Get the current location
+  const location = game.player.location;
+
+  // Get the current room
+  const room = game.rooms.find(room => room.name === location);
+
+  // Check if the room has a monster
+  if (room.monster) {
+    // Display the monster's description
+    console.log(room.monster.description);
+  } else {
+    // Display an error message
+    console.log("There is no one to fight!");
+  }
+
+  // Loop the game
+  gameLoop(game);
+}
+
+// Define a function that processes the save command
+function processSaveCommand(game) {
+  // Save the game
+  localStorage.setItem("game", JSON.stringify(game));
+
+  // Display a success message
+  console.log("Game saved!");
+
+  // Loop the game
+  gameLoop(game);
+}
+
+// Define a function that processes the load command
+function processLoadCommand(game) {
+  // Load the game
+  const savedGame = JSON.parse(localStorage.getItem("game"));
+
+  // Check if a saved game was found
+  if (savedGame) {
+    // Display a success message
+    console.log("Game loaded!");
+
+    // Loop the game
+    gameLoop(savedGame);
+  } else {
+    // Display an error message
+    console.log("No saved game found!");
+
+    // Loop the game
+    gameLoop(game);
+  }
+}
+
+// Define a function that processes the quit command
+function processQuitCommand() {
+  // Display a goodbye message
+  console.log("Goodbye!");
+
+  // Quit the game
+  return;
+}
+
+// Define a function that processes the unknown command
+function processUnknownCommand(game) {
+  // Display an error message
+  console.log("Unknown command!");
+
+  // Loop the game
+  gameLoop(game);
+}
+
+// Start the game
+startGame();
+
+// End of game.js