Weekly Challenge #7 – Snooker Loopy

A perfect challenge for any snooker fan. Or Chas & Dave fan…

There’s plenty of ways to go about solving these challenges; however, some are definitely more efficient than others!

Background Information

In a game of snooker, there are 21 balls: 15 Red, 1 Yellow, 1 Green, 1 Brown, 1 Blue, 1 Pink and 1 Black.

Points are awarded for potting balls as follows:

  • None – 0
  • Red – 1
  • Yellow – 2
  • Green – 3
  • Brown – 4
  • Blue – 5
  • Pink – 6
  • Black – 7

Bronze

Write a program which keeps track of how many points a player has scored in a game of snooker. The program repeatedly ask the user which colour ball they have potted, and the corresponding number of points should then be added to their total. Output their total after each shot.

Silver

Extend your program so that it now supports two players. Each player should take turns to have a shot; however, if a player pots a ball, they will be allowed another shot.

Gold

In snooker, a player may only pot one of the coloured balls (non-red) if they have just potted a red ball immediately beforehand, or if all of the 15 red balls have been potted. Extend your program so that points are only awarded for potting non-red balls if these criteria are met.

Red → Yellow → Red → Brown → None     would be valid (1 + 2 + 1 + 4 = 8 points)

Red → Red     would be invalid. Two reds cannot be potted in a row.

Yellow → Red → Brown → None    would be invalid. You cannot pot a colour without first potting a red, unless there are no reds left.

Platinum

Once all of the reds have been potted, each of the coloured balls are to be potted for one final time. This must occur in ascending order of value: Yellow → Green → Brown → Blue → Pink → Black.

Extend your program further to include this rule.