mirror of
https://github.com/opelly27/Sokobot.git
synced 2026-05-20 04:07:38 +00:00
fixed player spawning in a block
added mr command to reset the map if map is impossible
This commit is contained in:
@@ -67,6 +67,9 @@ public class Game {
|
|||||||
case "d":
|
case "d":
|
||||||
grid.getPlayer().moveRight();
|
grid.getPlayer().moveRight();
|
||||||
break;
|
break;
|
||||||
|
case "mr":
|
||||||
|
grid.resetMap();
|
||||||
|
break;
|
||||||
case "r":
|
case "r":
|
||||||
grid.reset();
|
grid.reset();
|
||||||
break;
|
break;
|
||||||
|
|||||||
@@ -2,38 +2,42 @@ package me.polymarsdev.sokobot.entity;
|
|||||||
|
|
||||||
import me.polymarsdev.sokobot.objects.Grid;
|
import me.polymarsdev.sokobot.objects.Grid;
|
||||||
|
|
||||||
public class Player
|
public class Player {
|
||||||
{
|
|
||||||
int x = 0;
|
int x = 0;
|
||||||
int y = 0;
|
int y = 0;
|
||||||
Grid currentGrid;
|
Grid currentGrid;
|
||||||
public Player(int x, int y, Grid currentGrid)
|
|
||||||
{
|
public Player(int x, int y, Grid currentGrid) {
|
||||||
this.x = x;
|
this.x = x;
|
||||||
this.y = y;
|
this.y = y;
|
||||||
this.currentGrid = currentGrid;
|
this.currentGrid = currentGrid;
|
||||||
}
|
}
|
||||||
public int getX()
|
|
||||||
{
|
public int getX() {
|
||||||
return x;
|
return x;
|
||||||
}
|
}
|
||||||
public int getY()
|
|
||||||
{
|
public int getY() {
|
||||||
return y;
|
return y;
|
||||||
}
|
}
|
||||||
public void setPosition(int x, int y)
|
|
||||||
{
|
public void resetPosition() {
|
||||||
this.x = x;
|
int setX = 2;
|
||||||
this.y = y;
|
int setY = 2;
|
||||||
|
while (currentGrid.isBoxRaw(setX, setY)) {
|
||||||
|
if (setX >= currentGrid.getWidth() - 1) {
|
||||||
|
setY++;
|
||||||
|
setX = 1;
|
||||||
|
} else setX++;
|
||||||
}
|
}
|
||||||
public boolean moveUp()
|
this.x = setX;
|
||||||
{
|
this.y = setY;
|
||||||
if (!currentGrid.isWall(x, y - 1))
|
}
|
||||||
{
|
|
||||||
if (currentGrid.isBox(x, y - 1))
|
public boolean moveUp() {
|
||||||
{
|
if (!currentGrid.isWall(x, y - 1)) {
|
||||||
if (currentGrid.getBox(x, y - 1).moveUp())
|
if (currentGrid.isBox(x, y - 1)) {
|
||||||
{
|
if (currentGrid.getBox(x, y - 1).moveUp()) {
|
||||||
y -= 1;
|
y -= 1;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -44,14 +48,11 @@ public class Player
|
|||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
public boolean moveDown()
|
|
||||||
{
|
public boolean moveDown() {
|
||||||
if (!currentGrid.isWall(x, y + 1))
|
if (!currentGrid.isWall(x, y + 1)) {
|
||||||
{
|
if (currentGrid.isBox(x, y + 1)) {
|
||||||
if (currentGrid.isBox(x, y + 1))
|
if (currentGrid.getBox(x, y + 1).moveDown()) {
|
||||||
{
|
|
||||||
if (currentGrid.getBox(x, y + 1).moveDown())
|
|
||||||
{
|
|
||||||
y += 1;
|
y += 1;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -62,14 +63,11 @@ public class Player
|
|||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
public boolean moveLeft()
|
|
||||||
{
|
public boolean moveLeft() {
|
||||||
if (!currentGrid.isWall(x - 1, y))
|
if (!currentGrid.isWall(x - 1, y)) {
|
||||||
{
|
if (currentGrid.isBox(x - 1, y)) {
|
||||||
if (currentGrid.isBox(x - 1, y))
|
if (currentGrid.getBox(x - 1, y).moveLeft()) {
|
||||||
{
|
|
||||||
if (currentGrid.getBox(x - 1, y).moveLeft())
|
|
||||||
{
|
|
||||||
x -= 1;
|
x -= 1;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -80,14 +78,11 @@ public class Player
|
|||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
public boolean moveRight()
|
|
||||||
{
|
public boolean moveRight() {
|
||||||
if (!currentGrid.isWall(x + 1, y))
|
if (!currentGrid.isWall(x + 1, y)) {
|
||||||
{
|
if (currentGrid.isBox(x + 1, y)) {
|
||||||
if (currentGrid.isBox(x + 1, y))
|
if (currentGrid.getBox(x + 1, y).moveRight()) {
|
||||||
{
|
|
||||||
if (currentGrid.getBox(x + 1, y).moveRight())
|
|
||||||
{
|
|
||||||
x += 1;
|
x += 1;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ import java.util.*;
|
|||||||
|
|
||||||
public class CommandListener extends ListenerAdapter {
|
public class CommandListener extends ListenerAdapter {
|
||||||
private static final ArrayList<String> commandsNoPrefix = new ArrayList<>(
|
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<>();
|
private static final HashMap<String, Command> commands = new HashMap<>();
|
||||||
|
|
||||||
public CommandListener() {
|
public CommandListener() {
|
||||||
|
|||||||
@@ -3,8 +3,7 @@ package me.polymarsdev.sokobot.objects;
|
|||||||
import me.polymarsdev.sokobot.entity.Player;
|
import me.polymarsdev.sokobot.entity.Player;
|
||||||
import me.polymarsdev.sokobot.util.Randomizer;
|
import me.polymarsdev.sokobot.util.Randomizer;
|
||||||
|
|
||||||
public class Grid
|
public class Grid {
|
||||||
{
|
|
||||||
final int GROUND = 0;
|
final int GROUND = 0;
|
||||||
final int WALL = 1;
|
final int WALL = 1;
|
||||||
final int BOX = 2;
|
final int BOX = 2;
|
||||||
@@ -20,15 +19,13 @@ public class Grid
|
|||||||
int color = 0;
|
int color = 0;
|
||||||
Player player;
|
Player player;
|
||||||
String playerEmote;
|
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;
|
this.playerEmote = playerEmote;
|
||||||
player = new Player(2, 2, this);
|
player = new Player(2, 2, this);
|
||||||
if (boxCount > MAX_BOXES)
|
if (boxCount > MAX_BOXES) boxCount = MAX_BOXES;
|
||||||
{
|
|
||||||
boxCount = MAX_BOXES;
|
|
||||||
}
|
|
||||||
this.boxCount = boxCount;
|
this.boxCount = boxCount;
|
||||||
boxes = new Box[boxCount];
|
boxes = new Box[boxCount];
|
||||||
destinations = new Destination[boxCount];
|
destinations = new Destination[boxCount];
|
||||||
@@ -37,127 +34,119 @@ public class Grid
|
|||||||
grid = new Tile[width][height];
|
grid = new Tile[width][height];
|
||||||
createBoxes();
|
createBoxes();
|
||||||
createDestinations();
|
createDestinations();
|
||||||
|
player.resetPosition();
|
||||||
updateGrid();
|
updateGrid();
|
||||||
}
|
}
|
||||||
public Player getPlayer()
|
|
||||||
{
|
public Player getPlayer() {
|
||||||
return player;
|
return player;
|
||||||
}
|
}
|
||||||
public void reset()
|
|
||||||
{
|
public void reset() {
|
||||||
player.setPosition(2, 2);
|
for (int i = 0; i < boxCount; i++) {
|
||||||
for (int i = 0; i < boxCount; i++)
|
|
||||||
{
|
|
||||||
boxes[i].reset();
|
boxes[i].reset();
|
||||||
}
|
}
|
||||||
|
player.resetPosition();
|
||||||
updateGrid();
|
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);
|
grid[x][y].setStatus(status);
|
||||||
}
|
}
|
||||||
public int getStatus(int x, int y)
|
|
||||||
{
|
public int getStatus(int x, int y) {
|
||||||
return grid[x][y].getStatus();
|
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;
|
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;
|
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
|
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++)
|
for (int i = 0; i < boxCount; i++) {
|
||||||
{
|
if (x == boxes[i].getX() && y == boxes[i].getY()) {
|
||||||
if (x == boxes[i].getX() && y == boxes[i].getY())
|
|
||||||
{
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
public boolean isDestination(int x, int y)
|
|
||||||
{
|
public boolean isDestination(int x, int y) {
|
||||||
return grid[x][y].getStatus() == DESTINATION;
|
return grid[x][y].getStatus() == DESTINATION;
|
||||||
|
|
||||||
}
|
}
|
||||||
public Box getBox(int x, int y)
|
|
||||||
{
|
public Box getBox(int x, int y) {
|
||||||
for (int i = 0; i < boxCount; i++)
|
for (int i = 0; i < boxCount; i++) {
|
||||||
{
|
if (x == boxes[i].getX() && y == boxes[i].getY()) {
|
||||||
if (x == boxes[i].getX() && y == boxes[i].getY())
|
|
||||||
{
|
|
||||||
return boxes[i];
|
return boxes[i];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
public void createBoxes()
|
|
||||||
{
|
public void createBoxes() {
|
||||||
color = rand.nextInt(6); //runs after each level
|
color = Randomizer.nextInt(6); //runs after each level
|
||||||
for (int i = 0; i < boxCount; i++)
|
for (int i = 0; i < boxCount; i++) {
|
||||||
{
|
int x = Randomizer.nextInt(width - 4) + 2;
|
||||||
int x = rand.nextInt(width - 4) + 2;
|
int y = Randomizer.nextInt(height - 4) + 2;
|
||||||
int y = rand.nextInt(height - 4) + 2;
|
for (int j = 0; j < i; j++) {
|
||||||
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())) {
|
||||||
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;
|
||||||
x = rand.nextInt(width - 4) + 2;
|
|
||||||
y = rand.nextInt(height - 4) + 2;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
boxes[i] = new Box(x, y, this);
|
boxes[i] = new Box(x, y, this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
public void createDestinations()
|
|
||||||
{
|
public void createDestinations() {
|
||||||
for (int i = 0; i < boxCount; i++)
|
for (int i = 0; i < boxCount; i++) {
|
||||||
{
|
int x = Randomizer.nextInt(width - 2) + 1;
|
||||||
int x = rand.nextInt(width - 2) + 1;
|
int y = Randomizer.nextInt(height - 2) + 1;
|
||||||
int y = rand.nextInt(height - 2) + 1;
|
for (int j = 0; j < i; j++) {
|
||||||
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;
|
||||||
while (((x == destinations[j].getX() && y == destinations[j].getY())) || isBoxRaw(x, y))
|
y = Randomizer.nextInt(height - 2) + 1;
|
||||||
{
|
|
||||||
x = rand.nextInt(width - 2) + 1;
|
|
||||||
y = rand.nextInt(height - 2) + 1;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
destinations[i] = new Destination(x, y, this);
|
destinations[i] = new Destination(x, y, this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
public void updateGrid()
|
|
||||||
{
|
public void updateGrid() {
|
||||||
for (int i = 0; i < height; i++)
|
for (int i = 0; i < height; i++) {
|
||||||
{
|
for (int j = 0; j < width; j++) {
|
||||||
for (int j = 0; j < width; j++)
|
|
||||||
{
|
|
||||||
grid[j][i] = new Tile(GROUND, playerEmote);
|
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);
|
grid[j][i] = new Tile(WALL, color, playerEmote);
|
||||||
}
|
}
|
||||||
for (int k = 0; k < boxCount; k++)
|
for (int k = 0; k < boxCount; k++) {
|
||||||
{
|
if (destinations[k].getX() == j && destinations[k].getY() == i) {
|
||||||
if (destinations[k].getX() == j && destinations[k].getY() == i)
|
|
||||||
{
|
|
||||||
grid[j][i] = new Tile(DESTINATION, playerEmote);
|
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);
|
grid[j][i] = new Tile(PLAYER, playerEmote);
|
||||||
}
|
}
|
||||||
for (int k = 0; k < boxCount; k++)
|
for (int k = 0; k < boxCount; k++) {
|
||||||
{
|
if (boxes[k].getX() == j && boxes[k].getY() == i) {
|
||||||
if (boxes[k].getX() == j && boxes[k].getY() == i)
|
if (boxes[k].onDestination()) {
|
||||||
{
|
|
||||||
if (boxes[k].onDestination())
|
|
||||||
{
|
|
||||||
grid[j][i] = new Tile(WALL, color, playerEmote);
|
grid[j][i] = new Tile(WALL, color, playerEmote);
|
||||||
} else {
|
} else {
|
||||||
grid[j][i] = new Tile(BOX, playerEmote);
|
grid[j][i] = new Tile(BOX, playerEmote);
|
||||||
@@ -168,49 +157,45 @@ public class Grid
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
public boolean hasWon()
|
|
||||||
{
|
public boolean hasWon() {
|
||||||
for (int i = 0; i < boxCount; i++)
|
for (int i = 0; i < boxCount; i++) {
|
||||||
{
|
if (!destinations[i].hasBox(this)) {
|
||||||
if (!destinations[i].hasBox(this))
|
|
||||||
{
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
public Tile[][] getGrid()
|
|
||||||
{
|
public Tile[][] getGrid() {
|
||||||
return grid;
|
return grid;
|
||||||
}
|
}
|
||||||
public Box[] getBoxes()
|
|
||||||
{
|
public Box[] getBoxes() {
|
||||||
return boxes;
|
return boxes;
|
||||||
}
|
}
|
||||||
public Destination[] getDestinations()
|
|
||||||
{
|
public Destination[] getDestinations() {
|
||||||
return destinations;
|
return destinations;
|
||||||
}
|
}
|
||||||
public int getBoxCount()
|
|
||||||
{
|
public int getBoxCount() {
|
||||||
return boxCount;
|
return boxCount;
|
||||||
}
|
}
|
||||||
public int getHeight()
|
|
||||||
{
|
public int getHeight() {
|
||||||
return height;
|
return height;
|
||||||
}
|
}
|
||||||
public int getWidth()
|
|
||||||
{
|
public int getWidth() {
|
||||||
return width;
|
return width;
|
||||||
}
|
}
|
||||||
public String toString()
|
|
||||||
{
|
public String toString() {
|
||||||
updateGrid();
|
updateGrid();
|
||||||
String result = "";
|
String result = "";
|
||||||
for (int i = 0; i < height; i++)
|
for (int i = 0; i < height; i++) {
|
||||||
{
|
for (int j = 0; j < width; j++) {
|
||||||
for (int j = 0; j < width; j++)
|
|
||||||
{
|
|
||||||
result += grid[j][i];
|
result += grid[j][i];
|
||||||
}
|
}
|
||||||
result += "\n";
|
result += "\n";
|
||||||
|
|||||||
@@ -34,7 +34,10 @@ public class GameUtil {
|
|||||||
EmbedBuilder embed = new EmbedBuilder();
|
EmbedBuilder embed = new EmbedBuilder();
|
||||||
embed.setTitle("Sokobot | Level " + level);
|
embed.setTitle("Sokobot | Level " + level);
|
||||||
embed.setDescription(game);
|
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);
|
embed.addField("Player", user.getAsMention(), false);
|
||||||
channel.sendMessage(embed.build()).queue();
|
channel.sendMessage(embed.build()).queue();
|
||||||
}
|
}
|
||||||
@@ -43,7 +46,10 @@ public class GameUtil {
|
|||||||
EmbedBuilder embed = new EmbedBuilder();
|
EmbedBuilder embed = new EmbedBuilder();
|
||||||
embed.setTitle("Sokobot | Level " + level);
|
embed.setTitle("Sokobot | Level " + level);
|
||||||
embed.setDescription(game);
|
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);
|
embed.addField("Player", user.getAsMention(), false);
|
||||||
message.editMessage(embed.build()).queue();
|
message.editMessage(embed.build()).queue();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,10 +6,6 @@ public class Randomizer{
|
|||||||
|
|
||||||
public static Random theInstance = null;
|
public static Random theInstance = null;
|
||||||
|
|
||||||
public Randomizer(){
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Random getInstance(){
|
public static Random getInstance(){
|
||||||
if(theInstance == null){
|
if(theInstance == null){
|
||||||
theInstance = new Random();
|
theInstance = new Random();
|
||||||
|
|||||||
Reference in New Issue
Block a user