Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/378.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Tictaoe游戏的边界外性感觉;问题:阵列?_Java_Arrays_Exception Handling_Arraylist - Fatal编程技术网

Java Tictaoe游戏的边界外性感觉;问题:阵列?

Java Tictaoe游戏的边界外性感觉;问题:阵列?,java,arrays,exception-handling,arraylist,Java,Arrays,Exception Handling,Arraylist,我开始写一个井字游戏。我刚刚运行了它,得到了以下堆栈跟踪: Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 at java.util.ArrayList.rangeCheck(ArrayList.java:571) at java.util.ArrayList.get(ArrayList.java:349) at TicTacToe.isMarked(TicTacToe.j

我开始写一个井字游戏。我刚刚运行了它,得到了以下堆栈跟踪:

 Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
 at java.util.ArrayList.rangeCheck(ArrayList.java:571)
 at java.util.ArrayList.get(ArrayList.java:349)
 at TicTacToe.isMarked(TicTacToe.java:23)
 at TicTacToe.mark(TicTacToe.java:59)
 at TicTacToe.main(TicTacToe.java:7)
我怀疑这是ArrayList设置方式的问题?我在某个地方读到过关于空值导致问题的文章,但这是我第一次处理数组,所以我对这个主题不太熟悉。无论如何,这是我的代码:

    import java.util.*;

    public class TicTacToe {

    public static void main(String[] args) {
    newBoard();
    ******************System.out.println(mark(1));************
    System.out.println(mark(5));
    System.out.println(mark(9));
   }

 // Creates a blank board.
 public static ArrayList<String> newBoard() {
  ArrayList<String> board = new ArrayList<String>(8);
  return board;
 }

 // Returns true if the square has been marked.
 public static boolean isMarked(int numberOfSquare) {
  if (numberOfSquare > 9 || numberOfSquare < 1) {
   throw new IllegalArgumentException("Input a valid square number.");
  }
  ************if (newBoard().get(numberOfSquare - 1) == null) {***********
   return false;
  } else
   return true;
 }

 // Returns the number of moves that have been made.
 public static int moveCount() {
  return countMove();
 }

 // If called, adds 1 to number of moves.
 public static int countMove() {
  int moveNumber = 0;
  moveNumber++;
  return moveNumber;
 }

 // Checks for a win at the specified array location and player (X or O).
 public static boolean checkForWin(int x, int y, int z, int player) {
  if (player == 0) {
   return (newBoard().get(x)).equals("O")
     && (newBoard().get(y)).equals("O")
     && (newBoard().get(y)).equals("O");
  } else {
   return (newBoard().get(x)).equals("O")
     && (newBoard().get(y)).equals("O")
     && (newBoard().get(y)).equals("O");
  }
 }

 // Places an X or O on the specified square.
 public static boolean mark(int markSquareNumber) {
  if (markSquareNumber > 9 || markSquareNumber < 1) {
   throw new IllegalArgumentException("Input a valid square number.");
  }
  ***********if (isMarked(markSquareNumber)) {*******************
   throw new IllegalArgumentException("Square is already marked.");
  }
  if (moveCount() % 2 != 0) {
   newBoard().add(markSquareNumber - 1, "X");
   countMove();
  } else {
   newBoard().add(markSquareNumber - 1, "O");
   countMove();
  }
  if (checkForWin(0, 1, 2, 1) || checkForWin(3, 4, 5, 1)
    || checkForWin(6, 7, 8, 1)) {
   System.out.println("Player-X just won horizontally!");
   return true;
  } else if (checkForWin(0, 3, 6, 1) || checkForWin(1, 4, 7, 1)
    || checkForWin(2, 5, 8, 1)) {
   System.out.println("Player-X just won vertically!");
   return true;
  } else if (checkForWin(0, 4, 5, 1) || checkForWin(2, 4, 6, 1)
    || checkForWin(0, 4, 8, 1)) {
   System.out.println("Player-X just won diagonally!");
   return true;
  }
  if (checkForWin(0, 1, 2, 0) || checkForWin(3, 4, 5, 0)
    || checkForWin(6, 7, 8, 0)) {
   System.out.println("Player-O just won horizontally!");
   return true;
  } else if (checkForWin(0, 3, 6, 0) || checkForWin(1, 4, 7, 0)
    || checkForWin(2, 5, 8, 0)) {
   System.out.println("Player-O just won vertically!");
   return true;
  } else if (checkForWin(0, 4, 5, 0) || checkForWin(2, 4, 6, 0)
    || checkForWin(0, 4, 8, 0)) {
   System.out.println("Player-O just won diagonally!");
   return true;
  } else
   return false;
 }
}
import java.util.*;
公共类Tictatcoe{
公共静态void main(字符串[]args){
新板();
******************系统输出打印LN(标记(1))************
系统输出打印LN(标记(5));
系统输出打印LN(标记(9));
}
//创建一个空白板。
公共静态数组列表newBoard(){
ArrayList board=新的ArrayList(8);
返回板;
}
//如果正方形已标记,则返回true。
公共静态布尔标记(int numberOfSquare){
如果(numberOfSquare>9 | | numberOfSquare<1){
抛出新的IllegalArgumentException(“输入有效的平方数”);
}
************if(newBoard().get(numberOfSquare-1)==null){***********
返回false;
}否则
返回true;
}
//返回已执行的移动数。
公共静态int moveCount(){
返回countMove();
}
//如果调用,则移动次数增加1。
公共静态int countMove(){
int moveNumber=0;
moveNumber++;
返回号码;
}
//在指定的阵列位置和玩家(X或O)检查是否获胜。
公共静态布尔checkForWin(整数x,整数y,整数z,整数播放器){
如果(玩家==0){
返回(newBoard().get(x)).equals(“O”)
&&(newBoard().get(y)).equals(“O”)
&&(newBoard().get(y)).equals(“O”);
}否则{
返回(newBoard().get(x)).equals(“O”)
&&(newBoard().get(y)).equals(“O”)
&&(newBoard().get(y)).equals(“O”);
}
}
//在指定的正方形上放置X或O。
公共静态布尔标记(int-markSquareNumber){
如果(markSquareNumber>9 | | markSquareNumber<1){
抛出新的IllegalArgumentException(“输入有效的平方数”);
}
***********如果(标记为(markSquareNumber)){*******************
抛出新的IllegalArgumentException(“正方形已标记”);
}
如果(moveCount()%2!=0){
新板()。添加(markSquareNumber-1,“X”);
countMove();
}否则{
新板()。添加(markSquareNumber-1,“O”);
countMove();
}
if(checkForWin(0,1,2,1)| | checkForWin(3,4,5,1)
||checkForWin(6,7,8,1)){
System.out.println(“Player-X刚刚横向获胜!”);
返回true;
}else if(checkForWin(0,3,6,1)| checkForWin(1,4,7,1)
||checkForWin(2,5,8,1)){
System.out.println(“Player-X刚刚垂直获胜!”);
返回true;
}else if(checkForWin(0,4,5,1)| checkForWin(2,4,6,1)
||checkForWin(0,4,8,1)){
System.out.println(“玩家X刚刚对角获胜!”);
返回true;
}
如果(checkForWin(0,1,2,0)| | checkForWin(3,4,5,0)
||checkForWin(6,7,8,0)){
System.out.println(“Player-O刚刚横向获胜!”);
返回true;
}else if(checkForWin(0,3,6,0)| | checkForWin(1,4,7,0)
||checkForWin(2,5,8,0)){
System.out.println(“Player-O刚刚垂直获胜!”);
返回true;
}else if(checkForWin(0,4,5,0)| | checkForWin(2,4,6,0)
||checkForWin(0,4,8,0)){
System.out.println(“Player-O刚刚对角获胜!”);
返回true;
}否则
返回false;
}
}
我只是在堆栈跟踪中出现的行旁边放了一大堆星号。如果有人能指出我哪里出了问题,那就太好了,谢谢

好的,这是我在你所有精彩的输入之后提出的解决方案:(请仅用于教育和参考目的,如果你在我的CS1410课堂上,你抄袭我,我不想被我的教授骂!!!!)

//由JTN为分配编写7.3-CS1410;2010年10月。
导入java.util.*;
公共类Tictatcoe{
私有静态int moveNumber=0;
私有静态ArrayList板=新ArrayList(8);
公共静态void main(字符串[]args){
新板();
标记(1);标记(2);
标记(5);标记(3);
马克(9);
boardString();
}
//返回已执行的移动数。
公共静态int moveCount(){
返回(countMove()-1);
}
//如果调用,则移动次数增加1。
公共静态int countMove(){
moveNumber=moveNumber+1;
返回号码;
}
//创建一个空白板。
公共静态数组列表newBoard(){
对于(int i=0;i 9 | | numberOfSquare<1){
抛出新的IllegalArgumentException(“输入有效的平方数”);
}
如果((board.get(numberOfSquare-1)).equals(“”)){
返回false;
}否则
返回true;
}
//在指定的阵列位置和玩家(X或O)检查是否获胜。
公共静态布尔checkForWin(整数x,整数y,整数z,整数播放器){
如果(玩家==0){
返回(board.get(x)).equals(“O”)
&&(board.get(y))等于(“O”)
&&(董事会获得(z))等于(“O”);
} 
否则{
返回(board.get(x))等于(“x”)
&&(board.get(y))等于(“X”)
&&(b.get(z))等于(“X”);
}
}
//在指定的正方形上放置X或O。
公共静态布尔标记(int-markSquareNumber){
如果(markSquareNumber>9 | | markSquareNumber<1){
抛出新的IllegalArgumentException(“输入有效的平方数”);
}
如果(标记为(markSquareNumber)){
抛出新的IllegalArgumentException(“正方形已标记”);
}       
如果((countMove()%2)==0){
板组(markSquareNumber-1,“O”);
}
否则{
板组(markSquareNumber-1,“X”);
} 
if(checkForWin(0,1,2,1)| | checkForWin(3,4,5,1)
||checkForWin(6,7,8,1)){
//Written by JTN for Assignment7.3- CS1410; October 2010. 
import java.util.*;

public class TicTacToe {
private static int moveNumber = 0;
private static ArrayList<String> board = new ArrayList<String>(8);
    public static void main(String[] args) {
        newBoard();
        mark(1);mark(2);
        mark(5);mark(3);
        mark(9);
        boardString();
    }
    // Returns the number of moves that have been made.
    public static int moveCount() {
        return (countMove()-1);
    }

    // If called, adds 1 to number of moves.
    public static int countMove() {
        moveNumber= moveNumber + 1;
        return moveNumber;
    }
    // Creates a blank board.
    public static ArrayList<String> newBoard() {
        for (int i = 0; i <= 8; i++)
            board.add("_");
        return board;
    }

    // Returns true if the square has been marked.
    public static boolean isMarked(int numberOfSquare) {
        if (numberOfSquare > 9 || numberOfSquare < 1) {
            throw new IllegalArgumentException("Input a valid square number.");
        }
        if ((board.get(numberOfSquare - 1)).equals("_")) {
            return false;
        } else
            return true;
    }



    // Checks for a win at the specified array location and player (X or O).
    public static boolean checkForWin(int x, int y, int z, int player) {
        if (player == 0) {
            return     (board.get(x)).equals("O")
                    && (board.get(y)).equals("O")
                    && (board.get(z)).equals("O");
        } 
        else {
            return     (board.get(x)).equals("X")
                    && (board.get(y)).equals("X")
                    && (board.get(z)).equals("X");
        }
    }

    // Places an X or O on the specified square.
    public static boolean mark(int markSquareNumber) {
        if (markSquareNumber > 9 || markSquareNumber < 1) {
            throw new IllegalArgumentException("Input a valid square number.");
        }
        if (isMarked(markSquareNumber)) {
            throw new IllegalArgumentException("Square is already marked.");
        }       
        if ((countMove() % 2) == 0){
            board.set(markSquareNumber - 1, "O");
        }
        else {
            board.set(markSquareNumber - 1, "X");
        } 

        if (checkForWin(0, 1, 2, 1) || checkForWin(3, 4, 5, 1)
                || checkForWin(6, 7, 8, 1)) {
            System.out.println("Player-X just won horizontally!");
            return true;
        } else if (checkForWin(0, 3, 6, 1) || checkForWin(1, 4, 7, 1)
                || checkForWin(2, 5, 8, 1)) {
            System.out.println("Player-X just won vertically!");
            return true;
        } else if (checkForWin(0, 4, 5, 1) || checkForWin(2, 4, 6, 1)
                || checkForWin(0, 4, 8, 1)) {
            System.out.println("Player-X just won diagonally!");
            return true;
        }
         else if (checkForWin(0, 1, 2, 0) || checkForWin(3, 4, 5, 0)
                || checkForWin(6, 7, 8, 0)) {
            System.out.println("Player-O just won horizontally!");
            return true;
        } else if (checkForWin(0, 3, 6, 0) || checkForWin(1, 4, 7, 0)
                || checkForWin(2, 5, 8, 0)) {
            System.out.println("Player-O just won vertically!");
            return true;
        } else if (checkForWin(0, 4, 5, 0) || checkForWin(2, 4, 6, 0)
                || checkForWin(0, 4, 8, 0)) {
            System.out.println("Player-O just won diagonally!");
            return true;
        } else
            return false;
    }

    public static String boardString(){
        String row1 = board.get(0)+"|"+board.get(1)+"|"+board.get(2);
        String row2 = board.get(3)+"|"+board.get(4)+"|"+board.get(5);
        String row3 = board.get(6)+"|"+board.get(7)+"|"+board.get(8);
        System.out.println(row1);
        System.out.println(row2);
        System.out.println(row3);
        return row1+row2+row3;
    }
}
ArrayList<String> board = new ArrayList<String>(8);
public static ArrayList<String> newBoard() {
    ArrayList<String> board = new ArrayList<String>(8);
    for (int i = 0; i < 9; i++)
        board.add("");
    return board;
}
newBoard().add(markSquareNumber - 1, "X");
newBoard().set(markSquareNumber - 1, "X");
public static ArrayList newBoard() { 
    ArrayList board = new ArrayList(8); 
    return board; 
}
public static ArrayList newBoard() { 
    ArrayList<String> board = new ArrayList<String>();
    for (int i = 0; i < 9; i++) {
        board.add("");
    }
    return board; 
}
String[] board = new String[9];
newBoard().get(numberOfSquare - 1) == null)