Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package sokoban2;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.Stack;
- public class Level {
- //Konstanter
- private final static char PLAYER = '@';
- private final static char PLAYER_ON_TARGET = '+';
- private final static char WALL = '#';
- private final static char BOX = '$';
- private final static char BOX_ON_TARGET= '*';
- private final static char TARGET = '.';
- private final static char EMPTY = ' ';
- private char[][] level /*= {{'#', '#', '#', '#', '#', '#', '#'},
- {'#', ' ', ' ', ' ', '$', '.', '#'},
- {'#', '.', '$', ' ', '.', ' ', '#'},
- {'#', ' ', '@', '$', ' ', ' ', '#'},
- {'#', ' ', ' ', ' ', ' ', ' ', '#'},
- {'#', '#', '#', '#', '#', '#', '#'}}*/;
- private int playerX;
- private int playerY;
- private ArrayList<Integer> targetX;
- private ArrayList<Integer> targetY;
- // Sokoban del 2 //
- public Stack<Level> undoStack;
- // Sokoban del 2 end //
- public Level(){
- targetX = new ArrayList<Integer>();
- targetY = new ArrayList<Integer>();
- // undoMoves = new Stack<char[][]>();
- findPlayerLocationAndTargets();
- }
- //Metode som tar imot string for så å konvertere til en char matrise, finner også spillerens posisjon og målene.
- public Level(String s){
- level = levelFromString(s);
- targetX = new ArrayList<Integer>();
- targetY = new ArrayList<Integer>();
- findPlayerLocationAndTargets();
- //Sokoban 2//
- undoStack = new Stack<Level>();
- }
- //Metode som tar imot string for så å plassere den i en todimensjonal char matrise. Returnere matrisen.
- public char[][] levelFromString(String s){
- int width = s.indexOf('|');
- int height = s.length() / width;
- char newLevel[][] = new char[height][width];
- // Hjelpeindex for oppslag i strengen som representerer brettet
- int index = 0;
- for (int i = 0; i < height; i++){
- for(int j = 0; j < width; j++){
- newLevel[i][j] = s.charAt(index);
- index++;
- }
- index++;
- }
- return newLevel;
- }
- //----SOKOBAN PART 2----//
- public Level(Level other) {
- level = other.level;
- targetX = other.targetX;
- targetY = other.targetY;
- other.findPlayerLocationAndTargets();
- }
- /*
- public void updateMoveStack()
- {
- this.undoMoves.add(getLevel().clone());
- }
- */
- /*
- //matrise med y og x posisjon til bokser. OKOKOKOK
- public int[][] addPosMatrix(char[][] map)
- {
- int[][] temp = new int[this.targetX.size()+1][2];
- int z = 1;
- //Legger spillerens posisjon øverst i matrisa.
- temp[0][0] = this.getPlayerY();
- temp[0][1] = this.getPlayerX();
- //Legger så antall flyttbarobjekter sin posisjon inn i matrisa.
- for(int y = 0; y < map.length; y++)
- {
- for(int x = 0; x < map[0].length; x++)
- {
- if(map[y][x] == BOX){
- temp[z][0] = y;
- temp[z++][1] = x;
- }
- }
- }
- return temp;
- }
- */
- /*
- public void undoMove(){
- setLevel(this.undoMoves.pop());
- }
- public void setLevel(char[][] nyLevel){
- this.level= nyLevel;
- }
- */
- //----SOKOBAN END PART 2----//
- //Get-metode for level.
- public char[][] getLevel(){
- return level;
- }
- //Metode som finner spillerens posisjon og målene. Spillerens posisjon blir tilordnet de globale variablene.
- private void findPlayerLocationAndTargets(){
- for (int i = 0; i < level.length; i++) {
- for (int j = 0; j < level[0].length; j++) {
- if (level[i][j] == PLAYER){
- playerX = j;
- playerY = i;
- } else if (level[i][j] == TARGET){
- targetX.add(j);
- targetY.add(i);
- }
- }
- }
- }
- //Get-metode for targets y-posisjon.
- public ArrayList<Integer> getTargetY(){
- return targetY;
- }
- //Get-metode for targets x-posisjon.
- public ArrayList<Integer> getTargetX(){
- return targetX;
- }
- //Get-metode for spillerens x-posisjon.
- public int getPlayerX(){
- return playerX;
- }
- //Get-metode for spillerens y-posisjon.
- public int getPlayerY(){
- return playerY;
- }
- //Set-metode for å sette tegn i angitt koordinat.
- public void setField(int y, int x, char c){
- level[y][x] = c;
- }
- //Get-metode for å hente char fra ønsket xy-posisjon.
- public char getField(int y, int x){
- return level[y][x];
- }
- //Metode som flytter spiller og samtidig endrer char etter flytt.
- public void playerMoveReplace(int dy, int dx){
- char field = level[playerY][playerX];
- switch (field){
- case PLAYER_ON_TARGET:
- setField(playerY, playerX, TARGET);
- break;
- case BOX_ON_TARGET:
- setField(playerY, playerX, PLAYER_ON_TARGET);
- break;
- case PLAYER:
- setField(playerY, playerX, EMPTY);
- break;
- }
- playerX += dx;
- playerY += dy;
- }
- //Metode som oppdatere spillerens dytt.
- public void playerPushReplace(int dy, int dx){
- char field = level[playerY][playerX];
- switch (field){
- case BOX_ON_TARGET:
- setField(playerY, playerX, PLAYER_ON_TARGET);
- break;
- case BOX:
- setField(playerY, playerX, PLAYER);
- break;
- }
- }
- //Metode som oppdatere boksen char ved flytt.
- public void boxMoveReplace(int boxY, int boxX, char c){
- switch (c){
- case TARGET:
- setField(boxY, boxX, BOX_ON_TARGET);
- break;
- case EMPTY:
- setField(boxY, boxX, BOX);
- break;
- }
- }
- //Skriver ut level.
- @Override
- public String toString(){
- String printString = "";
- for (int i = 0; i < level.length; i++) {
- for (int j = 0; j < level[0].length; j++){
- printString += level[i][j];
- }
- printString += "\n";
- }
- return printString;
- }
- //Sjekker om alle boksene er på målet.
- public boolean checkVictory(){
- for (int i = 0; i < getTargetX().size(); i++){
- if (level[getTargetY().get(i)][getTargetX().get(i)] != BOX_ON_TARGET){
- return false;
- }
- }
- return true;
- }
- //Boolean metode som sjekker om planlagt move er gyldig ved hjelp av validatePush-metoden.
- public boolean validateMove(int dx, int dy){
- try {
- int tempX = getPlayerX() + dx;
- int tempY = getPlayerY() + dy;
- // Checks array index out of bounds
- // Further check block-move / wall-collision etc
- char newPos = getField(tempY, tempX);
- // If new player position is on a box
- if (newPos == BOX || newPos == BOX_ON_TARGET){
- return validatePush(dx, dy);
- } else if (newPos == WALL){
- return false;
- } else return true;
- } catch (ArrayIndexOutOfBoundsException e) {
- // Returns false if user tries to exit level bounds
- // Invalid move due to end of map
- return false;
- } catch (Exception e) {
- System.out.println("UNKNOWN EXCEPTION, MOAR DEBUGGING!");
- return false;
- }
- }
- //Boolean metode som validerer om tenkt trekk er gyldig, hjelpemetode til validateMove.
- public boolean validatePush(int dx, int dy) {
- int tX = getPlayerX() + dx*2;
- int tY = getPlayerY() + dy*2;
- char newBoxPos = getField(tY, tX);
- if (newBoxPos != WALL && newBoxPos != BOX && newBoxPos != BOX_ON_TARGET){
- // Set change the box image on map
- // If box was in correct position
- return true;
- } else {
- // Invalid move due to box collision
- return false;
- }
- }
- //Metode for flytting av spiller.
- public void movePlayer(int dx, int dy){
- if (validateMove(dx, dy)){
- // Temp-verdier for ny posisjon til spiller
- int tX = playerX + dx;
- int tY = playerY + dy;
- char newPos = getField(tY, tX);
- switch (newPos){
- case BOX_ON_TARGET:
- pushBox(dy,dx);
- playerMoveReplace(dy,dx);
- playerPushReplace(dy,dx);
- return;
- case BOX:
- pushBox(dy,dx);
- playerMoveReplace(dy,dx);
- playerPushReplace(dy,dx);
- return;
- case EMPTY:
- setField(tY, tX, PLAYER);
- playerMoveReplace(dy,dx);
- return;
- case TARGET:
- // Flytter til et tomt target-felt
- setField(tY, tX, PLAYER_ON_TARGET);
- playerMoveReplace(dy, dx);
- return;
- default:
- System.out.println("Feilmelding.");
- return;
- }
- }
- }
- //Metode for flytting av boks.
- public void pushBox(int dy, int dx){
- // Position of box after push
- int newBoxX = playerX + dx*2;
- int newBoxY = playerY + dy*2;
- // Current element at new position
- char c = getField(newBoxY, newBoxX);
- // Updates the char for element on new position
- boxMoveReplace(newBoxY, newBoxX, c);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment