0

Below is my program artificial bug. How would i be able to access the coorordinates from main which the user has set, and assign them to an array which is set in the MyClass class, thanks in advance:)

//imports
import java.util.Scanner;
import java.util.Random;


public class main {

    public static void main(String[] args) {

        Scanner reader = new Scanner(System.in);
        ABug[] BugObj = new ABug[4]; //Creating object BugObj of class ABug
        int loop = 4;
        int i = 0;
        int cycles;
        MyClass worldnew = new MyClass();



        System.out.println("Please enter the number of cycles you wish to run:");   
        cycles = reader.nextInt(); //getting the amount of cycles to be run
        System.out.print("____Current World____\n\n");
        worldnew.printWorld(); //calling method to print out world

        System.out.println("____Key____\n_F_ - Food\n_O_ - Object\n_ _ - Space\nSymbol - Bug");


        do{

            BugObj[i] = new ABug();  //creating instance


            System.out.print("Please enter the symbol which you wish to represent the bug:");
            BugObj[i].symbol = reader.next();
            System.out.print("Please enter the name of the bug:");
            BugObj[i].name = reader.next(); 
            System.out.println("Please enter the species of the bug:");   
            BugObj[i].species = reader.next(); 
            System.out.println("Please enter the horizontal position of the bug:");   
            BugObj[i].horpos = reader.nextInt();
            System.out.println("Please enter the vertical postion of the bug:");   
            BugObj[i].vertpos = reader.nextInt();   


            System.out.println("_______________ Bug " +(+i+1) + " _______________\n" );
            System.out.println("Symbol: " + BugObj[i].symbol);     //Printing bug information out
            System.out.println("Name: " + BugObj[i].name);           
            System.out.println("Species: " + BugObj[i].species);
            System.out.println("Horizontal Position: " + BugObj[i].horpos);
            System.out.println("Vertical Postion: " + BugObj[i].vertpos + "\n\n");
            move(BugObj[i]);


            i++;
            System.out.println("Would you like to enter another bug? \n 0-No,  1-Yes\n");
            loop = reader.nextInt();
        }while(loop == 1);
    }


    public static void move(ABug bug){
        Scanner reader = new Scanner(System.in);
        System.out.println("Would you like this bug to move?\n 0-No,  1-Yes\n");
        if (reader.nextInt() == 0)
        {
            System.exit(0);
        }



        //get corordinate of bug
        //set map[coor] = symbol
        //print out map


        int r = (int) (Math.random() * (2- -2)) + -2;
        int originalHorpos = bug.horpos;
        int originalVertpos = bug.vertpos;
        bug.horpos = originalHorpos + r;
        bug.vertpos = originalVertpos + r;


        System.out.println("New Horizontal Position: " +bug.horpos );
        System.out.println("New Vertical Postion: " +bug.vertpos);

    }

}
enum Item {
    OBJECT ('O'),FOOD ('F'), SPACE (' ');

    private final char symbol;
    Item(char symbol) {
        this.symbol = symbol;
    }
    char getSymbol() { return symbol; }
}
class MyClass {
    Item[][] map = new Item[15][25];
    public void printWorld() {

        int v, h; //v - vert, h - hor

        for (v=1; v<=15; ++v)
        {
            for (h=1; h<=25; ++h)
            {

                final Item[] items = {Item.OBJECT, Item.FOOD, Item.SPACE, Item.SPACE, Item.SPACE, Item.SPACE, Item.SPACE, Item.SPACE};
                Random random = new Random();
                int selection = random.nextInt(items.length);
                map[v-1][h-1] = items[selection];
                System.out.print(map[v-1][h-1].getSymbol() + "_"); 


            }
            System.out.printf("\n");
        } 
    }
}
class ABug {                 //ABug class
    int horpos, vertpos, energy, id;
    String species, name, symbol;

}

edit

package buglife;



//imports
import java.util.Scanner;
import java.util.Random;


public class main {

	public static void main(String[] args) {

		Scanner reader = new Scanner(System.in);
		ABug[] BugObj = new ABug[4]; //Creating object BugObj of class ABug
		int loop = 4;
		int i = 0;
		int cycles;
		MyClass worldnew = new MyClass();



		System.out.println("Please enter the number of cycles you wish to run:");   
		cycles = reader.nextInt(); //getting the amount of cycles to be run
		System.out.print("____Current World____\n\n");
		worldnew.printWorld(); //calling method to print out world

		System.out.println("____Key____\n_F_ - Food\n_O_ - Object\n_ _ - Space\nSymbol - Bug");


		do{

			BugObj[i] = new ABug();  //creating instance


			System.out.print("Please enter the symbol which you wish to represent the bug:");
			BugObj[i].symbol = reader.next();
			System.out.print("Please enter the name of the bug:");
			BugObj[i].name = reader.next(); 
			System.out.println("Please enter the species of the bug:");   
			BugObj[i].species = reader.next(); 
			System.out.println("Please enter the horizontal position of the bug:");   
			BugObj[i].horpos = reader.nextInt();
			System.out.println("Please enter the vertical postion of the bug:");   
			BugObj[i].vertpos = reader.nextInt();	


			System.out.println("_______________ Bug " +(+i+1) + " _______________\n" );
			System.out.println("Symbol: " + BugObj[i].symbol);     //Printing bug information out
			System.out.println("Name: " + BugObj[i].name);           
			System.out.println("Species: " + BugObj[i].species);
			System.out.println("Horizontal Position: " + BugObj[i].horpos);
			System.out.println("Vertical Postion: " + BugObj[i].vertpos + "\n\n");
			move(BugObj[i], worldnew);


			i++;
			System.out.println("Would you like to enter another bug? \n 0-No,  1-Yes\n");
			loop = reader.nextInt();
		}while(loop == 1);
	}


	public static void move(ABug bug, MyClass wolrdnew){
		Scanner reader = new Scanner(System.in);
		System.out.println("Would you like this bug to move?\n 0-No,  1-Yes\n");
		if (reader.nextInt() == 0)
		{
			System.exit(0);
		}

		int x = bug.horpos;
		int y = bug.vertpos;


		worldnew.setMap(x,y,bug.symbol());
		worldnew.printWorld();

		//get corordinate of bug
		//set map[coor] = symbol
		//print out map


		int r = (int) (Math.random() * (2- -2)) + -2;
		int originalHorpos = bug.horpos;
		int originalVertpos = bug.vertpos;
		bug.horpos = originalHorpos + r;
		bug.vertpos = originalVertpos + r;


		//bug.horpos += r; 
		//bug.vertpos += r;

		System.out.println("New Horizontal Position: " +bug.horpos );
		System.out.println("New Vertical Postion: " +bug.vertpos);

	}
}

//public void smellFood (Direction d){
//	int MaxSensingDist = 2;
//}

//public void getRandomDirectionToMove (Direction d){
//
//}

//public void getDirectionOfFood (){

//}


enum Item {
	OBJECT ('O'),FOOD ('F'), SPACE (' ');

	private final char symbol;
	Item(char symbol) {
		this.symbol = symbol;
	}
	char getSymbol() { return symbol; }
}
class MyClass {

	public void setMap(int x, int y, Item symbol)
	{
		this.map[x][y] = symbol;
	}
	Item[][] map = new Item[15][25];
	public void printWorld() {

		int v, h; //v - vert, h - hor

		for (v=1; v<=15; ++v)
		{
			for (h=1; h<=25; ++h)
			{

				//map[0][0] = Item.TREE;
				//map[0][1] = Item.FOOD;
				//System.out.print(map[0][0].getSymbol());
				//System.out.print(map[0][1].getSymbol());

				//final String[] items = {"F", "O", ". ", ". ", ". ", ". ", ". ", ". "};
				//Random random = new Random();
				//int index = random.nextInt(items.length);
				//System.out.printf(items[index] + "\t");  

				final Item[] items = {Item.OBJECT, Item.FOOD, Item.SPACE, Item.SPACE, Item.SPACE, Item.SPACE, Item.SPACE, Item.SPACE};
				Random random = new Random();
				int selection = random.nextInt(items.length);
				map[v-1][h-1] = items[selection];
				System.out.print(map[v-1][h-1].getSymbol() + "_"); 


			}
			System.out.printf("\n");
		} 
	}
}
class ABug {                 //ABug class
	int horpos, vertpos, energy, id;
	String species, name, symbol;

}

1

1 Answer 1

1

how about just

int x = bug.get(horpos);
int y = bug.get(vertpos);

worldnew.setMap(x,y,bug.getSymbol());

worldnew.printWorld();

and in MyClass, add a new method like

void setMap(int x, int y, String symbol)
{
    this.map[x][y] = symbol;
}

you might need to make some changes to suit your code, but the basic idea remains the same

Edit

you will also need to change the

move(BugObj[i]);

to

move(BugObj[i],worldnew);

and change the definition of your move function as

public static void move(ABug bug, MyClass worldnew)
Sign up to request clarification or add additional context in comments.

12 Comments

Thanks, i have added it to my code and changed it around, however when i insert the 'worldnew.setMap(x,y,bug.symbol()); worldnew.printWorld();' into the move method, Its saying that 'worldnew is not resolved.
thats because worldnew does not exist in that function. please see the edit to my answer
Thanks, i have added that in, however its still not recognising the worldnew.setMap. Is there a way to allow access?
you have to create the setMap function in MyClass
worldnew.setMap(x,y,bug.symbol()); worldnew.printWorld(); worldnew cannot be resolved, this is the only error i get, not sure whats wrong as bug.horpos was working fine.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.