In: Computer Science
In Java please. I put down my code and what I was able to achieve so far:
public class Animal {
private String gender; //stores the gender of the
animal
private String type; //stores the type of the
animal(bear of fish)
private int strength; //stores the strength of the
animal
public Animal() {
gender = "none";
type = "none";
strength = 0;
}
public Animal (String g, String t, int s)
{
g = gender;
t = type;
s = strength;
}
public boolean setGender() {
Random ranGen = new Random();
boolean g =
ranGen.nextBoolean();
if (g == true)
gender =
"Male";
else
gender =
"Female";
return g;
}
public String getGender() {
return gender;
}
public boolean setType() {
Random ranGen = new Random();
boolean t =
ranGen.nextBoolean();
if (t == true)
type =
"Bear";
else
type =
"Fish";
return t;
}
public String getType() {
return type;
}
public void setStrength(int strength) {
this.strength = strength;
}
public int getStrength() {
return strength;
}
public boolean compareGender(Animal animal) {
if(this.getGender().equals(animal.getGender()))
{
return
true;
}
else {
return
false;
}
}
public boolean compareType(Animal animal) {
if(this.getClass().equals(animal.getClass())){
return true;
}
else{
return
false;
}
}
public boolean compareStrength(Animal animal) {
if(this.getStrength() ==
(animal.getStrength())){
return true;
}
else{
return
false;
}
}
public void increaseStrength() {
this.setStrength(this.getStrength()
+ 4);
}
public String toString() {
String str = new String ("The
animal: " + type + " + gender + strength);
return str;
}
}
public class River {
private Animal[] river;
private int numAnimals;
public River() throws NumberFormatException{
numAnimals = 0;
Random randNum = new Random();
int num = randNum.nextInt(20);
river = new Animal[num];
while(numAnimals < river.length/2)
{
int place =
randNum.nextInt(river.length);
if(river[place] == null)
{
river[place] =
new Animal();
numAnimals++;
}
}
}
public River(Animal[] size) throws
NumberFormatException{
setRiver(size);
}
public void setRiver(Animal[] s) throws
NumberFormatException
{
river= s;
if (s.length
>= 10 && s.length <= 20)
river = s;
else
throw new
NumberFormatException("Invalid Number: number out of range");
}
public Animal[] getRiver() {
return river;
}
public void play() {
Random randNum = new
Random();
int moviment;
for(int i=0; i<river.length;
i++) {
if(river[i] !=
null) {
moviment = randNum.nextInt();
if(moviment == 1) {
}
}
}
So my problem is on the play method. I need to iterate through the array.
If I find an animal I need to randomly decide if the animal id going to move back, forward or stay in place.
If the animal is moving back I need to make sure that he is not at the beginning of the array.
If the animal is moving forward I need to make sure that he is not at the last position of the array.
If the animal stays in place nothing needs to be done.
If the animal moves back/forward the index position needs to be checked if to see if its empty.
If is empty the animal can just move there. If is not the animal type needs to be checked.
If they are of the same type its gender needs to be checked. Same gender produces a baby, different gender they fight and the weaker animal dies (meaning it will be erased from the array).
If the animals are from different types one of the animals is going to be eaten.
**I really do not know how to do this method, I tried to start but I do not know how to move forward, so I would really appreciate if you put comments through the code. Also, can you please check if the parameterized constructor is right?
Thank you so much
package Game;
import java.util.Random;
class Animal
{
private String gender; //stores the gender of the
animal
private String type; //stores the type of the
animal(bear of fish)
private int strength; //stores the strength of the
animal
public Animal()
{
gender = "none";
type = "none";
strength = 0;
}
public Animal (String g, String t, int s)
{
g = gender;
t = type;
s = strength;
}
public boolean setGender()
{
Random ranGen = new
Random();
boolean g =
ranGen.nextBoolean();
if (g == true)
gender =
"Male";
else
gender =
"Female";
return g;
}
public String getGender()
{
return gender;
}
public boolean setType()
{
Random ranGen = new Random();
boolean t =
ranGen.nextBoolean();
if (t == true)
type =
"Bear";
else
type =
"Fish";
return t;
}
public String getType()
{
return type;
}
public void setStrength(int strength)
{
this.strength = strength;
}
public int getStrength()
{
return strength;
}
public boolean compareGender(Animal animal)
{
if(this.getGender().equals(animal.getGender()))
return
true;
else
return
false;
}
public boolean compareType(Animal animal)
{
if(this.getClass().equals(animal.getClass()))
return true;
else
return
false;
}
public boolean compareStrength(Animal animal)
{
if(this.getStrength() ==
(animal.getStrength()))
return true;
else
return
false;
}
public void increaseStrength()
{
this.setStrength(this.getStrength()
+ 4);
}
public String toString()
{
String str = new String ("The
animal: " + type + " " + gender + " " + strength);
return str;
}
}
class River
{
private Animal[] river;
private int numAnimals;
public River() throws NumberFormatException
{
numAnimals = 0;
Random randNum = new Random();
int num = randNum.nextInt(20);
river = new Animal[num];
while(numAnimals < river.length/2)
{
int place = randNum.nextInt(river.length);
if(river[place] == null)
{
river[place] = new Animal();
numAnimals++;
}
}
}
public River(Animal[] size) throws
NumberFormatException
{
setRiver(size);
}
public void setRiver(Animal[] s) throws
NumberFormatException
{
//river = s;
if (s.length >= 10 &&
s.length <= 20)
river = s;
else
throw new
NumberFormatException("Invalid Number: number out of range");
}
public Animal[] getRiver()
{
return river;
}
void ifSameType(int animal)
{
int pos = 0;
for( ; pos < numAnimals;
pos++)
if(river[pos] ==
null)
break;
river[pos] = new Animal();
river[pos].setGender();
river[pos].setType();
river[pos].setStrength(1);
}
public void play()
{
Random randNum = new
Random();
int movement;
int animal;
for(int i = 0; i < river.length;
i++)
{
movement =
randNum.nextInt(2);
animal =
randNum.nextInt(river.length);
if(river[animal]
!= null)
{
System.out.println(river[animal] + " Moves
forward.");
if(movement == 1)
{
if(animal == river.length -
1)
System.out.println("Cannot move forward at the end.");
else if(river[animal + 1] ==
null)
{
river[animal + 1] = river[animal];
river[animal] = null;
}
else
if(river[animal].getType().compareTo
(river[animal + 1].getType()) == 0)
ifSameType(animal);
else
if(river[animal].getStrength() < river[animal +
1].getStrength())
river[animal] = null;
else
river[animal + 1] = null;
}
else
{
System.out.println(river[animal] + " Moves backward.");
if(animal == 0)
System.out.println("Cannot move backward at the beginning.");
else if(river[animal - 1] ==
null)
{
river[animal -1] = river[animal];
river[animal] = null;
}
else
if(river[animal].getType().compareTo
(river[animal - 1].getType()) == 0)
ifSameType(animal);
else
if(river[animal].getStrength() < river[animal -
1].getStrength())
river[animal] = null;
else
river[animal - 1] = null;
}
}
System.out.println(river[i]);
}
}
public String toString()
{
String result = "";
for(int c = 0; c < numAnimals;
c++)
if(river[c] !=
null)
result += river[c].toString() + "\n";
return result;
}
}
public class AnimalFight
{
public static void main(String []s)
{
Random strength = new
Random();
Animal animals[] = new
Animal[11];
for(int c = 0; c <
animals.length; c++)
{
animals[c] = new
Animal();
animals[c].setGender();
animals[c].setType();
animals[c].setStrength(strength.nextInt(20));
}
River river = new River();
river.setRiver(animals);
System.out.println("\n Initial
State \n" + river);
river.play();
}
}
Sample Output:
Initial State
The animal: Bear Male 12
The animal: Fish Female 15
The animal: Bear Female 11
The animal: Fish Male 15
The animal: Bear Female 18
The animal: Fish Female 13
The animal: Bear Male 18 Moves forward.
The animal: Bear Male 18 Moves backward.
The animal: Bear Male 12
The animal: Bear Male 18 Moves forward.
The animal: Fish Female 15
The animal: Fish Female 15 Moves forward.
The animal: Fish Female 15 Moves backward.
The animal: Bear Female 11
The animal: Fish Male 15 Moves forward.
The animal: Fish Male 15 Moves backward.
The animal: Fish Male 15
The animal: Bear Female 18
The animal: Fish Male 16 Moves forward.
The animal: Fish Male 16 Moves backward.
The animal: Fish Female 13
null
The animal: Fish Female 15 Moves forward.
The animal: Fish Female 15 Moves backward.
The animal: Bear Male 18
The animal: Fish Male 16
The animal: Bear Female 10 Moves forward.
Cannot move forward at the end.
null
The animal: Fish Female 15 Moves forward.
The animal: Bear Female 10