Jgrasp Tic Tac Toe搜索二维数组

时间:2015-01-29 14:18:27

标签: arrays search jgrasp

今天我正在使用Java中的二维数组创建一个Tic-Tac-Toe程序。我已经编写了大部分代码并且对于" X"和" O"要在数组中设置。我似乎无法弄清楚如何搜索阵列来测试是否有胜利者。我目前的方法是:

if(board[0][0] && board[0][1] && board[0][2] == x)
   { 
      //Some player wins
   }

当然,这并没有给我带来我希望的结果。我很想解释如何检查我的阵列并调用一种获胜的方法。我恳请它不会为我完成,虽然这绝对太善良,但也不能让我进一步了解。非常感谢,我希望很快能收到你的回复!

程序:

import java.util.*;
import java.io.*;
import static java.lang.System.*;
public class TicTacToe
{
private String[][]board;
private static final int ROWS = 3;
private static final int COLUMNS = 3;

public TicTacToe()
{
  board = new String[ROWS][COLUMNS];
  for(int r=0; r<ROWS; r++)
     for(int c = 0; c<COLUMNS; c++)
        board[r][c] = " "; //fill array with spaces
}

public void set(int r, int c, String player)
{
  if (board[r][c].equals(" "))
     board[r][c] = player; //place the "x" or "o"
}

/* toString() creates a String representation of board, for example,
|x o|
| x |
| o|
*/
public String toString()
{
  String d = ""; //d is the display
  for(int r=0; r<ROWS; r++)
  {
     d = d + "|";
     for(int c = 0; c<COLUMNS; c++)
        d = d+board[r][c];
     d = d + "|\n";
  }
  return d;
}
/*PseudoCode for winner:
If three of same type match in diagonal, row, column, then return a winner      based on what varibale
EX: [0][0] [0][1] [0][2] all are X, therefore player with X wins
*/
public boolean winner(String player)
{
//Return Winner
}



public class TicTacToeDriver
{
  public void main(String [] args)
  {
     Scanner keyboard = new Scanner(System.in);
     String player = "x"; //first player
     TicTacToe game = new TicTacToe();
     boolean done = false;
     System.out.println(game); 
     while (!done)
     {
        System.out.print("Row for " + player + " (-1 TO EXIT): ");
        int row = keyboard.nextInt();

        if (row<0) //user wants to end the game
           done = true;
        else
        {
           System.out.print("Column for " + player + ": " );
           int col = keyboard.nextInt();

           game.set(row, col, player);//update board
           done = game.winner(player); //check for winner
           if(done)
              System.out.println("Player " + player + " is the winner");

           if(player.equals("x")) //change player
              player = "o";
           else
              player = "x";
        }
        System.out.println(game); //game over
     }
  }
}
}

1 个答案:

答案 0 :(得分:0)

您必须创建一个表或某种栅格。

https://www3.ntu.edu.sg/home/ehchua/programming/java/JavaGame_TicTacToe.html

  public static boolean hasWon(int theSeed, int currentRow, int currentCol) {
  return (board[currentRow][0] == theSeed         // 3-in-the-row
               && board[currentRow][1] == theSeed
               && board[currentRow][2] == theSeed
          || board[0][currentCol] == theSeed      // 3-in-the-column
               && board[1][currentCol] == theSeed
               && board[2][currentCol] == theSeed
          || currentRow == currentCol            // 3-in-the-diagonal
               && board[0][0] == theSeed
               && board[1][1] == theSeed
               && board[2][2] == theSeed
          || currentRow + currentCol == 2  // 3-in-the-opposite-diagonal
               && board[0][2] == theSeed
               && board[1][1] == theSeed
               && board[2][0] == theSeed);

}