Reputation: 75
a friend of mine came up with an idea for a racing game, and i'm trying to create that in java. now, i have made 3 classes for cars, 1 for player cars, 1 for computer(ai)cars and a main one that holds some variables like location (x,y on the screen) and name to name a few. the first 2 inherit from the last one. i hoped this would allow me to create one array with both the players and the computer players in it. this however doesnt work, and now my question:
is there any way it is possible to have an array with different kinds of objects in them, and if this is possible, how can i do it or are there any tutorials on it?
i have looked at interfaces, but i dont think that would do the trick, but please correct me if im wrong.
this was the idea that i had:
MainCar[] carsArray = new MainCar[totalPlayers];
for(int i = 0; i < totalHumanPlayers; i++)
{
carsArray[i] = new PlayerCar();
}
for(int i = 0; i < totalComputerPlayers; i++)
{
carsArray[i] = new ComputerCar();
}
the idea with it is that i can loop through all the players(human and computer) to draw them at their locations and to decide whos turn it is next turn
many thanks, and please forgive my english, i don't know if it's correct or not, not my first language :)
Upvotes: 3
Views: 151
Reputation: 104
Ok, with the second loop you are overwriting the values previously set, because you start at the same offset i=0.
I'd suggest something like:
for(int i = totalHumanPlayers; i < (totalHumanPlayers + totalComputerPlayers); i++)
You can hold both types of car in your array, just as you did (declaring the array to be of type MainCar), remember that MainCar can be a ComputerCar or a PlayerCar, you will need to cast to make use one of them (if you need to access to specific members of PlayerCar or ComputerCar) like this:
PlayerCar player = (PlayerCar)carsArray[x];
But in your case, if you only need coords at MainCar you just can access them directly (if they are members of MainCar of course) like:
carsArray[i].location.x
or
carsArrays[i].x
Just remember to initialize correctly the array. (Don't overlap your data)
EDIT:
And to determine if a car is of one type or another use the instanceof operator, here's an example:
if (carVar instanceof PlayerCar) {
PlayerCar player = (PlayerCar)carVar;
}
Upvotes: 2
Reputation: 31754
It might not be a good idea to have the same array for player car and computer car, unless they both inherit same class. For example its alright if:
interface MainCar{
public void horn();
}
class PlayerCar implements Car{
public void run(){ System.out.println("Broom Broom");}
}
class ComputerCar implements Car{
public void run(){ System.out.println("tak tak");}
}
MainCar[] cars = new MainCar[totalPlayers];
cars[0] = new PlayerCar();
cars[1] = new ComputerCar();
This happens due to co-variance nature of Arrays in Java.
Had PlayerCar
and ComputerCar
not inherited MainCar
, then the above would not had been possible (definitely not recommended). But due to co-variance you could:
Object[] cars = new Object[totalPlayers];
cars[0] = new PlayerCar();
cars[1] = new ComputerCar();
The reason it is not preferred is, if you access cars[0]
above. You do not know what type will it be as:
Object a = cars[0];
a
can be a String
, Animal
, List
or anything for that matter. And ig you have to use it as a car, you need to type cast it as which is very very very unsafe:
if(a instanceof PlayerCar){
//then do something
}else if(a instanceof ComputerCar){
//then do something
}
PS: Change your username from javaNoobsForever
, you cant be that bad :). Well we all are noobs :)
Upvotes: 1
Reputation: 99
Make a car interface that all of the cars implement, and then make the array of the interface type.
Note. You will only be able to call methods from the interface, unless you cast them back into their appropriate classes
Upvotes: -1
Reputation: 4185
public class PlayerCar : MainCar {}
public class ComputerCar : MainCar {}
MainCar[] carsArray = new MainCar[totalPlayers];
for (int i = 0; i < totalCarsCount; i++) {
if (carsArray[i] instanceof PlayerCar) {
System.out.println("Player car");
} else {
System.out.println("Computer Car");
}
}
Upvotes: 2