22// Lookup tables for possible rock / paper / scissor values
33const selfCodes = [ 'X' , 'Y' , 'Z' ]
44const opponentCodes = [ 'A' , 'B' , 'C' ]
5+ const strategyCodes = selfCodes // Same list, as lose / draw / win
56
67const scoreRound = ( opponent , self ) => {
78 const scoreShape = ( self ) => {
@@ -36,6 +37,22 @@ const scoreRound = (opponent, self) => {
3637 return scoreShape ( self ) + scoreOutcome ( opponent , self )
3738}
3839
40+ const strategizeRound = ( opponent , outcome ) => {
41+ const scoreOutcome = 3 * strategyCodes . indexOf ( outcome )
42+ const scoreShape = ( shape ) => {
43+ return opponentCodes . indexOf ( shape ) + 1
44+ }
45+
46+ const findPlay = ( opponent , outcome ) => {
47+ const offset = strategyCodes . indexOf ( outcome ) - 1
48+ let target = opponentCodes . indexOf ( opponent ) + offset
49+ if ( target >= opponentCodes . length ) { target = 0 }
50+ return opponentCodes [ target ]
51+ }
52+ console . debug ( scoreShape ( findPlay ( opponent , outcome ) ) , scoreOutcome )
53+ return scoreShape ( findPlay ( opponent , outcome ) ) + scoreOutcome
54+ }
55+
3956/**
4057 * Tallies the results of all rounds in a match
4158 * @param {* } guide
@@ -47,7 +64,15 @@ const scoreMatch = (guide) => {
4764 } ) . reduce ( ( sum , value ) => sum + value , 0 )
4865}
4966
67+ const strategizeMatch = ( guide ) => {
68+ return guide . map ( ( match ) => {
69+ return strategizeRound ( ...match )
70+ } ) . reduce ( ( sum , value ) => sum + value , 0 )
71+ }
72+
5073module . exports = {
5174 scoreMatch,
52- scoreRound
75+ scoreRound,
76+ strategizeMatch,
77+ strategizeRound
5378}
0 commit comments