ikar

Google Codejam 2013 - Qualification Round - Problem 1

Apr 14th, 2013
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 1.25 KB | None | 0 0
  1. // Iraklis Karagkiozoglou <i.kar@windowslive.com>
  2. // Solution to Google CodeJam 2013 Qualification Round - Problem 1
  3. object TicTacToeTomek {
  4.     def solve(arr: List[List[Char]]) : String = {
  5.         def checkWin(a: List[Char]) : String = {
  6.             val o = a.filter(c=>c=='O').length
  7.             val x = a.filter(c=>c=='X').length
  8.             val t = a.filter(c=>c=='T').length
  9.             if(o+t==4) return "O won"
  10.             else if(x+t==4) return "X won"
  11.             else return null
  12.         }
  13.         for(i<-0.to(3)) {
  14.             val h = arr(i)
  15.             val hres = checkWin(h)
  16.             if(hres!=null) return hres
  17.             val v = List(arr(0)(i),arr(1)(i),arr(2)(i),arr(3)(i))
  18.             val vres = checkWin(v)
  19.             if(vres!=null) return vres
  20.         }
  21.         val d1 =  List(arr(0)(0),arr(1)(1),arr(2)(2),arr(3)(3))
  22.         val d1res = checkWin(d1)
  23.         if(d1res!=null) return d1res
  24.         val d2 =   List (arr(0)(3),arr(1)(2),arr(2)(1),arr(3)(0))
  25.         val d2res = checkWin(d2)
  26.         if(d2res!=null) return d2res
  27.         arr.foreach(a=>a.foreach(c=>if(c=='.') return "Game has not completed"))
  28.         return "Draw"
  29.     }
  30.    
  31.     def main(args: Array[String]):Unit = {
  32.         val T = readLine().toInt
  33.         for(i<-1.to(T)) {
  34.             val arr = List[List[Char]](readLine().toList,readLine().toList,readLine().toList,readLine().toList)
  35.             val empt = readLine()
  36.             println("Case #"+i+": " + solve(arr))
  37.         }
  38.     }
  39. }
Add Comment
Please, Sign In to add comment