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.