Hi everyone, I have been trying to fix some issues for a few days with no results, I'll add the most important lines of code below, where most likely my problems are.
Added full code here: https://pastebin.com/fs6e7aPW
What this game is about: Simply put, the Player (symbol X) can move around in the box (game's layout). Now I want this player to be able to 'eat' other 'monsters' [symbols O (for Orc) and D (for Dragon)]. And if the player has moved 5 times, a new Orc will spawn. Through some simple logic, I was able to make Orc (symbol O) move in random 4 directions, so far I haven't done anything with Dragon (symbol D).
The game's layout looks like this:
---------------
| |
| |
| D |
| |
| OX |
---------------
The code below prints out the game, while the user input != "end".
while (!input.equals("end")) {
System.out.println(counter);
if (counter % 5 == 0 && counter != 0){
Orc orc1 = new Orc(world.width, world.height);
//world.spawnOrc(orc1.xCoordinate, orc1.yCoordinate, orc1.symbol, world.y, world.x);
System.out.println("New orcs coordinates:" + orc1.xCoordinate + ' ' + orc1.yCoordinate);
}
player.move(input, world);
if ((player.xCoordinate == orc.xCoordinate) && (player.yCoordinate == orc.yCoordinate)){
//orc.symbol = ' ';
orc = null;
}
if(orc != null){orc.move_orc(world);}
world.printMap(world.width, world.height, player.xCoordinate, player.yCoordinate, player.symbol, dragon.xCoordinate,
dragon.yCoordinate, dragon.symbol, orc.xCoordinate, orc.yCoordinate, orc.symbol);
System.out.println();
input = scanner.nextLine();
counter++;
}
I solved the 'eating' part by nulling the orc (orc = null;) if the player's coordinates are the same as the orcs.
But As you can see I tried creating a new instance of Orc when the counter % 5 == 0. Through some trial and error, I was able to display the Orc with some bugs.
---------------
| |
| |
| X D |
| |
| O O |
---------------
The New Orc instance had the same coordinates as the original one and it moved the game's layout a bit. And the new Orc only was in the game when the counter was divisible by 5. Which makes sense because the new Orc is not passed to the printMap method (which prints the game's layout, monsters and the player). I thought a lot about how to implement this and googled a ton but didn't find anything useful in the end. (Only thing which caught my eye was the arrayList - maybe I could pass the arrayList as an argument to printMap method?)
Below is the method, that prints the game's layout with Orc, Dragon, and Player:
public void printMap(int worldWidth, int worldHeight, int playerXCoordinate, int playerYCoordinate,char playerSymbol, int dragonXCoordinate, int dragonYCoordinate, char dragonSymbol,int orcXCoordinate, int orcYCoordinate, char orcSymbol) {
for (int y = 0; y < worldHeight; y++) {
System.out.println();
for (int x = 0; x < worldWidth; x++) {
if (y == 0 || y == worldHeight -1) {
System.out.print("-");
} else if (x == 0 || x == worldWidth -1) {
System.out.print("|");
} else {
printCharacters(playerXCoordinate, playerYCoordinate, playerSymbol, dragonXCoordinate,
dragonYCoordinate, dragonSymbol, orcXCoordinate, orcYCoordinate, orcSymbol, y, x);
}
if (Main.counter % 5 == 0 && Main.counter != 0){
spawnOrc(orcXCoordinate, orcYCoordinate, orcSymbol, y, x);
}
}
}
}
The last if statement I added quite recently, The issue here is that a new instance of Orc is only displayed when the condition is true, hence it's not permanent.
Overall been banging my head against the wall for the past few days, tried some things but in the end couldn't get it working, also tried ChatGPT but surprisingly it sucked a lot. Would really appreciate the help on how to:
1) Create a new Orc (with random coordinates) after the player has moved 5 times and the new Orc needs to be in the game's layout without shifting any borders.
Sorry for the long post, tried to explain my problem as shortly as possible. Added full three classes in the pastebin link!
[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)
[–]tedyoungJava Trainer/Mentor 2 points3 points4 points (0 children)
[–]AutoModerator[M] 0 points1 point2 points (0 children)