d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Help With Java Project
Prev12345Next
Add Reply New Topic New Poll
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Nov 5 2014 01:55am
PLAYER:

Code
public interface IPlayer
{
void setKey(IKey key);
void setLamp(ILamp lamp);
void setRoom(IRoom room);

ILamp getLamp();
IKey getKey();
IRoom getRoom();
boolean hasLamp();
boolean hasKey();

}

Code
public class Player implements IPlayer
{
private IKey key;
private ILamp lamp;
private IRoom room;

public void setKey(IKey key)
{
this.key = key;
}
public void setLamp(ILamp lamp)
{
this.lamp = lamp;
}
public void setRoom(IRoom room)
{
this.room = room;
}
public ILamp getLamp()
{
return lamp;
}
public IKey getKey()
{
return key;
}
public IRoom getRoom()
{
return room;
}
public boolean hasLamp()
{
return lamp != null;
}
public boolean hasKey()
{
return key != null;
}


}


Code
public interface IPlayerFactory
{
IPlayer createPlayer();
}


Code
public class PlayerFactory implements IPlayerFactory
{
public IPlayer createPlayer() {

return new Player();
}

}
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Nov 5 2014 01:56am
KEY:

Code

public interface IKey
{
void use(IChest onChest);
}


Code

public class Key implements IKey
{
//This method is called when you are ready to unlock a chest with the key obtained from the map
public void use(IChest onChest)
{
onChest.unLock(this);
}

}


Code

public interface IKeyFactory
{
IKey createKey();
}


Code
public class KeyFactory implements IKeyFactory
{
public IKey createKey()
{
return new Key();
}
}
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Nov 5 2014 01:57am
LAMP:

Code

public interface ILamp
{
void light();
boolean isLit();
}


Code
public class Lamp implements ILamp
{
private boolean lit;

public Lamp()
{
lit = false;
}

public void light()
{
lit = true;
}

public boolean isLit()
{
return lit;
}

}


Code
public interface ILampFactory
{
ILamp createLamp();
}


Code
public class LampFactory implements ILampFactory
{
public ILamp createLamp()
{
return new Lamp();
}
}
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Nov 5 2014 01:58am
CHEST:

Code
public interface IChest
{
void lock(IKey theKey);

void unLock(IKey theKey);

boolean isLocked();

String getContents();

void setContents(String contents);

}


Code
public class Chest implements IChest
{
private IKey key;
private boolean locked;
private String contents;

public Chest()
{
locked = false;
contents = "";
}

public void lock(IKey theKey)
{
key = theKey;
locked = true;
}

public void unLock(IKey theKey)
{
if(key == theKey)
{
locked = false;
}
}

public boolean isLocked()
{
return locked;
}

public String getContents()
{
return contents;
}

public void setContents(String contents)
{
this.contents = contents;
}
}


Code
public interface IChestFactory
{
IChest createChest();
}


Code
public class ChestFactory implements IChestFactory
{
public IChest createChest()
{
return new Chest();
}
}


Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Nov 5 2014 01:59am
ROOM:

Code
public interface IRoom
{
String getId();
String getDescription();
boolean canGoNorth();
boolean canGoSouth();
boolean canGoEast();
boolean canGoWest();
ILamp getLamp();
void clearLamp();
IKey getKey();
void clearKey();
IChest getChest();
boolean isDark();

boolean hasLamp();
boolean hasKey();
boolean hasChest();

}


Code
public class Room implements IRoom
{
private String description;
private boolean north;
private boolean south;
private boolean east;
private boolean west;
private boolean isDark;

private ILamp theLamp;
private IKey theKey;
private IChest theChest;
private String id;

public Room(String description, boolean north, boolean south, boolean east,
boolean west, boolean isDark, ILamp theLamp, IKey theKey,
IChest theChest, String id)
{
this.description = description;
this.north = north;
this.south = south;
this.east = east;
this.west = west;
this.isDark = isDark;
this.theLamp = theLamp;
this.theKey = theKey;
this.theChest = theChest;
this.id = id;
}

/**
* Returns the text description of this room
*/
public String getDescription() {
return description;
}

/**
* Returns true if the player can go north from this room
*/
public boolean canGoNorth() {
return north;
}

/**
* Returns true if the player can go south from this room
*/
public boolean canGoSouth() {
return south;
}

/**
* Returns true if the player can go east from this room
*/
public boolean canGoEast() {
return east;
}

/**
* Returns true if the player can go west from this room
*/
public boolean canGoWest() {
return west;
}

/**
* Returns the lamp object in this room.
* If no lamp is present, returns null
*/
public ILamp getLamp() {
return theLamp;
}

/**
* Sets the lamp variable in this room to null
*/
public void clearLamp() {
theLamp = null;
}

/**
* Returns the key object in this room.
* If no key is present, returns null
*/
public IKey getKey() {
return theKey;
}

/**
* Sets the key variable in this room to null
*/
public void clearKey() {
theKey = null;
}

/**
* Returns the chest object in this room.
* If no chest is present, returns null
*/
public IChest getChest() {
return theChest;
}

/**
* Returns true if there is no light in this room,
* veeeeeeeery dangerous!
*/
public boolean isDark() {
return isDark;
}

public boolean hasLamp()
{
return theLamp != null;
}
public boolean hasKey()
{
return theKey != null;
}

public boolean hasChest()
{
return theChest != null;
}

public String getId()
{
return id;
}

}


Code
public interface IRoomFactory
{
IRoom createRoom(String description, boolean north, boolean south, boolean east,
boolean west, boolean isDark, ILamp theLamp, IKey theKey,
IChest theChest, String id);
}


Code
public class RoomFactory implements IRoomFactory
{
public IRoom createRoom(String description, boolean north, boolean south, boolean east,
boolean west, boolean isDark, ILamp theLamp, IKey theKey,
IChest theChest, String id)
{
return new Room(description, north, south, east,
west,isDark, theLamp, theKey,
theChest, id);
}
}
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Nov 5 2014 02:01am
COMMAND:

Code
public interface ICommand
{
String executeCommand(IAdventure adventure);
}


Code
public interface ICommandFactory
{
ICommand createCommand(String command);
}


Code
public class GetCommand implements ICommand
{
private Item itemToGet;
public GetCommand(Item itemToGet)
{
this.itemToGet = itemToGet;
}

public String executeCommand(IAdventure adventure)
{
String result = "";

if(hasItem(adventure))
result = getItem(adventure);
else
result = String.format("No %s present.", itemToGet.toString());

return result;
}

private boolean hasItem(IAdventure adventure)
{
boolean result = false;
switch(itemToGet)
{
case LAMP:
result = adventure.getPlayer().getRoom().hasLamp();
break;
case KEY:
result = adventure.getPlayer().getRoom().hasKey();
break;
}

return result;
}
private String getItem(IAdventure adventure)
{
switch(itemToGet)
{
case LAMP:
adventure.getPlayer().setLamp(adventure.getPlayer().getRoom().getLamp());
adventure.getPlayer().getRoom().clearLamp();
break;
case KEY:
adventure.getPlayer().setKey(adventure.getPlayer().getRoom().getKey());
adventure.getPlayer().getRoom().clearKey();
break;
}

return "OK";
}
}


Code

public class LightCommand implements ICommand
{


public String executeCommand(IAdventure adventure)
{
String result = "";

if(adventure.getPlayer().hasLamp())
{
adventure.getPlayer().getLamp().light();
result = "OK";
}
else
result = "You don't have the lamp to light";

return result;

}

}
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Nov 5 2014 02:02am
COMMAND CONTINUED:

Code
public class LookCommand implements ICommand
{
public String executeCommand(IAdventure adventure)
{
String result = "";
IPlayer player = adventure.getPlayer();

if(player.getRoom().isDark() && !player.hasLamp() || (player.hasLamp() && !player.getLamp().isLit()))
result = "It is pitch black, you can't see anything. You may be eaten by a grue!";
else
{
result = player.getRoom().getDescription();

String lamp = player.getRoom().hasLamp() ? "There is an old oil lamp here that was made long ago\n" : "";
String key = player.getRoom().hasKey() ? "You see the outline of a key on a dusty shelf that's covered in dust.\n" : "";
String chest = player.getRoom().hasChest() ? "There is a large, wooden, massive, oaken chest here with the word “CHEST” carved into it\n" : "";
String exits = String.format("Exits are: %s", getExits(player.getRoom()));
result = String.format("%s\n%s%s%s%s",result,lamp,key,chest,exits);
}

return result;
}

private String getExits(IRoom room)
{
String exits = "";

if(room.canGoNorth()) exits += "north\n";
if(room.canGoSouth()) exits += "south\n";
if(room.canGoEast()) exits += "east\n";
if(room.canGoWest()) exits += "west\n";

return exits;
}
}

Code
public class MoveCommand implements ICommand
{
private Direction directionToMove;
public MoveCommand(Direction directionToMove)
{
this.directionToMove = directionToMove;
}

public String executeCommand(IAdventure adventure)
{
String result = "";

if(canMoveInDirection(adventure))
if(isCurrentRoomSafe(adventure))
result = moveToRoom(adventure);
else
{
result = "You have stumbled into a passing grue, and have been eaten";
adventure.endAdventure();
}
else
result = "Can't go that way";

return result;
}

private boolean canMoveInDirection(IAdventure adventure)
{
IRoom room = adventure.getPlayer().getRoom();

boolean canMove = false;

switch(directionToMove)
{
case NORTH: canMove = room.canGoNorth(); break;
case SOUTH: canMove = room.canGoSouth(); break;
case EAST: canMove = room.canGoEast(); break;
case WEST: canMove = room.canGoWest(); break;
}

return canMove;
}

private boolean isCurrentRoomSafe(IAdventure adventure)
{
IPlayer player = adventure.getPlayer();
IRoom room = adventure.getPlayer().getRoom();

return !room.isDark() || (player.hasLamp() && player.getLamp().isLit());
}

private String moveToRoom(IAdventure adventure)
{
IPlayer player = adventure.getPlayer();
IRoom roomToMove = adventure.getMap().getRoom(player.getRoom(), directionToMove);
String result = "";

player.setRoom(roomToMove);

if(player.getRoom().isDark() && !player.hasLamp() || (player.hasLamp() && !player.getLamp().isLit()))
result = "It is pitch black, you can't see anything. You may be eaten by a grue!";
else
result = player.getRoom().getDescription();

return result;
}
}


Code
public class OpenCommand implements ICommand
{
public String executeCommand(IAdventure adventure)
{
String result = "";
IPlayer player = adventure.getPlayer();

if(!player.getRoom().hasChest())
result = "No chest present";
else if(player.getRoom().getChest().isLocked())
result = "The chest is locked";
else
result = openChest(adventure);

return result;
}

private String openChest(IAdventure adventure)
{
String result = adventure.getPlayer().getRoom().getChest().getContents();
adventure.endAdventure();
return result;
}
}


Code
public class UnknownCommand implements ICommand
{
public String executeCommand(IAdventure adventure)
{
return "I'm sorry I don't know how to do that";
}
}


Code
public class UnlockCommand implements ICommand
{
public String executeCommand(IAdventure adventure)
{
IPlayer player = adventure.getPlayer();
String result = "";

if(!player.getRoom().hasChest())
result = "No chest to unlock";
else if(!player.getRoom().getChest().isLocked())
result = "The chest is unlocked";
else if(!player.hasKey())
result = "Need a key to do any unlocking!";
else
result = unlockChest(adventure);

return result;
}

private String unlockChest(IAdventure adventure)
{
IPlayer player = adventure.getPlayer();
IChest chest = player.getRoom().getChest();
IKey key = player.getKey();

key.use(chest);

return "OK";
}

}
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Nov 5 2014 02:04am
ADVENTURE ENGINE:

Code
public interface IAdventure
{
IMap getMap();
IPlayer getPlayer();

boolean isAdventuring();
void startAdventure();
void endAdventure();
}


Code
public class Adventure implements IAdventure
{
private IMapFactory mapFactory;
private IPlayerFactory playerFactory;

private IMap map;
private IPlayer player;

private boolean adventuring;


public Adventure(IMapFactory mapFactory, IPlayerFactory playerFactory)
{
this.mapFactory = mapFactory;
this.playerFactory = playerFactory;

this.map = mapFactory.createMap();
this.player = playerFactory.createPlayer();

player.setRoom(map.getStartingRoom());

adventuring = false;

}

public IMap getMap()
{
return map;
}

public IPlayer getPlayer()
{
return player;
}


public boolean isAdventuring()
{
return adventuring;
}

public void startAdventure()
{
adventuring = true;
System.out.println(player.getRoom().getDescription());

}

public void endAdventure()
{
adventuring = false;
}
}


Code
public interface IAdventureFactory
{
IAdventure createAdventure();
}


Code
public class AdventureFactory implements IAdventureFactory
{
public IAdventure createAdventure()
{
return new Adventure(new MapFactory(), new PlayerFactory());
}
}

Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Nov 5 2014 02:05am
ADVENTURE ENGINE CONTINUED:

Code

public interface IAdventureEngine
{
void run();
}


Code
import java.util.Scanner;

public class AdventureEngine implements IAdventureEngine
{
private IAdventureFactory adventureFactory;
private ICommandFactory commandFactory;
public AdventureEngine(IAdventureFactory adventureFactory, ICommandFactory commandFactory)
{
this.adventureFactory = adventureFactory;
this.commandFactory = commandFactory;
}

public void run()
{
IAdventure adventure = adventureFactory.createAdventure();

adventure.startAdventure();

Scanner in = new Scanner(System.in);
String input, output;

while(adventure.isAdventuring())
{
input = in.nextLine();

ICommand command = commandFactory.createCommand(input);

output = command.executeCommand(adventure);

System.out.println(output);

}
}
}


Code
public class AdventureEngineDriver
{
public static void main(String[] args)
{
new AdventureEngine(new AdventureFactory(), new CommandFactory()).run();
}
}
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Nov 5 2014 02:11am
What's that have to do with yanderes and waifus? Reported as off topic.
Go Back To Programming & Development Topic List
Prev12345Next
Add Reply New Topic New Poll