fixed player spawning in a block

added mr command to reset the map if map is impossible
This commit is contained in:
AffluentAvo
2020-08-09 15:09:45 +02:00
parent 971a9c3328
commit 718d2b6c15
6 changed files with 140 additions and 155 deletions
@@ -67,6 +67,9 @@ public class Game {
case "d":
grid.getPlayer().moveRight();
break;
case "mr":
grid.resetMap();
break;
case "r":
grid.reset();
break;
@@ -2,38 +2,42 @@ package me.polymarsdev.sokobot.entity;
import me.polymarsdev.sokobot.objects.Grid;
public class Player
{
public class Player {
int x = 0;
int y = 0;
Grid currentGrid;
public Player(int x, int y, Grid currentGrid)
{
public Player(int x, int y, Grid currentGrid) {
this.x = x;
this.y = y;
this.currentGrid = currentGrid;
}
public int getX()
{
public int getX() {
return x;
}
public int getY()
{
public int getY() {
return y;
}
public void setPosition(int x, int y)
{
this.x = x;
this.y = y;
public void resetPosition() {
int setX = 2;
int setY = 2;
while (currentGrid.isBoxRaw(setX, setY)) {
if (setX >= currentGrid.getWidth() - 1) {
setY++;
setX = 1;
} else setX++;
}
public boolean moveUp()
{
if (!currentGrid.isWall(x, y - 1))
{
if (currentGrid.isBox(x, y - 1))
{
if (currentGrid.getBox(x, y - 1).moveUp())
{
this.x = setX;
this.y = setY;
}
public boolean moveUp() {
if (!currentGrid.isWall(x, y - 1)) {
if (currentGrid.isBox(x, y - 1)) {
if (currentGrid.getBox(x, y - 1).moveUp()) {
y -= 1;
return true;
}
@@ -44,14 +48,11 @@ public class Player
}
return false;
}
public boolean moveDown()
{
if (!currentGrid.isWall(x, y + 1))
{
if (currentGrid.isBox(x, y + 1))
{
if (currentGrid.getBox(x, y + 1).moveDown())
{
public boolean moveDown() {
if (!currentGrid.isWall(x, y + 1)) {
if (currentGrid.isBox(x, y + 1)) {
if (currentGrid.getBox(x, y + 1).moveDown()) {
y += 1;
return true;
}
@@ -62,14 +63,11 @@ public class Player
}
return false;
}
public boolean moveLeft()
{
if (!currentGrid.isWall(x - 1, y))
{
if (currentGrid.isBox(x - 1, y))
{
if (currentGrid.getBox(x - 1, y).moveLeft())
{
public boolean moveLeft() {
if (!currentGrid.isWall(x - 1, y)) {
if (currentGrid.isBox(x - 1, y)) {
if (currentGrid.getBox(x - 1, y).moveLeft()) {
x -= 1;
return true;
}
@@ -80,14 +78,11 @@ public class Player
}
return false;
}
public boolean moveRight()
{
if (!currentGrid.isWall(x + 1, y))
{
if (currentGrid.isBox(x + 1, y))
{
if (currentGrid.getBox(x + 1, y).moveRight())
{
public boolean moveRight() {
if (!currentGrid.isWall(x + 1, y)) {
if (currentGrid.isBox(x + 1, y)) {
if (currentGrid.getBox(x + 1, y).moveRight()) {
x += 1;
return true;
}
@@ -17,7 +17,7 @@ import java.util.*;
public class CommandListener extends ListenerAdapter {
private static final ArrayList<String> commandsNoPrefix = new ArrayList<>(
Arrays.asList("w", "a", "s", "d", "up", "left", "down", "right", "r"));
Arrays.asList("w", "a", "s", "d", "up", "left", "down", "right", "r", "mr"));
private static final HashMap<String, Command> commands = new HashMap<>();
public CommandListener() {
@@ -3,8 +3,7 @@ package me.polymarsdev.sokobot.objects;
import me.polymarsdev.sokobot.entity.Player;
import me.polymarsdev.sokobot.util.Randomizer;
public class Grid
{
public class Grid {
final int GROUND = 0;
final int WALL = 1;
final int BOX = 2;
@@ -20,15 +19,13 @@ public class Grid
int color = 0;
Player player;
String playerEmote;
Randomizer rand = new Randomizer();
public Grid(int width, int height, int boxCount, String playerEmote) //create a random grid with specific width, height, and number of boxes
public Grid(int width, int height, int boxCount, String playerEmote) //create a random grid with specific width,
// height, and number of boxes
{
this.playerEmote = playerEmote;
player = new Player(2, 2, this);
if (boxCount > MAX_BOXES)
{
boxCount = MAX_BOXES;
}
if (boxCount > MAX_BOXES) boxCount = MAX_BOXES;
this.boxCount = boxCount;
boxes = new Box[boxCount];
destinations = new Destination[boxCount];
@@ -37,127 +34,119 @@ public class Grid
grid = new Tile[width][height];
createBoxes();
createDestinations();
player.resetPosition();
updateGrid();
}
public Player getPlayer()
{
public Player getPlayer() {
return player;
}
public void reset()
{
player.setPosition(2, 2);
for (int i = 0; i < boxCount; i++)
{
public void reset() {
for (int i = 0; i < boxCount; i++) {
boxes[i].reset();
}
player.resetPosition();
updateGrid();
}
public void setStatus(int x, int y, int status)
{
public void resetMap() {
boxes = new Box[boxCount];
destinations = new Destination[boxCount];
createBoxes();
createDestinations();
player.resetPosition();
updateGrid();
}
public void setStatus(int x, int y, int status) {
grid[x][y].setStatus(status);
}
public int getStatus(int x, int y)
{
public int getStatus(int x, int y) {
return grid[x][y].getStatus();
}
public boolean isWall(int x, int y)
{
public boolean isWall(int x, int y) {
return grid[x][y].getStatus() == WALL;
}
public boolean isBox(int x, int y)
{
public boolean isBox(int x, int y) {
return grid[x][y].getStatus() == BOX;
}
public boolean isBoxRaw(int x, int y) //allows you to check if a box is at a position before grid is set up
{
for (int i = 0; i < boxCount; i++)
{
if (x == boxes[i].getX() && y == boxes[i].getY())
{
for (int i = 0; i < boxCount; i++) {
if (x == boxes[i].getX() && y == boxes[i].getY()) {
return true;
}
}
return false;
}
public boolean isDestination(int x, int y)
{
public boolean isDestination(int x, int y) {
return grid[x][y].getStatus() == DESTINATION;
}
public Box getBox(int x, int y)
{
for (int i = 0; i < boxCount; i++)
{
if (x == boxes[i].getX() && y == boxes[i].getY())
{
public Box getBox(int x, int y) {
for (int i = 0; i < boxCount; i++) {
if (x == boxes[i].getX() && y == boxes[i].getY()) {
return boxes[i];
}
}
return null;
}
public void createBoxes()
{
color = rand.nextInt(6); //runs after each level
for (int i = 0; i < boxCount; i++)
{
int x = rand.nextInt(width - 4) + 2;
int y = rand.nextInt(height - 4) + 2;
for (int j = 0; j < i; j++)
{
while ((x == boxes[j].getX() && y == boxes[j].getY()) || (x == 2 && y == 2) || (x - 1 == boxes[j].getX() && y == boxes[j].getY()) || (x + 1 == boxes[j].getX() && y == boxes[j].getY()))
{
x = rand.nextInt(width - 4) + 2;
y = rand.nextInt(height - 4) + 2;
public void createBoxes() {
color = Randomizer.nextInt(6); //runs after each level
for (int i = 0; i < boxCount; i++) {
int x = Randomizer.nextInt(width - 4) + 2;
int y = Randomizer.nextInt(height - 4) + 2;
for (int j = 0; j < i; j++) {
while ((x == boxes[j].getX() && y == boxes[j].getY()) || (x == 2 && y == 2) || (x - 1 == boxes[j].getX()
&& y == boxes[j].getY()) || (x + 1 == boxes[j].getX() && y == boxes[j].getY())) {
x = Randomizer.nextInt(width - 4) + 2;
y = Randomizer.nextInt(height - 4) + 2;
}
}
boxes[i] = new Box(x, y, this);
}
}
public void createDestinations()
{
for (int i = 0; i < boxCount; i++)
{
int x = rand.nextInt(width - 2) + 1;
int y = rand.nextInt(height - 2) + 1;
for (int j = 0; j < i; j++)
{
while (((x == destinations[j].getX() && y == destinations[j].getY())) || isBoxRaw(x, y))
{
x = rand.nextInt(width - 2) + 1;
y = rand.nextInt(height - 2) + 1;
public void createDestinations() {
for (int i = 0; i < boxCount; i++) {
int x = Randomizer.nextInt(width - 2) + 1;
int y = Randomizer.nextInt(height - 2) + 1;
for (int j = 0; j < i; j++) {
while (((x == destinations[j].getX() && y == destinations[j].getY())) || isBoxRaw(x, y)) {
x = Randomizer.nextInt(width - 2) + 1;
y = Randomizer.nextInt(height - 2) + 1;
}
}
destinations[i] = new Destination(x, y, this);
}
}
public void updateGrid()
{
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
public void updateGrid() {
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
grid[j][i] = new Tile(GROUND, playerEmote);
if (j == 0 || j == width - 1 || i == 0 || i == height - 1)
{
if (j == 0 || j == width - 1 || i == 0 || i == height - 1) {
grid[j][i] = new Tile(WALL, color, playerEmote);
}
for (int k = 0; k < boxCount; k++)
{
if (destinations[k].getX() == j && destinations[k].getY() == i)
{
for (int k = 0; k < boxCount; k++) {
if (destinations[k].getX() == j && destinations[k].getY() == i) {
grid[j][i] = new Tile(DESTINATION, playerEmote);
}
}
if (player.getX() == j && player.getY() == i)
{
if (player.getX() == j && player.getY() == i) {
grid[j][i] = new Tile(PLAYER, playerEmote);
}
for (int k = 0; k < boxCount; k++)
{
if (boxes[k].getX() == j && boxes[k].getY() == i)
{
if (boxes[k].onDestination())
{
for (int k = 0; k < boxCount; k++) {
if (boxes[k].getX() == j && boxes[k].getY() == i) {
if (boxes[k].onDestination()) {
grid[j][i] = new Tile(WALL, color, playerEmote);
} else {
grid[j][i] = new Tile(BOX, playerEmote);
@@ -168,49 +157,45 @@ public class Grid
}
}
}
public boolean hasWon()
{
for (int i = 0; i < boxCount; i++)
{
if (!destinations[i].hasBox(this))
{
public boolean hasWon() {
for (int i = 0; i < boxCount; i++) {
if (!destinations[i].hasBox(this)) {
return false;
}
}
return true;
}
public Tile[][] getGrid()
{
public Tile[][] getGrid() {
return grid;
}
public Box[] getBoxes()
{
public Box[] getBoxes() {
return boxes;
}
public Destination[] getDestinations()
{
public Destination[] getDestinations() {
return destinations;
}
public int getBoxCount()
{
public int getBoxCount() {
return boxCount;
}
public int getHeight()
{
public int getHeight() {
return height;
}
public int getWidth()
{
public int getWidth() {
return width;
}
public String toString()
{
public String toString() {
updateGrid();
String result = "";
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
result += grid[j][i];
}
result += "\n";
@@ -34,7 +34,10 @@ public class GameUtil {
EmbedBuilder embed = new EmbedBuilder();
embed.setTitle("Sokobot | Level " + level);
embed.setDescription(game);
embed.addField("Enter direction (``up``, ``down``, ``left``, ``right``/``wasd``) or ``r`` to reset", "", false);
embed.addField(
"Enter direction (``up``, ``down``, ``left``, ``right``/``wasd``), ``r`` to reset or ``mr`` to "
+ "recreate the map",
"", false);
embed.addField("Player", user.getAsMention(), false);
channel.sendMessage(embed.build()).queue();
}
@@ -43,7 +46,10 @@ public class GameUtil {
EmbedBuilder embed = new EmbedBuilder();
embed.setTitle("Sokobot | Level " + level);
embed.setDescription(game);
embed.addField("Enter direction (``up``, ``down``, ``left``, ``right``/``wasd``) or ``r`` to reset", "", false);
embed.addField(
"Enter direction (``up``, ``down``, ``left``, ``right``/``wasd``), ``r`` to reset or ``mr`` to "
+ "recreate the map",
"", false);
embed.addField("Player", user.getAsMention(), false);
message.editMessage(embed.build()).queue();
}
@@ -6,10 +6,6 @@ public class Randomizer{
public static Random theInstance = null;
public Randomizer(){
}
public static Random getInstance(){
if(theInstance == null){
theInstance = new Random();