Browse files

Test and implement elo diff calculation

  • Loading branch information...
1 parent 9251c46 commit afff9bc0e34ecef190f1292a2333147755a0cced @ornicar committed May 21, 2012
Showing with 36 additions and 3 deletions.
  1. +2 −1 README.md
  2. +1 −1 project/Build.scala
  3. +1 −1 src/main/scala/Clock.scala
  4. +6 −0 src/main/scala/EloCalculator.scala
  5. +26 −0 src/test/scala/EloTest.scala
View
3 README.md
@@ -1,4 +1,5 @@
-Chess API written in scala with minimal dependencies
+Chess API written in scala.
+It is functional, immutable, and free of side effects.
INSTALL
-------
View
2 project/Build.scala
@@ -16,7 +16,7 @@ object ApplicationBuild extends Build with Resolvers with Dependencies {
private val buildSettings = Project.defaultSettings ++ Seq(
organization := "com.github.ornicar",
- version := "1.5",
+ version := "1.6",
scalaVersion := "2.9.1",
resolvers := Seq(iliaz),
libraryDependencies := Seq(scalaz, scalalib, hasher),
View
2 src/main/scala/Clock.scala
@@ -11,7 +11,7 @@ sealed trait Clock {
val blackTime: Float
val timerOption: Option[Double]
- def time(c: Color) = if (c == White) whiteTime else blackTime
+ def time(c: Color) = c.fold(whiteTime, blackTime)
def outoftime(c: Color) = remainingTime(c) == 0
View
6 src/main/scala/EloCalculator.scala
@@ -28,6 +28,12 @@ final class EloCalculator {
calculateUserElo(user2, user1.elo, winCode))
}
+ def diff(user1: User, user2: User, winner: Option[Color]): Int =
+ (user1 == user2).fold(
+ 0,
+ calculate(user1, user2, winner)._1 - user1.elo
+ )
+
private def calculateUserElo(user: User, opponentElo: Int, win: Int) = {
val score = (1 + win) / 2f
val expected = 1 / (1 + math.pow(10, (opponentElo - user.elo) / 400f))
View
26 src/test/scala/EloTest.scala
@@ -68,4 +68,30 @@ class EloTest extends ChessTest {
(nu1, nu2) must_== (1322, 1149)
}
}
+ "calculate diff" should {
+ "same level" in {
+ val (u1, u2) = (user(1200, 100), user(1200, 100))
+ "win" in {
+ diff(u1, u2, Some(White)) must_== 16
+ }
+ "loss" in {
+ diff(u1, u2, Some(Black)) must_== -16
+ }
+ "draw" in {
+ diff(u1, u2, None) must_== 0
+ }
+ }
+ "against higher rated player" in {
+ val (u1, u2) = (user(1200, 100), user(1500, 100))
+ "win" in {
+ diff(u1, u2, Some(White)) must_== 27
+ }
+ "loss" in {
+ diff(u1, u2, Some(Black)) must_== -5
+ }
+ "draw" in {
+ diff(u1, u2, None) must_== 11
+ }
+ }
+ }
}

0 comments on commit afff9bc

Please sign in to comment.