Skip to content

Commit 6d86e46

Browse files
author
hero
committed
添加elo
1 parent 09f382e commit 6d86e46

File tree

3 files changed

+50
-52
lines changed

3 files changed

+50
-52
lines changed

elo/elo.go

Lines changed: 0 additions & 51 deletions
This file was deleted.

game/elo/elo.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package elo
2+
3+
import (
4+
"fmt"
5+
"math"
6+
"strconv"
7+
)
8+
9+
const (
10+
// K is the default K-Factor
11+
K = 32
12+
// D is the default deviation
13+
D = 400
14+
)
15+
16+
type Elo struct {
17+
//输入值
18+
A uint32 //A玩家当前的Rating
19+
B uint32 //B玩家当前的Rating
20+
Sa float64 //实际胜负值,胜=1,平=0.5,负=0 传入值默认A的胜负 / 1 A胜利 B失败 / 0 B胜利 A失败
21+
22+
23+
}
24+
25+
func EloRating(elo Elo)(a uint32,b uint32) {
26+
var (
27+
Ea float64 //预期A选手的胜负值
28+
Eb float64 //预期B选手的胜负值
29+
Ra uint32 //A玩家进行了一场比赛之后的Rating
30+
Rb uint32 //B玩家进行了一场比赛之后的Rating
31+
)
32+
Ea = 1 / (1 + math.Pow(10, float64(elo.B-elo.A)/float64(D)))
33+
Ea=Decimal(Ea,"%.2f")
34+
Eb = 1-Ea
35+
var Sb float64
36+
if elo.Sa==0{
37+
Sb=1
38+
}
39+
Ra = elo.A + uint32(Decimal(float64(K)*(elo.Sa-Ea),"%.0f"))
40+
Rb = elo.B + uint32(Decimal(float64(K)*(Sb-Eb),"%.0f"))
41+
return Ra,Rb
42+
}
43+
//f 保留2 位小数 %.2f
44+
func Decimal(value float64,f string) float64 {
45+
value, _ = strconv.ParseFloat(fmt.Sprintf(f, value), 64)
46+
return value
47+
}

elo/elo_test.go renamed to game/elo/elo_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ package elo
33
import "testing"
44

55
func Test_EloRating(t *testing.T) {
6-
EloRating(Elo{
6+
a,b:= EloRating(Elo{
77
A: 1500,
88
B: 1600,
99
Sa: 1,
1010
})
11+
t.Log("a",a)
12+
t.Log("b",b)
1113
}
1214

1315
func Test_Decimal(t *testing.T) {

0 commit comments

Comments
 (0)