Advertisement
Guest User

Untitled

a guest
Jan 3rd, 2015
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 4.01 KB | None | 0 0
  1. import scala.collection.immutable.TreeMap
  2. import scala.util.Random
  3. import scala.util.control.Breaks._
  4. import scala.sys.process._
  5.  
  6. object Appl extends App {
  7.   val players = Seq.fill(2)(new Player)
  8.   val numberOfRolls = Variations.values.size //standard = 26
  9.   for (i <- 0 until numberOfRolls * 2) {
  10.     var player = players(i % 2)
  11.     "clear".!
  12.     println("Player " + (i % 2 + 1) + " plays:\n")
  13.     player.printResults()
  14.     val roll = Roll.roll()
  15.     print("\n Your roll: " + roll.sorted.mkString(", ") + "  , choose variation: ")
  16.     var retry = false //when given variation differs from 1-13 or has already been entered
  17.     do {
  18.       retry = false
  19.       try {
  20.         val variation = Variations.values.toSeq(Console.readInt - 1) //can throw exception (normally I would do it with an "if")
  21.         val sum = Game.variationResult(roll, variation) //try to add result
  22.         if (player.addResult(variation, sum) == false) { print("\nYou already entered this variation, try another one: "); retry = true }
  23.       } catch { case ex: java.lang.IndexOutOfBoundsException => { print("\nWrong variation (1-13), try another one: "); retry = true } }
  24.     } while (retry)
  25.   }
  26.   //end of game - summary
  27.   val sum1 = players(0).results.map(a => a._2.toInt).sum
  28.   val sum2 = players(1).results.map(a => a._2.toInt).sum
  29.   if (sum1 > sum2) println("Player 1 wins " + sum1 + ":" + sum2) else println("Player 2 wins " + sum2 + ":" + sum1)
  30. }
  31.  
  32. object Roll {
  33.   def roll(): Seq[Int] = {
  34.     Seq.fill(5)(Random.nextInt(6) + 1)
  35.   }
  36. }
  37.  
  38. object Variations extends Enumeration {
  39.   type Variations = Value
  40.   val Ones, Twos, Threes, Fours, Fiths, Sixths, ThreeOfaKind, FourOfaKind, FullHouse, SmallStraight, LargeStraight, Yahtzee, Chance = Value
  41. }
  42.  
  43. class Player {
  44.   import Variations._
  45.   var results = new TreeMap[Variations, Integer]
  46.   for (variation <- Variations.values) { results.+=((variation, null)) }
  47.  
  48.   def printResults() = {
  49.     val it = results.iterator
  50.     for (i <- 1 to Variations.values.size) println(i + ". " + it.next())
  51.   }
  52.   def addResult(variation: Variations.Value, sum: Int): Boolean = { //return false, if entry already exists
  53.     val isNull = (results.get(variation).get == null)
  54.     if (isNull) results.+=((variation, sum))
  55.     isNull
  56.   }
  57. }
  58.  
  59. object PatternMatch {
  60.   def repetitionAmount(roll:Seq[Int], number:Int): Int = { //returns how many times 'number' occurs in the roll
  61.     roll.map(a => if(a==number) 1 else 0).sum
  62.   }
  63.   def continuousAmount(roll:Seq[Int]): Int = { //returns 3, if small straight, returns 4 if large straight
  64.     var sorted = roll.sortWith(_.compareTo(_)>0)
  65.     (0 to 3).map(i => sorted(i)-sorted(i+1)).count(a => a==1)
  66.   }
  67. }
  68.  
  69. object Game {
  70.   def variationResult(roll: Seq[Int], variation: Variations.Value): Int = {
  71.     variation match {
  72.       case Variations.Ones          => roll.map(a => if (a == 1) a else 0).sum
  73.       case Variations.Twos          => roll.map(a => if (a == 2) a else 0).sum
  74.       case Variations.Threes        => roll.map(a => if (a == 3) a else 0).sum
  75.       case Variations.Fours         => roll.map(a => if (a == 4) a else 0).sum
  76.       case Variations.Fiths         => roll.map(a => if (a == 5) a else 0).sum
  77.       case Variations.Sixths        => roll.map(a => if (a == 6) a else 0).sum
  78.       case Variations.ThreeOfaKind  => (1 to 6).map(i => if(PatternMatch.repetitionAmount(roll, i)>=3) roll.sum else 0).sum
  79.       case Variations.FourOfaKind   => (1 to 6).map(i => if(PatternMatch.repetitionAmount(roll, i)>=4) roll.sum else 0).sum
  80.       case Variations.FullHouse     => (1 to 6).map(i => PatternMatch.repetitionAmount(roll, i)).filter(a => a!=0).reduce((x,y)=> if((y-x)==1 || (x-y)==1) 25 else 0).toInt
  81.       case Variations.SmallStraight => if(PatternMatch.continuousAmount(roll)>=3) 30 else 0
  82.       case Variations.LargeStraight => if(PatternMatch.continuousAmount(roll)==4) 40 else 0
  83.       case Variations.Yahtzee       => (1 to 6).map(i => if(PatternMatch.repetitionAmount(roll, i)==5) 50 else 0).sum
  84.       case Variations.Chance        => roll.sum
  85.     }
  86.   }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement