-
Notifications
You must be signed in to change notification settings - Fork 0
/
Codewars style ranking system.cs
112 lines (98 loc) · 2.2 KB
/
Codewars style ranking system.cs
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System;
public class User
{
public int rank;
public int progress;
public User()
{
rank = 8;
progress = 0;
}
public void incProgress(int actRank)
{
if (actRank < -8 || actRank == 0 || actRank > 8)
{
throw new ArgumentException();
}
else if (actRank > rank)
{
var valueProgress = 10 * Math.Pow(differenceInRanks(actRank, rank), 2);
increaseProgress((int)valueProgress);
}
else if (actRank == rank)
{
var valueProgress = 3;
increaseProgress(valueProgress);
}
else if (actRank == subtractFromRank(1))
{
var valueProgress = 1;
increaseProgress(valueProgress);
}
if (rank == 8)
{
progress = 0;
}
}
private int differenceInRanks(int fRank, int sRank)
{
var result = Math.Abs(fRank - sRank);
if ((fRank < 0 && sRank > 0) || (fRank > 0 && sRank < 0))
{
return result - 1;
}
else
{
return result;
}
}
private void increaseProgress(int n)
{
var newProgress = progress + n;
if (newProgress > 100)
{
rank = addToRank(newProgress / 100);
progress = newProgress % 100;
}
else
{
progress = newProgress;
}
}
private int subtractFromRank(int n)
{
var newRank = rank - n;
if (newRank == 0)
{
return -1;
}
else if (newRank < -8)
{
return -8;
}
else
{
return newRank;
}
}
private int addToRank(int n)
{
var newRank = rank + n;
if (newRank == 0)
{
return 1;
}
else if (newRank > 8)
{
progress = 0;
return 8;
}
else
{
return newRank;
}
}
}