-1

I am making a knowledge based game , in which user participate and if they win and get their rank within criteria they earn gems , coins etc.

Example- A game in which their are 500 participants , and predefined Prize Distribution is Like...

Rank 1 gets 50000 coins
Rank 2 gets 40000 coins
Rank 3 to 50 gets 20000 coins
Rank 51 to 200 gets 5000 coins
Rank 201 to 500 gets 1000coins

If User plays the game and suppose he gets the rank 81 . So how can I check the distribution and award him the prize i.e, 5000 coins.

Can i use Hashmap to create some kind of key-value pair ..as shown below..

HashMap<Integer, Integer> distribution = new HashMap<>();
distribution.add(1,50000);
.
.
.
distribution.add(500,1000);

Any suggestions will boost my work.

Avni Krtigya
  • 31
  • 1
  • 4

2 Answers2

0

I would recommend you to make a Range wrapper class and lookup the value for a key in the range; This might help: https://stackoverflow.com/a/1314708/10052779

SIMULATAN
  • 833
  • 2
  • 17
0

Well you could do a few loops if you want to use HashMaps.

HashMap<Integer, Integer> distribution = new HashMap<>();
//first and second place
distribution.add(1,50000); 
distribution.add(2,40000);

//3rd through 50th place
for(int i = 3; i < 51; i++) 
    distribution.add(i,20000);

//51 through 200th place
for(int i = 51; i < 201; i++) 
    distribution.add(i, 5000);

//201 through 500th place
for (int i = 201; i < 501; i++)
    distribution.add(i, 1000);

and tada you are done! However, unless you want it to run every single time, I would make distribution a static variable so you only have to run it once and it will stay for every run after that.

James E
  • 186
  • 9
  • Yes, that is why I stated to make it a static variable. There are much better ways than this. You could make just 1 array of [50000, 40000, 20000, 5000, 1000], then just say if 1st place then get array[0], else if second place get array[1], else if 3 <= rank <= 50 get array[2], else if 51 <= rank <= 200 get array[3], else if 201 <= rank <= 500 get array[4]. – James E Oct 18 '21 at 13:27
  • boom you just answered your own question, make an array of all of the possible coins in order, then get them according to the persons "rank range" (between 2 to 100 get array[1] etc). (btw yes I did just make up the term rank range) – James E Oct 18 '21 at 13:30
  • Oops i accidently deleted my comment. But hey thanks, i understood your approach. Will accept the answer in couple of hours . Thanks mate – Avni Krtigya Oct 18 '21 at 13:32