簡體   English   中英

2D陣列:TicTacToe程序

[英]2D Arrays: TicTacToe program

我下面的程序是一個Tic Tac Toe游戲。 以下是作業的詳細信息:

井字游戲

  • 將游戲板存儲在單個2D陣列中。 (3 x 3)X
  • 添加移動的方法。
  • 顯示板的方法。
  • 判斷輪到誰的方法(X或O)。
  • 找到贏家或平局的方法。
  • 將游戲初始化到開始的方法。
  • 允許兩個玩家在同一鍵盤上輸入回合的主要方法。

我的問題是,我不知道如何獲取用戶輸入的坐標並將其轉換為“ X”或“ O”值,以供用戶將其轉換為數組,然后在數組顯示給用戶后,將其顯示在板上發揮作用。 在線上有編譯錯誤; ticTac.showBoard(char [] [] displayArray);. 絕對歡迎您提出任何其他關於如何簡化事物或錯誤的注釋和錯誤!

public class TicTacToeMain //main class that runs the system.
    {
      public static void main(String[] args)
      {

        System.out.println("             TIC TAC TOE");
        System.out.println();
        System.out.println("Instruction: You will be asked to enter the row number(0-2) and the column number(0-2) of");
        System.out.println("the board you wish to play your piece. You are to decide which player is X's and O's and");
        System.out.println("to move as prompted. X's always start first. To win you need to place 3 pieces in a row ");
        System.out.println("horizontally, vertically, or diagonally. An example of the board layout is below. Enjoy!");
        System.out.println();
        System.out.println("  0   1   2");
        System.out.println();
        System.out.println("0");
        System.out.println();
        System.out.println("1");
        System.out.println();
        System.out.println("2");
        System.out.println();

        TicTacToe ticTac = new TicTacToe();
        ticTac.showBoard(char[][] displayArray); //SYNTAX ERROR ON TOKEN "char" and "displayArray".
        ticTac.readInput();



      }
    }





import java.util.Scanner;

public class TicTacToe //helper methods class.
{
  private int moveCount;
  private char playerTurn;
  private int row, col;
  private char[][] board = new char[3][3];

  public TicTacToe() //constructor method
  { 
    char[][] board = new char[3][3];
    for(char row = 0 ; row < 3; row++)
      for(int col = 0; col < 3; col++)
      board[row][col] = ' ';
   playerTurn = 'X';
    moveCount = 0;



  }
   public void findResult() //constructor to find winner/tie and print to user.
  {
     this.setPlayerTurn();


     if(board[row][0] == board[row][1] && board[row][1] == board[row][2] && (board[row][0] == 'X' || board[row][0] == 'O'))
      System.out.println( + playerTurn + " wins!");

    else if(board[0][col] == board[1][col] && board[1][col] == board[2][col] && (board[0][col] == 'X' || board[0][col] == 'O'))
      System.out.println( + playerTurn + " wins!");

    else if(board[0][0] == board[1][1] && board[1][1] == board[2][2] && (board[0][0] == 'X' || board [0][0] == 'O'))
      System.out.println( + playerTurn + " wins!");

    else if(board[2][0] == board [1][1] && board[1][1] == board[0][2] && (board [2][0] == 'X' || board[2][0] == 'O'))
      System.out.println( + playerTurn + " wins!");

    else if(moveCount == 9)
     System.out.println("Tie game!");

}

 public void readInput() //method to read user input.
  {
    int newRow, newCol;
    this.setPlayerTurn();
    do
    {
    Scanner keyboard = new Scanner(System.in);

      this.setPlayerTurn();
    System.out.println("Turn " + moveCount);
    System.out.println("Player " + playerTurn + " please select the row you wish to place your next move.");
    newRow = keyboard.nextInt();
    if(newRow < 0 || newRow > 2)
       System.out.println("Invalid Entry. Please re-enter.");
    else
    {
      row = newRow;
    System.out.println("Now, enter the column.");
    newCol = keyboard.nextInt();
    if(newCol > 2 || newCol < 0)
    System.out.println("Invalid Entry. Please re-enter.");
    else
      col = newCol;
      moveCount++;
      System.out.println("You entered row " + row + " and column " + col +".");
      System.out.println();
      findResult();
    }
    }while(moveCount <= 8);


  }

  public  void showBoard(char[][]displayArray) //to add inputs to as well as  display board.
  {
    int rowInput, colInput;
    readInput();
    rowInput = row;
    colInput = col;
    for(rowInput = 0; rowInput < displayArray.length; row++)
     {
      for(colInput = 0; colInput < displayArray[row].length; col++)
      System.out.print(" " + displayArray[row][col] + " ");
      System.out.println();
    }
  }

  private char setPlayerTurn() //method to find which players turn it is.
  {
    {
    if (moveCount == 0 || moveCount % 2 == 0)
      playerTurn = 'X'; 
    else
      playerTurn = 'O';
    }
    return playerTurn;



}

}

將您的通話更改為主叫板

ticTac.showBoard(); 

由於您已經在TicTacToe對象中放置了板子。

然后showBoard()應該看起來像這樣:

 public  void showBoard() //to add inputs to and display board.
  {
    int rowInput, colInput;
    readInput();
    rowInput = row;
    colInput = col;
    for(rowInput = 0; rowInput < board.length; row++)
     {
      for(colInput = 0; colInput < board[row].length; col++)
      System.out.print(" " + board[row][col] + " ");
      System.out.println();
    }
  }

這樣做的原因是因為您的TicTacToe對象已經有一個木板,您無需將其傳遞給它。

另外,作為另一個提示,您不需要其他類來運行TicTacToe。 您可以簡單地將TicTacToe的主體放在里面,如下所示:

public class TicTacToe {

public TicTacToe(...){
...
}
public static void main(String[] args){
...read inputs and make board here...
}

...other methods...

}

首先在類的主體中創建類的實例有點令人困惑,但是您會習慣了……而且文件少了很多:-)。

不過,在進行此操作時要注意一點,您確實需要創建類的對象才能在main中使用其方法,因為main是“靜態”方法。 靜態方法是在程序中所有其他事物之前分配的,而main是在Java程序中運行的第一個方法,因此,如果您不創建對象,則main不知道如何訪問其中的方法。 希望這不會引起更多的混亂。

作為另一個指針,如果您確實要創建一個新的2d char數組以傳遞給該方法,則可以這樣做

ticTac.showBoard(new char[3][3]);

我不知道如何獲取用戶輸入的坐標,然后將其轉換為x或o值,以供播放到數組中,然后顯示棋盤本身。

readInput您的工作已readInput 您具有從用戶輸入的rowcol坐標。 以相同的方式,當您顯示棋盤時查看board[row][col]中的值,而當檢查獲勝時則查看board[row][0]中的值,您想要將board陣列中的位置設置為當前玩家。 這可以通過說board[something][something] = value 什么somethingvalue應該是什么?

絕對歡迎您提出任何其他關於如何簡化事物或錯誤的注釋和錯誤!

看起來程序的幾乎所有部分都已經准備就緒。 但是,將所有內容聯系在一起的程序流程存在一些問題。 有一些編程原理可以在這里應用:

  • SRP(單一責任原則) :程序的每個單元都應該只有一個目的
  • SoC(關注點分離) :每個模塊或單元都應有清晰的分界線。 兩種方法不應部分重疊。

考慮到這些想法,我將對您的程序流程提供一些意見:

  1. 通常,您可以告訴您是否有一個干凈的設計,即方法的名稱是否確實是它們的實際用途。 例如, findResult()調用setPlayerTurn() 這具有誤導性,因為當前玩家每次只想查看游戲結果時都會更改。 您到底應該何時轉彎?
  2. 展示板是誰的工作? 您正在主程序中顯示板,但是您永遠不會看到,因為readInput()在到達showBoard()調用之前循環了8次。 顯示板可能是TicTacToe的責任。 main方法應該只是開始游戲。
  3. readInput()讀取輸入還是玩整個游戲? 聽起來應該讀取一組坐標輸入,但是它具有處理整個游戲的循環。 將其分為兩種方法可能更有意義-一種方法處理單個動作,另一種方法循環9次,顯示木板並調用readInput()方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM