-
Notifications
You must be signed in to change notification settings - Fork 0
/
CodewarsStyleRankingSystem.java
62 lines (57 loc) · 3.45 KB
/
CodewarsStyleRankingSystem.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package me.m_zebrak.kyu4;
/**
* Write a class called User that is used to calculate the amount that a user will progress through a ranking system similar to the one Codewars uses.
* <p>
* Business Rules:
* A user starts at rank -8 and can progress all the way to 8.
* There is no 0 (zero) rank. The next rank after -1 is 1.
* Users will complete activities. These activities also have ranks.
* Each time the user completes a ranked activity the users rank progress is updated based off of the activity's rank
* The progress earned from the completed activity is relative to what the user's current rank is compared to the rank of the activity
* A user's rank progress starts off at zero, each time the progress reaches 100 the user's rank is upgraded to the next level
* Any remaining progress earned while in the previous rank will be applied towards the next rank's progress (we don't throw any progress away). The exception is if there is no other rank left to progress towards (Once you reach rank 8 there is no more progression).
* A user cannot progress beyond rank 8.
* The only acceptable range of rank values is -8,-7,-6,-5,-4,-3,-2,-1,1,2,3,4,5,6,7,8. Any other value should raise an error.
* The progress is scored like so:
* <p>
* Completing an activity that is ranked the same as that of the user's will be worth 3 points
* Completing an activity that is ranked one ranking lower than the user's will be worth 1 point
* Any activities completed that are ranking 2 levels or more lower than the user's ranking will be ignored
* Completing an activity ranked higher than the current user's rank will accelerate the rank progression. The greater the difference between rankings the more the progression will be increased. The formula is 10 * d * d where d equals the difference in ranking between the activity and the user.
* Logic Examples:
* If a user ranked -8 completes an activity ranked -7 they will receive 10 progress
* If a user ranked -8 completes an activity ranked -6 they will receive 40 progress
* If a user ranked -8 completes an activity ranked -5 they will receive 90 progress
* If a user ranked -8 completes an activity ranked -4 they will receive 160 progress, resulting in the user being upgraded to rank -7 and having earned 60 progress towards their next rank
* If a user ranked -1 completes an activity ranked 1 they will receive 10 progress (remember, zero rank is ignored)
* Code Usage Examples:
* User user = new User();
* user.rank; // => -8
* user.progress; // => 0
* user.incProgress(-7);
* user.progress; // => 10
* user.incProgress(-5); // will add 90 progress
* user.progress; // => 0 // progress is now zero
* user.rank; // => -7 // rank was upgraded to -7
*/
public class CodewarsStyleRankingSystem {
int rank = -8;
int progress = 0;
public void incProgress(int rank) {
if (Math.abs(rank) > 8 || rank == 0) throw new IllegalArgumentException();
this.progress += calcProgress(rank);
while (this.progress >= 100 && this.rank < 8) {
if (++this.rank == 0) this.rank++;
this.progress -= 100;
}
if (this.rank == 8) this.progress = 0;
}
private int calcProgress(int rank) {
int diff = Math.abs(this.rank - rank);
if ((this.rank < 0 && rank > 0) || (this.rank > 0 && rank < 0)) diff--;
if (this.rank < rank) return 10 * diff * diff;
if (this.rank > rank && diff == 1) return 1;
if (this.rank == rank) return 3;
return 0;
}
}