Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2018
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 1.23 KB | None | 0 0
  1. import akka.actor.Actor
  2. import akka.actor.ActorSystem
  3. import akka.actor.Props
  4. import akka.actor.ActorRef
  5.  
  6. case class StartGame(duration: Int, msg:String, other: ActorRef)
  7. case class HitBall(duriation: Int, msg: String)
  8.  
  9. class Player extends Actor {
  10.   def receive = {
  11.     case StartGame(duration, msg, other) =>
  12.       print(self.path.name + ": ")
  13.       println(msg)
  14.       other ! HitBall(duration*2-1, msg)
  15.     case HitBall(duration, msg) =>
  16.       if(duration > 0){
  17.         //println(msg)
  18.         msg match {
  19.           case "ping" =>
  20.             print(self.path.name + ": ")
  21.             println("pong")
  22.             sender ! HitBall(duration-1, "pong")
  23.           case "pong" =>
  24.             print(self.path.name + ": ")
  25.             println("ping")
  26.             sender ! HitBall(duration-1, "ping")
  27.         }
  28.       }
  29.       else {
  30.         println("Game Over")
  31.         context.system.shutdown()
  32.       }
  33.          
  34.   }
  35. }
  36.  
  37. object Main extends App {
  38.    
  39.     val system = ActorSystem("MySystem")
  40.     val player1 = system.actorOf(Props[Player],"Kazimierz")
  41.     val player2 = system.actorOf(Props[Player],"Janusz")
  42.     val duration = 5;
  43.     val startMessage = "ping"
  44.     player1 ! StartGame(duration, startMessage, player2)
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement