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,13 +236,17 @@ 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;
}
private static Building determineBuildingType(String argument){
argument = argument.toLowerCase();
char c = determineChar(argument);
if (type instanceof Building) {
if (argument.contains("gold") || argument.contains("good") || c == 'g') { if (argument.contains("gold") || argument.contains("good") || c == 'g') {
return new SaulGoodMine(ResourceStages.goldStages[0]); return new SaulGoodMine(ResourceStages.goldStages[0]);
} else if (argument.contains("iron") || c == 'i') { } else if (argument.contains("iron") || c == 'i') {
@ -254,8 +258,15 @@ public class GameEngine<T> implements Runnable {
} else if (argument.contains("can") || c == 'c') { } else if (argument.contains("can") || c == 'c') {
return new Cannon(); return new Cannon();
} }
} else if (type instanceof Inhabitant) {
if (argument.contains("soldier") || argument.contains("sold") || c == 's') { return null;
}
private static Inhabitant determineInhabitantType(String argument) {
argument = argument.toLowerCase();
char c = determineChar(argument);
if (argument.contains("soldier") || c == 's') {
return new Soldier(); return new Soldier();
} else if (argument.contains("knight") || c == 'k') { } else if (argument.contains("knight") || c == 'k') {
return new Knight(); return new Knight();
@ -268,7 +279,7 @@ public class GameEngine<T> implements Runnable {
} else if (argument.contains("arch") || c == 'a') { } else if (argument.contains("arch") || c == 'a') {
return new Archer(); return new Archer();
} }
}
return null; return null;
} }