Как бы я распечатать координаты из 2d массива, заполненного символами. Например, распечатать (1,3) при поиске символа «J» - PullRequest
0 голосов
/ 16 апреля 2020

Я создаю пейнтбольную игру, которая в основном генерирует 3 случайных персонажа (2 цели и 1 солдат) в двухмерном массиве. После их генерации я должен распечатать координаты всех трех и определить, кто ближе к солдату. Я застрял на том, как распечатать координаты второй цели. Я пробовал имплантировать два разных метода, но в итоге получилось печатать координаты одной цели два раза. Любая помощь будет оценена !!

import java.util.Random;


public class comprehensiveLab2_Perea{
public static void main (String [] args){
    char[][] myary= new char[10][10];
    myary = CreateGrid(myary);
    modifyGrid(myary);
    DisplayGrid(myary);
    int []soldierLocation = getCoordinates(myary,0,0);
    System.out.println("Soldiers coordinates: (" + soldierLocation[1] + ", " +soldierLocation[0] + ")");
    int []firstTarget= firstTCoordinates(myary, 0,0);
    System.out.println("First target: (" + firstTarget[1] + ", " + firstTarget[0] + ") ");
    int []secondTarget= secondTCoordinates(myary, 0,0, firstTarget);
    System.out.println("Second target: (" + secondTarget[1] + ", " + secondTarget[0] + ") ");



}

   public static char [][] CreateGrid(char [][] Grid){
        for(int i=0; i<Grid.length; i++){
        for(int j=0; j<Grid[i].length; j++){
            Grid[i][j] = '-';
        }
    }

    return Grid;
}


/**
    This method prints what's inside the grid in a 
    nice and organized format.
    @param grid of 2d array of characters.
*/
public static void DisplayGrid(char [][] Grid){

    System.out.print("  ");
    for(int i=0; i<Grid.length;i++){
        System.out.print(i + " ");
    }
    System.out.println();
    for(int i=0; i<Grid.length; i++){
        System.out.print(i + " ");
        for(int j=0; j<Grid[i].length; j++){
            System.out.print(Grid[i][j] + " ");
        }
        System.out.println();
    }
}

public static char [][] modifyGrid(char [][] Grid){
    Random rand = new Random();
    Grid[rand.nextInt(10)][rand.nextInt(10)]='S';
    Grid[rand.nextInt(10)][rand.nextInt(10)]='T';
    Grid[rand.nextInt(10)][rand.nextInt(10)]='T';

    return Grid;

}

public static int[] getCoordinates (char[][] Grid,int x, int y) {
if(Grid[x][y]=='S'){
        int [] soldierLocation = {x,y};
        return soldierLocation;
    }
else if((x< (Grid.length-1)) || (y< (Grid[x].length-1))){
        y += 1;
        if(y>Grid[x].length-1){
            y = 0;
            x+= 1;
        }
        return getCoordinates(Grid,x,y);
    }
    int [] coordinates = {-1,-1};
    return coordinates;
}

    public static int[] firstTCoordinates (char[][] Grid,int x, int y) {
    if(Grid[x][y]=='T'){
        int [] soldierLocation = {x,y};
        return soldierLocation;
    }
else if((x< (Grid.length-1)) || (y< (Grid[x].length-1))){
        y += 1;
        if(y>Grid[x].length-1){
            y = 0;
            x+= 1;
        }
        return firstTCoordinates(Grid,x,y);
    }

    int [] coordinates = {-1,-1};
    return coordinates;
}
     public static int[] secondTCoordinates (char[][] Grid,int x, int y,      int[]firstTarget) {
if(Grid[x][y]=='T'){
        int [] soldier2Location = {x,y};
        return soldier2Location;
    }
else if((x< (Grid.length-1)) || (y< (Grid[x].length-1))){
        y += 1;
        if(y>Grid[x].length-1){
            y = 0;
            x+= 1;
        }
        return secondTCoordinates(Grid,x,y,firstTarget);
    }

    int [] coordinates = {-1,-1};
    return coordinates;
}

}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...