2
2
// Lookup tables for possible rock / paper / scissor values
3
3
const selfCodes = [ 'X' , 'Y' , 'Z' ]
4
4
const opponentCodes = [ 'A' , 'B' , 'C' ]
5
+ const strategyCodes = selfCodes // Same list, as lose / draw / win
5
6
6
7
const scoreRound = ( opponent , self ) => {
7
8
const scoreShape = ( self ) => {
@@ -36,6 +37,22 @@ const scoreRound = (opponent, self) => {
36
37
return scoreShape ( self ) + scoreOutcome ( opponent , self )
37
38
}
38
39
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
+
39
56
/**
40
57
* Tallies the results of all rounds in a match
41
58
* @param {* } guide
@@ -47,7 +64,15 @@ const scoreMatch = (guide) => {
47
64
} ) . reduce ( ( sum , value ) => sum + value , 0 )
48
65
}
49
66
67
+ const strategizeMatch = ( guide ) => {
68
+ return guide . map ( ( match ) => {
69
+ return strategizeRound ( ...match )
70
+ } ) . reduce ( ( sum , value ) => sum + value , 0 )
71
+ }
72
+
50
73
module . exports = {
51
74
scoreMatch,
52
- scoreRound
75
+ scoreRound,
76
+ strategizeMatch,
77
+ strategizeRound
53
78
}
0 commit comments