Skip to content
Snippets Groups Projects
Select Git revision
  • main
1 result

README.md

Blame
  • Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    GameState.java 2.85 KiB
    package de.fossag.hackatron;
    
    import java.util.HashMap;
    
    public class GameState {
        public int ownID, ownX, ownY;
        public GameEngine.Move lastMove = GameEngine.Move.Up;
        private int mapWidth, mapHeight;
        private final Integer[][] map;  //[width][height]
        private final HashMap<Integer, String> playerNames;
    
        public GameState(int mapWidth, int mapHeight, int ownID) {
            playerNames = new HashMap<>();
            this.ownID = ownID;
            map = new Integer[mapWidth][mapHeight];
    
            this.mapWidth = mapWidth;
            this.mapHeight = mapHeight;
        }
    
        public Integer[][] getMap() {
            return map;
        }
    
        public void cleanupMap(int untilPriority) {
            for (int x = 0; x < mapWidth; x++) {
                for (int y = 0; y < mapHeight; y++) {
                    if (map[x][y] != null && map[x][y] <= untilPriority) map[x][y] = null;
                }
            }
        }
    
        public void addPos(int id, int x, int y) {
            if (x < 0 | y < 0 | x >= mapWidth || y >= mapHeight) {
                throw new UnsupportedOperationException("Position: (" + x + "," + y + ") out of bounds");
            }
            map[x][y] = id;
    
            if (id == ownID) return;
            addLookaheadPosRec(x, y, 1);
        }
    
        public void addLookaheadPosRec(int x, int y, int depth) {
            if (depth >= AlgConfig.maxDepth) return;
    
            for (GameEngine.Move m : GameEngine.Move.values()) {
                int[] nextPos = GameUtils.getNextPosition(getMap(), x, y, m);
                int newX = nextPos[0];
                int newY = nextPos[1];
                Integer currentCell = map[newX][newY];
    
                if (currentCell == null || currentCell < -1 - depth) {
                    map[newX][newY] = -1 - depth;
                    addLookaheadPosRec(newX, newY, depth + 1);
                }
            }
        }
    
        public void setPlayerInfo(int playerId, String name) {
            playerNames.put(playerId, name);
        }
    
        public void printMap() {
            System.out.println("------------");
            for (int y = 0; y < mapHeight; y++) {
                System.out.print("|");
                for (int x = 0; x < mapWidth; x++) {
                    Integer currentPixel = map[x][y];
                    if(currentPixel == null) {
                        System.out.print(" ");
                    } else if(currentPixel == ownID) {
                        System.out.print("X");
                    } else {
                        System.out.print((char) (map[x][y] + 64+1));
                    }
                }
                System.out.println("|");
            }
            System.out.println("------------");
        }
    
        public void playerDies(int playerId) {
            playerNames.remove(playerId);
            for (int i = 0; i < mapWidth; i++) {
                for (int j = 0; j < mapHeight; j++) {
                    Integer val = map[i][j];
                    if (val != null && val == playerId) {
                        map[i][j] = null;
                    }
                }
            }
        }
    }