简体   繁体   中英

Matching a user input String to an object of a 2D array with the same Name attribute in Java

So I've created a 2d Array of objects of Type "Champion" that represent avatars on a 2D 10x10 grid, where null spaces indicate "open" spots and not null spaces are obviously occupied with Champions.

private static void createCharacter() 
{       
    System.out.println("New Character Name");
    String name = sc.next();
    System.out.println("Character x coord");
    int xc = sc.nextInt();
    System.out.println("Character y coord");
    int yc =sc.nextInt();   
    Champion tempchamp = new Champion(name,xc,yc);
    if(grid[xc][yc]!= null)
    {
        System.out.println("Error, Space Already Occuped");
    }//End If 
    else
    {
        grid[xc][yc] = tempchamp;
    }//End Else

My character constructor allows for name, x and y coordinates but the coordinates are also represented on the grid, obviously. The output looking something like this Output My issue now is finding a way to allow user to select the Character they want to move. Is there a way of iterating through a 2D array to compare if any index's name attribute contains the same value as that of a user input String or would I be looking at an entirely different solution. Any and all help much appreciated. Also apologies if formatting is off. First post.

Currently Move method targets actual coordinates of Champions on Grid

private static void moveCharacter() 
    {
        System.out.println("Enter Co-ords of Champ to Move");
        int posx = sc.nextInt();
        int posy = sc.nextInt();
        if(grid[posx][posy]!=null)
        {
            String moving = grid[posx][posy].getName();
            System.out.println("Enter Target Coordinates");
            int tarx = sc.nextInt();
            int tary = sc.nextInt();
            if(grid[tarx][tary]==null)
            {
                grid[posx][posy]=null;
                Champion champ = new Champion(moving, tarx, tary);
                grid[tarx][tary]=champ;
                printgrid();
                double distance = calculateDistance(posx, posy, tarx, tary);
                System.out.println("Distace Travelled " + distance);
            }//End if
        }//End If Pos Null
        else
            System.out.println("Target Position is Empty");
            {
            moveCharacter();
            }//End Else
    }//End Move Character

Was toying around with something like

String ChampToMove = "Brian";
for(int i = 0; i<grid.length; i++)
    {
        for(int j = 0; j<grid[i].length;j++)
        {
            if(grid[i][j].getName.equals(ChampToMove))
            {
             moveCharacter(ChampToMove );
            }
        }//End Nested For

The last piece isn't currently what I'm using but more what I was playing around with earlier, its just a general gist of what I was thinking would work, however I feel it's not efficient whatsoever.

I would recommend using a binarySearch, it has a runtime of O(nlog(n)), compared to your current algorithm, which has a runtime of O(n^2).

A possible way to implement is :

for(int i = 0; i<grid.length; i++)
    {
            if(binarySearch(grid[i], ChampToMove))
            {
             moveCharacter(ChampToMove );
             break;
            }
        }//End Nested For

and the binarySearch algorithm :

public static boolean binarySearch(String[] a, String x) {
int low = 0;
int high = a.length - 1;
int mid;

while (low <= high) {
    mid = (low + high) / 2;
    System.out.println(a[mid]);
    if (a[mid].compareTo(x) < 0) {
        low = mid + 1;
    } else if (a[mid].compareTo(x) > 0) {
        high = mid - 1;
    } else {
        return true;
    }
}

return false;
}

if you have any questions about how any of this works, just ask.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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