2016-05-31 52 views
0

我無法讓我寫入文件代碼工作的錯誤消息 - 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(); 
    } 

回答

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)中的第一條語句。希望能幫助到你。