找不到ArrayList对象

时间:2016-09-09 17:41:31

标签: java arraylist

NumberTile类模拟一个数字tile,它是一个4个整数的arrayList,TileGame类在棋盘中插入一个tile,以及一个用于启动游戏的测试类。 hand是5 NumberTiles的arraylist,但是当我编译它时,每次我在NumberTile中引用TileGame的ArrayList时,它都找不到符号NumberTile。

我是否需要创建一个包才能识别它?我的导师提供了大部分陈述,我无法改变。我没有输入其他方法,因为我不认为它们是必要的。

此外,行TileGame game = new TileGame();表示无法找到符号。什么是正确的初始化方法?

我需要任何帮助。谢谢。

班级TileGame

public class TileGame 
{ 
    //provided by instructor
private ArrayList<NumberTile> board ;

  // Creates an empty board
   public TileGame() 
   {
     //do not modify this method
       board = new ArrayList<NumberTile>();
   }

  // Accessor for the board
   public ArrayList<NumberTile> getBoard() 
   {
    // Do not modify this method
       return board ;
   }

  // Creates and returns a hand of 5 random number tiles
   public ArrayList<NumberTile> getHand() 
    {
       ArrayList<NumberTile> hand = new ArrayList<NumberTile>() ;

       for (int a = 0; a < 5; a++)
       {
        hand.add(a, new NumberTile());
    }

       return hand;
   }

   // If the current tile fits in the board (without rotating) then
   // return the index i of a tile in the board so that the current tile 
   // fits before ti for i = 0..k-1, or return k if the current tile fits
   // after the last tile.   If the tile does not fit, return -1
   public int getIndexForFit(NumberTile currentTile) 
   {
       NumberTile firstTile = board.get(0);
       NumberTile lastTile = board.get(board.size() - 1);

       if(firstTile.getLeft() == currentTile.getRight())
       {
           return 0;
       }
       else if (lastTile.getRight() == currentTile.getLeft())
       {
           return board.size() - 1;
       }
       else 
       {   
        return -1 ;
       }
   }

   // Call the method getIndexForFit to see whether a tile can be inserted
   // into the board. In this method the tile can be rotated. If the tile
   // can be inserted, return true.  If the tile does not fit after 
   // rotating (at most 3 times), return false.
   public boolean canInsertTile(NumberTile currentTile) 
   {
       //call get index for fit
       int canInsert = getIndexForFit(currentTile);
       boolean canInsertOrNot = false;;
       //if true, modify index
       if(canInsert == -1)
       {
           //rotate
           for (int rotations = 0; rotations < 3; rotations++)
           {
              currentTile.rotate();
              int didRotationWork = getIndexForFit(currentTile);

              if (didRotationWork == -1)
              {
                  continue;
              }

              else if (didRotationWork != -1)
              {
                  canInsertOrNot = true;
              }

           }

           return false;
       }

          else if(canInsert != -1)
          {
              return true;
          }
       return canInsertOrNot;

   }

   // Make a move. I.e. if a tile in the hand fits on the board
   // then remove it from the hand and place it in the board. If no tile
   // from the hand fits, then add another tile to the hand
   public void makeMove(ArrayList<NumberTile> hand) 
   {
       boolean fits;

       for (int x = 0; x < hand.size(); x++)
       {
           //call caninterserttile
           fits = canInsertTile(hand.get(x));

           if(fits)
           {
               int index = getIndexForFit(hand.get(x));
               board.add(index, hand.get(x));
               hand.remove(x);
               break;
           }
           else
           {
               hand.add(hand.size() -1, new NumberTile());
           }

       }
    }

   public String toString() 
   {
       // Do not modify this method
       return board.toString() ;  // ArrayList as a String
   }
 } // end of TileGame class 

班级NumberTile

public class NumberTile 
{
  public ArrayList<Integer> tile = new ArrayList<>();

// Constructs a NumberTile object using 4 random integers in the 
// range 1 to 9
 public NumberTile() 
  {
     Random generator = new Random() ;

    for (int a = 0; a < 4; a++)
    { 
        int random = generator.nextInt(9);
        tile.add(a, random);
    }

  }
// Rotate the tile 90 degrees
public void rotate() 
{
    int temp = tile.get(0);
    tile.set(0, tile.get(1));
    tile.set(1, tile.get(3));
    tile.set(3, tile.get(2));
    tile.set(2, temp);
}

public int getLeft() 
{
      // Do not modify this method
    return tile.get(0) ;
}

public int getRight() 
{
    // Do not modify this method
    return tile.get(2) ;
}

public String toString() 
{
    String out = "";

        out += "   "+tile.get(0)+"   ";
        out += tile.get(1) + "   " + tile.get(2);
        out += "  "+tile.get(3)+"   ";

    return out;
 }

} // end of NumberTile class

班级TileGameTester

public class TileGameTester {

public static void main(String[] args){
    TileGame game = new TileGame();
    boolean winner = false;

    //get two hands
    ArrayList<NumberTile> hand1 = game.getHand();
    ArrayList<NumberTile> hand2 = game.getHand();

    //create an empty board
    System.out.println(game.getBoard());

    do{
        //make moves
        game.makeMove(hand1);
        game.makeMove(hand2);

        //check if they won
        if (hand1.isEmpty() || hand2.isEmpty())
        {
            winner = true;
        }

    }while(!winner);

    hand1.toString();
    hand2.toString();

    if (hand1.isEmpty() && hand2.isEmpty())
        {
            System.out.println("It is a tie!");
        }
        else if (hand1.isEmpty())
        {
            System.out.println("Player 1 won!");
        }
        else if (hand2.isEmpty())
            System.out.println("Player 2 won!");


   }
}

3 个答案:

答案 0 :(得分:1)

虽然它不完整所以我不确定你是否要向arraylist添加数据,但如果你没有找到Symbol。

我也无法看到任何导入语句。您是否导入了对象

例如TileGameTester应该有导入 - &gt; import com.something.TileGameimport com.something.NumberTile

还检查TileGame中是否有import语句以及arraylist

等常见导入

答案 1 :(得分:0)

我认为您可能需要插入&#34; import java.util.Random;&#34;和&#34; import java.util.ArrayList;&#34;在您的包声明(如果有的话)和使用这些声明的文件中的类声明之间。

答案 2 :(得分:0)

您可以将TileGame类导入到可能解决您问题的TileGameTester类中。