当前位置: 首页 > 知识库问答 >
问题:

构建跳棋游戏时出错

华永新
2023-03-14

错误消息:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
at neegjar.Play.canJump(Play.java:113)
at neegjar.Play.getLegalMoves(Play.java:278)
at neegjar.Board.printLegalMoves(Board.java:198)
at neegjar.main.main(main.java:17)
Java Result: 1

我正在尝试用java构建一个跳棋游戏(目前在控制台中)。我有6门课:

>

  • 板:显示并跟踪板。它是一个名为mySquare的Square对象的2d数组。

    CheckerMobile:包含来自X、fromY、toX、toY和布尔isAJump的变量。在Play中,我创建了一个CheckerMobile对象数组来找出哪些移动是有效的。

    单件:单个棋盘格件。每个玩家都有一个由12件物品组成的数组可供使用。包含变量xCoord、yCoord、isKing、isAlive和team。

    玩法:基本上是项目的骨干。处理什么是有效的移动,什么是“跳跃”等。它包含在下面。

    方形:方形对象是棋盘上的单个平铺。它包含变量squareColor、hasAPiece和hasARedPiece(player one)。

    main(包含main方法)

    当我尝试运行创建棋盘、放置棋子和打印合法移动的主方法时,一切正常,直到它尝试打印合法移动(它显示上面的错误消息)。

    为什么要这样做?我不知道。我将非常感谢您的帮助,并感谢您抽出时间阅读本文!

    游戏内容如下:

    package neegjar;
    import java.util.ArrayList;
    
    
    public class Play extends Board
    {
    
    public int currentPlayer;
    public boolean gameOver;
    public int turnNumber;
    
    
    
    public Play()
    {
        currentPlayer = 1;
        gameOver = false;
        turnNumber = 0;
    
    
    
    }
    
    public void takeTurn()
    {
        System.out.println("It is player "+ this.currentPlayer + "'s         turn");         
    //for Player 1
        have user select a piece
        System.out.println("Select a piece by typing and x coordinate,              followed by a y coordinate (seperated by commas");
        System.out.println("For an example input, type h");
    
    }
    
    //can the piece jump from x1,y1 TO x2,y2?
    public boolean canJump(int player, int x1, int y1, int x2, int y2) 
    {
        boolean CURRENT_KING = false;  //by default
        int CURRENT_PIECE = -1;
    
        if(player == 1)
        {
            if((x2 != x1+2) && (x2 != x1-2) && (y2 != y1+2) && (y2 != y1-2))   return false;
    
    
            for(int i=0; i<12; i++)
            {
                if((player1Pieces[i].getX() == x1)&&(player1Pieces[i].getY() == y1))
                {
                    CURRENT_PIECE = i;
                    break;  //get out of foor loop
                }                    
            }                
            if(player1Pieces[CURRENT_PIECE].isAKing() == true)
            {
                CURRENT_KING = true;    
    
            }
    
            //top left spot
            if((x2 == x1-2)&&(y2 == y1-2))
            {
                //first check to see if there is an enemy black piece on the adjacent square
                if((mySquare[y1-1][x1-1].getHasAPiece()==true)&&(mySquare[y1-1][x1-1].getHasARedPiece()==false))
                    if((mySquare[y1-2][x1-2].getHasAPiece()==true)||(y1-2 < 0)||(x1-2 < 0)) //check for a blocker
                    {    
                        return false;
                    }
                    else
                        return true;
            }
    
            //top right spot
            if((x2 == x1+2)&&(y2 == y1-2))
            {   
                //first check to see if there is an enemy black piece on the adjacent square
                if((mySquare[y1-1][x1+1].getHasAPiece()==true)&&(mySquare[y1-1][x1+1].getHasARedPiece()==false))
                    if((mySquare[y1-2][x1+2].getHasAPiece()==true)||(y1-2 < 0)||(x1+2 > 7)) //check for a blocker
                    {    
                        return false;
                    }
                    else
                        return true;
            }
    
            //Next 2 are for Kings only!
            if(CURRENT_KING = true)
            {
                //bottom left
                if((x2 == x1-2)&&(y2 == y1+2))    
                {
                    if((mySquare[y1+1][x1-1].getHasAPiece()==true)&&(mySquare[y1+1][x1-1].getHasARedPiece()==false))
                        if((mySquare[y1+2][x1-2].getHasAPiece()==true)||(y1+2 > 7)||(x1-2 < 0)) //check for a blocker or out of bounds
                        {    
                            return false;
                        }
                        else
                            return true;    
    
                }
    
                //bottom right
                if((x2 == x1+2)&&(y2 == y1+2))
                {
                    if((mySquare[y1+1][x1+1].getHasAPiece()==true)&&(mySquare[y1+1][x1+1].getHasARedPiece()==false))
                        if((mySquare[y1+2][x1+2].getHasAPiece()==true)||(y1+2 > 7)||(x1+2 >7)) //check for a blocker
                        {    
                            return false;
                        }
                        else
                            return true;     
                }
    
    
            }//end if King
    
        }//end p1
    
    
        if(player == 2)
        {
            if((x2 != x1+2) && (x2 != x1-2) && (y2 != y1+2) && (y2 != y1-2))   return false;
    
            for(int i=0; i<12; i++)
            {
                if((player2Pieces[i].getX() == x1)&&(player2Pieces[i].getY() == y1))
                {
                    CURRENT_PIECE = i;
                    break;  //get out of foor loop
                }                    
            }                
            if(player2Pieces[CURRENT_PIECE].isAKing() == true)
            {
                CURRENT_KING = true;    
    
            }
    
            //bottom left
            if((x2 == x1-2)&&(y2 == y1+2))    
            {
                if((mySquare[y1+1][x1-1].getHasAPiece()==true)&&(mySquare[y1+1][x1-1].getHasARedPiece()==false))
                    if((mySquare[y1+2][x1-2].getHasAPiece()==true)||(y1+2 > 7)||(x1-2 < 0)) //check for a blocker or out of bounds
                    {    
                        return false;
                    }
                    else
                        return true;    
    
            }
    
            //bottom right
            if((x2 == x1+2)&&(y2 == y1+2))
            {
                if((mySquare[y1+1][x1+1].getHasAPiece()==true)&&(mySquare[y1+1][x1+1].getHasARedPiece()==false))
                    if((mySquare[y1+2][x1+2].getHasAPiece()==true)||(y1+2 > 7)||(x1+2 >7)) //check for a blocker
                    {    
                        return false;
                    }
                    else
                        return true;     
            }
    
            //Next 2 are for Kings only!
            if(CURRENT_KING = true)
            {
                //top left spot
                if((x2 == x1-2)&&(y2 == y1-2))
                {
                    //first check to see if there is an enemy black piece on the adjacent square
                    if((mySquare[y1-1][x1-1].getHasAPiece()==true)&&(mySquare[y1-1][x1-1].getHasARedPiece()==false))
                        if((mySquare[y1-2][x1-2].getHasAPiece()==true)||(y1-2 < 0)||(x1-2 < 0)) //check for a blocker
                        {    
                            return false;
                        }
                        else
                            return true;
                }
    
                //top right spot
                if((x2 == x1+2)&&(y2 == y1-2))
                {   
                    //first check to see if there is an enemy black piece on the adjacent square
                    if((mySquare[y1-1][x1+1].getHasAPiece()==true)&&(mySquare[y1-1][x1+1].getHasARedPiece()==false))
                        if((mySquare[y1-2][x1+2].getHasAPiece()==true)||(y1-2 < 0)||(x1+2 > 7)) //check for a blocker
                        {    
                            return false;
                        }
                        else
                            return true;
                }     
    
                }//end if king
        } //end p2
    
        //default, or if nothing is found
        return false;
    
    
    }  // end canJump()
    
    /**
       * This is called by the getLegalMoves() method to determine whether
       * the player can legally move from (r1,c1) to (r2,c2).  It is
       * assumed that (r1,r2) contains one of the player's pieces and
       * that (r2,c2) is a neighboring square.
       */
    public boolean canMove(int player, int x1, int y1, int x2, int y2) 
    {
    
       if (y2 < 0 || y2 >= 8 || x2 < 0 || x2 >= 8)  //check to see if move is on the board
          return false;
    
       if (mySquare[y2][x2].getHasAPiece() == true)
          return false;  // (r2,c2) already contains a piece.
    
       if (player == 1) 
       {
          if (mySquare[y1][x1].getHasARedPiece() == true && y2 < y1)
             return false;  //red can only move up if not a king
          return true; 
       }
       else     //player == 2
       {
          if (mySquare[y1][x1].getHasAPiece() == true && mySquare[y1][x1].getHasARedPiece()==false && y2 > y1)
             return false;  // black can only move down if not a king
          return true;  
       }
    
    }  // end canMove()
    
    public CheckerMove[] getLegalMoves(int whichPlayer)
    {
        int pX =0;
        int pY = 0;
        int CURRENT_PIECE = -1;    //used to signify desired piece
        boolean CURRENT_KING = false;   //default not a king
        ArrayList<CheckerMove> legalMoves = new ArrayList<CheckerMove>();  //ADJUSTABLE arraylist of legalMoves
        if((whichPlayer != 1)&&(whichPlayer != 2)) return null; //not a valid player
    
    
    //player One /********************************************************/
        if(whichPlayer == 1)
        {
            //loop through array to find piece
            for(int i=0; i<12; i++)
            {
                if(player1Pieces[i].getIsAlive() == false) break;
                if(player1Pieces[i].isAKing() == true)
                    CURRENT_KING = true;
    
                pX = player1Pieces[i].getX();
                pY = player1Pieces[i].getY();
    
                //check for Jumps 1st
                    if (canJump(1, pX, pY, pX+2, pY+2))
                        legalMoves.add(new CheckerMove(pX, pY, pX+2, pY+2, true));
                    if (canJump(1, pX, pY, pX-2, pY+2))
                        legalMoves.add(new CheckerMove(pX, pY, pX-2, pY+2, true));
                    if (canJump(1, pX, pY, pX+2, pY-2))
                        legalMoves.add(new CheckerMove(pX, pY, pX+2, pY-2, true));
                    if (canJump(1, pX, pY, pX-2, pY-2))
                        legalMoves.add(new CheckerMove(pX, pY, pX-2, pY-2, true));
    
    
            }//end for
    
            if (legalMoves.size() == 0) //if there are no jumps
            {
                //loop through pieces array to find valid moves for each piece
                for(int ctr = 0; ctr < 12; ctr++)
                {
                    if(player1Pieces[ctr].isAlive == true)
                    {
                        int currX = player1Pieces[ctr].getX();
                        int currY = player1Pieces[ctr].getY();
                        if(canMove(1,currX,currY,currX-1,currY-1))
                            legalMoves.add(new CheckerMove(currX,currY,currX-1,currY-1,false));
                        if(canMove(1,currX,currY,currX+1,currY-1))
                            legalMoves.add(new CheckerMove(currX,currY,currX+1,currY-1,false));
                        if(canMove(1,currX,currY,currX-1,currY+1))
                            legalMoves.add(new CheckerMove(currX,currY,currX-1,currY+1,false));
                        if(canMove(1,currX,currY,currX+1,currY+1))
                            legalMoves.add(new CheckerMove(currX,currY,currX+1,currY+1,false));
    
                    }
                }
    
            }//if no legal moves exist
    
    
        }//end player One /**********************************************/
    //player Two
        if(whichPlayer == 2)
        {
            //loop through array to find piece
            for(int i=0; i<12; i++)
            {
    
                if(player2Pieces[i].getIsAlive() == false) break;
    
                if(player2Pieces[i].isAKing() == true)
                    CURRENT_KING = true;
    
                pX = player2Pieces[i].getX();
                pY = player2Pieces[i].getY();
    
                //check for Jumps 1st
                    if (canJump(2, pX, pY, pX+2, pY+2))
                        legalMoves.add(new CheckerMove(pX, pY, pX+2, pY+2, true));
                    if (canJump(2, pX, pY, pX-2, pY+2))
                       legalMoves.add(new CheckerMove(pX, pY, pX-2, pY+2, true));
                    if (canJump(2, pX, pY, pX+2, pY-2))
                       legalMoves.add(new CheckerMove(pX, pY, pX+2, pY-2, true));
                    if (canJump(2, pX, pY, pX-2, pY-2))
                       legalMoves.add(new CheckerMove(pX, pY, pX-2, pY-2, true));
            }//end for
    
            if (legalMoves.size() == 0) //if there are no jumps
            {
                //loop through pieces array to find valid moves for each piece
                for(int ctr = 0; ctr < 12; ctr++)
                {
                    if(player2Pieces[ctr].isAlive == true)
                    {
                        int currX = player2Pieces[ctr].getX();
                        int currY = player2Pieces[ctr].getY();
                        if(canMove(2,currX,currY,currX-1,currY-1))
                            legalMoves.add(new CheckerMove(currX,currY,currX-1,currY-1,false));
                        if(canMove(2,currX,currY,currX+1,currY-1))
                            legalMoves.add(new CheckerMove(currX,currY,currX+1,currY-1,false));
                        if(canMove(2,currX,currY,currX-1,currY+1))
                            legalMoves.add(new CheckerMove(currX,currY,currX-1,currY+1,false));
                        if(canMove(2,currX,currY,currX+1,currY+1))
                            legalMoves.add(new CheckerMove(currX,currY,currX+1,currY+1,false));
    
                    }
                }
    
            }
    
        }//end p2
    
        if (legalMoves.size() == 0)
            return null;    //no legal moves found
        else    //there are legal moves, so copy array list to an array and return
        {
            CheckerMove[] validMoves = new CheckerMove[legalMoves.size()];
            for (int lastCtr = 0; lastCtr < legalMoves.size(); lastCtr++)
            {
                validMoves[lastCtr] = legalMoves.get(lastCtr);
            }
            return validMoves;
        }
    
    }//end getLegalMoves
    
    
    
    
    }//end class
    

    这是黑板:

    package neegjar;
    
    
    
    
    public class Board
    {
    public Square mySquare[][] = new Square[8][8];
    public boolean gameover;
    
    //initialize an Array of 12 Pieces (for the checkers) for EACH player 
    public Piece player1Pieces[] = new Piece[12];; //declaration
    public Piece player2Pieces[] = new Piece[12]; //declaration
    
    public Board()
    {
    //CREATE THE BOARd  
    //mySquare = new Square[8][8];
    //initialize mySquare 
    for(int i = 0; i < 8; i++) //not sure if I need this
    {
        for(int j = 0; j < 8; j++)
        {
            mySquare[i][j] = new Square();
        }
    } 
    //initialize player1 and 2 Pieces arrays
    initPieces(1);
    initPieces(2);
    
    
    //alternating colors  
    /*
    for (int row = 0; row < 8; row++)
    {
      for (int col = 0; col < 8; col++)
      {
    
        if ((row == 0) || (row % 2 == 0) && ((col == 0) || (col % 2 == 0)))
        {
          mySquare[row][col].squareColor = "white";
        }
        else
        {
          // do nothing, the square will be black by default
        }
      }
    }//end for loop
    */
    
    //
    
    }//end constructor 
    
    public void placePieces()
    {  
    //set location of the pieces' initial location
    //player 1    
    player1Pieces[0].move(0,5); 
        mySquare[5][0].setHasAPiece(true);
        mySquare[5][0].setHasARedPiece(true);
    player1Pieces[1].move(2,5);
        mySquare[5][2].setHasAPiece(true);
        mySquare[5][2].setHasARedPiece(true);
    player1Pieces[2].move(4,5);
        mySquare[5][4].setHasAPiece(true);
        mySquare[5][4].setHasARedPiece(true);
    player1Pieces[3].move(6,5);
        mySquare[5][6].setHasAPiece(true);
        mySquare[5][6].setHasARedPiece(true);
    
    player1Pieces[4].move(1,6);
        mySquare[6][1].setHasAPiece(true);
        mySquare[6][1].setHasARedPiece(true);
    player1Pieces[5].move(3,6);
        mySquare[6][3].setHasAPiece(true);
        mySquare[6][3].setHasARedPiece(true);
    player1Pieces[6].move(5,6);
        mySquare[6][5].setHasAPiece(true);
        mySquare[6][5].setHasARedPiece(true);
    player1Pieces[7].move(7,6);
        mySquare[6][7].setHasAPiece(true);
        mySquare[6][7].setHasARedPiece(true);
    
    player1Pieces[8].move(0,7);
        mySquare[7][0].setHasAPiece(true);
        mySquare[7][0].setHasARedPiece(true);
    player1Pieces[9].move(2,7);
        mySquare[7][2].setHasAPiece(true);
        mySquare[7][2].setHasARedPiece(true);
    player1Pieces[10].move(4,7);
        mySquare[7][4].setHasAPiece(true);
        mySquare[7][4].setHasARedPiece(true);
    player1Pieces[11].move(6,7);
        mySquare[7][6].setHasAPiece(true);
        mySquare[7][6].setHasARedPiece(true);
    //player 2
    player2Pieces[0].move(1,0);
        mySquare[0][1].setHasAPiece(true);
    player2Pieces[1].move(3,0);
        mySquare[0][3].setHasAPiece(true);
    player2Pieces[2].move(5,0);
        mySquare[0][5].setHasAPiece(true);
    player2Pieces[3].move(7,0);
        mySquare[0][7].setHasAPiece(true);
    
    player2Pieces[4].move(0,1);
        mySquare[1][0].setHasAPiece(true);
    player2Pieces[5].move(2,1);
        mySquare[1][2].setHasAPiece(true);
    player2Pieces[6].move(4,1);
        mySquare[1][4].setHasAPiece(true);
    player2Pieces[7].move(6,1);
        mySquare[1][6].setHasAPiece(true);
    
    player2Pieces[8].move(1,2);
        mySquare[2][1].setHasAPiece(true);
    player2Pieces[9].move(3,2);
        mySquare[2][3].setHasAPiece(true);
    player2Pieces[10].move(5,2);
        mySquare[2][5].setHasAPiece(true);
    player2Pieces[11].move(7,2);
        mySquare[2][7].setHasAPiece(true);
    
    
    }
    
    public void alternateSquares()    //assign every other to white
    {
        for(int row = 0; row < 8; row+=2)
        {
            for(int col = 0; col < 8; col+=2)
            {
                mySquare[row][col].setSquareColor("white");
            }          
        }    
    
        for(int row = 1; row < 8; row+=2)
        {
            for(int col = 1; col < 8; col+=2)
            {
                mySquare[row][col].setSquareColor("white");
            }          
        }       
    
    }
    
    public void initPieces(int whichPlayer)
    {
      for(int p = 0; p <12; p++)
      {
          if(whichPlayer == 1)
              player1Pieces[p] = new Piece();
          else
              player2Pieces[p] = new Piece();
      }
    }
    
    public void printBoard()
    {
    System.out.print("     0     1     2     3     4     5     6     7    \n");
    System.out.print("---------------------------------------------------\n");
    for(int row=0; row<8; row++)
        {
            System.out.print(row);
            for(int col = 0; col < 8; col++)
            {
                System.out.print(" | ");
                if(mySquare[row][col].getHasAPiece() == false)
                    System.out.print("   ");   
                else if(mySquare[row][col].getHasARedPiece()==true)
                    System.out.print(" R ");
                else if(mySquare[row][col].getHasARedPiece()==false)
                    System.out.print(" * ");
    
            }
            System.out.print(" |\n");
            System.out.print("---------------------------------------------------\n");
        }
    }
    
    public void printPieces()
    {
    for(int i=0; i<12; i++)
        {
            System.out.print(player1Pieces[i].getX() + ", ");
            System.out.print(player1Pieces[i].getY());
            System.out.println();
    
        }  
    
    }
    
    public void printLegalMoves()
    {
      Play p1 = new Play();
      CheckerMove[] temp = p1.getLegalMoves(1);
    
      for(int i=0; i<temp.length; i++)
      {
          System.out.println("From: "+temp[i].fromX+", "+temp[i].fromY);
          System.out.println("To: "+temp[i].toX+", "+temp[i].toY);
    
    
    
      }
    }
    
    
    }//end class
    

    主要内容:

    包尼加尔;导入java。util。ArrayList;

    公共类主{

    public static void main(String[] args)
    {
        Board newBoard = new Board();
        //newBoard.alternateSquares();
        //newBoard.alternateSquares();
            //System.out.println(newBoard.player1Pieces[0].getX());
        newBoard.placePieces();
        //newBoard.printBoard();
        newBoard.printLegalMoves();
    
        //newBoard.printPieces();
    
    
    
    }    
    

    }//结束类

  • 共有1个答案

    席安康
    2023-03-14

    这是一个非常基本的调试问题。

    您的方法canJump()有几个地方,它用一个数字索引数组,从中减去1或2;在其中一个地方,结果是-1,并且数组(或任何java数组)中没有-1索引。

    错误消息会准确地告诉您这发生在哪一行;我无法从您的来源判断,因为行号与您发布的内容不匹配。但是,它将准确显示您的来源。

    学习查看java错误消息。它们是关于问题所在的优秀信息源(这不是语言运行时的通用信息)。它将虚拟机和强类型语言相结合,使它能够比其他一些语言更准确地指出问题的根源。

    并学习使用调试器。您可以单步执行问题所在的代码,并查看变量在执行过程中的值。它对于您自己发现和解决问题是非常宝贵的。

     类似资料:
    • 我正在为一项任务做一个跳棋游戏。除了一件奇怪的事之外,整个事情都在按它应该的方式运行。这是我的黑板: 我通过给出源行和列,然后是目标行和列来移动。 如果我试图将一个工件移动到无效的位置(不是对角线),我应该打印出一个错误。所以如果我试着从5.2- 对于大多数情况,它都可以工作,但如果我尝试直接向下移动一个空间(例如,2 3- 我卡住了!你知道为什么会这样吗?如果需要,我可以发布更多代码。

    • 嗨,我正在尝试构建一个跳棋游戏,但在计算如何用单个棋子填充2D阵列游戏板时遇到了问题。任何帮助都将不胜感激。 我有两个独立的类RedPiece和BlackPiece来实现接口块。在额外的类板中,我试图将块放在一边,但我遇到了一个问题,在构建新板时找不到可变块: 这是我到目前为止为SetBoard()所做的: 有什么想法吗?谢谢。

    • 五子棋(Gomoku)是一款经典的线上五子棋游戏,5 子连成一线者胜。  

    • 我正在为跳棋电脑游戏做最后一年的机器学习项目。 在这个游戏中,我自动化了一个玩家(随机移动),我希望第二个玩家学习随机性,并通过更多的游戏和试验变得聪明。 正如我所说,第一个玩家是自动化的,所以它工作得很好,但是说到第二个玩家,我对它的动作有些问题。 我正在使用目标函数作为 > v(b)=w0 w1x1 w2x2 w3x3 w4x4 w5x5 w6x6 其中x1=白色碎片数量x2=黑色碎片数量x3

    • 《游戏跳跃》是一款模拟开发游戏的放置游戏,和《卡牌冒险》《稳定宝石放置》一样,用的同一个增量游戏开发模板开发的。

    • 当我尝试构建Unity项目(Android)时,它说: 命令调用失败:无法更新SDK。请手动运行SDK管理器以确保您安装了最新的工具集和所需的平台。 以下是我试图修复它的事情: > 将添加到路径中。然后从cmd中,我尝试了,然后键入,但它给了我: 错误:无法找到或加载主类项目\Android原因:java.lang.ClassNotFoundExc的:项目\Android 从Android Stu