Skip to content

Instantly share code, notes, and snippets.

@adamretter
Last active December 31, 2015 08:18
Show Gist options
  • Save adamretter/7959226 to your computer and use it in GitHub Desktop.
Save adamretter/7959226 to your computer and use it in GitHub Desktop.
package wlhn
import java.net.ServerSocket
import scala.io.BufferedSource
import java.io.PrintStream
import java.net.Socket
import java.net.InetAddress
import scala.collection.mutable.ArrayBuffer
/**
* An automated maze solver
*
* Created in 1 hour 30 minutes at the
* West London Hack night (2013-12-10)
*
* Connects to a Server provided by Kris Jenkins
* and attempts to steer the reindeer through the maze
* to the present.
*
* Team Scala was 3 hackers (Mat, Mark and Adam)
*
* Disclaimer: The code below is not idiomatic Scala
* and you *should* never write code like this,
* however at the end of a long day
* when you just gotta get it done, you gotta get it done.
*
* And, that said... Team Scala won :-)
*/
object Main extends App {
val s = new Socket(InetAddress.getByName("10.112.148.185"), 8080)
lazy val in = new BufferedSource(s.getInputStream()).getLines()
val out = new PrintStream(s.getOutputStream())
var x = 0;
var y = 0;
val moves : ArrayBuffer[(String, Int, Int)] = ArrayBuffer(("X", 0, 0))
play()
def play() = {
//game loop
while(in.hasNext) {
val current = in.next
println(current)
if(current == "Language/team name?") {
//team login
out.println("Scala Team")
} else if(current == "WIN" || current == "LOOSE") {
//round over
} else {
def isOnlyOption(us: Int)(otherA:Int, otherB:Int, otherC:Int) = {
canTravel(us) && (otherA == otherB == otherC == 0)
}
def isOnlyOptionExcludingLast(us: Int)(otherA:Int, otherB:Int, otherC:Int) = {
canTravel(us) && (otherA == 0 || otherA == moves.last._1) && (otherB == 0 || otherB == moves.last._1) && (otherC == 0 || otherC == moves.last._1)
}
def howManyTimesHaveWeBeenHereBefore(x: Int,y :Int) : Int = {
moves.foldLeft(0)((count, tuple) => if (x == tuple._2 && y == tuple._3) count + 1 else count)
}
def toCoordinate(moveTo: String) : (Int, Int) = {
if(moveTo == "E") (x + 1, y)
else if(moveTo == "W") (x - 1, y)
else if(moveTo == "N") (x, y + 1)
else if(moveTo == "S") (x, y - 1)
else (x, y)
}
def canTravel(x: Int) = x > 0
def getNextMove2(n:Int, e:Int, s:Int, w:Int, p:String) : String = {
if (p != "?") p
else if (isOnlyOptionExcludingLast(e)(n, w, s)) "E"
else if (isOnlyOptionExcludingLast(n)(e, w, s)) "N"
else if (isOnlyOptionExcludingLast(w)(n, e, s)) "W"
else if (isOnlyOptionExcludingLast(s)(n, w, e)) "S"
else {
val allMoves = List(("E", e), ("N", n), ("W", w), ("S", s)
val allPossible = allMoves.filter(tuple => canTravel(tuple._2))
val reasonable = allPossible
def sortFunc(tuple: (String, Int)) = {
val (x, y) = toCoordinate(tuple._1)
howManyTimesHaveWeBeenHereBefore(x, y)
}
val movesByVisited = reasonable.sortBy(sortFunc)
println(movesByVisited)
movesByVisited.head._1
}
}
def getNextMove(n:Int, e: Int, s: Int, w: Int, p: String) : String = {
if (p != "?") p
else if (isOnlyOption(e)(n, w, s) || (canTravel(e) && moves.last._1 != "W")) "E"
else if (isOnlyOption(n)(e, w, s) || (canTravel(n) && moves.last._1 != "S")) "N"
else if (isOnlyOption(w)(n, e, s) || (canTravel(w) && moves.last._1 != "E")) "W"
else if (isOnlyOption(s)(n, w, e) || (canTravel(s) && moves.last._1 != "N")) "S"
else "" //this never happens - yuck! fix later!
}
val Pattern = """N([0-9]+) E([0-9]+) S([0-9]+) W([0-9]+) P([NESWX\?])""".r
current match {
case Pattern(n, e, s, w, p) =>
println(s"n=$n,e=$e,s=$s,w=$w,p=$p")
if(p == "X") {
println("WE WIN!!!!")
} else {
val moveTo = getNextMove2(n.toInt, e.toInt, s.toInt, w.toInt, p)
val toCoord = toCoordinate(moveTo)
x = toCoord._1
y = toCoord._2
moves += ((moveTo, x, y))
println(s"$moveTo($x,$y)")
out.println(moveTo)
}
case x => {
println(x)
}
}
}
}
}
out.close();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment