Programming Resources
For Fun and Learning
Charles Cusack
Computer Science
Hope College
main

Python
C++

JAVA


PHP
SQL
Assignments

Zuul


Game.java

/**
 *  This class is the main class of the "World of Zuul" application. 
 *  "World of Zuul" is a very simple, text based adventure game.  Users 
 *  can walk around some scenery. That's all. It should really be extended 
 *  to make it more interesting!
 * 
 *  To play this game, create an instance of this class and call the "play"
 *  method.
 * 
 *  This main class creates and initializes all the others: it creates all
 *  rooms, creates the parser and starts the game.  It also evaluates and
 *  executes the commands that the parser returns.
 *
 * ************************************************************************************
 * ************************************************************************************
 * This version allows you to create a text file with commands and have it run them
 * so you can more easily test your code. 
 * Place a file in the same directory called something like "commands.txt" and then
 * when you create a game, call autoPlay(String filename) and include "commands.txt"
 * as the argument.
 * ************************************************************************************
 * ************************************************************************************
 * 
 * @author  Michael Kölling and David J. Barnes
 * @version 2016.02.29
 * @author Charles Cusack (added ability to use file as input and a lot of other stuff.)
 * @version 4/10/2024
 */

public class Game 
{
    private Parser parser;
    private Player player;
    
    /**
     * Create the game and initialize its internal map.
     */
    public Game() 
    {
        parser = new Parser();
        player = new Player();
        createRooms();
    }

    /**
     * Create all the rooms and link their exits together.
     */
    private void createRooms()
    {
		//**********************************************************
        // Your stuff here.
		//**********************************************************
    }

    /**
     *  Main play routine.  Loops until end of play.
     */
    public void play() 
    {            
        printWelcome();

        // Enter the main command loop.  Here we repeatedly read commands and
        // execute them until the game is over.
        boolean finished = false;
        while (! finished) {
            Command command = parser.getCommand();
            if(parser.usingFile()) {
                System.out.println(command);
            }
            finished = processCommand(command);
            System.out.println();
        }
        System.out.println("Thank you for playing.  Good bye.");
        System.out.println("---------------------------------------------------------------\n");
    }

    public void autoPlay(String filename) {
        parser = new Parser(filename);
        play();
    }
    
    /**
     * Print out the opening message for the player.
     */
    private void printWelcome()
    {
        System.out.println("---------------------------------------------------------------");
        System.out.println("Welcome to the World of Zuul!");
        System.out.println("World of Zuul is a new, incredibly boring adventure game.");
        System.out.println("Type 'help' if you need help.");
        System.out.println();
        System.out.println(player.getCurrentRoom().getLongDescription());
    }

    /**
     * Given a command, process (that is: execute) the command.
     * @param command The command to be processed.
     * @return true If the command ends the game, false otherwise.
     */
    private boolean processCommand(Command command) 
    {
        boolean wantToQuit = false;

        if(command.isUnknown()) {
            System.out.println("I don't know what you mean...");
            return false;
        }

        String commandWord = command.getCommandWord();
        String second = command.getSecondWord();
        if (commandWord.equals("help")) {
            printHelp();
        }
        else if (commandWord.equals("go")) {
            goRoom(command);
        }
        else if (commandWord.equals("quit")) {
            wantToQuit = quit(command);
        } 
		
		//**********************************************************
		// Add your other commands here in else if statements
		//
		//**********************************************************
		
        return wantToQuit;
    }

    /**
     * Print out some help information.
     * Here we print some stupid, cryptic message and a list of the 
     * command words.
     */
    private void printHelp() 
    {
        System.out.println("You are lost. You are alone. You wander");
        System.out.println("around at the university.");
        System.out.println();
        System.out.println("Your command words are:");
        parser.showCommands();
    }

    /** 
     * Try to in to one direction. If there is an exit, enter the new
     * room, otherwise print an error message.
     */
    private void goRoom(Command command) 
    {
        if(!command.hasSecondWord()) {
            // if there is no second word, we don't know where to go...
            System.out.println("Go where?");
            return;
        }
        String direction = command.getSecondWord();
		//**********************************************************
		// Insert your code here for making the player go a direction
		// This might be different than I did it...
		//**********************************************************
    }
	
	
	//**********************************************************
	// Insert your other helper methods here. Or below quit. 
	// I don't really care.
	//**********************************************************

    /** 
     * "Quit" was entered. Check the rest of the command to see
     * whether we really quit the game.
     * @return true, if this command quits the game, false otherwise.
     */
    private boolean quit(Command command) 
    {
        if(command.hasSecondWord()) {
            System.out.println("Quit what?");
            return false;
        }
        else {
            return true;  // signal that we want to quit
        }
    }
}