这是一个基于文本的游戏,其目的是收集所有的黄金(共4件),并使其成为步骤(在获得所有黄金后解锁)而不被怪物吃掉(也是4)
我为玩家(冒险家)提供了关于董事会,怪物,步骤和主要方法的课程。我开始了一个很好的开始。玩家可以四处走动,冷柜也能工作。看起来有效,所以我几乎为怪物类做了相同的代码(减去输入,因为他们假设自己移动)。玩家如何移动是因为它使一个字符串等于数组的部分,然后修改字符串,然后将其插回到原始地图中。当我评论怪物移动方法游戏运行正常,但如果我尝试实施怪物移动方法游戏开始但不会通过初始板并允许玩家移动。
主要方法如下:
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
Map board = new Map();
STEVE = new Adventurer();
Monster m1 = new Monster();
Stairs step = new Stairs();
do{
STEVE.finished();
while(!STEVE.finished()){
System.out.print(board.toString());
System.out.println("Current gold held is:"+Adventurer.gold);
System.out.println("Which direction would you like to move?");
String dup = scan.nextLine();
char dir = dup.charAt(0);
int direction = java.lang.Character.getNumericValue(dir);
if (direction == W){
STEVE.move(UP);
}
if (direction == S){
STEVE.move(DOWN);
}
if (direction == A){
STEVE.move(LEFT);
}
if (direction == D){
STEVE.move(RIGHT);
}
m1.move();
//step.isLocked(Adventurer.gold);
if(STEVE.finished()){
System.out.print(board.toString());
System.out.println("Congrats! You beat the game!");
fin = true;
}
}
}while (!fin);
}
冒险家搬家方法:
public void move(int direction){
String[] map = Map.table;
String money = "$";
String wall = "*";
String stairs = "\\";
char shiny = '$';
char block = '*';
char stairz = '\\';
if(direction == UP){
String yup = map[getRow()-1];
if(yup.charAt(getColumn())==(shiny)){
getGold();
}
String up = yup.substring(0, getColumn())+"P"+yup.substring(getColumn()+1);
if(yup.charAt(getColumn())==(block)){
up = yup;
map[getRow()+1]=map[getRow()+1];
}
else{
map[getRow()-1] = up;
map[getRow()+1] = map[getRow()+1].substring(0, getColumn())+"."+map[getRow()+1].substring(getColumn()+1);
}
Map.table = map;
}
if(direction == DOWN){
String yup = map[getRow()+1];
if(yup.charAt(getColumn())==(shiny)){
getGold();
}
String up = yup.substring(0, getColumn())+"P"+yup.substring(getColumn()+1);
if(yup.charAt(getColumn())==(block)||yup.charAt(getColumn())==(stairz)){
up = yup;
map[getRow()-1]=map[getRow()-1];
}
else{
map[getRow()+1] = up;
map[getRow()] = map[getRow()].substring(0, getColumn())+"."+map[getRow()].substring(getColumn()+1);
}
Map.table = map;
}
if(direction == LEFT){
String yup = map[getRow()];
if(yup.substring(getColumn()-1, getColumn()).equals(money)){
getGold();
}
String up = yup.substring(0, getColumn()-1)+"P"+"."+yup.substring(getColumn()+1, yup.length());
if(yup.substring(getColumn()-1, getColumn()).equals(wall)){
up=yup;
}
map[getRow()] = up;
Map.table = map;
}
if(direction == RIGHT){
String yup = map[getRow()];
if(yup.substring(getColumn()+1, getColumn()+DOWN).equals(money)){
getGold();
}
String up = yup.substring(0, getColumn())+"."+"P"+yup.substring(getColumn()+DOWN, yup.length());
if(yup.substring(getColumn()+1, getColumn()+DOWN).equals(wall)||yup.substring(getColumn()+1, getColumn()+DOWN).equals(stairs)){
up=yup;
}
map[getRow()] = up;
Map.table = map;
}
}
怪物移动方法:
public void move(){
String[] map = Map.table;
String mon=map[row1].substring(col1, col1+1);
int counter = 1;
while(counter<FIVE){
if(counter==1){
if(mon.equals("^")){
String up = map[row1-1];
String cur = map[row1];
up=up.substring(0, col1)+"^"+up.substring(col1+1);
cur=cur.substring(0, col1)+"."+cur.substring(col1+1);
map[row1-1]=up;
map[row1]=cur;
mon=map[row2].substring(col2, col2+1);
Map.table = map;
counter=2;
}
else if(mon.equals("v")){
String down = map[row1+1];
String cur = map[row1];
down = down.substring(0, col1)+"v"+down.substring(col1+1);
cur =cur.substring(0, col1)+"."+cur.substring(col1+1);
map[row1+1]=down;
map[row1]=cur;
mon=map[row2].substring(col2, col2+1);
Map.table = map;
counter=2;
}
else if(mon.equals("<")){
String left = map[row1];
left = left.substring(0, col1-1)+"<"+"."+left.substring(col1+1);
map[row1]=left;
mon=map[row2].substring(col2, col2+1);
Map.table = map;
counter=2;
}
else if(mon.equals(">")){
String right = map[row1];
String fix = "";
fix = right.substring(0, col1)+"."+">"+right.substring(col1+OFFSET, right.length());
map[row1]=fix;
mon=map[row2].substring(col2, col2+1);
Map.table = map;
counter = 2;
}
}
if(counter==2){
if(mon.equals("^")){
String up = map[row2-1];
String cur = map[row2];
up=up.substring(0, col2)+"^"+up.substring(col2+1);
cur=cur.substring(0, col2)+"."+cur.substring(col2+1);
map[row2-1]=up;
map[row2]=cur;
mon=map[row3].substring(col3, col3+1);
Map.table = map;
counter=3;
}
else if(mon.equals("v")){
String down = map[row2+1];
String cur = map[row2];
down = down.substring(0, col2)+"v"+down.substring(col2+1);
cur =cur.substring(0, col2)+"."+cur.substring(col2+1);
map[row2+1]=down;
map[row2]=cur;
mon=map[row3].substring(col3, col3+1);
Map.table = map;
counter=3;
}
else if(mon.equals("<")){
String left = map[row2];
left = left.substring(0, col2-1)+"<"+"."+left.substring(col2+1);
map[row2]=left;
mon=map[row3].substring(col3, col3+1);
Map.table = map;
counter=3;
}
else if(mon.equals(">")){
String right = map[row2];
String fix = "";
fix = right.substring(0, col2)+"."+">"+right.substring(col2+OFFSET, right.length());
map[row2]=fix;
mon=map[row3].substring(col3, col3+1);
Map.table = map;
counter = 3;
}
}
if(counter==3){
if(mon.equals("^")){
String up = map[row3-1];
String cur = map[row3];
up=up.substring(0, col3)+"^"+up.substring(col3+1);
cur=cur.substring(0, col3)+"."+cur.substring(col3+1);
map[row3-1]=up;
map[row3]=cur;
mon=map[row4].substring(col4, col4+1);
Map.table = map;
counter=4;
}
else if(mon.equals("v")){
String down = map[row3+1];
String cur = map[row3];
down = down.substring(0, col3)+"v"+down.substring(col3+1);
cur =cur.substring(0, col3)+"."+cur.substring(col3+1);
map[row3+1]=down;
map[row3]=cur;
mon=map[row4].substring(col4, col4+1);
Map.table = map;
counter=4;
}
else if(mon.equals("<")){
String left = map[row3];
left = left.substring(0, col3-1)+"<"+"."+left.substring(col3+1);
map[row3]=left;
mon=map[row4].substring(col4, col4+1);
Map.table = map;
counter=4;
}
else if(mon.equals(">")){
String right = map[row3];
String fix = "";
fix = right.substring(0, col3)+"."+">"+right.substring(col3+OFFSET, right.length());
map[row3]=fix;
mon=map[row4].substring(col4, col4+1);
Map.table = map;
counter = 4;
}
}
if(counter==4){
if(mon.equals("^")){
String up = map[row4-1];
String cur = map[row4];
up=up.substring(0, col4)+"^"+up.substring(col4+1);
cur=cur.substring(0, col4)+"."+cur.substring(col4+1);
map[row4-1]=up;
map[row4]=cur;
Map.table = map;
counter=5;
}
else if(mon.equals("v")){
String down = map[row4+1];
String cur = map[row4];
down = down.substring(0, col4)+"v"+down.substring(col4+1);
cur =cur.substring(0, col4)+"."+cur.substring(col4+1);
map[row4+1]=down;
map[row4]=cur;
Map.table = map;
counter=5;
}
else if(mon.equals("<")){
String left = map[row4];
left = left.substring(0, col4-1)+"<"+"."+left.substring(col4+1);
map[row4]=left;
Map.table = map;
counter=5;
}
else if(mon.equals(">")){
String right = map[row4];
String fix = "";
fix = right.substring(0, col4)+"."+">"+right.substring(col4+OFFSET, right.length());
map[row4]=fix;
Map.table = map;
counter = 5;
}
}
}
}
怪物之所以如此长的原因是因为它们中有4个并且它们根据它们面向的方向移动并且如果它们看到了玩家。并完成董事会/地图:
public static String[] table= {
"....................",
"....................",
"....P...............",
"....................",
"....................",
"......*******.......",
"......*.............",
"......*.............",
"......*.....>.......",
"......*.............",
".....<..............",
".$....^.............",
"....................",
"....................",
"....................",
"......*.............",
"...$..*..........$..",
"......***...........",
".............<......",
"................$..\\"};
对不起,但我相信我所包含的一切都是必要的。如果你关心的话,星号(*)就是墙。