简体   繁体   English

在Java中搜索2D数组并返回Point2D对象

[英]Searching 2D array and returning Point2D object in Java

I'm working on a 15-piece sliding tile game, where the user tries to arrange tiles 1-15 in order on a grid. 我正在开发15件滑动瓷砖游戏,用户尝试在网格中按顺序排列瓷砖1-15。 I'm stuck on an early part where, after the user enters a tile label, we locate where the tile currently is on the board. 我停留在早期部分,在该区域中,用户输入磁贴标签后,我们将定位磁贴当前在板上的位置。 My aim is to search the 2D array where the tiles are located for the given tile, and then, once located, set a Point2D object's x and y values to the row and column of the found tile. 我的目的是为给定图块搜索图块所在的2D数组,然后在找到后将Point2D对象的x和y值设置为找到的图块的行和列。 I'm not sure where I've gone wrong with this. 我不确定我哪里出错了。 Any help? 有什么帮助吗?

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);
    }
} 

Your code is ok. 您的代码还可以。 This part of code had to be upgraded 这部分代码必须升级

 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);

and the method getTilePosition had to return the value: 并且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);
    }

Having this all together this is the code 将所有这些结合在一起就是代码

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();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM