Skip to content

Instantly share code, notes, and snippets.

@basdirks
Forked from joebo/scoring
Last active August 29, 2015 14:22
Show Gist options
  • Save basdirks/478a45926967940d8107 to your computer and use it in GitHub Desktop.
Save basdirks/478a45926967940d8107 to your computer and use it in GitHub Desktop.
tbl=:3 4 $ 0; 2; 'A'; 'B'; 3; 1; 'B'; 'C'; 1; 1; 'C'; 'A'
init=:((~. @: , @: (2&}."1)) ; (0 #~ #)) tbl
NB. given a table of team scores, score a game and return new team score
score =: 4 : 0
'allteams scores'=. > y
diff =. - / (> @: (2&{.)) > x
teams=. 2&}. > x
allteamIdx=. allteams i. teams
team1Total=. (0{:: allteamIdx) { scores
team2Total=. (1{:: allteamIdx) { scores
NB. add 1 pt if spread is more than 5 between both players
NB. add 2 pt if spread is more than 10 pts between players..
adder=: (| team1Total-team2Total) I.~ (0 5 10 15)
NB. both teams get 1 point
if. diff=0 do.
scores=. (1+team1Total) (0{::allteamIdx) } scores
scores=. (1+team2Total) (1{::allteamIdx) } scores
NB. team 1 won
elseif. diff > 0 do.
scores=. ((1+adder)+team1Total) (0{::allteamIdx) } scores
NB. team 2 won
elseif. diff < 0 do.
scores=. ((1+adder)+team2Total) (1{::allteamIdx) } scores
end.
ret=.allteams;scores
< ret
)
NB. tie game
assert ((<(0;0;'A';'B')) score <(('A';'B';'C');(0 0 0))) -: <(('A';'B';'C');(1 1 0))
NB. team 2 wins
assert ((<(0;1;'A';'B')) score <(('A';'B';'C');(0 0 0))) -: <(('A';'B';'C');(0 1 0))
NB. team 1 wins against a good opponent
assert ((<(1;0;'A';'B')) score <(('A';'B';'C');(0 6 0))) -: <(('A';'B';'C');(3 6 0))
tbl=:4 4 $ 0; 2; 'A'; 'B'; 3; 1; 'B'; 'C'; 1; 1; 'C'; 'B'; 1; 0; 'A';'B'
score/ (|. <"1 tbl), <init
┌───────────────┐
│┌───────┬─────┐│
││┌─┬─┬─┐│2 4 1││
│││A│B│C││ ││
││└─┴─┴─┘│ ││
│└───────┴─────┘│
└───────────────┘
score/ (|. <"1 (2#tbl)), <init
┌───────────────┐
│┌───────┬─────┐│
││┌─┬─┬─┐│6 9 2││
│││A│B│C││ ││
││└─┴─┴─┘│ ││
│└───────┴─────┘│
└───────────────┘
score/ (|. <"1 (20#tbl)), <init
┌────────────────────┐
│┌───────┬──────────┐│
││┌─┬─┬─┐│100 202 20││
│││A│B│C││ ││
││└─┴─┴─┘│ ││
│└───────┴──────────┘│
└────────────────────┘
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment