1

RMI と Tomcat を使用する Android での小さな戦艦ゲームの作成。

テキスト ファイル「boards.txt」からデータをロードし、場合によってはそれを HashMap に格納してクエリを実行する必要があります。

ファイルを添付できませんが、以下はメモ帳での .txt ファイルのスクリーンショットです。これは、10x10 グリッドである 10,000 の戦艦ゲーム ボード (とにかくその数だと思います) のボード レイアウトを表示します。

したがって、ファイルの各 100 文字は、それぞれが改行で区切られた 10 x 10 グリッドのさまざまな船と空白の場所の場所になります。

私が苦労しているのは、実際の解析です。データを HashMap に保存すると、ランダムなボード レイアウトを選択するための検索が容易になると思います。以下は、現在データを読み取るために使用しているコードです。

public static void main(String[] args) throws FileNotFoundException {
        Scanner scanner = new Scanner(new FileReader("C:\\Path\\boards.txt"));

        HashMap<String, String> map = new HashMap<String, String>();

        while (scanner.hasNextLine()) {
            String columns = scanner.nextLine();
            map.put(columns, columns);
        }

        System.out.println(map);
    }

今、私がこのようにした主な理由は、エラーを取り除くことでした。以前、私のコードは次のようでした:

public static void main(String[] args) throws FileNotFoundException {
        Scanner scanner = new Scanner(new FileReader("C:\\Path\\boards.txt"));

        HashMap<String, String> map = new HashMap<String, String>();

        while (scanner.hasNextLine()) {
            String[] columns = scanner.nextLine().split(" ");
            map.put(columns[0], columns[1]);
        }

        System.out.println(map);
    }

しかし、範囲外の例外が発生していました。分割する空白スペースがないため、最も可能性が高いです。

私の主な問題は、.txt ファイル全体を HashMap に保存し、一度に 100 文字の 1 行だけを表示するにはどうすればよいかということです。

そして第二に(優先度が低い)-ランダムに1行を選択したいので、1〜10000の乱数を作成して1行を表示できます。String、Integer HashMap を使用して、どういうわけか各行にインデックス値を設定することを考えましたが、どうすればよいかわかりません。

**編集申し訳ありませんが、スクリーンショットを追加するのを忘れていました。ここにあります: ここに画像の説明を入力

あなたの答えをありがとう、私は今それらを読みます

4

4 に答える 4

2

質問に直接答えるには:

Map1)ファイルリーダーはほとんどありましたが、気にする必要はありません.aでList十分です。

Scanner scanner = new Scanner(new FileReader("C:\\Path\\boards.txt"));
List<String> lines = new ArrayList<String>();
while (scanner.hasNextLine()) {
    String columns = scanner.nextLine();
    lines.add(columns);
}

2) ランダムな行を取得するには、List.

Random random = new Random();
int randomLineIndex = random.nextInt(lines.size());
String randomLine = lines.get(randomLineIndex);

とはいえ、ファイル全体をロードする必要がない場合は、ロードしないでください。ファイルからランダムな行を取得する必要があるだけの場合は、それを選択して、計算とメモリを節約してください。

そのためには、まずファイル内の行数を知る必要があります。 この質問により、次のことがわかります。

String file = "C:\\Path\\boards.txt";
BufferedReader reader = new BufferedReader(new FileReader(file));
int lineCount = 0;
while (reader.readLine() != null) lineCount++;
reader.close();

行数がわかったので、ランダムに 1 つを選択できます。

Random random = new Random();
int randomLineIndex = random.nextInt(lineCount);

次に、その行を取得して、残りを無視できます。

reader = new BufferedReader(new FileReader(file));
int i = 0;
while (++i < randomLineIndex)
    // Skip all lines before the one we want
    reader.readLine();
String randomLine = reader.readLine();
于 2012-12-19T22:13:51.590 に答える
2

これは私がそれを行う方法です。同じシードを使用して最大 2^48 の異なるゲームを再生できます。これにより、ファイルを読み取ったり、メモリにインデックスを保存したりする必要がなくなります。必要に応じてボードゲームを再現できます。

public class Baord {
    private static final int[] SHIP_SIZES = {4, 4, 3, 3, 3, 2, 2, 2, 2,  1, 1};
    public static final char EMPTY = '.';

    private final Random random;
    private final char[][] grid;
    private char letter = 'a';
    private final int width;
    private final int height;

    public Baord(int height,int width,  int seed) {
        this.width = width;
        this.height = height;
        this.random = new Random(seed);
        this.grid = new char[height][width];
        for (char[] chars : grid)
            Arrays.fill(chars, EMPTY);
        for (int len : SHIP_SIZES)
            placeShip(len);
    }

    private void placeShip(int len) {
        OUTER:
        while (true) {
            if (random.nextBoolean()) {
                // across
                int x = random.nextInt(width - len + 1);
                int y = random.nextInt(height);
                for (int j = Math.max(0, y - 1); j < Math.min(height, y + 2); j++)
                    for (int i = Math.max(0, x - 1); i < Math.min(width, x + len + 2); i++)
                        if (grid[j][i] > EMPTY)
                            continue OUTER;
                for (int i = 0; i < len; i++)
                    grid[y][x + i] = letter;

            } else {
                // down
                int y = random.nextInt(height - len + 1);
                int x = random.nextInt(width);
                for (int j = Math.max(0, x - 1); j < Math.min(width, x + 2); j++)
                    for (int i = Math.max(0, y - 1); i < Math.min(height, y + len + 2); i++)
                        if (grid[i][j] > EMPTY)
                            continue OUTER;
                for (int i = 0; i < len; i++)
                    grid[y + i][x] = letter;
            }
            break;
        }
        letter++;
    }

    public String toString() {
        StringBuilder ret = new StringBuilder();
        for (int y = 0; y < grid.length; y++)
            ret.append(new String(grid[y])).append("\n");
        return ret.toString();
    }

    public static void main(String... args) {
        for (int i = 0; i < 3; i++)
            System.out.println(new Baord(8, 16, i));
    }
}

版画

..........ddd...
.....aaaa.......
eee.......j.....
.............i..
..bbbb.......i.c
.......g.......c
.k.h...g...f...c
...h.......f....

ii..............
.....ccc..ff...b
...............b
aaaa...........b
.........j..h..b
....gg......h...
..............k.
eee..ddd........

.d.....eee.....f
.d.............f
.d..............
......j.ccc....g
.............b.g
.............b..
h.i..........b..
h.i...aaaa.k.b..

.......h....aaaa
.......h........
.e.........ccc..
.e..............
.e.bbbb...ii..ff
........d.......
.g..j...d.......
.g......d.....k.

............ccc.
.....bbbb.......
.ii.......k..d..
.....f.......d..
eee..f.......d..
................
.j.g........hh..
...g.aaaa.......
于 2012-12-19T22:24:59.210 に答える
0

キーと値が同じであるHashMapを使用しているのはなぜですか?あなたが説明しているのは、基本的に、各バケットに100文字の行が含まれている単なる配列です。ランダムマップを引き出すということは、1〜10000の間に乱数を作成し、そのバケットの内容を印刷することを意味します。

構文解析に関しては、構文の問題を解決するために、ファイル自体の例を確認する必要があります。

于 2012-12-19T21:58:44.553 に答える