Project #6: Lion's Den
Escape the Lion's Den with the power of 2D Arrays and OOP

DESCRIPTION

Requirements

Use the starter code at the bottom of this page.  Your program must use this structure.

STEP BY STEP

Part A - Set Up Project 

Part B - Tour The Code

Part C - Accessor Methods

Part D - Den - Display

Part E - Moving the Player

Part F - Moving the Lions


Part G - Testing


EXTRA CREDIT IDEAS (+10%)

Additional Danger

STARTER CODE
Main, Den, Player, Lion, Map.txt

Main

// Main - Controls main event loop

// DO NOT MODIFY


public class Main

{

   // Global Variables    

   public static boolean win = false;

   public static boolean lose = false;


   public static Den den;


   public static void main(String[] args)

   {

      den = new Den();


      // Keep game loop going until win or loss

     

      while(!win && !lose)

      {

         // Display and update the game

         den.display();

         den.update();

      }


      // Win or loss message

      if(win)

      {

         System.out.println(" \n*** YOU WIN *** \n");

      }


      else

      {

         System.out.println(" \n*** YOU LOSE *** \n");

      }

   }

}


Den

// Den - Handles map and objects


import java.io.File;

import java.io.FileNotFoundException;

import java.util.Scanner;


public class Den

{

   // Data

   private char[][] cells;

   private Player player;

   private Lion[] lions;

   private final int ROWS = 10;

   private final int COLS = 10;

   private final int NUM_LIONS = 5;  


   // Constructor

   public Den()

   {

      cells = new char[ROWS][COLS];

      lions = new Lion[NUM_LIONS];

      readFile();

   }


   // Accessors

   public boolean inBounds(int row, int col)

   {

      return true;      // fix me

   }

   public boolean canMoveToCell(int row, int col)

   {

      return true;    // fix me

   }

   public boolean hasExit(int row, int col)

   {

      return false;     // fix me

   }

   public boolean hasPlayer(int row, int col)

   {

      return false;     // fix me

   }

   public boolean hasLion(int row, int col)

   {

      return false;     // fix me

   }


   // Mutators

   public void update()

   {

      // player moves

      player.move();


      // add code to make all lions move

   }


   public void display()

   {

      for(int i = 0; i < ROWS; i++) 

      {

         for(int j = 0; j < COLS; j++)

         {

            // If there is a player, print P in BLUE

            // Else If there is a lion, print & in YELLOW

            // Else print the value of the cell

            // Make sure you put a " " after each character

            // This makes the map more readable and shows its actual shape

         }

         System.out.println();

      }

   }


   // This method is complete.  You don't need to edit it, but must *understand* it.

   public void readFile()

   {

      try

      {

         File mapFile = new File("map.txt");

         Scanner scan = new Scanner(mapFile);


         int lionCount = 0;


         // Loop through the rows

         for(int i = 0; i < ROWS; i++)  

         {

            String row = scan.nextLine();


            // Loop through the columns

            for(int j = 0; j < COLS; j++)     

            {

               char input = row.charAt(j);


               // If there's a P, make the player at that spot

               // The cell under the player is just blank

               if(input == 'P')

               {

                  player = new Player(i, j);

                  cells[i][j] = ' ';

               }


               // If there's an &, make a lion at that spot.  Iterate through them.

               // The cell under the lion is just blank

               else if(input == '&')

               {

                  lions[lionCount] = new Lion(i, j);

                  lionCount++;

                  cells[i][j] = ' ';

               }


               // Otherwise, simply store the data in cells

               else

               {

                  cells[i][j] = input;

               }

            }

         }

         scan.close();     

      }


      catch(FileNotFoundException e)

      {

         System.out.println("Cannot find file!");

      }

   }

}



Player

// Class Player - defines a player's behavior


import java.util.Scanner;


public class Player

{

   // Data


   private int row;

   private int col;


   // Constructor


   public Player(int r, int c)

   {

      row = r;

      col = c;

   }


   // Accessors


   public int getRow()

   {

      return 0;     // fix me

   }

  

   public int getCol()

   {

      return 0;     // fix me

   }


   // Mutators


   public void move()

   {

      // Get User Input

     

      Scanner scan = new Scanner(System.in);

      System.out.print("Choose a direction (n, s, e, w): ");

      String input = scan.next();


      // Move the player's position


      // If you're on an exit, you win


      // If you're on a lion, you lose


         // Hint:  To access global variables, use "Main."


         // Ex: Main.den.hasLion(row, col)

   }

}


Lion

// Class Lion - Define's a lion's behavior


public class Lion

{

   // Data

   private int row;

   private int col;


   // Constructor

   public Lion(int r, int c)

   {

      row = r;

      col = c;

   }


   // Accessors

   public int getRow()

   {

      return 0;     // fix me

   }


   public int getCol()

   {

      return 0;     // fix me

   }



   // Mutators

   public void move()

   {

      // Choose random direction

      // Move

         // Lion's can't walk through walls or each other

         // If a lion enters a player space, it's game over

   }


}  


Map <-- Lives in project file, but not src folder

##########

#P    & ##

# # #    #

#    ## ##

# &     &#

#    #   #

##       E

#  #     #

# &  #&  #

##########