Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
35
Task/Playing-cards/Scala/playing-cards-1.scala
Normal file
35
Task/Playing-cards/Scala/playing-cards-1.scala
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
import scala.annotation.tailrec
|
||||
import scala.util.Random
|
||||
|
||||
object Pip extends Enumeration {
|
||||
type Pip = Value
|
||||
val Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King, Ace = Value
|
||||
}
|
||||
|
||||
object Suit extends Enumeration {
|
||||
type Suit = Value
|
||||
val Diamonds, Spades, Hearts, Clubs = Value
|
||||
}
|
||||
|
||||
import Suit._
|
||||
import Pip._
|
||||
case class Card(suit:Suit, value:Pip){
|
||||
override def toString():String=value + " of " + suit
|
||||
}
|
||||
|
||||
class Deck private(val cards:List[Card]) {
|
||||
def this()=this(for {s <- Suit.values.toList; v <- Pip.values} yield Card(s,v))
|
||||
def shuffle:Deck=new Deck(Random.shuffle(cards))
|
||||
|
||||
def deal(n:Int=1):(Seq[Card], Deck)={
|
||||
@tailrec def loop(count:Int, c:Seq[Card], d:Deck):(Seq[Card], Deck)={
|
||||
if(count==0 || d.cards==Nil) (c,d)
|
||||
else {
|
||||
val card :: deck = d.cards
|
||||
loop(count-1, c :+ card, new Deck(deck))
|
||||
}
|
||||
}
|
||||
loop(n, Seq(), this)
|
||||
}
|
||||
override def toString():String="Deck: " + (cards mkString ", ")
|
||||
}
|
||||
15
Task/Playing-cards/Scala/playing-cards-2.scala
Normal file
15
Task/Playing-cards/Scala/playing-cards-2.scala
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
val deck=new Deck()
|
||||
val deckShuffled:Deck=deck.shuffle
|
||||
|
||||
println(deck)
|
||||
println(deckShuffled)
|
||||
|
||||
val (a, rest) = deckShuffled.deal()
|
||||
val (b, rest2) = rest.deal()
|
||||
|
||||
println(a head)
|
||||
println(b head)
|
||||
|
||||
val (cards, rest3) =deckShuffled deal 6
|
||||
println(cards)
|
||||
println(rest3)
|
||||
Loading…
Add table
Add a link
Reference in a new issue