Cohesion of methods and classes (Game class)...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Dököll
    Recognized Expert Top Contributor
    • Nov 2006
    • 2379

    Cohesion of methods and classes (Game class)...

    In this Article, we will play a game to get an understanding how cohesion methods and classes work.

    Cohesion of methods, by definition hints that one method is responsible for one and only one well-defined task.

    Cohesion of classes, by definition hints that each class should represent one single well-defined entity in the problem domain.

    The principle of cohesion can be applied to both classes and methods; classes should display high degree of cohesion, and so should methods.
    (David J Barnes / Michael Kölling)


    class Game (The driver)

    /**
    * @3rd Annual Bahamas Pelletier School Food-a-thon and Gaming Session
    * @programmer Dököll
    * @version 2007.12.9
    */

    public class Game
    {
    private Parser parser;
    private Room currentRoom;
    private Food currentFood;
    private Eat currentEat;
    private View currentView;


    /**
    * Print out the opening message for the player.
    * This can be used ofr user instructions
    */
    private void printWelcome()
    {
    System.out.prin tln();
    System.out.prin tln("Welcome to the 3rd Annual Bahamas Pelletier School Food-a-thon and Gaming Session!");
    System.out.prin tln("The Bahamas Pelletier School Food-a-thon proudly presents FREE food for all after the game.");
    System.out.prin tln("You have 4 options in this game. Please do one option at a time.");
    System.out.prin tln("First option deals with areas of a certain school establishment." );
    System.out.prin tln("After looking aroung the establishment, take a look in the lounge refrigerator for treats.");
    System.out.prin tln("Take a closer view of the types of food inlcuded in this now, wonderful fridge of yours.");
    System.out.prin tln("Be sure to make use of the free food, then you can go home. How's that for a deal?.");
    System.out.prin tln();
    System.out.prin tln("Your key verbs are 'go' for first option, 'look' for second, 'view' for third," + "\n" + "and 'eat' for last option in the game");
    System.out.prin tln();
    System.out.prin tln("Type 'help' if you need help.");
    System.out.prin tln("Type 'quit' if you need to end the game.");
    System.out.prin tln();
    System.out.prin tln("Let us begin!");
    System.out.prin tln();
    System.out.prin tln("You can type 'go' then the posted Exit to visit the school establishment." + "\n" + currentRoom.get LongDescription ());
    System.out.prin tln();
    System.out.prin tln("Now, type 'look' then the posted Exit to have a look around." + "\n" + currentFood.get LongDescription ());
    System.out.prin tln();
    System.out.prin tln("Type 'view' then the posted Exit to look inside the fridge." + "\n" + currentView.get LongDescription ());
    System.out.prin tln();
    System.out.prin tln("Now that you've seen it all, type 'eat' then the posted Exit to have fun with free meals." + "\n" + currentEat.getL ongDescription( ));


    }



    /**
    * Create the game and initialise its internal map.
    * Create 4 items for the game, 4 different classes.
    */
    public Game()
    {
    createRooms();
    createFoods();
    createEats();
    createViews();
    parser = new Parser();
    }

    /**
    * Create all the rooms and link their exits together.
    * Create specific subclasses for each item in each class.
    */
    private void createRooms()
    {
    Room outside, theatre, pub, lab, office, dorm;

    // create the rooms
    outside = new Room("outside of the theatre and looking at a 1300 penny");
    theatre = new Room("given free concert tickets and invited to a rare event with friends");
    pub = new Room("in a local pub and having the soda pop of the year");
    lab = new Room("saving this mouse from the lab");
    office = new Room("in your office and you're not allowed to take paper clips");
    dorm = new Room("outside your dorm and has been awarded with a purple candy bar");

    // initialise room exits
    outside.setExit ("west", theatre);

    theatre.setExit ("north", pub);

    pub.setExit("ea st", lab);

    lab.setExit("so uth", dorm);

    office.setExit( "north", office);

    dorm.setExit("w est", outside);

    currentRoom = lab; // start game in the lab
    }


    /**
    * Create all the foods and link their exits together.
    * Create specific subclasses for each item in each class.
    */
    private void createFoods()
    {
    Food veggies, rice, beans, soymilk, soysages, soup;

    // create the foodgroupss
    veggies = new Food("a piece of pita and are no longer hungry, now looking");
    rice = new Food("a bowl of pasta and are no longer hungry, now looking");
    beans = new Food("some grape leaves and are no longer hungry, now looking");
    soymilk = new Food("a small amount of hummus and crakers and are no longer hungry, now looking");
    soysages = new Food("a bowl of soycream and are no longer hungry, now looking");
    soup = new Food("some tofu scambled and are no longer hungry, now looking");

    // initialise room exits
    veggies.setExit ("down", soymilk);

    rice.setExit("u p", soup);

    beans.setExit(" left", soysages);

    soymilk.setExit ("right", rice);

    soysages.setExi t("straight", veggies);

    soup.setExit("i n", beans);



    currentFood = beans; // start looking at beans
    }

    /**
    * Create all the views and link their exits together.
    * Create specific subclasses for each item in each class.
    */
    private void createViews()
    {
    View bottom, top, left, right, middle, back;

    //create the views
    bottom = new View("the remnants of the pita");
    top = new View("an empty small pasta bowl");
    left = new View("the remains of grape leaves");
    right = new View("the remnants of the hummus and crakers");
    middle = new View("an empty container of soycream");
    back = new View("the remains of yesterday's tofu scrambled");

    // initialise room exits
    bottom.setExit( "beneath", top);

    top.setExit("in side", middle);

    left.setExit("o utside", right);

    right.setExit(" atop", bottom);

    middle.setExit( "nearby", top);

    back.setExit("u nder", right);

    currentView = top; // start viewing on top
    }

    /**
    * Create all the treats and link their exits together.
    * Create specific subclasses for each item in each class.
    */
    private void createEats()
    {
    Eat granola, pasta, leaves, hummus, soycream, tofu;

    // create these treats
    granola = new Eat("a piece of pita but still you are still hungry! Now looking");
    pasta = new Eat("a bowl of pasta but still you are still hungry! Now looking");
    leaves = new Eat("some grape leaves but still you are still hungry! Now looking");
    hummus = new Eat("a small amount of hummus and crakers but still you are still hungry! Now looking");
    soycream = new Eat("a bowl of soycream but still you are still hungry! Now looking");
    tofu = new Eat("some tofu scambled but still you are still hungry! Now looking");

    //initialise eats exits
    granola.setExit ("northeast" , pasta);

    pasta.setExit(" northwest", granola);

    leaves.setExit( "southwest" , leaves);

    hummus.setExit( "southeast" , pasta);

    soycream.setExi t("midesat", granola);

    tofu.setExit("m idwest", leaves);



    currentEat = granola; // start eating a granola bar
    }


    /**
    * 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.getComma nd();
    finished = processCommand( command);
    }
    System.out.prin tln("Thank you for playing. Good bye.");
    }


    /**
    * 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.isUn known()) {
    System.out.prin tln("I don't know what you mean...");
    return false;
    }

    String commandWord = command.getComm andWord();
    if (commandWord.eq uals("help")) {
    printHelp();
    }
    else if (commandWord.eq uals("go")) {
    goRoom(command) ;
    }


    else if (commandWord.eq uals("eat")) {
    goEat(command);

    }


    else if (commandWord.eq uals("view")) {
    goView(command) ;

    }


    else if (commandWord.eq uals("look")) {
    goFood(command) ;
    }
    else if (commandWord.eq uals("quit")) {
    wantToQuit = quit(command);
    }


    // else command not recognised.
    return wantToQuit;
    }

    // implementations of user commands:

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

    /**
    * Try to go to one direction. If there is an exit, enter the new
    * room, otherwise print an error message.
    */
    private void goRoom(Command command)
    {
    if(!command.has SecondWord()) {
    // if there is no second word, we don't know where to go...
    // user must type the direction to go after typing in 'go'...
    System.out.prin tln("Go where?");
    return;
    }

    String direction = command.getSeco ndWord();

    // Try to leave current room.
    Room nextRoom = currentRoom.get Exit(direction) ;

    if (nextRoom == null) {
    System.out.prin tln("There is no door!");
    }
    else {
    currentRoom = nextRoom;
    System.out.prin tln(currentRoom .getLongDescrip tion());
    }


    }


    /**
    * Try to go to one direction. If there is an exit, enter the new
    * room, otherwise print an error message.
    */
    private void goEat(Command command)
    {
    if(!command.has SecondWord()) {
    // if there is no second word, we don't know where to go...
    // user must type the direction to go after typing in 'eat'...
    System.out.prin tln("Eat what?");
    return;
    }

    String direction = command.getSeco ndWord();

    // Go ahead, try another type of food.
    Eat nextEat = currentEat.getE xit(direction);

    if (nextEat == null) {
    System.out.prin tln("Ain't a thing here!");
    }
    else {
    currentEat = nextEat;
    System.out.prin tln(currentEat. getLongDescript ion());
    }


    }


    /**
    * Try to go to one direction. If there is an exit, enter the new
    * room, otherwise print an error message.
    */
    private void goView(Command command)
    {
    if(!command.has SecondWord()) {
    // if there is no second word, we don't know where to go...
    // user must type the direction to go after typing in 'view'...
    System.out.prin tln("View what?");
    return;
    }

    String direction = command.getSeco ndWord();

    //Take a closer view at current food.
    View nextView = currentView.get Exit(direction) ;

    if (nextView == null) {
    System.out.prin tln("There is nothing here!");
    }
    else {
    currentView = nextView;
    System.out.prin tln(currentView .getLongDescrip tion());
    }


    }



    /**
    * Try to look to one direction. If there is an exit, enter the new
    * foodgroup, otherwise print an error message.
    */
    private void goFood(Command command)
    {
    if(!command.has SecondWord()) {
    // if there is no second word, we don't know where to go...
    // user must type the direction to go after typing in 'look'...
    System.out.prin tln("Fetch what?");
    return;
    }

    String direction = command.getSeco ndWord();

    // Try to look at next food item.
    Food nextFood = currentFood.get Exit(direction) ;

    if (nextFood == null) {
    System.out.prin tln("You're looking the wrong way!");
    }
    else {
    currentFood = nextFood;
    System.out.prin tln(currentFood .getLongDescrip tion());
    }

    }




    /**
    * 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.hasS econdWord()) {
    System.out.prin tln("Quit what?");
    return false;
    }
    else {
    return true; // signal that we want to quit
    }
    }
    }

    See next class (Command)...
Working...