Everyone always wants to implement Conway's Game of Life. That's boring! Let's do cops and robbers instead!
You'll have two teams: the cops and the robbers. Each team has 5 members with 50 health each. The program will loop continuously. Each iteration, the following will occur:
For each team, print the first letter (
Cfor the cops,
Rfor the robbers), a space, a space-separated list of the members' HP, and a newline. This is the teams' status. After both are done, print another newline. For instance, here's what it might look like the first round:
C 50 50 50 50 50 R 50 50 50 50 50
Pick a random number from 1 to 10 (including both 1 and 10). We'll call the number
Nis even, the robbers lose this round; if odd, the cops lose.
Pick a random member of the losing team whose HP is greater than 0 and deduct
NHP. The members' HP should never appear go below 0 on the status.
Restart the loop.
The game ends when all the members of one team lose all their HP. Then, the following will be printed if the cops win:
and if the robbers win:
This is code golf, so the shortest number of characters wins.
Here's a sample implementation in Python 2:
import random cops = *5 robbers = *5 while any(cops) and any(robbers): # print the status print 'C', ' '.join(map(str, cops)) print 'R', ' '.join(map(str, robbers)) print # pick N N = random.randint(1, 10) # pick the losing team (robbers if N is even, else cops) losers = robbers if N % 2 == 0 else cops # pick a member whose HP is greater than 0 losing_member = random.choice([i for i in range(len(losers)) if losers[i]]) losers[losing_member] -= N # make sure the HP doesn't visibly drop below 0 if losers[losing_member] < 0: losers[losing_member] = 0 if any(cops): # robbers lost print 'C+' print 'R-' elif any(robbers): # cops lost print 'C-' print 'R+'