我希望这对你们来说不是太业余,但是我很难用Java创建一个基于Java的小型基于文本的游戏。到目前为止,我已经为Player,Item(这将在以后使用,现在我有更简单的目标),Room,Inventory(再次,以后使用)和Main Class编写了类。我应该用什么来跟踪我的位置?我想像在Zork那样来回穿过位置(例如北上,向南等)我想过使用一个包含每个位置的ArrayList,但是这再次让我更加困扰。到目前为止我写的:
class Player{
//int healthPoints; for later use
private String playerName;
public void setPlayerName(String playerNameParam)
{
playerName=playerNameParam;
}
public String getPlayerName(){
return playerName;
}
}
class Item{
private String itemName;
public void setItemName(String itemNameParam)
{
itemName=itemNameParam;
}
public String getItemName()
{
return itemName;
}
}
class ExitRoom{
}
class Room{
private String roomName;
public void setRoomName(String roomNameParam){
roomName=roomNameParam;
}
public String getRoomName(){
return roomName;
}
private String roomDescription;
public void setRoomDescription(String roomDescriptionParam){
roomDescription=roomDescriptionParam;
}
public String getRoomDescription(){
return roomDescription;
}
}
class Inventory{
private ArrayList<Item> items= new ArrayList<Item>();
public boolean findItem(String itemToFind)
{
for(int i=0;i<items.size();i++){
if(items.get(i).getItemName()==itemToFind){
return true;
}
}
return false;
}
}
public class Main {
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
Player player = new Player();
boolean gameRunning=true;
while(gameRunning){
System.out.println("Welcome to TextBasedGamev1!"
+ "Before beginning, please enter your name");
String name=scanner.nextLine();
player.setPlayerName(name);
Room forestWelcome=new Room();
Room forestSouth=new Room();
Room forestNorth=new Room();
Room abandonedHouse=new Room();
}
非常感谢任何帮助!
答案 0 :(得分:2)
可能的方法(不一定是最好的)是在每个房间存储与其他房间的连接。例如:
enum Direction {
NORTH, SOUTH, EAST, WEST;
}
class Room {
private Map<Direction, Room> connections;
...
}
答案 1 :(得分:1)
一般来说,方法是使用每个单元格的矩阵和Point来标记玩家的位置,例如
Class Map {
private Room[][] matrix;
private Player player;
...
}
class Player{
private Point position;
....
}
答案 2 :(得分:0)
文字冒险实际上是状态机。
你可能想看一下我在C#for Games中为人工智能写的书中的3个免费章节,因为它涵盖了这一点,Java和C#非常相似。