Bridge Hands
August 11, 2015
[ Thanks to all who wrote with good wishes after my post last Friday. I am fully recovered and back at work. ]
Newspapers and textbooks often print bridge hands in the format shown below, then discuss the proper playing of the hand:
NORTH S: A Q J 10 8 H: 5 4 2 D: 9 C: 10 7 6 2 WEST EAST S: 7 S: 6 H: Q J 7 6 3 H: K 10 8 D: J 10 6 4 3 D: A K Q 5 C: A 8 C: K 9 5 4 3 SOUTH S: K 9 5 4 3 2 H: A 9 D: 8 7 2 C: Q J
Your task is to write a program to generate random bridge hands and print them in the format shown above. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.
In Python.
Good to see you recovered!
Hereby my Python solution..
Scala:
import scala.util.Random
type Card = Tuple2[String, String]
val color: Seq[String] = List(“S”, “H”, “D”, “C”)
val numbers: Seq[String] = (2 to 10 map { _.toString }) ++ List(“J”, “Q”, “K”, “A”)
def numberSorter(a: String, b: String): Boolean = numbers.indexOf(a) > numbers.indexOf(b)
val cards: Seq[Card] = Random.shuffle(for (c <- color; n List(filter(x, “S”), filter(x, “H”), filter(x, “D”), filter(x, “C”)))
val blockLines = blocks.map(x => (List(x._1) ++ x._2).map(x => x + ” ” * (20 – x.length())))
val north = for (i <- 0 to 4) yield " " * 20 + blockLines(0)(i) + " " * 20
val west_east = for (i <- 0 to 4) yield blockLines(1)(i) + " " * 20 + blockLines(2)(i)
val south = for (i <- 0 to 4) yield " " * 20 + blockLines(3)(i) + " " * 20
println((north ++ west_east ++ south).mkString("\r\n"))
@Paul, @Rutger: could you please tell me how do you insert your code in such a beauty way? Which website do you use?
@Andras.I am using method Third as mentioned in the “Howto: Posting Source Code”. You have to know the mnemonic for Scala (I guess it is scala). Further the tag is usefull if you want to use a link in your post. I hope this helps.
@Andras, the “a href” tag messed up part of my reply. You only have to google for this tag and see how it is used.
@Paul Thanks it works fine!
A Haskell version.
[…] Bridge hands problem in C++ […]
[…] bridge hands problem on programming praxis in Common […]