Skip to content

Commit c06c6bc

Browse files
committed
Scala bot
1 parent 025785d commit c06c6bc

11 files changed

+63
-42
lines changed

Diff for: build.gradle

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,17 @@ buildscript {
66
jcenter()
77
}
88
}
9-
apply plugin: 'java'
9+
apply plugin: 'scala'
1010

1111
repositories {
1212
jcenter()
1313
}
1414

1515
dependencies {
16+
compile 'org.scala-lang:scala-library:2.11.7'
1617
compile 'com.squareup.retrofit:retrofit:2.0.0-beta2'
1718
compile 'com.squareup.retrofit:adapter-rxjava:2.0.0-beta2'
1819
compile 'com.squareup.retrofit:converter-gson:2.0.0-beta2'
1920
testCompile group: 'junit', name: 'junit', version: '4.11'
21+
testCompile 'org.scalatest:scalatest_2.11:2.2.6'
2022
}

Diff for: src/main/java/JavaBot.java

-16
This file was deleted.

Diff for: src/main/scala/ScalaBot.scala

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import common.{AtomasBotRunner, Game, MoveCalculator}
2+
3+
object ScalaBot extends MoveCalculator {
4+
5+
//TODO: Add your logic here
6+
override def calculateMove(game: Game): Int = 0
7+
8+
def main(args: Array[String]): Unit = {
9+
new AtomasBotRunner(ScalaBot).run(/*Put optional nick here*/)
10+
}
11+
}

Diff for: src/main/java/common/AtomasBotRunner.java renamed to src/main/scala/common/AtomasBotRunner.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package common;
22

3-
import java.util.ArrayList;
3+
import scala.collection.immutable.List;
44

55
public class AtomasBotRunner {
66

77
private final MoveCalculator moveCalculator;
88
private final AtomasBotsApi atomasBotsApi;
9-
private Game bestGame = new Game("id", new ArrayList<Integer>(), -1000, 0, 0);
9+
private GameJava bestGame = new GameJava("id", null, -1000, 0, 0);
1010

1111
public AtomasBotRunner(MoveCalculator moveCalculator) {
1212
this(moveCalculator, true);
@@ -23,19 +23,19 @@ public void run() throws Exception {
2323

2424
public void run(String name) throws Exception {
2525
while (true) {
26-
Game newGame = atomasBotsApi.newGame(name).execute().body();
26+
GameJava newGame = atomasBotsApi.newGame(name).execute().body();
2727
while (!newGame.isEndOfGame()) {
2828
newGame = doMove(newGame);
2929
}
3030
logStatus(newGame);
3131
}
3232
}
3333

34-
private Game doMove(Game game) throws Exception {
35-
return atomasBotsApi.move(game.id, moveCalculator.calculateMove(game)).execute().body();
34+
private GameJava doMove(GameJava game) throws Exception {
35+
return atomasBotsApi.move(game.id, moveCalculator.calculateMove(GameConverter.toGame(game))).execute().body();
3636
}
3737

38-
private void logStatus(Game game) {
38+
private void logStatus(GameJava game) {
3939
if (game.score >= bestGame.score) {
4040
bestGame = game;
4141
}

Diff for: src/main/java/common/AtomasBotsApi.java renamed to src/main/scala/common/AtomasBotsApi.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414
interface AtomasBotsApi {
1515

1616
@GET("/move/{gameId}/{target}")
17-
Call<Game> move(@Path("gameId") String gameId, @Path("target") int target);
17+
Call<GameJava> move(@Path("gameId") String gameId, @Path("target") int target);
1818

1919
@GET("/new_game")
20-
Call<Game> newGame(@Query("name") String name);
20+
Call<GameJava> newGame(@Query("name") String name);
2121

2222
class Builder {
2323
static AtomasBotsApi createApi(boolean remote) {

Diff for: src/main/scala/common/Game.scala

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package common
2+
3+
case class Game(id: String,
4+
board: List[Int],
5+
next: Int,
6+
round: Int,
7+
score: Int) {
8+
9+
def isEndOfGame: Boolean = {
10+
next == -1000
11+
}
12+
13+
def getId = id
14+
15+
def getScore = score
16+
}

Diff for: src/main/scala/common/GameConverter.scala

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package common
2+
3+
import scala.collection.JavaConversions
4+
5+
object GameConverter {
6+
7+
def toGame(gameJava: GameJava): Game = {
8+
val scalaIterator: List[Int] = JavaConversions.asScalaIterator(gameJava.board.iterator()).map(_.intValue()).toList
9+
new Game(gameJava.id, scalaIterator, gameJava.next, gameJava.round, gameJava.score)
10+
}
11+
}

Diff for: src/main/java/common/Game.java renamed to src/main/scala/common/GameJava.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,22 @@
22

33
import java.util.List;
44

5-
public class Game {
5+
public class GameJava {
66
public final String id;
77
public final List<Integer> board;
88
public final int next;
99
public final int round;
1010
public final int score;
1111

12-
public Game(String id, List<Integer> board, int next, int round, int score) {
12+
public GameJava(String id, List<Integer> board, int next, int round, int score) {
1313
this.id = id;
1414
this.board = board;
1515
this.next = next;
1616
this.round = round;
1717
this.score = score;
1818
}
1919

20-
boolean isEndOfGame() {
20+
public boolean isEndOfGame() {
2121
return next == -1000;
2222
}
2323
}
File renamed without changes.

Diff for: src/test/java/JavaBotTest.java

-14
This file was deleted.

Diff for: src/test/scala/ScalaBotTest.scala

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import common.Game
2+
import org.scalatest.{FlatSpec, Matchers}
3+
4+
class ScalaBotTest extends FlatSpec with Matchers {
5+
6+
"Calculate move" should "return value in proper range " in {
7+
val game: Game = new Game("", List(1, 2, 3, 4, 5), 0, 0, 0)
8+
ScalaBot.calculateMove(game) should be >= 0
9+
ScalaBot.calculateMove(game) should be <= 5
10+
}
11+
}

0 commit comments

Comments
 (0)