9
\$\begingroup\$

You decided to organize a rock-paper-scissors championship to find out who is the best. You don't want to let luck to decide the winner so everyone has to give you his or her tactic in writing before the competition. You also like simple things so a move of a competitor (showing rock, paper or scissors) has to be based only the previous turn (RvR, RvP, RvS, PvR, PvP, PvS, SvR, SvP or SvS). In the first turn a player has to show a fixed sign.

You decided to write a program (or function) to simulate the championship.

Details of the competition

  • There will be at least 2 contestants.
  • Every player plays exactly one match with everyone else.
  • One match lasts 7 rounds.
  • In every round the winner gets 2 points the loser gets none. In the event of a tie both player score 1 point.
  • A players score in a match is the sum of his or her points over the turns of the match.
  • A players final score in the championship is the sum of his or her points over all matches.

Details of the input:

  • your program or function receives N 10 character long strings each of them corresponds to a players strategy. All characters are (lowercase) r p or s meaning that in the given situation the player will show rock paper or scissors.
  • The first letter codes the first turn (in every match for that competitor). The second shows what happens if the last round was rock vs rock. The next ones are RvP, RvS, PvR, PvP, PvS, SvR, SvP and SvS where the first letter is the player's sign and the second is the opponent's. E.g. rrpsrpsrps means the player starts with rock and then copies the opponent's last move.
  • You can input the list of strings as a list/array or similar data of your language or as one string. In the latter case some kind of separator character is a must.

Details of the output:

  • Your program or function should output the final scores of each player in the same order as the input was supplied.
  • Scores should be separated by spaces or newlines. Trailing space or newline is allowed.

Examples:

Input: ['rrpsrpsrps', 'rpppsprrpr']

Output: 5 9 (turns are rvr rvp pvs svp pvr rvp pvs)

Input: ['rrpsrpsrps', 'rpppsprrpr', 'ssssssssss']

Output: 13 17 12 (matches are 5-9 (1st vs 2nd), 8-6 (1st vs 3rd) and 8-6 (2nd vs 3rd))

This is code-golf so the shortest entry wins.

\$\endgroup\$
  • \$\begingroup\$ Inspired by Numberphile? ;-) \$\endgroup\$ – Jakube Jan 30 '15 at 11:19
  • \$\begingroup\$ Is the second example correct? I think the 1st looses against the 3rd with 6-8 and the second looses against the third with 6-8. \$\endgroup\$ – Jakube Jan 30 '15 at 11:48
  • \$\begingroup\$ @Jakube Corrected example input. Thx. \$\endgroup\$ – randomra Jan 30 '15 at 11:58
2
\$\begingroup\$

Python 2: 201 188 chars

def f(Q):c=lambda m:'rps'.index(m);l=len(Q);r=[0]*l;i=0;exec'p,q=i/l,i%l;m,n=c(Q[p][0]),c(Q[q][0]);exec"r[p]+=(p!=q)*(m+1-n)%3;m,n=c(Q[p][m*3+n+1]),c(Q[q][n*3+m+1]);"*7;i+=1;'*l*l;return r

The logic of the program: I convert the letters to number (r=0,p=1,s=2). m is the number of the first, n the number of the second person. Because the game is cyclic, (m-n)%3 already determines the result. And of course, I can shift the result by one f=(m+1-n)%3. Now f=0 means, second player q wins, f=1 means a tie, and f=2, first player p wins. It also is already the score for player 1. Therefore I only need to add all values (p!=q)*(m+1-n)%3 for each player.

Test it with print f(['rrpsrpsrps', 'rpppsprrpr', 'ssssssssss'])

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.