【问题标题】:Java tic tac toe爪哇井字游戏
【发布时间】:2013-10-22 23:47:08
【问题描述】:

嗨,我正在写一个井字游戏。我已经在我的代码中用 cmets 拼出了我需要的东西。我现在遇到的问题是制作一个 getMove 方法。我假设我需要在按下行和列后在 if/else 语句中调用 getMove 方法?

我不确定如何从获取行号/列号并将它们放入我的板上 对于用户输入的内容。

这是我的代码:

import java.util.*;

public class TicTac{
   //declare a constant variable
   public static final int SIZE = 3; //size of each row and each column

   public static void main(String[] args) {

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

      //display the board
      displayBoard(board);

      //prompt for the first player
      //determine if X or O is pressed

      System.out.println("Who wants to go first (X or O)? ");
      Scanner xOrO = new Scanner(System.in);
      String entOp = xOrO.nextLine();
      char enterOp = entOp.charAt(0);

      if (enterOp == 'X'){
           System.out.println("Enter a row (0,1,2) for player X: ");
           Scanner enterRow = new Scanner(System.in);
           int fTurn = enterRow.nextInt();

           System.out.println("Enter a column (0,1,2) for player X: ");
           Scanner enterCol = new Scanner(System.in);
           int fCol = enterCol.nextInt();

      }  else if (enterOp == 'O') {
           System.out.println("Enter a row (0,1,2) for player O: ");
           Scanner enterRow = new Scanner(System.in);
           int fTurn = enterRow.nextInt();

           System.out.println("Enter a column (0,1,2) for player X: ");
           Scanner enterCol = new Scanner(System.in);
           int fCol = enterCol.nextInt();

      }  else {
           System.out.println("Must enter either X or O");  
         }

      //and display the board
      //displayBoard(board);

      }

   //initializeBoard method


   //displayBoard method
   public static void drawLine() {
      for (int i = 0; i <= 9 * SIZE; i++) {
         System.out.print("-");
      }
      System.out.println();
   }

   public static void displayBoard(char[][] board) {
      drawLine();
      for (int i = 0; i < SIZE; i++) {
         for (int j = 0; j < SIZE; j++) {
            System.out.print("| " + board[i][j] + " ");
         }
         System.out.println("|");
         drawLine();
      }
      System.out.println();
   }


   //getMove method: to prompt the current player for target position. And place the mark in the position if the position is available.
//    public static void getMove() {
//    
//    
//    
//    
//    
//    }
   //findWinner method: after each move, check the board see if there is a winner



   //hasEmptyCell method: check if there is still empty spot in the board
}

【问题讨论】:

  • 您可以在下面的帖子中参考我的答案。虽然我使用了一些不同的逻辑 - stackoverflow.com/questions/4020757/…
  • 你有棋盘的 x/y 坐标。您需要确定单元格中是否已经存在某些内容。如果有,您需要再次提示用户输入有效的行/列。如果没有,您只需要记录移动 (board[fCol][fTurn] = enterOp) 并重新绘制棋盘
  • 我有点理解。这又回到了我最初的问题,我将如何编写一个方法来获取 x/y 坐标并将它们放在板上?

标签: java arrays methods tic-tac-toe


【解决方案1】:

我会尝试这样的事情来取得进展。请记住,自从我玩过 java 以来,这已经有点过了。我假设 2D char 数组以空值开头。我建议将该声明更改为并将其移动到全局可能:

 char[][] board = {{'', '', ''},{'', '', ''},{'', '', ''}};

然后我会在第二次读取完成后调用 getMove:

int x = Integer.parseInt(firstTurn);
int y = Integer.parseInt(firstCol);
getMove(x, y, 'X');

你会想要捕捉一个异常,因为你不知道用户是否真的会输入一个整数。某种类型的循环(例如 while)可以很好地做到这一点。

你的 getMove 函数应该是这样的:

public static bool getMove(int x, int y, char player) {
    if (board[x][y] == '') {
        board[x][y] = player;
        return true;  //Here you are returning true to show the spot was available.
    }
    return false; //And here you are returning false to show the spot was not available.
}

【讨论】:

  • '' 不是有效字符
  • "将其移入全局"Java 没有全局变量。
  • 就像我说的,已经有一段时间了。我相信你可以弄清楚你需要做什么。例如,只需将 '' 更改为 'N' 或其他什么,只是为了测试。全局,我的意思是将它移出主函数,以便类中的其余函数可以访问它。如果不需要,请不要担心。
  • @AndrewJackson 我喜欢努力的人!我确实明白你所说的它是全局的,但是我的方法是静态的,可以在我的程序中的任何地方调用。我会尝试你所说的。
猜你喜欢
  • 1970-01-01
  • 2018-03-09
  • 2015-06-12
  • 2015-01-08
  • 1970-01-01
  • 2014-04-12
相关资源
最近更新 更多