Reputation: 377
I'm making game, and the game is based on shuffled tab[] of Integers. Every level of game takes array with different parameters (scope of numbers is changing) and an integer for let's say length of level. For now, I made arrayFatory class which makes that arrays with given parameters.
In code there are made 6 levels and they are represented by 6 arrayFactory objects which are assigned to currentArray. The currentArray is an array chosen to be "the current for that level" by switch case. And the cases are integers determining length of levels.
array1 = new RandomArrayFactory(3, 100);
array2 = new RandomArrayFactory(101, 200);
array3 = new RandomArrayFactory(201, 310);
array4 = new RandomArrayFactory(396, 720);
array5 = new RandomArrayFactory(721, 999);
array5 = new RandomArrayFactory(1000, 1310);
array6 = new RandomArrayFactory(1396, 2000);
switch (scoreCount) {
case 0:
progressScope = 13;
currentArray = array1.getNumbertab();
break;
case 13:
progressScope = 31;
currentArray = array2.getNumbertab();
break;
case 44:
progressScope = 56;
currentArray = array3.getNumbertab();
break;
case 100:
progressScope = 106;
currentArray = array4.getNumbertab();
break;
case 206:
progressScope = 214;
currentArray = array5.getNumbertab();
break;
case 420:
progressScope = currentArray.length;
currentArray = array6.getNumbertab();
break;
}
as you can see there is a lot of hardcoded variables and this is what I want to change. I would like to have kind of class with constructor that takes tab with scopes for every level and "progressScope" int, which I could use in place where currentArray is doing it's job.
And it is possible for me only when there is final amount of levels, then I could just copy that switch statement to my new class and return tab and int. But I would like to have possibility of defining new levels on as simple way as I can, I believe the simplest way would be the constructor that takes tab[][][] parameter, two first are values for arrayScope and second one for progressScope.
But how to make body of method so flexible to carry variable amount of levels to create? Or maybe my thinking is totally wrong and I should made it totally different? I'm not looking for specific answers, rather some tips or conceptions
Upvotes: 1
Views: 109
Reputation: 131396
To be able to add/remove any new levels, you should replace the switch
with hardcoded values by a loop with a Collection
.
Actually you choose a level according to the score count and you associate some information to your level (progressScope
, numberTab
).
This approach seems to have a drawback. It supposes that the actual score reaches exactly the score associated to each level. But how to handle if the current score exceeds a score corresponding to a level without never be equal to ?
So a switch
that performs a perfect matching (equals) is so maybe not the more suitable solution.
As alternative, I propose you to define a List of Level
ordered by descending score where Level
will contain all information to represent a Level
(core level data but also side but useful information as progressScope
, numberTab
and requiredScore
).
You could create your levels in this way :
List<Level> levels = new ArrayList<>();
levels.add(new Level(3, 100, 0, 13));
levels.add(new Level(201, 310, 13, 31));
and introduce a method to retrieve a Level
"matching" to a score:
public Level findLevel(List<Level> levels, int score) {
for (Level level : levels) {
if (score >= level.getRequiredScore()) {
return level;
}
}
throw new RuntimeException("should not come here !");
}
Now you can invoke it:
Level currentLevel = findLevel(levels, currentScore);
progressScope = currentLevel.getProgressScope();
currentArray = currentLevel.getCurrentArray();
Upvotes: 2