Καλώς ορίσατε στο dotNETZone.gr - Σύνδεση | Εγγραφή | Βοήθεια

Παρουσίαση με Ετικέτες

Όλες οι Ετικέτε... » scala   (RSS)

Link to pastebin

object TicTacToeTomek {
    def solve(arr: List[List[Char]]) : String = {
        def checkWin(a: List[Char]) : String = {
            val o = a.filter(c=>c=='O').length
            val x = a.filter(c=>c=='X').length
            val t = a.filter(c=>c=='T').length
            if(o+t==4) return "O won"
            else if(x+t==4) return "X won"
            else return null
        }
        for(i<-0.to(3)) {
            val h = arr(i)
            val hres = checkWin(h)
            if(hres!=null) return hres
            val v = List(arr(0)(i),arr(1)(i),arr(2)(i),arr(3)(i))
            val vres = checkWin(v)
            if(vres!=null) return vres
        }
        val d1 =  List(arr(0)(0),arr(1)(1),arr(2)(2),arr(3)(3))
        val d1res = checkWin(d1)
        if(d1res!=null) return d1res
        val d2 =   List    (arr(0)(3),arr(1)(2),arr(2)(1),arr(3)(0))
        val d2res = checkWin(d2)
        if(d2res!=null) return d2res
        arr.foreach(a=>a.foreach(c=>if(c=='.') return "Game has not completed"))
        return "Draw"
    }
    
    def main(args: Array[String]):Unit = {
        val T = readLine().toInt
        for(i<-1.to(T)) {
            val arr = List[List[Char]](readLine().toList,readLine().toList,readLine().toList,readLine().toList)
            val empt = readLine()
            println("Case #"+i+": " + solve(arr))
        }
    }
}

0 σχόλια
Δημοσίευση στην κατηγορία: , , , , , , ,

Below is my implementation of a Dynamic Service Locator

ServiceLocator.scala

import scala.reflect.runtime.{universe => ru}
trait ServiceLocator {
  def getService[T](implicit t: ru.TypeTag[T]) : T
  def registerService[T](obj: T)(implicit t: ru.TypeTag[T]) : Unit
}

ServiceLocatorImpl.scala

import scala.reflect.runtime.{ universe => ru }
import scala.reflect.runtime.universe.Type

class ServiceLocatorImpl extends ServiceLocator {
  val rep = new scala.collection.mutable.HashMap[reflect.runtime.universe.Type, Any]()

  private def internalServiceSearch(t: Type): Option[Any] = {
    val tpe = t
    for {
      s <- rep.keys
      if (s <:< tpe || s =:= tpe)
    } return Some(rep(s))
    None
  }
  def getService[T](implicit t: ru.TypeTag[T]): T = {
    val res = internalServiceSearch(t.tpe)
    res match{
      case None => throw new Exception("Service not found")
      case _ => res.get.asInstanceOf[T]
    }
  }
  def registerService[T](obj: T)(implicit t: ru.TypeTag[T]): Unit = {
    if (internalServiceSearch((t).tpe) == None) rep.put(t.tpe, obj.asInstanceOf[Any])
  }
}

0 σχόλια
Δημοσίευση στην κατηγορία: ,