-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Set winning team for each hand and round, set next player to move
- Add hands key to match model - Set next player to move based on previous hand result (the winner player of the prev hand should start) - Set played card as played - Set round winner when ending - At the end of each hand set hand winner and next hand initial player (to determine the ending of the hand)
- Loading branch information
Showing
5 changed files
with
178 additions
and
42 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
const R = require("ramda"); | ||
|
||
const getHandTeamWinner = handCards => { | ||
const cardsScores = R.pipe( | ||
R.map(card => | ||
R.pipe( | ||
R.find(R.propEq("card", card)), | ||
R.prop("score") | ||
)(cards) | ||
) | ||
)(handCards); | ||
|
||
const highestScoreIndex = cardsScores.reduce( | ||
(highestScoreIndexes, currentScore, i, scores) => { | ||
if (Math.max(...scores) === currentScore) { | ||
return highestScoreIndexes.concat(i); | ||
} | ||
return highestScoreIndexes; | ||
}, | ||
[] | ||
); | ||
|
||
const handWinnerTeam = | ||
highestScoreIndex.length > 1 | ||
? "tie" | ||
: highestScoreIndex[0] % 2 === 0 | ||
? "first" | ||
: "second"; | ||
|
||
// Get index of first player with highest score | ||
const nextPlayerIndex = handWinnerTeam !== "tie" && highestScoreIndex[0]; | ||
|
||
return { handWinnerTeam, handStarterPlayerIndex: nextPlayerIndex }; | ||
}; | ||
|
||
const createCardsOfAllSets = ({ number, score }) => | ||
["SWORD", "BASTO", "GOLD", "CUP"].map(set => ({ | ||
card: `${number}-${set}`, | ||
score | ||
})); | ||
|
||
const cards = [ | ||
{ card: "1-SWORD", score: 14 }, | ||
{ card: "1-BASTO", score: 13 }, | ||
{ card: "7-SWORD", score: 12 }, | ||
{ card: "7-GOLD", score: 11 }, | ||
...createCardsOfAllSets({ number: 3, score: 10 }), | ||
...createCardsOfAllSets({ number: 2, score: 9 }), | ||
{ card: "1-GOLD", score: 8 }, | ||
{ card: "1-CUP", score: 8 }, | ||
...createCardsOfAllSets({ number: 12, score: 7 }), | ||
...createCardsOfAllSets({ number: 11, score: 6 }), | ||
...createCardsOfAllSets({ number: 10, score: 5 }), | ||
{ card: "7-BASTO", score: 4 }, | ||
{ card: "7-CUP", score: 4 }, | ||
...createCardsOfAllSets({ number: 6, score: 3 }), | ||
...createCardsOfAllSets({ number: 5, score: 2 }), | ||
...createCardsOfAllSets({ number: 4, score: 1 }) | ||
]; | ||
|
||
module.exports = { | ||
getHandTeamWinner, | ||
cards | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
const R = require("ramda"); | ||
|
||
const isTeamWinner = (handsResults, team, isFirstTeam) => | ||
R.anyPass([ | ||
// If the team won 2 or more hands | ||
R.pipe( | ||
R.filter(R.equals(team)), | ||
R.length, | ||
winningRounds => winningRounds >= 2 | ||
), | ||
// If there is one tie and won 1 hand | ||
R.both(R.find(R.equals("tie")), R.find(R.equals(team))), | ||
// If it's the first team and all hands are a tie | ||
R.both(R.always(isFirstTeam), R.equals(["tie", "tie", "tie"])) | ||
])(handsResults); | ||
|
||
const getRoundWinnerTeam = handsResults => { | ||
if (isTeamWinner(handsResults, "first", true)) { | ||
return "first"; | ||
} | ||
if (isTeamWinner(handsResults, "second")) { | ||
return "second"; | ||
} | ||
return false; | ||
}; | ||
|
||
module.exports = getRoundWinnerTeam; |