⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 board.java

📁 j2me Games for example
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
     */    private void findTarget(int t, byte pathlen) {        if (array[t] > STORE) {            return; // Either a wall or looped back to player        }        // Already tried here and this way is longer        if (pathmap[t] <= pathlen) {            return;        }        pathmap[t] = pathlen++; // set path length to this location        if (t == pusher) {            return;        }        // avoiding ArrayIndexOutOfBoundException        if ((t - 1) >= 0) {            findTarget(t - 1, pathlen); // to previous cell        }        if ((t + 1) < array.length) {            findTarget(t + 1, pathlen); // to next cell        }        if ((t - width) >= 0) {            findTarget(t - width, pathlen); // to previous row        }        if ((t + width) < array.length) {            findTarget(t + width, pathlen); // to next row        }    }    /**     * Return the pieces at the location.     * @param x location in the board.     * @param y location in the board.     * @return flags indicating what pieces are in this board location.     * Bit flags; combinations of WALL, PUSHER, STORE, PACKET.     */    public byte get(int x, int y) {        int offset = index(x, y);        if (offset == pusher) {            return (byte)(array[offset] | PUSHER);        } else {            return array[offset];        }    }    /**     * Set the value of the location.     */    private void set(int x, int y, byte value) {        array[index(x, y)] = value;    }    /**     * Compute the index in the array of the x, y location.     */    private int index(int x, int y) {        if ((x < 0) || (x >= width) || (y < 0) || (y >= height)) {            return -1;        }        return (y * width) + x;    }    /**     * Get the location of the pusher.     * It is returned as an int with the lower 16 bits being     * the x index and the upper 16 bits being the y index.     * @return the encoded location of the pusher.     */    public int getPusherLocation() {        int x = pusher % width;        int y = pusher / width;        return (y << 16) + x;    }    /**     * Compute the offset in the array of the cell relative     * to the current pusher location in the direction of the move.     * Note: the walls around the edge always make a +/- guard band.     * Also, the order of evaluation should never try to get to +/- 2.     */    private int indexOffset(int move) {        switch (move & 3) {        case LEFT:            return -1;        case RIGHT:            return +1;        case UP:            return -width;        case DOWN:            return +width;        }        return 0;    }    /**     * Read a board from a stream.     * Read it into a fixed size array and then shrink to fit.     */    public void read(java.io.InputStream is, int l) {        final int W = 20;        final int H = 20;        byte[] b = new byte[W * H];        // Add resize code later.        int c;        // Add resize code later.        int w = 0;        int x = 0;        int y = 0;        int xn = 0;        int yn = 0;        int npackets = 0;        try {            while ((c = is.read()) != -1) {                switch (c) {                case '\n':                    if (x > w) {                        w = x;                    }                    y++;                    x = 0;                    break;                case '$':                    b[(y * W) + x++] = PACKET;                    npackets++;                    break;                case '#':                    b[(y * W) + x++] = WALL;                    break;                case ' ':                    b[(y * W) + x++] = GROUND;                    break;                case '.':                    b[(y * W) + x++] = STORE;                    break;                case '*':                    b[(y * W) + x] = PACKET;                    b[(y * W) + x++] |= STORE;                    npackets++;                    stored++;                    break;                case '+': // player and store in same place                    b[(y * W) + x++] = STORE;                case '@':                    xn = x;                    yn = y;                    x++;                    break;                }            }        } catch (java.io.IOException ex) {            ex.printStackTrace();        }        if (y > 0) {            array = new byte[w * y];            if (y > w) { // Switch x for y while copying                width = y;                height = w;                for (y = 0; y < width; y++) {                    for (x = 0; x < w; x++) {                        array[index(y, x)] = b[(y * W) + x];                    }                }                pusher = index(yn, xn);            } else {                width = w;                height = y;                array = new byte[width * height];                for (y = 0; y < height; y++) {                    for (x = 0; x < width; x++) {                        array[index(x, y)] = b[(y * W) + x];                    }                }                pusher = index(xn, yn);            }            stored = 0;            packets = npackets;            level = l;            nmoves = 0;            npushes = 0;        }    }    /**     * Get the width of the game board.     */    public int getWidth() {        return width;    }    /**     * Get the height of the board.     */    public int getHeight() {        return height;    }    /**     * Get the number of moves to get this far.     */    public int getMoves() {        return nmoves;    }    /**     * Get the number of packets pushed around.     */    public int getPushes() {        return npushes;    }    /**     * Get the number of packets stored.     */    public int getStored() {        return stored;    }    /**     * Convert a left/right direction into an offset.     */    private int dx(int dir) {        if (dir == LEFT) {            return -1;        }        if (dir == RIGHT) {            return +1;        }        return 0;    }    /**     * Convert a up/down direction into an offset.     */    private int dy(int dir) {        if (dir == UP) {            return -1;        }        if (dir == DOWN) {            return +1;        }        return 0;    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -