I am currently working on a text based RPG and am still learning java and programming in general. My question is this, I have come to a point in my program where the user will select their units and I dont know how to accomplish this without large if statements that are very restrictive in terms of expanding or altering the game play. How could I write something that could condense my if statements and have a more modular approve overall?
Here is what the user first see:
Welcome to RPG Game!
Please select 3 units!
Enter 1 to add a Ranger 100 4 10
Enter 2 to add a Heavy 100 4 15
enter 3 to add a Medic 100 4 10
To respond to this I have codded the following
if (resp == 1){
if (rangerCount == 0){
ArrayList<Item> rangerInv0 = new ArrayList<Item>();
Ranger rangerObj0 = new Ranger("Ranger", 100, 4, 10, rangerInv0);
rangerObj0.pickUp(mh1000);
rangerObj0.pickUp(s1000);
rangerObj0.pickUp(t1000);
rangerObj0.equip(mh1000);
rangerObj0.equip(s1000);
rangerObj0.equip(t1000);
playerUnits.add(rangerObj0);
playerUnitsCount++;
rangerCount++;
}
else if (rangerCount == 1){
ArrayList<Item> rangerInv1 = new ArrayList<Item>();
Ranger rangerObj1 = new Ranger("Ranger", 100, 4, 10, rangerInv1);
rangerObj1.pickUp(mh1000);
rangerObj1.pickUp(s1000);
rangerObj1.pickUp(t1000);
rangerObj1.equip(mh1000);
rangerObj1.equip(s1000);
rangerObj1.equip(t1000);
playerUnits.add(rangerObj1);
playerUnitsCount++;
rangerCount++;
}
else if (rangerCount == 2){
ArrayList<Item> rangerInv2 = new ArrayList<Item>();
Ranger rangerObj2 = new Ranger("Ranger", 100, 4, 10, rangerInv2);
rangerObj2.pickUp(mh1000);
rangerObj2.pickUp(s1000);
rangerObj2.pickUp(t1000);
rangerObj2.equip(mh1000);
rangerObj2.equip(s1000);
rangerObj2.equip(t1000);
playerUnits.add(rangerObj2);
playerUnitsCount++;
rangerCount++;
}
}
else if (resp == 2) {
if (heavyCount == 0) {
ArrayList<Item> heavyInv0 = new ArrayList<Item>();
Heavy heavyObj0 = new Heavy("Heavy", 100, 4, 15, heavyInv0);
heavyObj0.pickUp(mh4000);
heavyObj0.pickUp(s1000);
heavyObj0.pickUp(t1000);
heavyObj0.equip(mh4000);
heavyObj0.equip(s1000);
heavyObj0.equip(t1000);
playerUnits.add(heavyObj0);
playerUnitsCount++;
heavyCount++;
}
else if (heavyCount == 1){
ArrayList<Item> heavyInv1 = new ArrayList<Item>();
Heavy heavyObj1 = new Heavy("Heavy", 100, 4, 15, heavyInv1);
heavyObj1.pickUp(mh4000);
heavyObj1.pickUp(s1000);
heavyObj1.pickUp(t1000);
heavyObj1.equip(mh4000);
heavyObj1.equip(s1000);
heavyObj1.equip(t1000);
playerUnits.add(heavyObj1);
playerUnitsCount++;
heavyCount++;
}
else if (heavyCount == 2){
ArrayList<Item> heavyInv2 = new ArrayList<Item>();
Heavy heavyObj2 = new Heavy("Heavy", 100, 4, 15, heavyInv2);
heavyObj2.pickUp(mh4000);
heavyObj2.pickUp(s1000);
heavyObj2.pickUp(t1000);
heavyObj2.equip(mh4000);
heavyObj2.equip(s1000);
heavyObj2.equip(t1000);
playerUnits.add(heavyObj2);
playerUnitsCount++;
heavyCount++;
}
}
else if (resp == 3) {
if (medicCount == 0) {
ArrayList<Item> medicInv0 = new ArrayList<Item>();
Medic medicObj0 = new Medic("Medic", 100, 4, 10, medicInv0);
medicObj0.pickUp(mh3000);
medicObj0.pickUp(s1000);
medicObj0.pickUp(t1000);
medicObj0.equip(mh3000);
medicObj0.equip(s1000);
medicObj0.equip(t1000);
playerUnits.add(medicObj0);
playerUnitsCount++;
medicCount++;
}
else if (medicCount == 1) {
ArrayList<Item> medicInv1 = new ArrayList<Item>();
Medic medicObj1 = new Medic("Medic", 100, 4, 10, medicInv1);
medicObj1.pickUp(mh3000);
medicObj1.pickUp(s1000);
medicObj1.pickUp(t1000);
medicObj1.equip(mh3000);
medicObj1.equip(s1000);
medicObj1.equip(t1000);
playerUnits.add(medicObj1);
playerUnitsCount++;
medicCount++;
}
else if (medicCount == 2) {
ArrayList<Item> medicInv2 = new ArrayList<Item>();
Medic medicObj2 = new Medic("Medic", 100, 4, 10, medicInv2);
medicObj2.pickUp(mh3000);
medicObj2.pickUp(s1000);
medicObj2.pickUp(t1000);
medicObj2.equip(mh3000);
medicObj2.equip(s1000);
medicObj2.equip(t1000);
playerUnits.add(medicObj2);
playerUnitsCount++;
medicCount++;
}
}
else {
System.out.println("Please enter a valid responce!");
}
The objects you see referenced in the multiple instances of pickUp() and equip() look like this for reference:
static Item mh1000 = new Item ("AR1", "Damage", 2, null, 0, null, 0, false, false);
to me this all seems very cumbersome. What if I want to make it a game with 5 units or 10 units, do I really have to add 2 to 7 lines of code per entry to accomplish this? it seems like there should be a much easier way to do this. Please advise.
Aucun commentaire:
Enregistrer un commentaire