Guten Abend,
ich benötige Hilfe beim einlesen einer Textdatei mithilfe des BufferredReaders, welches im Folgeschritt mein CharArray überschreiben soll.
Hier mein Quellqode :
Alles anzeigen
Jedoch komme ich nicht weiter, über Lösungsansätze würde ich mich freuen!
ich benötige Hilfe beim einlesen einer Textdatei mithilfe des BufferredReaders, welches im Folgeschritt mein CharArray überschreiben soll.
Hier mein Quellqode :
Java-Quellcode
- // needed to use Scanner for user input
- import java.util.Scanner;
- import java.io.BufferedReader;
- import java.io.File;
- 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 SokobanTesto {
- /**
- * The Main method for the Sokoban game with contains all of the game logic
- *
- * @param unused
- * is unused
- */
- public static void main(String[] unused) throws IOException {
- BufferedReader br;
- String line;
- FileReader fr;
- File soki = new File("soki.txt");
- br = new BufferedReader(new FileReader(soki));
- while ((line = br.readLine()) != null) {
- System.out.println(line);
- }
- // player position (horizontal)
- int xPlayer = 0;
- // player position (vertical)
- int yPlayer = 0;
- // create room
- char[][] room = new char[20][20];
- // initialize room with dots
- for (int x = 0; x < room.length; x++) {
- for (int y = 0; y < room[x].length; y++) {
- room[x][y] = '.';
- }
- }
- // set player start position in top left corner (origin)
- room[xPlayer][yPlayer] = 'P';
- // 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] = 'P'; // 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] = 'P';
- } else {
- System.out.println("You can not go there!");
- }
- break;
- case "a":
- case "left":
- if (xPlayer > 0) {
- room[xPlayer][yPlayer] = '.';
- xPlayer--;
- room[xPlayer][yPlayer] = 'P';
- } 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] = 'P';
- } 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");
- }
- }