将字符的位置存储在arraylist中的2d数组中

时间:2016-01-28 19:14:24

标签: java arrays arraylist file-io 2d-games

我需要在游戏中推送对象。我加载一个文本文件并使用2D数组创建一个地图。我需要将每个角色的位置存储在我的2D数组中,这样我就可以在需要时更新并移动它们。

我的文字档案;

SSSSSSSSSSSSSSSSSSSS
SWWWWWWWWWWWWWWWWWWW
SWSSSSSSSSCSSSSSSSSW
SWSSSSSSSSSSSSSSSSSW
SWSSSSSSSSSSSSSSSSSW
SWSSSCSSSSSSSSSSSSSW
SWSSSSSSSSSSSSSSSSSW
SWSSSSSSSSSSSSSSSSSW
SWSSSSSSSSDSSSSSSSSW
SWSSSSSSSSSSSSSSSSSW
SWSSSSSSSSSSSSSSSSSW
SWSSSSSSSSSSSSSSSSSW
SWSSSSSSSSSSSSSSSSSW
SWWWWWWWWWWWWWWWWWWW
SSSSSSSSSSSSSSSSSSSS
SSSSSSSSSSSSSSSSSSSS
SSSSSSSSSSSSSSSSSSSS
SSSSSSSSSSSSSSSSSSSS
SSSSSSSSSSSSSSSSSSSS
SSSSSSSSSSSSSSSSSSSS
SSSSSSSSSSSSSSSSSSSS

以下是我如何将地图加载到游戏中;

package soko2Dgame;

import java.awt.Image;
import java.io.BufferedReader;
import java.io.FileReader;

import javax.swing.ImageIcon;

public class Map {

    private char[][] Map = new char[20][20];



    private Image wall, diamond, crate, WhiteSpace;

    public Map() {


        ImageIcon img = new ImageIcon("F://APP//2D Games//FinalSokoban//src//soko2Dgame//wall.png");
        wall = img.getImage();

        img = new ImageIcon("F://APP//2D Games//FinalSokoban//src//soko2Dgame//diamond.png");
        diamond = img.getImage();

        img = new ImageIcon("F://APP//2D Games//FinalSokoban//src//soko2Dgame//crate.png");
        crate = img.getImage();

        img = new ImageIcon("F://APP//2D Games//FinalSokoban//src//soko2Dgame//WhiteSpace.png");
        WhiteSpace = img.getImage();



        openFile();

    }


    public Image getWall() {
        return wall;
    }

    public Image getDiamond() {
        return diamond;
    }

    public Image getCrate() {
        return crate;
    }

    public Image getWhiteSpace() {
        return WhiteSpace;
    }

    // y position is what row
    // x position is what column
    // basically checking what character is in each position.
    public char getMap(int x, int y) {
        char index = Map[y][x];
        return index;
    }

    // load the file
    // read file
    private void openFile() {
        try {
            FileReader fr = new FileReader("F://APP//2D Games//FinalSokoban//src//soko2Dgame//level1.txt");
            BufferedReader br = new BufferedReader(fr);
            String s = "";
            char[] r2;
            int i = 0, j = 0;
            while ((s = br.readLine()) != null) {
                r2 = s.toCharArray();

                for (j = 0; j < r2.length; j++) {
                    Map[i][j] = r2[j];
//                  System.out.print("" + Map[i][j]);
//                  System.out.print(",");
                }
                i++;
//              System.out.println("Element at index 0: "
//                      + Map[2][10]);
            }
            fr.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

创建一个类,例如:MapHelper,如下所示

class MapHelper {

   int x, y;
   char ch;

   public MapHelper(int x, int y, char ch) {
      this.x = x;
      this.y = y;
      this.ch = ch;
   }

   @Override
   public String toString() {
      return "[" + x + "," + y + "," + ch + "]";
   }

}

您只需创建此2D array的<{1}},

即可
MapHelper

并在内部MapHelper map_helper[][] = new MapHelper[20][20];

内初始化其成员
for-loop

通过此代码,您可以保存地图数据[字符和位置]