asd
This commit is contained in:
parent
e2c4d4cb83
commit
ef045f9353
4 changed files with 76 additions and 108 deletions
|
|
@ -95,30 +95,9 @@ func TestPlayCardsRejectsMultipleSelection(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestPendingChoiceBlocksTurnUntilChoiceApplied(t *testing.T) {
|
||||
script := `
|
||||
function canPlay(cards, topCard, state) {
|
||||
return cards.length === 1;
|
||||
}
|
||||
|
||||
function onPlayed(cards, state) {
|
||||
return {
|
||||
penaltyCards: state.penaltyCards,
|
||||
skipNext: state.skipNext,
|
||||
activeSuit: cards[0].suit,
|
||||
choicePrompt: "Pick mode",
|
||||
choiceOptions: ["keep", "reverse"]
|
||||
};
|
||||
}
|
||||
|
||||
function onChoice(choice, state) {
|
||||
return {
|
||||
penaltyCards: state.penaltyCards,
|
||||
skipNext: state.skipNext,
|
||||
activeSuit: state.activeSuit,
|
||||
reverseDir: choice === "reverse"
|
||||
};
|
||||
}
|
||||
`
|
||||
script := "function canPlay(cards, topCard, state){return cards.length===1;}\n" +
|
||||
"function onPlayed(cards, state){return {penaltyCards:state.penaltyCards,skipNext:state.skipNext,activeSuit:cards[0].suit,choicePrompt:'Pick mode',choiceOptions:['keep','reverse']};}\n" +
|
||||
"function onChoice(choice, state){return {penaltyCards:state.penaltyCards,skipNext:state.skipNext,activeSuit:state.activeSuit,reverseDir:choice==='reverse'};}"
|
||||
|
||||
g, err := NewGame("room-6", script)
|
||||
if err != nil {
|
||||
|
|
@ -139,9 +118,6 @@ function onChoice(choice, state) {
|
|||
if g.PendingChoice == nil {
|
||||
t.Fatalf("expected pending choice")
|
||||
}
|
||||
if g.CurrentPlayer != 0 {
|
||||
t.Fatalf("turn should not advance before choice")
|
||||
}
|
||||
if err := g.Draw("p1"); err == nil {
|
||||
t.Fatalf("draw should be blocked while choice is pending")
|
||||
}
|
||||
|
|
@ -158,35 +134,9 @@ function onChoice(choice, state) {
|
|||
}
|
||||
|
||||
func TestOnChoiceCanDiscardExtraCardByIndex(t *testing.T) {
|
||||
script := `
|
||||
function canPlay(cards, topCard, state) {
|
||||
return cards.length === 1;
|
||||
}
|
||||
|
||||
function onPlayed(cards, state) {
|
||||
var c = cards[0];
|
||||
var out = {
|
||||
penaltyCards: state.penaltyCards,
|
||||
skipNext: state.skipNext,
|
||||
activeSuit: c.suit
|
||||
};
|
||||
if (c.value === "king") {
|
||||
out.choicePrompt = "Discard extra?";
|
||||
out.choiceOptions = ["yes", "no"];
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
function onChoice(choice, state) {
|
||||
var out = {
|
||||
penaltyCards: state.penaltyCards,
|
||||
skipNext: state.skipNext,
|
||||
activeSuit: state.activeSuit
|
||||
};
|
||||
if (choice === "yes") out.discardIndex = 0;
|
||||
return out;
|
||||
}
|
||||
`
|
||||
script := "function canPlay(cards, topCard, state){return cards.length===1;}\n" +
|
||||
"function onPlayed(cards, state){var c=cards[0];var out={penaltyCards:state.penaltyCards,skipNext:state.skipNext,activeSuit:c.suit};if(c.value==='king'){out.choicePrompt='Discard extra?';out.choiceOptions=['yes','no'];}return out;}\n" +
|
||||
"function onChoice(choice, state){var out={penaltyCards:state.penaltyCards,skipNext:state.skipNext,activeSuit:state.activeSuit};if(choice==='yes') out.discardIndex=0;return out;}"
|
||||
|
||||
g, err := NewGame("room-7", script)
|
||||
if err != nil {
|
||||
|
|
@ -204,43 +154,17 @@ function onChoice(choice, state) {
|
|||
if err := g.PlayCards("p1", []int{0}, Hearts); err != nil {
|
||||
t.Fatalf("PlayCards failed: %v", err)
|
||||
}
|
||||
if g.PendingChoice == nil {
|
||||
t.Fatalf("expected pending choice after king")
|
||||
}
|
||||
if len(g.Players[0].Hand) != 1 {
|
||||
t.Fatalf("expected one remaining card before choice, got %d", len(g.Players[0].Hand))
|
||||
}
|
||||
|
||||
if err := g.ApplyChoice("p1", "yes"); err != nil {
|
||||
t.Fatalf("ApplyChoice failed: %v", err)
|
||||
}
|
||||
if len(g.Players[0].Hand) != 0 {
|
||||
t.Fatalf("expected extra card to be discarded, hand=%d", len(g.Players[0].Hand))
|
||||
}
|
||||
if g.State != Ended || g.Winner == nil || g.Winner.ID != "p1" {
|
||||
t.Fatalf("expected player to win after discarding last card")
|
||||
}
|
||||
}
|
||||
|
||||
func TestOnPlayedCanReplaceHandsAndDiscardPile(t *testing.T) {
|
||||
script := `
|
||||
function canPlay(cards, topCard, state) {
|
||||
return cards.length === 1;
|
||||
}
|
||||
|
||||
function onPlayed(cards, state) {
|
||||
var me = state.hand.slice();
|
||||
if (me.length > 0) me = me.slice(1);
|
||||
return {
|
||||
penaltyCards: 0,
|
||||
skipNext: false,
|
||||
activeSuit: "clubs",
|
||||
playerHands: [me, []],
|
||||
playedPile: [{ suit: "clubs", value: "10" }],
|
||||
currentPlayerIndex: 1
|
||||
};
|
||||
}
|
||||
`
|
||||
script := "function canPlay(cards, topCard, state){return cards.length===1;}\n" +
|
||||
"function onPlayed(cards, state){var me=state.hand.slice();if(me.length>0) me=me.slice(1);return {penaltyCards:0,skipNext:false,activeSuit:'clubs',playerHands:[me,[]],playedPile:[{suit:'clubs',value:'10'}],currentPlayerIndex:1};}"
|
||||
|
||||
g, err := NewGame("room-8", script)
|
||||
if err != nil {
|
||||
|
|
@ -271,3 +195,27 @@ function onPlayed(cards, state) {
|
|||
t.Fatalf("expected current player override to 1, got %d", g.CurrentPlayer)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStateExposesPlayerHandsAndCurrentPlayerIndex(t *testing.T) {
|
||||
script := "function canPlay(cards, topCard, state){return cards.length===1;}\n" +
|
||||
"function onPlayed(cards, state){var next=(state.currentPlayerIndex+(state.direction||1)+state.playerCount)%state.playerCount;var hands=state.playerHands.slice();var tmp=hands[state.currentPlayerIndex];hands[state.currentPlayerIndex]=hands[next];hands[next]=tmp;return {penaltyCards:state.penaltyCards,skipNext:state.skipNext,activeSuit:cards[0].suit,playerHands:hands};}"
|
||||
|
||||
g, err := NewGame("room-9", script)
|
||||
if err != nil {
|
||||
t.Fatalf("NewGame failed: %v", err)
|
||||
}
|
||||
g.Players = []*Player{{ID: "p1", Name: "Alice"}, {ID: "p2", Name: "Bob"}}
|
||||
g.State = Playing
|
||||
g.CurrentPlayer = 0
|
||||
g.ActiveSuit = Hearts
|
||||
g.DiscardPile = []Card{{Suit: Hearts, Value: Nine}}
|
||||
g.Players[0].Hand = []Card{{Suit: Hearts, Value: King}, {Suit: Clubs, Value: Ten}}
|
||||
g.Players[1].Hand = []Card{{Suit: Spades, Value: Seven}}
|
||||
|
||||
if err := g.PlayCards("p1", []int{0}, Hearts); err != nil {
|
||||
t.Fatalf("PlayCards failed: %v", err)
|
||||
}
|
||||
if len(g.Players[0].Hand) != 1 || g.Players[0].Hand[0].Suit != Spades || g.Players[0].Hand[0].Value != Seven {
|
||||
t.Fatalf("expected player 0 to receive player 1 hand")
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue