fix weird generic function

main
Brett 2023-03-06 16:45:35 -05:00
parent 349d908486
commit 33feedc3b1
1 changed files with 46 additions and 35 deletions

View File

@ -86,8 +86,8 @@ public class GameEngine<T> implements Runnable {
"2. Train inhabitants {command: '2 <unit name>'}\n"+ "2. Train inhabitants {command: '2 <unit name>'}\n"+
"3. Upgrade Building\n"+ "3. Upgrade Building\n"+
"4. Explore\n"+ "4. Explore\n"+
"5. Check Village Stats\n"+ "5. Print Village Stats\n"+
"6. Quit"); "6. Quit\n");
} }
public void attackVillage(Map map) { public void attackVillage(Map map) {
@ -199,7 +199,7 @@ public class GameEngine<T> implements Runnable {
if (args.length < 2) { if (args.length < 2) {
System.err.println("Args must include type!"); System.err.println("Args must include type!");
} else { } else {
Building type = (Building)determineType(args[1],new Cannon()); Building type = determineBuildingType(args[1]);
if (type == null) if (type == null)
System.err.println("Args are not a valid building!"); System.err.println("Args are not a valid building!");
else if (this.map.build(new Tile(), type) ) { else if (this.map.build(new Tile(), type) ) {
@ -211,7 +211,7 @@ public class GameEngine<T> implements Runnable {
if (args.length < 2) { if (args.length < 2) {
System.err.println("Args must include type!"); System.err.println("Args must include type!");
} else { } else {
Inhabitant type = (Inhabitant)determineType(args[1],new Worker()); Inhabitant type = determineInhabitantType(args[1]);
if (type == null) if (type == null)
System.err.println("Args are not a valid inhabitant!"); System.err.println("Args are not a valid inhabitant!");
else if (this.map.train(type) ) { else if (this.map.train(type) ) {
@ -220,7 +220,7 @@ public class GameEngine<T> implements Runnable {
} }
break; break;
case '5': case '5':
printState(this.map,"Current Village State"); printState(this.map,"Home Village");
break; break;
case '6': case '6':
System.exit(0); System.exit(0);
@ -236,39 +236,50 @@ public class GameEngine<T> implements Runnable {
} }
} }
private static <T> Object determineType(String argument, T type){ private static char determineChar(String str){
argument = argument.toLowerCase();
char c = ' '; char c = ' ';
if (argument.trim().length() == 1) if (str.trim().length() == 1)
c = argument.charAt(0); c = str.charAt(0);
return c;
}
if (type instanceof Building) { private static Building determineBuildingType(String argument){
if (argument.contains("gold") || argument.contains("good") || c == 'g') { argument = argument.toLowerCase();
return new SaulGoodMine(ResourceStages.goldStages[0]); char c = determineChar(argument);
} else if (argument.contains("iron") || c == 'i') {
return new IronMine(ResourceStages.ironStages[0]); if (argument.contains("gold") || argument.contains("good") || c == 'g') {
} else if (argument.contains("wood") || argument.contains("lumber") || c == 'w' || c == 'l') { return new SaulGoodMine(ResourceStages.goldStages[0]);
return new LumberMine(ResourceStages.woodStages[0]); } else if (argument.contains("iron") || c == 'i') {
} else if (argument.contains("archer") || c == 'a') { return new IronMine(ResourceStages.ironStages[0]);
return new ArcherTower(); } else if (argument.contains("wood") || argument.contains("lumber") || c == 'w' || c == 'l') {
} else if (argument.contains("can") || c == 'c') { return new LumberMine(ResourceStages.woodStages[0]);
return new Cannon(); } else if (argument.contains("archer") || c == 'a') {
} return new ArcherTower();
} else if (type instanceof Inhabitant) { } else if (argument.contains("can") || c == 'c') {
if (argument.contains("soldier") || argument.contains("sold") || c == 's') { return new Cannon();
return new Soldier();
} else if (argument.contains("knight") || c == 'k') {
return new Knight();
} else if (argument.contains("work") || c == 'w') {
return new Worker();
} else if (argument.contains("collect") || c == 'c') {
return new Collector();
} else if (argument.contains("cat")) {
return new Catapult();
} else if (argument.contains("arch") || c == 'a') {
return new Archer();
}
} }
return null;
}
private static Inhabitant determineInhabitantType(String argument) {
argument = argument.toLowerCase();
char c = determineChar(argument);
if (argument.contains("soldier") || c == 's') {
return new Soldier();
} else if (argument.contains("knight") || c == 'k') {
return new Knight();
} else if (argument.contains("work") || c == 'w') {
return new Worker();
} else if (argument.contains("collect") || c == 'c') {
return new Collector();
} else if (argument.contains("cat")) {
return new Catapult();
} else if (argument.contains("arch") || c == 'a') {
return new Archer();
}
return null; return null;
} }