Advertisement
Guest User

Game.java

a guest
Nov 21st, 2018
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.63 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. /**
  4.  * Handles the Game loop, and Game logic along with input/ responses
  5.  */
  6. public class Game {
  7.     private boolean running = true;
  8.     public int playX;
  9.     public int playY;
  10.     Entity e1;
  11.     Item item;
  12.     private Scanner input = new Scanner(System.in);
  13.  
  14.     /**
  15.      * Constructor for the Game loop / logic
  16.      *
  17.      * @param world The world the game interacts with
  18.      */
  19.     public Game(World world) {
  20.         while (running) {
  21.  
  22.             //Prompt and input
  23.             System.out.println("\nYou are located in the " + world.world[playX][playY].name);
  24.             System.out.print(": ");
  25.             String command = input.next().toLowerCase();
  26.  
  27.             //Perspective/ Player movement
  28.             switch (command) {
  29.                 case "east":
  30.                 case "e":
  31.                     if (world.world[playX + 1][playY] != null
  32.                             && playX + 1 < world.world.length) {
  33.                         playX += 1;
  34.                     } else {
  35.                         System.out.println("You cannot go that way.");
  36.                     }
  37.                     break;
  38.                 case "west":
  39.                 case "w":
  40.                     if (playX > 0) {             //NOTE: Kept throwing an out of bounds for 2D array, just did plain greater than bounds check
  41.                         playX -= 1;              //NOTE: Wanted to do (world.world[playX-1][playY] != null) but no go
  42.                     } else {
  43.                         System.out.println("You cannot go that way.");
  44.                     }
  45.                     break;
  46.                 case "north":
  47.                 case "n":
  48.                     if (playY > 0) {            //NOTE: Kept throwing an out of bounds for 2D array, just did plain greater than bounds check
  49.                         playY -= 1;             //NOTE: Wanted to do (world.world[playX][playY-1] != null) but no go
  50.                     } else {
  51.                         System.out.println("You cannot go that way.");
  52.                     }
  53.                     break;
  54.                 case "south":
  55.                 case "s":
  56.                     if (world.world[playX][playY + 1] != null
  57.                             && playY + 1 < world.world[0].length) {
  58.                         playY += 1;
  59.                     } else {
  60.                         System.out.println("You cannot go that way.");
  61.                     }
  62.                     break;
  63.                 case "l":
  64.                 case "look":
  65.                     System.out.println(world.world[playX][playY].desc);
  66.                     if (world.world[playX][playY].contents.size() > 0) {
  67.                         world.world[playX][playY].contents();
  68.                     }
  69.                     break;
  70.                 case "quit":
  71.                 case "exit":
  72.                 case "q":
  73.                     running = false;
  74.                     break;
  75.                 case "get":
  76.                     System.out.println("What would you like to get?");
  77.                     String command2 = input.next().toLowerCase();
  78.                         if(item.name.equals(command2)){
  79.                             e1.ItemGet(item);
  80.                             world.world[playX][playY].removeItem(item);
  81.                             System.out.println("You've gotten the " + item);
  82.                         }else{
  83.                             System.out.println("I do not see the " + item);
  84.                         }
  85.                 default:
  86.                     System.out.println("Your command of " + command + " was not understood.");
  87.                     break;
  88.             }
  89.         }
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement