# Play a game of Dice Cricket

Dice Cricket is a game I was introduced to as a child and have used as a way to pass time for years since. I couldn't find a Wikipedia page so I'll explain the rules below.

# Dice Cricket Rules

## Premise

Dice Cricket is a game similar to scoring a game of cricket as you watch it but rather than watching and recording the result, you are rolling a die and recording the result.

The results are recorded in a table as displayed at the bottom.

## Display

Dice Cricket uses a specific display to show all the information happening.

The table has 11 rows. Each row represents a batter. The layout of the row is explained below.

+------+------------------------------+---------+-----+
| Name |    Runs                      | How Out |Score|
+------+------------------------------+---------+-----+

• Name: The name must be a string made up entirely of letters, upper or lower case
• Runs: A batter can face 30 balls. Each ball can be one of 1 2 4 6 . /. This will be explained in more detail below
• How Out: The way the batter was out. Can be any of Bowled, LBW (Leg Before Wicket), Caught, Retired or Not Out
• Score: The sum of all numbers in Runs

## How the Game Works

In a game, there are always 2 batters out on the pitch. The first player is by default the current batter and the second in the "off" batter.

A game is made up of "balls": each ball in a cricket match is represented by a dice roll. Each roll does a different command:

• 1,2,4 and 6 make the batter score that much. If 1 is rolled, the current batter becomes the "off" batter and the "off" batter becomes the current
• 3 is a "dot ball", meaning that nothing happens. It is represented in the Runs section as a . and scores 0. A 0 may not be used to represent it.
• 5 is a wicket. If 5 is rolled, the current batter is "out", This means that a / is added to the runs and from then on, the batter cannot score anymore runs. The batter is then swapped with the next batter who has not batted. The How Out section is a random choice of the possible ways to get out: Bowled, LBW, Caught

Example for a wicket (this is just for clarity, this isn't how its outputted)

player a is on 4,6,2,6,4
player b is on 6,4,2,6,6
player c hasn't batted
player a is current batter

WICKET!!!

player a is on 4,6,2,6,4,/
player b in on 6,4,2,6,6
player c is on NOTHING
player c is current batter


Every 6 balls, the two batters switch; the current batter becomes the "off" batter and the "off" batter becomes the current batter

If the Runs section is filled (30 balls), the batter is out and the How Out section is set to Retired. A / isn't placed at the end of the Runs box.

# Actual Challenge (yes all that was rules of the game)

Your challenge is to output a completed table (like the example at the end), given a list of names. The contents of the output should contain only the table and/or leading or trailing whitespace.

## Rules

• Standard loopholes are disallowed
• All 11 players should have something in the Runs section.
• Only 1 player can be Not Out. Every other non-retired player should be out of a choice of [Bowled, LBW, Caught]
• The names can be any length between 1 and 6 that matches the regex A-Za-z
• The final line in the table should be the total line (see example)
• You don't have to align the text in the table in any way, but the row and column separators must be aligned.

## Example

Input:
['Fred', 'Sonya', 'David', 'Ben', 'Cody', 'Hazel', 'Nina', 'Kim', 'Cath', 'Lena', 'Will']
Output:
+------+------------------------------+---------+-----+
| Name | Runs                         | How Out |Total|
+------+------------------------------+---------+-----+
|Fred  |.662/                         | Caught  | 14  |
+------+------------------------------+---------+-----+
|Sonya |1164/                         | Caught  | 12  |
+------+------------------------------+---------+-----+
|David |/                             |   LBW   |  0  |
+------+------------------------------+---------+-----+
|Ben   |424/                          |   LBW   | 10  |
+------+------------------------------+---------+-----+
|Cody  |62/                           | Bowled  |  8  |
+------+------------------------------+---------+-----+
|Hazel |/                             |   LBW   |  0  |
+------+------------------------------+---------+-----+
|Nina  |161.6226166..44261442/        | Caught  | 64  |
+------+------------------------------+---------+-----+
|Kim   |11/                           | Caught  |  2  |
+------+------------------------------+---------+-----+
|Cath  |6.21/                         |   LBW   |  9  |
+------+------------------------------+---------+-----+
|Lena  |/                             | Bowled  |  0  |
+------+------------------------------+---------+-----+
|Will  |2                             | Not Out |  2  |
+------+------------------------------+---------+-----+
|               Total Runs                      | 121 |
+-----------------------------------------------+-----+

• Sandbox – caird coinheringaahing Dec 14 '17 at 18:00
• Heh, I used to play a similar variant of this called "Hand cricket". – totallyhuman Dec 14 '17 at 18:43
• What determines when the game ends? Is it when a batter is out and there is no one left to replace him? – KSmarts Dec 14 '17 at 19:40
• @KSmarts Correct. – caird coinheringaahing Dec 14 '17 at 19:55
• @JonathanAllan Yeah, it isn't a good requirement. Removed – caird coinheringaahing Dec 14 '17 at 20:05

# Python 3, 650621582572 588 bytes

from random import*
h=str
c=h.center
a='+'.join(map('-'.__mul__,[0,6,30,9,5,0]))+'\n'
b=lambda x,r=6:x.ljust(r,' ')
j=''.join
t=lambda a:sum(map(int,a[:-1].replace(*'.0')))
P=print
def s(i=30):
while i:x=choice('12.4/6');yield x;i=('/'!=x)*~-i
def f(n,T=0):
n=[*map(b,n)]
P(a+f'| Name | Runs{" "*25}| How Out |Total|')
for x in n[:-1]:S=j(s());T+=t(S);P(a,x,b(S,30),c(choice(['Bowled','LBW','Caught']),9),c(h(t(S)),5),sep='|',end='|\n')
S=j(s());P(a,n[-1],b(S,30),' Not Out ',c(h(t(S)),5),sep='|',end='|\n');P(a+f'|{15*" "}Total Runs{15*" "}       |{c(h(T),5)}|\n+{47*"-"}+{5*"-"}+')


Try it online!

Well, it's been over 24 hours and this took me about an hour to whip up, so I hope I'm not FGITW'ing anyone, and I haven't golfed in Python in a while, so this was fun (although this is the second time I've answered one of my own questions with a long Python answer)

Please feel free to post golf suggestions, Python isn't my best language for golfing.

-68 bytes thanks to FlipTack!

-8 bytes thanks to Mr. Xcoder

+16 bytes due to a bug

• At first glance, I can see quite a few golfs... 1) In one instance you still use print instead of P. 2) sum can directly take a map object, no need to convert it to a list. 3) Rather than writing yield x twice, why not yield x and then break if necessary? 4) T=0 could be placed inside the function header. 5) In your last function, you've used several newlines where you could just use one line with semicolons, for example the first three statements, the last three statements, and the statements in the for loop. – FlipTack Dec 17 '17 at 9:41
• @FlipTack Nice golfs, and thanks! – caird coinheringaahing Dec 18 '17 at 15:50
• You can golf s considerably... def s(i=30): and then while i:x=choice('12.4/6');yield x;i=('/'!=x)*~-i. Also, you only call o() once, so there's no need to even declare it, just use the statement directly. – FlipTack Dec 18 '17 at 17:14
• The same applies to b ^^ – FlipTack Dec 18 '17 at 17:24
• @FlipTack Added the golfs. Thanks! – caird coinheringaahing Dec 19 '17 at 15:42

# Charcoal, 277 255 bytes

≔Ｅ¹¹⟦⟦⟧⁰Ｓ⟧θ≔⮌θηＷ⊖Ｌη«≔⊟ηι≔‽12.4/6ζ⊞υζ⊞§ι⁰ζ≔⎇⁼ζ/⁺²‽³⁼³⁰Ｌ§ι⁰ζ¿ζ§≔ι¹ζ⊞ηι¿⊖Ｌη¿⁼¬﹪Ｌυ⁶¬εＦ⟦⊟η⊟η⟧⊞ηκ»”|⁴Ｂ\[⎇⁻℅↧Ｔ⁵·5ＫＭＫ⟲M≦»→´⁶_⭆∨Ｒ▷↥l⁹KＧ…≦”Ｆθ«◨⊟ι⁷◨Σ§ι⁰¦³²§⪪”(3⪪⪫⧴πＺJL:∨ＸＩ±ＵＲＤ↗Σ9⟦FZ∕↖l⪪”⁷⊟ι◧ＩΣＥ⊟ιΣκ⁶⸿⸿»”|ＱºWＰD⟧zＮφ[v？Π'vＧ”◧ＩΣＥυΣι²⁸Ｊ±¹±¹ＦＥ¹³⁻²⁷⊗ι«Ｂ⁵⁵ιＢ⁴⁹ι¿‹ι²⁷«Ｂ³⁹ιＢ⁸ι


Try it online! Link is to verbose version of code. Explanation:

≔Ｅ¹¹⟦⟦⟧⁰Ｓ⟧θ


Read in the 11 names (input is flexible: JSON, space separated, or newline separated) and create an array q of 11 batters, represented by their balls (as an array), status (as an integer) and name.

≔⮌θη


Create a reversed copy of the batters h. This represents the batters that are not out. The last two elements are the off and current batters.

Ｗ⊖Ｌη«


Repeat while there are at least two batters available.

≔⊟ηι


Extract the current batter to i.

≔‽12.4/6ζ


Generate a random ball in z.

⊞υζ


Add it to the overall list of balls using the predefined empty list u.

⊞§ι⁰ζ


Add it to the current batter's balls.

≔⎇⁼ζ/⁺²‽³⁼³⁰Ｌ§ι⁰ζ


If the ball is a /, then generate a random status 2..4, otherwise if this is the batter's 30th ball then set the status to 1 otherwise 0.

¿ζ§≔ι¹ζ⊞ηι


If the batter is out then store the batter's status otherwise put the batter back in to bat.

¿⊖Ｌθ¿⁼¬﹪ΣＥηＬκ⁶¬ζ


If there are at least two batters left, and the batter was out xor 6 balls have been played, then...

Ｆ⟦⊟η⊟η⟧⊞ηκ»


...take the off and current batters and put them back in reverse order.

”|⁴Ｂ\[⎇⁻℅↧Ｔ⁵·5ＫＭＫ⟲M≦»→´⁶_⭆∨Ｒ▷↥l⁹KＧ…≦”


Ｆθ«


Loop over the batters.

◨⊟ι⁷


Print the batter's name.

◨Σ§ι⁰¦³²


Print the batter's balls.

§⪪”(3⪪⪫⧴πＺJL:∨ＸＩ±ＵＲＤ↗Σ9⟦FZ∕↖l⪪”⁷⊟ι


Print the batter's status by indexing into the string Not OutRetiredBowled Caught LBW split into substrings of length 7.

◧ＩΣＥ⊟ιΣκ⁶


Print the batter's score.

⸿⸿»


Move to the start of the next line but one.

”|ＱºWＰD⟧zＮφ[v？Π'vＧ”◧ＩΣＥυΣι²⁸


Print the total.

Ｊ±¹±¹ＦＥ¹³⁻²⁷⊗ι«Ｂ⁵⁵ιＢ⁴⁹ι¿‹ι²⁷«Ｂ³⁹ιＢ⁸ι


Draw boxes around everything.