Skip to content

Instantly share code, notes, and snippets.

@intinig
Created June 18, 2013 13:59
Show Gist options
  • Select an option

  • Save intinig/5805584 to your computer and use it in GitHub Desktop.

Select an option

Save intinig/5805584 to your computer and use it in GitHub Desktop.
def getCol(source: scala.io.BufferedSource, col: Int): String = {
return source.reset.getLines.map { line => line.charAt(col)}.mkString
}
def checkWinner(line: String) {
if (line == "xxx") {
println("x wins")
} else if (line == "ooo") {
println("o wins")
}
}
def getDiagonal(source: scala.io.BufferedSource, back: Int): String = {
var diagonal = ""
var i = 0
if (back > 0) {
i = 0
} else {
i = 2
}
source.reset.getLines.foreach { line =>
diagonal = diagonal + line.charAt(i)
i = i + back
}
return diagonal
}
val lines = io.Source.fromFile(args(0))
println("Rows: ")
lines.getLines.foreach { line =>
checkWinner(line)
}
val col1 = getCol(lines, 0)
val col2 = getCol(lines, 1)
val col3 = getCol(lines, 2)
println("Cols: ")
checkWinner(col1)
checkWinner(col2)
checkWinner(col3)
println("Diags: ")
checkWinner(getDiagonal(lines, 1))
checkWinner(getDiagonal(lines, -1))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment