Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import scala.collection.immutable.TreeMap
- import scala.util.Random
- import scala.util.control.Breaks._
- import scala.sys.process._
- object Appl extends App {
- val players = Seq.fill(2)(new Player)
- val numberOfRolls = Variations.values.size //standard = 26
- for (i <- 0 until numberOfRolls * 2) {
- var player = players(i % 2)
- "clear".!
- println("Player " + (i % 2 + 1) + " plays:\n")
- player.printResults()
- val roll = Roll.roll()
- print("\n Your roll: " + roll.sorted.mkString(", ") + " , choose variation: ")
- var retry = false //when given variation differs from 1-13 or has already been entered
- do {
- retry = false
- try {
- val variation = Variations.values.toSeq(Console.readInt - 1) //can throw exception (normally I would do it with an "if")
- val sum = Game.variationResult(roll, variation) //try to add result
- if (player.addResult(variation, sum) == false) { print("\nYou already entered this variation, try another one: "); retry = true }
- } catch { case ex: java.lang.IndexOutOfBoundsException => { print("\nWrong variation (1-13), try another one: "); retry = true } }
- } while (retry)
- }
- //end of game - summary
- val sum1 = players(0).results.map(a => a._2.toInt).sum
- val sum2 = players(1).results.map(a => a._2.toInt).sum
- if (sum1 > sum2) println("Player 1 wins " + sum1 + ":" + sum2) else println("Player 2 wins " + sum2 + ":" + sum1)
- }
- object Roll {
- def roll(): Seq[Int] = {
- Seq.fill(5)(Random.nextInt(6) + 1)
- }
- }
- object Variations extends Enumeration {
- type Variations = Value
- val Ones, Twos, Threes, Fours, Fiths, Sixths, ThreeOfaKind, FourOfaKind, FullHouse, SmallStraight, LargeStraight, Yahtzee, Chance = Value
- }
- class Player {
- import Variations._
- var results = new TreeMap[Variations, Integer]
- for (variation <- Variations.values) { results.+=((variation, null)) }
- def printResults() = {
- val it = results.iterator
- for (i <- 1 to Variations.values.size) println(i + ". " + it.next())
- }
- def addResult(variation: Variations.Value, sum: Int): Boolean = { //return false, if entry already exists
- val isNull = (results.get(variation).get == null)
- if (isNull) results.+=((variation, sum))
- isNull
- }
- }
- object PatternMatch {
- def repetitionAmount(roll:Seq[Int], number:Int): Int = { //returns how many times 'number' occurs in the roll
- roll.map(a => if(a==number) 1 else 0).sum
- }
- def continuousAmount(roll:Seq[Int]): Int = { //returns 3, if small straight, returns 4 if large straight
- var sorted = roll.sortWith(_.compareTo(_)>0)
- (0 to 3).map(i => sorted(i)-sorted(i+1)).count(a => a==1)
- }
- }
- object Game {
- def variationResult(roll: Seq[Int], variation: Variations.Value): Int = {
- variation match {
- case Variations.Ones => roll.map(a => if (a == 1) a else 0).sum
- case Variations.Twos => roll.map(a => if (a == 2) a else 0).sum
- case Variations.Threes => roll.map(a => if (a == 3) a else 0).sum
- case Variations.Fours => roll.map(a => if (a == 4) a else 0).sum
- case Variations.Fiths => roll.map(a => if (a == 5) a else 0).sum
- case Variations.Sixths => roll.map(a => if (a == 6) a else 0).sum
- case Variations.ThreeOfaKind => (1 to 6).map(i => if(PatternMatch.repetitionAmount(roll, i)>=3) roll.sum else 0).sum
- case Variations.FourOfaKind => (1 to 6).map(i => if(PatternMatch.repetitionAmount(roll, i)>=4) roll.sum else 0).sum
- 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
- case Variations.SmallStraight => if(PatternMatch.continuousAmount(roll)>=3) 30 else 0
- case Variations.LargeStraight => if(PatternMatch.continuousAmount(roll)==4) 40 else 0
- case Variations.Yahtzee => (1 to 6).map(i => if(PatternMatch.repetitionAmount(roll, i)==5) 50 else 0).sum
- case Variations.Chance => roll.sum
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement