2017-03-31 172 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