gpt4 book ai didi

java - 有人可以指导我如何获取玩家对象并将其移动到二维平铺对象数组中吗?

转载 作者:行者123 更新时间:2023-12-02 04:25:30 25 4
gpt4 key购买 nike

我创建了一个名为 Tile 的对象,它包含有关其图 block 上的内容以及图 block 类型的信息。然后,我创建了这些 Tile 对象的 2D 数组,并循环显示过程以在控制台中将网格输出为 ASCII 网格,循环检查以查看图 block 上的内容,并将用空白等填充关联的网格方 block 。

我想知道您是否可以指导我如何获取玩家对象,并开始将其放入网格内,因为我很困惑从哪里开始。我很好奇如何让 Tiles.contains() 向我显示玩家对象,以便我可以开始针对该对象执行功能,例如 Player.moveLeft 等。

感谢您的帮助!

我很困惑从哪里开始。

/**
* @aim Tile Object which contains information about each grid square
*/

public class Tile
{
//-------------------------------------------------------------------------------------
// Class Variables
//-------------------------------------------------------------------------------------

private String szType;
private boolean bIsEmpty;
private String szOnTile;

//-------------------------------------------------------------------------------------
// Constructor
//-------------------------------------------------------------------------------------

public Tile()
{
reset();
return;
}

public Tile(String Type, boolean IsEmpty, String OnTile)
{
reset();
setType(Type);
setIsEmpty(IsEmpty);
setOnTile(OnTile);
}


//-------------------------------------------------------------------------------------
// Setters
//-------------------------------------------------------------------------------------

public void setType(String Type)
{
this.szType = Type;
return;
}

public void setIsEmpty(boolean IsEmpty)
{
this.bIsEmpty = IsEmpty;
return;
}

public void setOnTile(String OnTile)
{
this.szOnTile = OnTile;
return;
}

//-------------------------------------------------------------------------------------
// Getters
//-------------------------------------------------------------------------------------

public String getType()
{
return(this.szType);
}

public boolean getIsEmpty()
{
return(this.bIsEmpty);
}

public String getOnTile()
{
return(this.szOnTile);
}

//-------------------------------------------------------------------------------------
// Utilities
//-------------------------------------------------------------------------------------

public void displayObject()
{
System.out.println("Type = " + getType());
System.out.println("IsEmpty = " + getIsEmpty());
System.out.println("OnTile = " + getOnTile());
System.out.println();
}

private void reset()
{
setType("Blank");
setIsEmpty(true);
setOnTile("None");
}

//-------------------------------------------------------------------------------------
// Test-Rig
//-------------------------------------------------------------------------------------

public static void main(String[] args)
{


}
}
public class TestMap 
{

public static final int iGRID_WIDTH = 10;
public static final int iGRID_HEIGHT = 10;
public static Tile[][] Map = new Tile[iGRID_HEIGHT][iGRID_WIDTH];

public static void generateMap ()
{
int i = 0;
int j = 0;

for(i = 0 ; i < iGRID_HEIGHT ; i++)
{

for(j = 0 ; j < iGRID_WIDTH ; j++)
{

Map[i][j] = new Tile();

}
}
return;
}

public static void displayMap()
{
int i = 0;
int j = 0;

System.out.println("---------------------");
for(i = 0 ; i < iGRID_HEIGHT ; i++)
{

System.out.print("|");

for(j = 0 ; j < iGRID_WIDTH ; j++)
{
if(Map[i][j].getOnTile() == "None")
{
System.out.print(" " + "|");
}
}

System.out.print("\n");
System.out.println("---------------------");
}

}
public static void main(String[] args)
{

generateMap();
displayMap();

}

}

我希望能够获取一个玩家对象并开始在网格中遍历它。我知道如何使用普通的二维数组来做到这一点;但我对如何将其传输到二维对象数组感到困惑。我知道我的 Tile 对象并不完全符合 OOP 规则,它仍在进行中。

编辑:

我现在已经实现了所讨论的更改(或尝试),这就是结果:

/**
* @author Damien Newnham
* @aim Tile Object which contains information about each grid square
*/

public class Tile implements TileObjectInterface
{
//-------------------------------------------------------------------------------------
// Class Variables
//-------------------------------------------------------------------------------------

private String szType;
private boolean bIsEmpty;
private String szOnTile;

//-------------------------------------------------------------------------------------
// Constructor
//-------------------------------------------------------------------------------------

public Tile()
{
reset();
return;
}

public Tile(String Type)
{
reset();
setType(Type);
}


//-------------------------------------------------------------------------------------
// Setters
//-------------------------------------------------------------------------------------

public void setType(String Type)
{
this.szType = Type;
return;
}

//-------------------------------------------------------------------------------------
// Getters
//-------------------------------------------------------------------------------------

public String getType()
{
return(this.szType);
}

public boolean isObjectHere(TileObjectInterface searchingObject)
{
boolean isObjectHere = false;
if(containedObjectList.contains(searchingObject) == true)
{
isObjectHere = true;
}
return isObjectHere;
}
//-------------------------------------------------------------------------------------
// Utilities
//-------------------------------------------------------------------------------------

public void displayObject()
{
System.out.println("Type = " + getType());
System.out.println();
}

public void removeObject(TileObjectInterface implementingObject)
{
containedObjectList.remove(implementingObject);
}

public void addObject(TileObjectInterface implementingObject)
{
containedObjectList.add(implementingObject);
}



private void reset()
{
setType("Blank");
}

//-------------------------------------------------------------------------------------
// Test-Rig
//-------------------------------------------------------------------------------------

public static void main(String[] args)
{


}

}
public class TestMap extends Globals
{

public static void generateMap ()
{
int i = 0;
int j = 0;

for(i = 0 ; i < iGRID_HEIGHT ; i++)
{

for(j = 0 ; j < iGRID_WIDTH ; j++)
{

Map[i][j] = new Tile();

}
}
return;
}

public static void spawnPlayer()
{
Map[0][0].addObject(player);
}

public static void displayMap()
{
int i = 0;
int j = 0;

System.out.println("-------------------------------");
for(i = 0 ; i < iGRID_HEIGHT ; i++)
{

System.out.print("|");

for(j = 0 ; j < iGRID_WIDTH ; j++)
{
if(Map[i][j].isObjectHere(player) == true)
{
System.out.print("XX" + "|");
}
else
{
System.out.print(" " + "|");
}
}

System.out.print("\n");
System.out.println("-------------------------------");
}
System.out.println("\n\n");
}
public static void main(String[] args)
{


}

}

public class PlayerMovementController extends Globals
{

public static int iPlayerRow = 0;
public static int iPlayerColumn = 0;

public static void movePlayer(char chDirection)
{
chDirection = Character.toUpperCase(chDirection);

if (chDirection == 'L')
{
Map[iPlayerRow][iPlayerColumn].removeObject(player);
Map[iPlayerRow - 1][iPlayerColumn].addObject(player);
iPlayerRow = iPlayerRow - 1;
}
else
{
if (chDirection == 'R')
{
Map[iPlayerRow][iPlayerColumn].removeObject(player);
Map[iPlayerRow + 1][iPlayerColumn].addObject(player);
iPlayerRow = iPlayerRow + 1;
}
else
{
if (chDirection == 'U')
{
Map[iPlayerRow][iPlayerColumn].removeObject(player);
Map[iPlayerRow][iPlayerColumn - 1].addObject(player);
iPlayerColumn = iPlayerColumn - 1;
}
else
{
if(chDirection == 'D')
{
Map[iPlayerRow][iPlayerColumn].removeObject(player);
Map[iPlayerRow][iPlayerColumn + 1].addObject(player);
iPlayerColumn = iPlayerColumn + 1;
}
}

}

}
}

public static void main(String[] args)
{

}

}


public class Globals
{
public static Player player = new Player("Damo");

public static final int iGRID_WIDTH = 10;
public static final int iGRID_HEIGHT = 10;
public static Tile[][] Map = new Tile[iGRID_HEIGHT][iGRID_WIDTH];



public static void main(String[] args)
{

}

}


public class TestRig
{

public static void main(String[] args)
{

TestMap.generateMap();
TestMap.spawnPlayer();
TestMap.displayMap();

PlayerMovementController.movePlayer('R');
TestMap.displayMap();
PlayerMovementController.movePlayer('R');
TestMap.displayMap();

PlayerMovementController.movePlayer('D');
TestMap.displayMap();
PlayerMovementController.movePlayer('L');
TestMap.displayMap();
}

}

/**
* @author Damien Newnham
* @aim Player Object which contains information about the player
*/

public class Player implements TileObjectInterface
{
//-------------------------------------------------------------------------------------
// Class Variables
//-------------------------------------------------------------------------------------

private String szCharacterName;

//-------------------------------------------------------------------------------------
// Constructor
//-------------------------------------------------------------------------------------

public Player()
{
reset();
return;
}

public Player(String CharacterName)
{
reset();
setCharacterName(CharacterName);
}


//-------------------------------------------------------------------------------------
// Setters
//-------------------------------------------------------------------------------------

public void setCharacterName(String CharacterName)
{
this.szCharacterName = CharacterName;
return;
}

//-------------------------------------------------------------------------------------
// Getters
//-------------------------------------------------------------------------------------

public String getCharacterName()
{
return(this.szCharacterName);
}

//-------------------------------------------------------------------------------------
// Utilities
//-------------------------------------------------------------------------------------

public void displayObject()
{
System.out.println("CharacterName = " + getCharacterName());
System.out.println();
}

private void reset()
{
setCharacterName("Bob");
}

//-------------------------------------------------------------------------------------
// Test-Rig
//-------------------------------------------------------------------------------------

public static void main(String[] args)
{


}

}
import java.util.LinkedList;

public interface TileObjectInterface
{
LinkedList<TileObjectInterface> containedObjectList = new LinkedList<>();
}

现在的问题是添加和删除对象时出现逻辑错误或语法错误,运行 TestRig 的输出如下所示

-------------------------------
|XX|XX|XX|XX|XX|XX|XX|XX|XX|XX|
-------------------------------
|XX|XX|XX|XX|XX|XX|XX|XX|XX|XX|
-------------------------------
|XX|XX|XX|XX|XX|XX|XX|XX|XX|XX|
-------------------------------
|XX|XX|XX|XX|XX|XX|XX|XX|XX|XX|
-------------------------------
|XX|XX|XX|XX|XX|XX|XX|XX|XX|XX|
-------------------------------
|XX|XX|XX|XX|XX|XX|XX|XX|XX|XX|
-------------------------------
|XX|XX|XX|XX|XX|XX|XX|XX|XX|XX|
-------------------------------
|XX|XX|XX|XX|XX|XX|XX|XX|XX|XX|
-------------------------------
|XX|XX|XX|XX|XX|XX|XX|XX|XX|XX|
-------------------------------
|XX|XX|XX|XX|XX|XX|XX|XX|XX|XX|
-------------------------------



-------------------------------
|XX|XX|XX|XX|XX|XX|XX|XX|XX|XX|
-------------------------------
|XX|XX|XX|XX|XX|XX|XX|XX|XX|XX|
-------------------------------
|XX|XX|XX|XX|XX|XX|XX|XX|XX|XX|
-------------------------------
|XX|XX|XX|XX|XX|XX|XX|XX|XX|XX|
-------------------------------
|XX|XX|XX|XX|XX|XX|XX|XX|XX|XX|
-------------------------------
|XX|XX|XX|XX|XX|XX|XX|XX|XX|XX|
-------------------------------
|XX|XX|XX|XX|XX|XX|XX|XX|XX|XX|
-------------------------------
|XX|XX|XX|XX|XX|XX|XX|XX|XX|XX|
-------------------------------
|XX|XX|XX|XX|XX|XX|XX|XX|XX|XX|
-------------------------------
|XX|XX|XX|XX|XX|XX|XX|XX|XX|XX|
-------------------------------



-------------------------------
|XX|XX|XX|XX|XX|XX|XX|XX|XX|XX|
-------------------------------
|XX|XX|XX|XX|XX|XX|XX|XX|XX|XX|
-------------------------------
|XX|XX|XX|XX|XX|XX|XX|XX|XX|XX|
-------------------------------
|XX|XX|XX|XX|XX|XX|XX|XX|XX|XX|
-------------------------------
|XX|XX|XX|XX|XX|XX|XX|XX|XX|XX|
-------------------------------
|XX|XX|XX|XX|XX|XX|XX|XX|XX|XX|
-------------------------------
|XX|XX|XX|XX|XX|XX|XX|XX|XX|XX|
-------------------------------
|XX|XX|XX|XX|XX|XX|XX|XX|XX|XX|
-------------------------------
|XX|XX|XX|XX|XX|XX|XX|XX|XX|XX|
-------------------------------
|XX|XX|XX|XX|XX|XX|XX|XX|XX|XX|
-------------------------------



-------------------------------
|XX|XX|XX|XX|XX|XX|XX|XX|XX|XX|
-------------------------------
|XX|XX|XX|XX|XX|XX|XX|XX|XX|XX|
-------------------------------
|XX|XX|XX|XX|XX|XX|XX|XX|XX|XX|
-------------------------------
|XX|XX|XX|XX|XX|XX|XX|XX|XX|XX|
-------------------------------
|XX|XX|XX|XX|XX|XX|XX|XX|XX|XX|
-------------------------------
|XX|XX|XX|XX|XX|XX|XX|XX|XX|XX|
-------------------------------
|XX|XX|XX|XX|XX|XX|XX|XX|XX|XX|
-------------------------------
|XX|XX|XX|XX|XX|XX|XX|XX|XX|XX|
-------------------------------
|XX|XX|XX|XX|XX|XX|XX|XX|XX|XX|
-------------------------------
|XX|XX|XX|XX|XX|XX|XX|XX|XX|XX|
-------------------------------



-------------------------------
|XX|XX|XX|XX|XX|XX|XX|XX|XX|XX|
-------------------------------
|XX|XX|XX|XX|XX|XX|XX|XX|XX|XX|
-------------------------------
|XX|XX|XX|XX|XX|XX|XX|XX|XX|XX|
-------------------------------
|XX|XX|XX|XX|XX|XX|XX|XX|XX|XX|
-------------------------------
|XX|XX|XX|XX|XX|XX|XX|XX|XX|XX|
-------------------------------
|XX|XX|XX|XX|XX|XX|XX|XX|XX|XX|
-------------------------------
|XX|XX|XX|XX|XX|XX|XX|XX|XX|XX|
-------------------------------
|XX|XX|XX|XX|XX|XX|XX|XX|XX|XX|
-------------------------------
|XX|XX|XX|XX|XX|XX|XX|XX|XX|XX|
-------------------------------
|XX|XX|XX|XX|XX|XX|XX|XX|XX|XX|
-------------------------------

所需的输出应该是第一个图像左上角单元格中的 XX,然后向右移动一次,再向右移动一次,向下移动一次,然后向左移动一次。正如所见,它似乎在每个单元格中显示 XX。

再次感谢!

最佳答案

这个想法是用一个代表 map 的 Tile 对象的二维数组。您可能想问自己,图 block / map 是否应该能够对它们包含的对象执行操作。当您将 Tile 类视为可以包含其他对象的容器时,您的实现会更加清晰。

该类的目的是包含对象并删除/添加新对象。因此,一个好主意是将接口(interface)中包含的对象抽象出来。每个单独的图 block 充当实现接口(interface)的这些对象的容器。

这会导致数据层除了保存与程序(游戏)相关的数据外没有其他用途。在此层之上,您可以构建一个逻辑层,现在可以使用数据层对其执行操作。逻辑层将包含您的二维数组遍历。

请记住,这是一种非常基本的方法,一旦扩展程序,您可能需要进行更多抽象。

更新

Class diagram showing the container

如何在 Java 中实现此功能的代码示例:

// in Tile class
// please consider renaming the interface etc. (just for clearance here)

LinkedList<TileObjectInterface> containedObjectList = new LinkedList<>();

// method example to add to the container (which is Tile)
// consider adding error checks

public void add(TileObjectInterface implementingObject) {
this.containedObjectList.add(implementingObject);
}

我无法回答您有关如何设计容器的问题,因为这将取决于稍后的使用情况。如果您制作一个简单的列表实现,您可能会遇到运行时类检查,这可能是不需要的。但我可以想象,当您持有例如玩家对象的外部引用时,可以对容器进行操作。

关于java - 有人可以指导我如何获取玩家对象并将其移动到二维平铺对象数组中吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56609473/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com