Reputation: 987
I have a class "Level" I am coding in java but it could be any object oriented language.
In that class, I have 2 static variables:
int ID;
int STARSREWARD;
During the game, everytime the player finishes a level he is rewarded a certain number of stars, defined by the value of STARSREWARD.
For example:
etc.
In my code, I set the value of STARSREWARD whenever the player starts a level i.e. the value of STARSREWARD for level 10 is not set when the player has only started to play level 1. However each STARSREWARD values are static and are already written in the code.
Is there a way to directly calculate the sum of total number of stars of all levels? In my example it would be 1 + 3 + 2 = 6.
Upvotes: 0
Views: 3087
Reputation: 7894
How about an enum:
public enum StarsReward {
LEVEL_1(1),LEVEL_2(3),LEVEL_3(2);
private int stars;
private static int starsTotal;
StarsReward(int stars) {
this.stars = stars;
addToStarsTotal(stars);
}
public int getStars() {
return stars;
}
private static void addToStarsTotal(int stars) {
starsTotal += stars;
}
public static int getStarsTotal() {
return starsTotal;
}
}
Then use the static getStarsTotal method.
The benefits here are that you don't need to worry about calculating the total number of stars. It is done automatically for you. The same applies when adding another enum value.
You also need not worry with looking up an array or a map to find the stars, simply call the getStars method on the appropriate enum value.
Upvotes: 2
Reputation: 4435
It would make the most sense to store these STARSREWARD
values in an array. This would make accessing each levels STARSREWARD
easy, and you could just loop through the array and add up all the values to get the total number of stars for all levels.
static int[] STARSREWARDS = new int[numLevels];
STARSREWARDS[0] = 1; // level 1
STARSREWARDS[1] = 3; // level 2
STARSREWARDS[2] = 2; // level 3
// etc...
Access like this:
// subtract 1, since the indices start at 0, and level starts at 1
STARSREWARDS[currentLevel - 1];
Get sum like this:
int sum = 0;
for(int i = 0; i < STARSREWARDS.length; i++) {
sum += STARSREWARDS[i];
}
Additional Notes: If you are getting the sum more than once, you could put that loop in a function. Also, if it is possible for more levels to be added after the original array of levels is first initialized, then you could use an ArrayList
instead of an Array
. Also, you could make a function for getting the number of stars based on the level, so you wouldn't have to worry about subtracting 1 from the level number each time you get the number of stars for a level (the function would handle that for you).
Upvotes: 2
Reputation: 46
I will assist you to the best of my ability. Alright, so what I understand so far is that you have a predetermined number of stars set for each level already, however you want to set the STARSREWARD
for that specific level when the player begins said level.
I assume you made certain to have some sort of storage for each specific value of stars. Such a method could pertain to using an array or an ArrayList
. An example would be like;
ArrayList<Integer> starvalues = new ArrayList<Integer>();
starvalues.add(1);
starvalues.add(3);
starvalues.add(2);
// More starvalues additions ///
or
// A really shoddy method of inputting random values ///
// (IN OTHER WORDS, NOT RECOMMENDED IN THE LEAST BIT!) ///
int[] starvalues = new int[];
starvalues[0] == 1;
starvalues[1] == 3;
starvalues[2] == 2;
I would personally recommend the ArrayList
to storing the STARREWARD
values simply because you won't be storing too much considering you most likely will have a two digit number of levels (meaning not many STARREWARD
values to store).
An ArrayList
in this case is just easier for adding values. For example, if the number of levels exceeds your expectations, then it doesn't have to be resized like an array, but instead can be added upon.
And then I'm certain you have or will have a level number bound to each specific level. This is just a number that represents which level the user is currently playing. This would look something like 'getCurrentLevelNum()
'.
With this you would be able to check through a for loop that is the size of your total number of levels. The code would look something like this:
for(int i = 0; i < totalLevelNum; i++){
// i May start at whichever number you started first level at ///
if(i == getCurrentLevelNum()){
STARREWARD = starvalues.get(i);
}
}
I realize you mentioned that the STARREWARD
variable is static
, however that isn't going to work out well for you. It is constantly being modified as the user progresses, so it would make it indubitably inconvenient to keep it as static
. I recommend making it public
and putting it in your game class.
I also recommend making a separate method for this. (You should probably copy-paste these into an actual editor as a test class so you can get a better look at it.) The class would end up looking something like this:
(Remember, this is just an example)
import java.util.ArrayList;
public class GameTest{
private ArrayList<Integer> starvalues = new ArrayList<Integer>(); // An ArrayList of type Integer ///
private int STARREWARD; // the Star reward's variable ///
private int levelNum; // The Level Num variable ///
public GameTest(){
// This is used to make an object so we can test GameTest's methods with our static main() method ///
}
// Build Star Values (stores the STARREWARD values in order of each level) ///
public void buildStarValues(){
starvalues.add(1); // Level 0;
starvalues.add(3); // Level 1;
starvalues.add(2); // Level 2;
// More STARREWARD additions here ///
} // end buildStarValues()
// Get Current Level Num (Get's the current level the user is on) ///
public int getCurrentLevelNum(){
// Let's say the user chooses Level 1 ///
return levelNum = 1;
// This is only an example. This is the number representing the level the user chooses.
} // end getCurrentLevelNum()
// Set Star Reward Value (Checks level number and sets the STARREWARD variable accordingly, and then returns it) ///
public int getStarRewardValue(){
stop:{ // Used to jump out of for-loop once you set STARREWARD
for(int i = 0; i < starvalues.size(); i++){
if(i == getCurrentLevelNum()){
STARREWARD = starvalues.get(i);
}
}
}
return STARREWARD; // return statement
} // end setStarRewardValue()
// Main ///
public static void main(){
GameTest g = new GameTest();
g.buildStarValues();
System.out.println("STARREWARD = " + g.getStarRewardValue());
} // end main()
} // end class
You could also use a switch-case statement which may be more useful if you plan on making level numbers that are negative, etc. This would get rid of the ArrayList
starvalues
and the method buildStarValues()
from the previous code:
public class GameTest2{ // Class with switch-case (No ArrayList) ///
private int STARREWARD; // the Star reward's variable ///
private int levelNum; // The Level Num variable ///
// Game Test 2 ///
public GameTest2(){
// This is used to make an object so we can test GameTest2's methods with our static main() method ///
} // end gameTest2()
// Get Current Level Num (Get's the current level the user is on) ///
public int getCurrentLevelNum(){
// Let's say the user chooses Level 1 ///
return levelNum = 1;
// This is only an example. This is the number representing the level the user chooses.
} // end getCurrentLevelNum()
// Set Star Reward Value (Checks level number and sets the STARREWARD variable accordingly, and then returns it) ///
public int getStarRewardValue(){
switch(getCurrentLevelNum()){
case 0: STARREWARD = 1; break;
case 1: STARREWARD = 3; break;
case 2: STARREWARD = 2; break;
// More switch-case statements ///
}
return STARREWARD; // return statement
} // end setStarRewardValue()
// Main ///
public static void main(){
GameTest2 g = new GameTest2();
System.out.println("STARREWARD = " + g.getStarRewardValue());
} // end main()
} // end class
To add it you would just add a STARREWARDTOT variable and add onto it every time the user completes the level.
IMPORTANT: Either way, considering your STARREWARD
values will be decided by you yourself, this will be a long, arduous, MANUAL PROCESS. I SUGGEST that when you make a separate class for each level, that you give a number to represent the level. This would be stored in a public
method like getLevelNum()
. This way you could manage the ArrayList
in a more organized matter. You would be able to set the number of the element in the ArrayList
to that of the level's getLevelNum()
method.
I hope this may have helped or at least sparked some ideas in you. If you're confused about anything or feel that something is wrong, feel free to ask or correct me.
Upvotes: 1
Reputation: 1122
Try to make a static Method in every Level- class which returns the amount of stars the user can get, then make a ArrayList of the levels; afterwards you just have to iterate through the levels and call the method.
Upvotes: -1
Reputation:
You can just use a Map data structure and add level number as key and the number of stars as the value.
Upvotes: 1