运行代码时,出现“错误:找不到符号”
tile = board.get(col-1); //get tile
。
我尝试将板子数组列表从构造函数中取出,但是当到达board = b.getBoard();
时,我得到了错误:<标识符>预期。我不知道我在做什么错。我在编码方面很糟糕,因此非常感谢您提供帮助。
import java.util.ArrayList;
/**
*The game class holds data about the game
*/
public class Game
{
private int row; //row
private int col; //columb
//create array list of rows
ArrayList<Integer> rows = new ArrayList<>();
//create array list of columbs
ArrayList<Integer> columbs = new ArrayList<>();
//create array list of selected tiles
ArrayList<Tile> selected = new ArrayList<>();
private int index = 0; //set index to 0
/**
This constructor sets up a game board
*/
public Game()
{
//create new Board
Board b = new Board();
//create array list tiles of type Tile
ArrayList<Tile> board = new ArrayList<>();
//call Board's getBoard method
board = b.getBoard();
}
/**
This constructor sets columb and row variabules
*/
public Game(int r, int c)
{
row = r; //row of tile being tested
col = c; //columb of tile being tested
}
/**
isValidSelection method takes the columb and row of
a tile and returns a boolean value for if the tile is
adjacent to the previous tile or not
@param the row of the tile
@param the columb of the tile
@return true if tile is adjacent t previous tile
*/
public boolean isValidSelection(int row, int col)
{
//check if array is empty (no previous tile)
if (rows.isEmpty())
{
rows.add(row); //add to rows array
columbs.add(col); //add to columbs array
return true; //return that tile is adjacent
}
else
{
//check if tile is adjacent to previous
if (Math.abs(rows.get(index)-row)<=1)
{
rows.add(row); //add to rows array
columbs.add(col); //add to columbs array
return true; //return that tile is adjacent
}
//check if tile is adjacent to previous
else if (Math.abs(columbs.get(index)-col)<=1)
{
rows.add(row); //add to rows array
columbs.add(col); //add to columbs array
return true; //return that tile is adjacent
}
//case if tile is not adjacent
else
{
return false; //return that tiles aren't adjacent
}
}
index += 1; //add 1 to index
}
/**
addToSelected method takes the columb and row of a
tile and adds them to array of selected tile row and col
@param the row of the tile
@param the columb of the tile
*/
public void addToSelected(int row, int col)
{
if (row == 1)
{
Tile tile; //tile
tile = board.get(col-1);//get tile
selected.add(tile); // add to selected
}
else if (row == 2)
{
Tile tile; //tile
tile = board.get(col+3);//get tile
selected.add(tile); // add to selected
}
else if (row == 3)
{
Tile tile; //tile
tile = board.get(col+7);//get tile
selected.add(tile); // add to selected
}
else
{
Tile tile; //tile
tile = board.get(col+11);//get tile
selected.add(tile); // add to selected
}
}
/**
removeFromSelected method takes the columb and row of a tile
and deletes them from array of selected tile row and col
@param the row of the tile
@param the columb of the tile
*/
public void removeFromSelected(int row, int col)
{
if (row == 1)
{
Tile tile; //tile
tile = board.get(col-1);//get tile
selected.add(tile); // add to selected
}
else if (row == 2)
{
Tile tile; //tile
tile = board.get(col+3);//get tile
selected.add(tile); // add to selected
}
else if (row == 3)
{
Tile tile; //tile
tile = board.get(col+7);//get tile
selected.add(tile); // add to selected
}
else
{
Tile tile; //tile
tile = board.get(col+11);//get tile
selected.add(tile); // add to selected
}
}
/**
toString method
@return A string containing the board
*/
public String toString()
{
// Create a string representing the board
String str = board.get(0) + board.get(1) +
board.get(2) + board.get(3) + "\n"+
board.get(4) + board.get(5) +
board.get(6) + board.get(7) + "\n"+
board.get(8) + board.get(9) +
board.get(10) + board.get(11) + "\n"+
board.get(12) + board.get(13) +
board.get(14) + board.get(15);
//return the string
return str;
}
}
import java.util.ArrayList;
import java.util.Scanner;
public class BoggleText {
public static void main(String[] args) {
// create game
Game g = new Game();
// create scanner for user input
Scanner keyboard = new Scanner(System.in);
// variables for user input
int row, col;
String input;
// create flag to end game
boolean stop = false;
while (!stop) {
// print board
System.out.println(g);
// prompt user for choices
System.out.print("(s)elect, (d)eselect, (l)ist selected, " +
"(c)lear selected, (t)est selected, (e)nd: ");
// get choice
input = keyboard.nextLine();
// select
if (input.equalsIgnoreCase("s")) {
// prompt for row & column
System.out.print("row / column [r c]: ");
// get row, col from input
row = keyboard.nextInt();
col = keyboard.nextInt();
input = keyboard.nextLine(); // clr new line left in buffer
// test if the r/c combination is a valid move
if (g.isValidSelection(row, col)){
// add tile to selected tiles
g.addToSelected(row, col);
}
else {
System.out.println("Invalid selection! Please select " +
"a letter adjacent to the previously " +
"selected letter.");
}
}
// deselect
else if (input.equalsIgnoreCase("d")) {
// prompt for row & column
System.out.print("row / column [r c]: ");
// get row, col from input
row = keyboard.nextInt();
col = keyboard.nextInt();
input = keyboard.nextLine(); // clr new line left in buffer
// remove tile from selected tiles
g.removeFromSelected(row,col);
}
// list currently selected tiles
else if (input.equalsIgnoreCase("l")) {
ArrayList<Tile> selected = g.getSelectedTiles();
System.out.println(selected);
}
// clear currently selected tiles
else if (input.equalsIgnoreCase("c")) {
g.clearSelected();
}
else if (input.equalsIgnoreCase("t")) {
g.testSelected();
}
// end game
else if (input.equalsIgnoreCase("e")) {
stop = true;
}
}
}
}
我还没有写完游戏课,但是boggletext应该是正确的
答案 0 :(得分:-1)
这完全像指南针所说的。如果要访问变量Board'b',则应在构造函数外部定义Board b,然后在构造函数内部进行初始化。
public class Game{
...
private Board b;
...
public Game(){
b = new Board();
...
}
}