我正在进行15件滑动平铺游戏,用户试图在网格上按顺序排列1-15块。我被困在早期部分,在用户输入瓷砖标签后,我们找到了瓷砖当前在电路板上的位置。我的目标是搜索给定图块的图块所在的2D数组,然后,一旦找到,将Point2D对象的x和y值设置为找到图块的行和列。我不确定我在哪里出错了。有什么帮助吗?
package tilegame;
import java.awt.geom.Point2D;
import java.util.Scanner;
public class TileGame
{
public TileGame()
{
GameBoard gameBoard = new GameBoard();
Scanner user_input = new Scanner(System.in);
int userTileNumber = 0;
while (userTileNumber != -1)
{
System.out.print("Enter tile number: ");
userTileNumber = user_input.nextInt();
if (userTileNumber < 0) //terminates game if user enters -1
break;
else
continue;
}
getTilePosition(userTileNumber);
}
public static void main(String[] args)
{
new TileGame();
}
}
class GameBoard
{
int [][] tiles = { {-1, -1, -1, -1, -1, -1}, //-1's represent the boundaries of the gameboard
{-1, 1, 2, 3, 4, -1},
{-1, 5, 6, 7, 8, -1},
{-1, 9, 10, 11, 12, -1},
{-1, 13, 14, 0, 15, -1}, //zero represents an empty space
{-1, -1, -1, -1, -1, -1} };
public GameBoard()
{
printGameBoard(tiles);
}
private void printGameBoard(int[][] tiles) //Prints current position of all the tiles on the board
{
for(int i = 0; i < tiles.length; i++)
{
for(int j = 0; j < tiles[0].length; j++)
{
System.out.printf("%5s ", tiles[i][j]);
}
System.out.println();
}
}
public Point2D getTilePosition(int userTileNumber)
{
for(int i = 0; i < tiles.length; i++)
{
for(int j = 0; j < tiles[i].length; j++)
{
if (tiles[i][j] == userTileNumber)
{
Point2D searchedTile = new Point2D(i,j);
return searchedTile;
}
}
}
System.out.print("Tile number: " + userTileNumber + "/nRow: " + i + "/nColumn: " + j);
}
}
答案 0 :(得分:0)
您的代码没问题。这部分代码必须升级
while (userTileNumber != -1)
{
System.out.print("Enter tile number: ");
userTileNumber = user_input.nextInt();
if (userTileNumber < 0) //terminates game if user enters -1
break;
else
continue;
}
getTilePosition(userTileNumber);
并且方法getTilePosition必须返回值:
public Point2D getTilePosition(int userTileNumber)
{
for(int i = 0; i < tiles.length; i++)
{
for(int j = 0; j < tiles[i].length; j++)
{
if (tiles[i][j] == userTileNumber)
{
Point2D searchedTile = new Point2D(i,j);
return searchedTile;
}
}
}
System.out.print("Tile number: " + userTileNumber + "/nRow: " + i + "/nColumn: " + j);
}
这一切都是代码
import java.awt.geom.Point2D;
import java.util.Scanner;
public class TileGame
{
public static final int BOUNDARY = -1;
public static final int EMPTY = 0;
public static final int QUIT = -1;
public static final int MIN_TILE = 1;
public static final int MAX_TILE = 15;
public static class GameBoard
{
int [][] tiles = {
{BOUNDARY, BOUNDARY, BOUNDARY, BOUNDARY, BOUNDARY, BOUNDARY},
{BOUNDARY, 1 , 2, 3, 4, BOUNDARY},
{BOUNDARY, 5 , 6, 7, 8, BOUNDARY},
{BOUNDARY, 9 , 10, 11, 12, BOUNDARY},
{BOUNDARY, 13, 14, EMPTY, 15, BOUNDARY},
{BOUNDARY, BOUNDARY, BOUNDARY, BOUNDARY, BOUNDARY, BOUNDARY} };
public GameBoard()
{
printGameBoard(tiles);
}
public int[][] getTiles()
{
return tiles;
}
public void setTiles(int[][] tiles)
{
this.tiles = tiles;
}
public int getTile(int x, int y)
{
return getTiles()[x][y];
}
public int[] getRow(int i)
{
return getTiles()[i];
}
public static boolean isValidTile(int tile)
{
return MIN_TILE <= tile && tile <= MAX_TILE;
}
private void printGameBoard(int[][] tiles) //Prints current position of all the tiles on the board
{
for(int i = 0; i < tiles.length; i++)
{
for(int j = 0; j < tiles[0].length; j++)
{
System.out.printf("%5s ", tiles[i][j]);
}
System.out.println();
}
}
public Point2D getTilePosition(int userTileNumber)
{
Point2D searchedTile = null;
for(int i = 0; i < getTiles().length && null == searchedTile ; i++)
{
for(int j = 0; j < getRow(i).length && null == searchedTile; j++)
{
if (getTile(i,j) == userTileNumber)
{
searchedTile = new Point2D.Double(i,j);
}
}
}
return searchedTile;
}
}
public TileGame()
{
GameBoard gameBoard = new GameBoard();
Scanner user_input = new Scanner(System.in);
int userTileNumber = 0;
while (QUIT != userTileNumber)
{
System.out.print("Enter tile number: ");
userTileNumber = user_input.nextInt();
if (QUIT != userTileNumber && GameBoard.isValidTile(userTileNumber))
{
Point2D searchedTile = gameBoard.getTilePosition(userTileNumber);
int x = (int)searchedTile.getX();
int y = (int)searchedTile.getY();
System.out.format("Tile's position (x,y) is (%d,%d).%n", x,y);
}
}
}
public static void main(String[] args)
{
new TileGame();
}
}