I am creating a game using an array, I have my Hunter class which looks somewhat like this ;
public static int x= 11;
public static int y =11;
public static String name = "H";`
And a method for its path using x and y.
I have declared hunter as an array in my board (2d array) class this way;
public Hunter hunters[] = new Hunter[5];
and the position of a hunter is declared in the board class as ;
a2[Hunter.x][Hunter.y] = Hunter.name;
Question: I want 5 hunters to appear on the board, how do I use the array to spawn additional 4 hunters? Thanks.
you created your array fine all you need to do is use it:
for (int i = 0; i < 5; ++i)
{
a2[hunters[i].x][hunters[i].y] = hunters[i].name
}
also, you need to make your Hunter members non-static
class Hunter
{
private int x, y;
public void setLocation(int x_, int y_)
{
x = x_; y = y_;
}
}
you get the idea :)
The static keyword (assuming you are using C++, java, C#) means that the variable is shared among all instances of the Hunter class. To allow each Hunter to have its own position, remove the static keyword and initialize them with a constructor.
I'll assume you're using Java bases on your use of String:
public class Hunter {
public int x;
public int y;
public String name;
public Hunter(int x, int y, string name) {
this.x = x;
this.y = y;
this.name = name;
}
}
Then to initialize 5 you would do
int numHunters = 5;
for (int i = 0; i < numHunters; i ++) {
hunters[i] = new Hunter(/* put x and y and name here */);
}
You can then use them to populate the board:
for (int i = 0; i < numHunters; i ++) {
Hunter h = hunters[i];
a2[h.x][h.y] = h.name;
}
You didn't specify what language you're using. That would help a bit.
At a minimum, try removing the "static" keyword from your property definitions.
In C#, your Hunter class might look like
public class Hunter
{
public int x;
public int y;
public String name;
public Hunter(int newX, int newY, String newName)
{
x = newX;
y = newY;
name = newName;
}
}
You create new Hunters using Hunter h1 = new Hunter(11, 11, "H");. Once created, you can do whatever with it.
You may want to do some reading up on Object Oriented Programming - see Intro to OOP esp sections 4.3 - 4.5 (they're short)
Related
I'm lost and don't know where to begin. I'm testing the following condition, L=W=H on Package1's method and I want to call it into the Class Runtime's main method.
I created an array of objects already. But after that, I don't know how to utilize it or even if I have to. Again I'm completely lost!...thanks for your help.
I feel as if Coding is a young man's world!, Damn you Marine Corps!
public class Package1
{
double length;
double width;
double height;
Package1(double a,double b, double c)
{
length=a;
width=b;
height=c;
}
public void isCube()
{
if(length==width && width==height)
System.out.println("The box is a cube.");
else
System.out.println("Box is not a cube. ");
}
public class Runtime{
public static void main(String[] args){
Package1[] boxes = new Package1[rows];
for(int j = 0; j < boxes.length; j++)
{
boxes[j] = new Package1(arr[j][0], arr[j][1], arr[j][2]);
}
}
}
Like this.
for(int j = 0; j < boxes.length; j++)
{
boxes[j] = new Package1(arr[j][0], arr[j][1], arr[j][2]);
boxes[j].isCube(); // this is the line that you need
}
Any element in an array can be accessed by passing the index of the item into the brackets. For instance boxes[0] is the first element in the array. boxes[ boxes.length - 1 ] is the last element in the array.
I have two scores
A = 0
B = 0
I want to randomly pick one and add 7 to it.
I believe I have to do an array, but can't figure that out.
I also believe I need to do a random.range, but because I can't figure out the array I can't get beyond this step.
I would also like to set Which every team scored to "teamScored"
code:
public float A = 0;
public float B = 0;
public float [] scores = {A,B};
public float teamScore=??;
public void Start{
teamScored = scores(Random.Range(0,2));
teamScored +7;
}
I know this is broken, but this is how I see it suppose to be working in my head.
public int A = 0;
public int B = 0;
public int[] scores = {A,B};
public void Start{
int teamScored = Random.Range(0,2);
scores[teamScored] += 7;
}
I have a Player class which has
int number;
In main, I store them in Array.
Array<Player> players;
How can I get a player which has for example number=2?
This question is programming in general, and has several ways to do what you say, I recommend you look for OOP.
a solution, general, would encapsulate the variable, "creating getter and setter, you need, eg:
this a simple class;
private int number;
public Player(int num){
this.number = num;
}
public int getNumber (){
return number;
}
public void setNumber (int n){
this.number = n;
}
.
In your code for search you can use the solution, using a for, or an iterator
for (int a = 0; a < players.size(); a++){
int tmpNumber = players.get(a).getNumber();
if(tmpNumber == 2){
players.get(a); //the object array with index equal to
//the value of 'a', have, number 2 stored
//in the variable 'number'
}
}
but this question I think is a little matter of taste or needs you have
I'm going to make some assumptions to demonstrate it:
Let's assume you have a constructor in your Player class:
public Player(int num){
this.number = num;
}
And you have added some players in your arraylist:
players.add(new Player(1));
players.add(new Player(2));
players.add(new Player(3));
Now loop through your array and find the one where number == 2
for (int i = 0; i < players.size(); i++){
Player tmp = players.get(i);
if(tmp.number == 2){
System.out.println("Index of player number 2: " + players.indexOf(tmp));
}
}
I realised that I'd misunderstood the question with my previous answer. Here's a better one...
Those recommending you iterate through the Array are correct, but if you have a lot of Players this will be ineffeicient. In that case, it would be better to use a Map where the key was the player number, and the value is the player itself.
Here's some sample code showing the approach (most of it is just setting up some test data)...
import java.util.HashMap;
import java.util.Map;
public class Temp {
public static void main(String[] args) {
// Create a map instead of an array
Map<Integer, Player> players = new HashMap<>();
// Quick hack just to put some data in it.
for(int i = 0; i < 1000; i++) {
players.put(i, new Player(i));
}
// Once they're in a map, retrieving them is simple...
Player playerFromMap = players.get(537);
// Check we got it right
System.out.println(playerFromMap.getNumber()); // 537
}
private static class Player {
private int number;
private Player(int number) {
this.number = number;
}
public int getNumber() {
return number;
}
}
}
I recently moved from Windows applications to WPF . The programming language I using is the visual basic net . My question is how to connect how to my own class and how to send the parameter from main application window. If this question is duplicate then send me link.
You just pass the parameter in the ctor
Instance Constructors
class CoOrds
{
public int x, y;
// Default constructor:
public CoOrds()
{
x = 0;
y = 0;
}
// A constructor with two arguments:
public CoOrds(int x, int y)
{
this.x = x;
this.y = y;
}
// Override the ToString method:
public override string ToString()
{
return (String.Format("({0},{1})", x, y));
}
}
Before I state my question, I would like to thank everyone who helped me on my previous question. Anywho, I am currently getting the memory offsets as result even when I place it to the to a toString method. I've read most of the questions regarding the toString method and have somewhat of a understanding, I just wanted to make sure if I'm implementing this correctly. If you feel that this is a redundant question, I understand. Thanks for the help in advance.
Ship class
import java.util.Scanner;
public class Ship{
int type;
public String [][] shipPiece = new String[11][11];
Scanner in = new Scanner(System.in);
//Coordinate xy = new Coordinate();
//private Coordinate[] bawdPiece = {new section()};
public Ship(){
}
public String[][] placeShip()
{
System.out.println();
for(int x = 1; x<10; x++)
{
for(int y = 1; y<10; y++)
{
shipPiece[x][y] = "0";
System.out.print("|" + shipPiece);
}
}
return shipPiece;
}
public String toString()
{
StringBuilder temp = new StringBuilder();
temp.append(shipPiece);
return temp.toString();
}
public String toString()
{
StringBuilder temp = new StringBuilder();
for(int x = 0; x < 10; x++)
{
for(int y = 0; y < 10; y++)
{
temp.append(String.valueOf(shipPiece[x][y]);
}
temp.append("\n");
}
return temp.toString();
}
Also another thing I would like to point out is your array initialization. You initialize it with 11 x 11 indices, but your loop is only adding 9 values to each row and column Loop starts at 1 and ends at 9. You may want to fix your initialization to 10 x 10 and make your loop start at 0.
If I understand the question correctly, you are trying to get the index of the shipPiece as a string?
If that's the case try String.ValueOf(index), you'll obviously have to do this twice as you have a 2-dimensional array.
Ex:
String s = String.ValueOf(xIndex) + "," + String.ValueOf(yIndex);