Game

import java.util.*; //We will use this import statement for a few utilities, such as the List and Scanner classes public class Game { List<Object> currentActionList; //This is the list of actions currently available to the player. Changes as you move to different rooms Scanner readInput = new Scanner(System.in); //We will be using this object to get user input from the player Room currentRoom; //What room are we in right now? boolean continueGame = true; //This value will tell us if we should continue running the game loop public Game() //Our no-argument constructor for the Game object. For now, let's start with creating all the available rooms. { createRooms(); } private void createRooms() { /* This method is going to create all the rooms, and fill them with appropriate exits. We'll start with just a few of each, to help us get the basic functionality right. Notice that we're writing out the code to create each room and exit. That's ok for now, since we're starting small, but think about how inefficient this is. If we want to add more rooms and exits, we'll have to add at least 4 lines of code for each room, assuming that they all have one exit, which will not always be true! Right now we have these lines for each room/exit combo: 1. A Room constructor call 2. A call to set the room's description. 3. An Exit constructor call 4 and 5. Method calls to set an exit's origin and destination. 6. A method call to set an exit's description. 7. A line of code to add an Exit to a Room There are MUCH better ways to do this, so we're going to clean this up at some point. */ Room mainHall = new Room(); Room basement = new Room(); Room kitchen = new Room(); Room pantry = new Room(); kitchen.setDescription("You are in a brightly lit kitchen. You don't see anyone, but you smell the lingering scent of decadent food."); mainHall.setDescription("You are in a giant main hall, covered in mysterious tapestries."); basement.setDescription("You are in a dank, smelly dark basement. There doesn't seem to be anything here."); pantry.setDescription("You are in a narrow, but tall room, filled with shelves, packed with food."); Exit e1 = new Exit(); //main hall to kitchen Exit e2 = new Exit(); //main hall to basement Exit e3 = new Exit(); //kitchen to pantry Exit e4 = new Exit(); //basement to main hall Exit e5 = new Exit(); //pantry to kitchen Exit e6 = new Exit(); //kitchen to main hall e1.setDescription("An open door with delicious smells radiating."); e2.setDescription("A heavy-looking trap door."); e3.setDescription("An entrance to the pantry."); e4.setDescription("A heavy-looking trap door."); e5.setDescription("An entrance to the kitchen."); e6.setDescription("An open door to the main hall."); e1.origin = mainHall; e1.destination = kitchen; e2.origin = mainHall; e2.destination = basement; e3.origin = kitchen; e3.destination = pantry; e4.origin = basement; e4.destination = mainHall; e5.origin = pantry; e5.destination = kitchen; e6.origin = kitchen; e6.destination = mainHall; mainHall.exit = e1; kitchen.exit = e3; pantry.exit = e5; currentRoom = mainHall; } private void getPlayerAction() { currentActionList = currentRoom.actionList; /*A useful tip for when you know you need to come back and complete something later is to add a TODO comment in your code. Then, when you're making sure that you've done everything you need to do, you can use your text editor to search for all TODOs, and pick them off one-by-one. Good for tasks and questions that you need to revisit. For example, see below! */ //TODO: HOW WILL I GET THE ACTIONS AND ACT ON THEM? int choice = -1; int maxChoices = currentActionList.size() + 1; if(readInput.hasNextInt()) //Our Scanner object will check to see if the player entered an integer value { choice = readInput.nextInt(); } if(choice == -1) /*For now we'll say that if the player enters -1, or a non-integer (in which case the choice variable will stay at -1), we'll end the game loop.*/ { continueGame = false; } else if(choice <= 0 || choice > maxChoices) //Our list of displayed choices will be numbered at least at 1 { System.out.println("Please enter a valid choice"); } else //If we reach this block, the player entered a valid choice { Object selection = currentActionList.get(choice - 1); if(selection instanceof Exit) //Again, instanceof is very useful to us { /*If the player has chosen a valid exit, then we will grab the Exit object, figure out that exit's destination, and set the current room to that new destination. And because we have a game loop, the next thing that will happen is that the game will give us info about our new current room! */ Exit e = (Exit)selection; //This is an example of casting, which we'll discuss later Room r = e.destination; currentRoom = r; } } } public static void main(String[] args) { Game game = new Game(); //Game Loop, which runs until the player quits, or there is a game over while(game.continueGame) { System.out.println("\n\n"); game.currentRoom.explainRoom(); game.getPlayerAction(); } } }
This class is a basic representation of the game controller in my text-based RPG, The Java Secret. This class will change as the game is developed.

A lot of this code might seem confusing if you're a new Java developer, but I'll be explaining it all in due time. If you'd like to start learning, I'm writing lessons and tutorials on Java game development here: https://itch.io/jam/summer-learn-java-jam-month-1/community

If you want to learn more about the game, visit http://bit.ly/2K3mYoZ

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.