Good folk of Scala Users,
I have a puzzle, that I’m embarrassed to admit, is causing me some trouble to solve. I wonder whether anyone here would like to suggest a way to solve this? Here is the code…
` /*
* This is a sample solution to the Wish Solitaire game, as described below, and at:
* Wish Solitaire | Bicycle Playing Cards
*
* The Pack
* Remove all 2s through 6s, to form a deck of 32 cards
*
* The Deal
* Shuffle cards and deal 4 cards face down into a pile on the table. Deal the whole
* deck into piles of 4 cards, lining the piles up so that there are 8 total piles
* in a row from left to right.
*
* The Play
* Turn over the top cards of each pile so that they are face up. Take any cards
* that are pairs of the same kind, regardless of suit - two 10’s, two Kings, etc.
* and clear them away.
*
* Once you have removed a card from the top of the pile, turn over the next card
* on the pile so it is face up.
*
* To win the game, you must clear away all piles in pairs.
*
*/
package example
import scala.util.Random
case class Card(suit: String, rank: String) {}
object WishSolitaire {
val suits = List[String]("Hearts", "Clubs", "Diamonds", "Spades")
val ranks = List[String]("Ace", "7", "8", "9", "10", "Jack", "Queen", "King")
val aDeck = for {
s <- suits
r <- ranks
} yield Card(s, r)
def main(args: Array[String]) : Unit = {
val piles = Random.shuffle(aDeck).grouped(4).toList
val r = piles map { x => x map { _.rank } } // just to see result of shuffle....
r.foreach(println)
/* Recursive function to match pairs, remove the 'head' from the list containing matching ranks
* until game is finished, or no more progress can be made.
def play(deck: List[List[Card]], remainingHands: Int, nomatch: Boolean ) = {
if (0 < remainingHands && nomatch) println("Game has no solution")
else if (0 == remainingHands) println("Game Over!!")
else {
// compare ranks of all 8 hands for 2 equal ranks
// for all pairs of 2 matched rank, remove the head
// repeat
// if a 'hand' (list.length == 0) has been emptied, decrement remainingHands
// if no matches set cannot complete to true??
val cannotBeCompleted = true
play(deck, remainingHands, cannotBeCompleted)
}
}
*/
// play(piles, piles.length, false)
}
}