.txt Datei in einem Array speichern

actlyc

Mitglied
Hallo,

mein Programm erstellt ein Array (Spielfeld) in dem man eine Variable (Spieler) mit Konsoleneingaben bewegen kann. Nun möchte ich das Spielfeld über eine .txt Datei einlesen.
Dazu habe ich einen Bufferedreader mit einer Schleife eingebaut, der mir alle Zeilen der .txt Datei ausgibt.
Leider funktioniert das Programm nicht so wie ich möchte, da dass Array die .txt Datei nicht übernimmt, sondern dass Programm erstmal ein Array und danach die eingelesene Datei ausgibt.

Die .txt Datei sieht so aus:
-----------------------------------
######
# #
#@. #
# #
# #
######

Freue mich über jeden Ansatz

Hier mein Quellcode:

Code:
// needed to use Scanner for user input

import java.util.Scanner;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

/**
* This class is the first part for the Sokoban game with a very simple rule set.
*
* @author Jane Doe 1234567 Group 42h
* @author John Doe 1234567 Group 42h
*/
class SokobanTest {

    /**
     * The Main method for the Sokoban game with contains all of the game logic
     *
     * @param unused is unused
     */
    public static void main(String[] unused) {


        // player position (horizontal)
        int xPlayer = 0;
        // player position (vertical)
        int yPlayer = 0;

        // create room
        char[][] room = new char[6][6];


        BufferedReader br = null;
        String line;
        FileReader fr;

        try {
        br = new BufferedReader (new FileReader ("trivialmap.txt"));

            while((line = br.readLine())!= null ){
            System.out.println(line);
            }
        }
    catch (IOException e ) {
        e.printStackTrace();
    }

        // set player start position in top left corner (origin)
        room[xPlayer][yPlayer] = '@';

        // create new Scanner that reads from console
        Scanner scan = new Scanner(System.in);

        // flag if we quit the program
        boolean run = true;

        do {
            // print room row for row (thats why we start with y instead of x)
            for (int y = 0; y < room[0].length; y++) {
                for (int x = 0; x < room.length; x++) {
                    System.out.print(room[x][y]);
                }
                System.out.println();
            }

            System.out.println("Do you want to go up, down, left, right or exit the program?");

            // check which command was chosen and execute it
            switch (scan.next()) {
                case "w":
                case "up":
                    if (yPlayer > 0) {
                        room[xPlayer][yPlayer] = ' '; // set dot on old player position
                        yPlayer--; // move player to new place
                        room[xPlayer][yPlayer] = '@'; // set new player position
                    } else {
                        System.out.println("You can not go there!");
                    }
                    break;
                case "s":
                case "down":
                    if (yPlayer < room[0].length - 1) {
                        room[xPlayer][yPlayer] = ' ';
                        yPlayer++;
                        room[xPlayer][yPlayer] = '@';
                    } else {
                        System.out.println("You can not go there!");
                    }
                    break;
                case "a":
                case "left":
                    if (xPlayer > 0) {
                        room[xPlayer][yPlayer] = ' ';
                        xPlayer--;
                        room[xPlayer][yPlayer] = '@';
                    } else {
                        System.out.println("You can not go there!");
                    }
                    break;
                case "d":
                case "right":
                    if (xPlayer < room.length - 1) {
                        room[xPlayer][yPlayer] = ' ';
                        xPlayer++;
                        room[xPlayer][yPlayer] = '@';
                    } else {
                        System.out.println("You can not go there!");
                    }
                    break;
                case "exit":
                    run = false;
                    break;
                // if the user input is not one of our commands print help
                default:
                    System.out.println("Command unknown! Please type up, down, left or right to move or exit to quit this program");
            }
        } while (run);
        System.out.println("Goodbye");

    }
}
 
Wenn du die Text-Datei in dem Room speichern willst, dann müsstest du es so machen, denn bisher liest du nur die Datei zeilenweise in ein String line ein und gibst ihn aus, speicherst es aber nicht im Array:
Code:
int i=0;
  try
  {
      br = new BufferedReader(new FileReader("trivialmap.txt"));

      while ((line = br.readLine()) != null)
      {
          char[] charLine = line.toCharArray();
          for (int j = 0; j < charLine.length; j++)
          {
              room[i][j]=charLine[j];
          }
         System.out.println(line);
          i++
      }
  }

oder?
 

Zurück
Oben