2017-03-31 90 views
-5

如何修復我的tic tac toe遊戲?下面的代碼是我所知道的。它運行,但沒有使用Eclipse霓虹燈Java eclipse tic tac toe遊戲

import java.util.Scanner; 

public class TicTacToeGame { 

// Declare variables 

    public char[][] board; //the game board 
    public boolean xTurn; //if true X's turn, if false O's turn 
    public Scanner keyboard; //reads user input 

    public TicTacToeGame() { 



    xTurn = true; 
    keyboard = new Scanner(System.in); 

     //create the board 
     board = new char[3][3]; 

     //initialize the board 
     for(int r = 0; r < 3; r++) { 

      for(int c = 0; c < 3; c++) 
       board[r][c] = ' '; 
     } 
    } 

     public void displayRow(int row) { 

      System.out.println(" " + board[row][0] + " | " + board[row][1] + " | " + board[row][2]); 
     } 


     //the rest of the game board 

     public void displayBoard() { 

      displayRow(0); 
      System.out.println("-----------"); 
      displayRow(1); 
      System.out.println("-----------"); 
      displayRow(2); 

      System.out.println("Which row, column would you like to move to? Enter two numbers between 0-2 separated by a space to indicate position."); 
     } 


     public boolean getMove() { 

      boolean invalid = true; 
      int row = 0, column = 0; 

      //user input 
      while(invalid) { 

       row = keyboard.nextInt(); 
       column = keyboard.nextInt(); 

       //check for valid spot 
       if(row >= 0 && row <= 2 && column >= 0 && column <= 2) { 

        //check that the position is not taken 
        if(board[row][column] != ' ') 
         System.out.println("That position is already taken"); 
        else 
         invalid = false; 
       } 
      } 

      //fill in the game board with the player mark 
      if(xTurn) 
       board[row][column] = 'X'; 
      else 
       board[row][column] = 'O'; 

      return winner(row,column); 
     } 


     //check for win 


     public boolean winner(int lastR, int lastC) { 

      boolean winner = false; // no winner 
      char symbol = board[lastR][lastC]; //last made mark 

      //check left-right 
      int numFound = 0; 
      for(int c = 0; c < 3; c++) { 
       if(board[lastR][c] == symbol) 
        numFound++; 
      } 

      if(numFound == 3) 
       winner = true; 

      //check up-down 
      numFound = 0; 
      for(int r = 0; r < 3; r++) { 
       if(board[r][lastC] == symbol) 
        numFound++; 
      } 

      if(numFound == 3) 
       winner = true; 

      //check both diagonals 
      numFound = 0; 
      for(int i = 0; i < 3; i++) { 
       if(board[i][i] == symbol) 
        numFound++; 
      } 

      if(numFound == 3) 
       winner = true; 

      numFound = 0; 
      for(int i = 0; i < 3; i++) { 
       if(board[i][2-i] == symbol) 
        numFound++; 
      } 

      if(numFound == 3) 
       winner = true; 

      return winner; 
     } 


     public boolean boardFull() { 

      //how many spots that are taken by each player 
      int numSpotsFilled = 0; 

      for(int r = 0; r < 3; r++) { 

       for(int c = 0; c < 3; c++) { 
        if(board[r][c] == 'X' || board[r][c] == 'O') 
         numSpotsFilled++; 
       } 
      } 

      return numSpotsFilled == 9; 

     } 

     //start game. 


     public void play() { 

      while(true) { 

       displayBoard(); 

       if(xTurn) 
        System.out.println("X's Turn!"); 
       else 
        System.out.println("O's Turn!"); 

       int choice = keyboard.nextInt(); 

       if(choice == 1) { 

        if(getMove()) { 
         // winner! 
         displayBoard(); //display winner board 

         if(xTurn) 
          System.out.println("X Wins!"); 
         else 
          System.out.println("O Wins!"); 

         System.exit(0); 
        } 
        else if(boardFull()) { 
         //tie 
         displayBoard(); //display tie board 

         System.out.println("Tie!"); 

         System.exit(0); 
        } 
        else { 
         //no winner 
         xTurn = !xTurn; //switch players 
        } 
       } 
      } 
    } 


    public static void main(String[] args){ 
        TicTacToeGame game = new TicTacToeGame(); 

        game.play(); 
       } 

     } 
+2

請參閱:[爲什麼「某人可以幫我解答問題?」)(https://meta.stackoverflow.com/questions/284236/why-is-can-someone-help-me-not-an-actual - 請說明什麼是不工作的,預期的輸出和實際輸出 – Frakcool

+1

隨意擴展*「它運行但不正確」* – shash678

+0

當它第一次運行板是在那裏,但你必須把col列 –

回答

1

在你play()方法你正在閱讀一個名爲choice變量,因爲它從來沒有使用過(但開始遊戲)properly.please help.I'm,如果去掉這些行:

int choice = keyboard.nextInt(); //Remove this line 

if (choice == 1) { //Remove this line 
    //Keep the code here 
} //Remove this line 

你的遊戲將正常運行。

否則,如果你運行一個輸入:例如2 0它永遠不會工作。

我也建議你總是用花括號爲您if-else/for/while /等指令。

+0

好吧我會試試這個的。 –

+0

我帶走了這些線條不會運行,我應該拿出別的東西嗎? –

+0

也許你在它之前拿出了一些其他的代碼,複製粘貼你發佈的代碼並刪除我說的代碼... – Frakcool