Project #6: Lion's Den
Escape the Lion's Den with the power of 2D Arrays and OOP
DESCRIPTION
Requirements
Write a program that allows the user to escape a den of Lions
This project must read a map from a file into a 2D Array and be written in an object oriented style.
Use the starter code at the bottom of this page. Your program must use this structure.
STEP BY STEP
Part A - Set Up Project
Download the starter code below
Create files
Copy paste code
Part B - Tour The Code
Take some time to read over everything that's in the project
Make sure you understand what each part of the program is trying to do
Part C - Accessor Methods
Look for accessor methods with "fix me." You'll find these in: Player, Lion, and Den
Carefully implement these methods. Think about edge cases and efficiency.
For example, consider boolean canMoveToCell(int row, int col)
Can I move to a position that is out of the bounds of the array?
Can I move into a wall?
Does the order of these two checks matter?
Can I use another accessor method I just wrote to avoid code duplication?
Part D - Den - Display
Implement this method and make sure everything is displaying properly
Part E - Moving the Player
Complete the player's move method
Make sure you call it in Den's update() method
The valid input options are n s e and w. Do not change the control scheme.
Suggested process:
Basic version moves without thinking about obstacles. Test it.
Then worry about walls. Moving into a wall should just do nothing.
Then handle special cases:
If the player moved onto an exit, the player wins the game
If the player moved into a lion, the player loses the game
Test this! Make sure your conditions are all handled correctly before moving on.
Part F - Moving the Lions
Now you can implement the lion's move method.
Lion's cannot move into walls
Lions cannot move into each other
If a lion enters the player's space, the player loses the game.
Part G - Testing
This is a complex program with a lot of moving parts
Try and break it before submitting it.
EXTRA CREDIT IDEAS (+10%)
Additional Danger
Consider adding an additional enemy type that has a different behavior from lions.
Make sure you reduce the number of Lions accordingly. Your game shouldn't be too hard to beat.
It must represent additional complexity in your coding and not just be a copy/paste or simpler version of the same class.
Good Examples:
Jaguar moves toward the player, rather than moving randomly
Giant Frog moves two spaces at a time, leaping over obstacles
Boar only moves every other turn. When it moves, it moves an entire row or column - stopping at a wall.
Bad Examples:
Ghost Lion is a normal lion but can go through walls (this is just the lion but with bugs)
Cheetah is a normal lion but it moves twice per round (this is a single line of code)
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
# # #
# & #& #
##########