为一个tic tac toe游戏编写findWinner()

时间:2015-02-25 23:37:38

标签: java

我正在写一个tictactoe游戏而且我一直在编写findWinner()。以下是我所拥有的,但当玩家赢得任何对角线时它不会做任何事情。

我使用access1access2来评估数组的所有成员是否相等。由于循环发生了2次,如果它们等于2,那么我返回true。谢谢

public static boolean diagonalTest()
{
  /*create a TicTacToe object to access private fields*/
  TicTacToe temp = new TicTacToe();


  /*an ArrayList to store the values*/
  ArrayList<Character> major = new ArrayList<Character>();

  int k = 0;
  while(k < temp.getRow())
  {
     major.add(temp.getBoard()[k][k]);
     k++;
  }   

  /*to test whether the values in the array are equal to each other*/
  int access1 = 0;
  for(int i = 1; i < major.size(); i++)
     if(major.get(0) != ' ' && major.get(0) == major.get(i))
        access1++;

  ArrayList<Character> minor = new ArrayList<Character>();

  k = 0;
  while(k < temp.getRow())
  {
     minor.add(temp.getBoard()[k][temp.getColumn() - 1 - k]);
     k++;      
  }

  int access2 = 0;
  for(int a = 1; a < minor.size(); a++)
     if(minor.get(0) != ' ' && minor.get(0) == minor.get(a))
        access2++;

  if(access1 == 2 || access2 == 2)
     return true;      

 return false;

}

0 个答案:

没有答案