我无法将写入文件的部分代码工作

时间:2016-05-31 17:22:00

标签: java eclipse text-files subroutine writetofile

我无法将文件代码写入错误消息 - writeFile无法解析。我试图将棋盘位置写入文本文件,以便可以保存游戏。如果用户选择保存游戏,那么它应该调用一个新的子程序,将子文件写入文件。

/*
     * Skeleton program code for the AQA COMP1 Summer 2016 examination
     * This code to be used in conjunction with the Preliminary Material
     * written by the AQA Programmer Team
     * Developed in the NetBeans 7.3.1. programming environment
     * Additional classes AQAConsole2016, AQAReadTextFile2016 and 
     * AQAWriteTextFile2016 may be used.
     * 
     * A package name may be chosen and private and public modifiers added - 
     * permission to make these changes to the Skeleton Program does not need 
     * to be obtained from AQA or the AQA Programmer
     */



    import java.util.Random;

    public class Aaa {
      AQAConsole2016 console = new AQAConsole2016();
      Random random = new Random();
      int boardSize;
      public Aaa() {
        char choice;
        String playerName;
    //    int boardSize;
        boardSize = 6;
        playerName = "";
        do {
          displayMenu();
          choice = getMenuChoice(playerName);
          switch (choice) {
            case 'p' : playGame(playerName, boardSize);
                       break;
            case 'e' : playerName = getPlayersName();
                       break;
            case 'c' : boardSize = changeBoardSize();
                       break;
          }
        } while (choice != 'q');
      }

      void setUpGameBoard(char[][] board, int boardSize) {
        for (int row = 1; row <= boardSize; row++) {
          for (int column = 1; column <= boardSize; column++) {
            if (row == (boardSize + 1) / 2 && column == (boardSize + 1) / 2 + 1 || column == (boardSize + 1) / 2 && row == (boardSize + 1) / 2 + 1) {
              board[row][column] = 'C';
            } else {
              if (row == (boardSize + 1) / 2 + 1 && column == (boardSize + 1) / 2 + 1 || column == (boardSize + 1) / 2 && row == (boardSize + 1) / 2) {
                board[row][column] = 'H';
              } else {
                board[row][column] = ' ';
              }
            }
          }
        }
      }

      int changeBoardSize() {
        int boardSize;
        do {
          console.print("Enter a board size (between 4 and 9): ");
          boardSize = console.readInteger("");
        } while (!(boardSize >= 4 && boardSize <= 9));
        return boardSize;
      }

      int getHumanPlayerMove(String playerName) {
        int coordinates;
        console.print(playerName + " enter the coordinates of the square where you want to place your piece: ");
        coordinates = console.readInteger("");
        return coordinates;
      }

      int getComputerPlayerMove(int boardSize) {
        return ((random.nextInt(boardSize) + 1) * 10 + (random.nextInt(boardSize) + 1));
      }

      boolean gameOver(char[][] board, int boardSize) {
        for (int row = 1; row <= boardSize; row++) {
          for (int column = 1; column <= boardSize; column++) {
            if (board[row][column] == ' ')
              return false;
            }
          }
        return true;
      }

      String getPlayersName() {
        String playerName;
        console.print("What is your name? ");
        playerName = console.readLine();
        return playerName;
      }

      boolean checkIfMoveIsValid(char[][] board, int move) {
        int row;
        int column;
        boolean moveIsValid;
        row = move % 10;
        column = move / 10;
        moveIsValid = false;
        if (((row<=boardSize) &&(row>0)) && ((column<=boardSize) && (column>0))){
        if (board[row][column] == ' ') {
          moveIsValid = true;
        }
        }
        return moveIsValid;
      }

      int getPlayerScore(char[][] board, int boardSize, char piece) {
        int score;
        score = 0;
        for (int row = 1; row <= boardSize; row++) {
          for (int column = 1; column <= boardSize; column++) {
            if (board[row][column] == piece) {
              score = score + 1;
            }
          }
        }
        return score;
      }

      boolean checkIfThereArePiecesToFlip(char[][] board, int boardSize, int startRow, int startColumn, int rowDirection, int columnDirection) {
        int rowCount;
        int columnCount;
        boolean flipStillPossible;
        boolean flipFound;
        boolean opponentPieceFound;
        rowCount = startRow + rowDirection;
        columnCount = startColumn + columnDirection;
        flipStillPossible = true;
        flipFound = false;
        opponentPieceFound = false;
        while (rowCount <= boardSize && rowCount >= 1 && columnCount >= 1 && columnCount <= boardSize && flipStillPossible && !flipFound ) {
          if (board[rowCount][columnCount] == ' ') {
            flipStillPossible = false;
          } else {
            if (board[rowCount][columnCount] != board[startRow][startColumn]) {
              opponentPieceFound = true;
            } else {
              if (board[rowCount][columnCount] == board[startRow][startColumn] && !opponentPieceFound) {
                flipStillPossible = false;
              } else {
                flipFound = true;
              }
            }
          }
          rowCount = rowCount + rowDirection;
          columnCount = columnCount + columnDirection;
        }
        return flipFound;
      }

      void flipOpponentPiecesInOneDirection(char[][] board, int boardSize, int startRow, int startColumn, int rowDirection, int columnDirection) {
        int rowCount;
        int columnCount;
        boolean flipFound;
        flipFound = checkIfThereArePiecesToFlip(board, boardSize, startRow, startColumn, rowDirection, columnDirection);
        if (flipFound) {
          rowCount = startRow + rowDirection;
          columnCount = startColumn + columnDirection;
          while (board[rowCount][columnCount] != ' ' && board[rowCount][columnCount] != board[startRow][startColumn]) {
            if (board[rowCount][columnCount] == 'H') {
              board[rowCount][columnCount] = 'C';
            } else {
              board[rowCount][columnCount] = 'H';
            }
            rowCount = rowCount + rowDirection;
            columnCount = columnCount + columnDirection;
          }
        }
      }

      void makeMove(char[][] board, int boardSize, int move, boolean humanPlayersTurn) {
        int row;
        int column;
        row = move % 10;
        column = move / 10;
        if (humanPlayersTurn) {
          board[row][column] = 'H';
        } else {
          board[row][column] = 'C';
        }
        flipOpponentPiecesInOneDirection(board, boardSize, row, column, 1, 0);
        flipOpponentPiecesInOneDirection(board, boardSize, row, column, -1, 0);
        flipOpponentPiecesInOneDirection(board, boardSize, row, column, 0, 1);
        flipOpponentPiecesInOneDirection(board, boardSize, row, column, 0, -1);
      }

      void printLine(int boardSize) {
        console.print("   ");
        for (int count = 1; count <= boardSize * 2 - 1; count++) {
          console.print("_");
        }
        console.println();
      }

      void displayGameBoard(char[][] board, int boardSize) {
        console.println();
        console.print("  ");
        for (int column = 1; column <= boardSize; column++)
        {
          console.print(" ");
          console.print(column);
        }
        console.println();
        printLine(boardSize);
        for (int row = 1; row <= boardSize; row++) {
          console.print(row);
          console.print(" ");
          for (int column = 1; column <= boardSize; column++) {
            console.print("|");
            console.print(board[row][column]);
          }
          console.println("|");
          printLine(boardSize);
          console.println();
        }
      }

      void displayMenu() {
        console.println("(p)lay game");
        console.println("(e)nter name");
        console.println("(c)hange board size");
        console.println("(q)uit");
        console.println();
      }

      char getMenuChoice(String playerName) {
        char choice;
        console.print(playerName + " enter the letter of your chosen option: ");
        choice = console.readChar();
        return choice;
      }

      void playGame(String playerName, int boardSize) {
        char[][] board = new char[boardSize + 1][boardSize + 1];
        boolean humanPlayersTurn;
        int move;
        int humanPlayerScore;
        int computerPlayerScore;
        boolean moveIsValid;
        setUpGameBoard(board, boardSize);
        humanPlayersTurn = false;
        int NoOfMoves=0;
        do {
          humanPlayersTurn = !humanPlayersTurn;
          displayGameBoard(board, boardSize);
          moveIsValid = false;
          do {
            if (humanPlayersTurn) {
              move = getHumanPlayerMove(playerName);
            } else {
              move = getComputerPlayerMove(boardSize);
            }
            moveIsValid = checkIfMoveIsValid(board, move);
          } while (!moveIsValid);
          if (!humanPlayersTurn) {
              NoOfMoves++;
              console.println("The number of moves completed so far: " +NoOfMoves);
            console.print("Press the Enter key and the computer will make its move");
            console.readLine("");
          }


          makeMove(board, boardSize, move, humanPlayersTurn);
          console.println();
          String answer = console.readLine("Do you want to save the board? (y/n)");
          if (answer.equalsIgnoreCase("y")){
            writeBoard(board, boardSize);
            console.println("Saved!");
          }
        } while (!gameOver(board, boardSize));
        displayGameBoard(board, boardSize);
        humanPlayerScore = getPlayerScore(board, boardSize, 'H');
        computerPlayerScore = getPlayerScore(board, boardSize, 'C');
        if (humanPlayerScore > computerPlayerScore) {
          console.println("Well done, " + playerName + ", you have won the game!");
        }
        else {
          if (humanPlayerScore == computerPlayerScore) {
            console.println("that was a draw!");
          } else {
            console.println("The computer has won the game!");
          }
          console.println();
        }
      }

    void writeBoard(char[][] board, int boardSize) {
        String filename = "myFile.txt";
        String piece = null;
        writeFile.openFile(filename);
        for(int row=1; row<=boardSize; row++){
            for (int column = 1; column <= boardSize; column++) {
                piece= Character.toString(board [row][column]);
                writeFile.writeToTextFile(piece);
            }
        }
        writeFile.closeFile();
    }
      public static void main(String[] args) {
        new Aaa();
      }
    }

此部分包含错误:

 void writeBoard(char[][] board, int boardSize) {
        String filename = "myFile.txt";
        String piece = null;
        writeFile.openFile(filename);
        for(int row=1; row<=boardSize; row++){
            for (int column = 1; column <= boardSize; column++) {
                piece= Character.toString(board [row][column]);
                writeFile.writeToTextFile(piece);
            }
        }
        writeFile.closeFile();
    }

1 个答案:

答案 0 :(得分:0)

有问题的部分包含一个名为writeFile的变量,无法解析。在您共享的类Aaa中没有任何位置,此变量既未声明也未使用其类型的实例进行初始化。所以方法void writeBoard(char[][] board, int boardSize)给出了编译错误。

从这个变量的用法来看,很明显它属于一个具有以下实例方法的类:

1)openFile(String filename)

2)writeToTextFile(String piece)

3)closeFile()

请搜索具有上述方法的类,并查找创建该实例的方法作为方法void writeBoard(char[][] board, int boardSize)中的第一个语句。希望能帮助到你。