fix(engine): single-word rule connects along the play line
CI / changes (pull_request) Successful in 1s
CI / unit (pull_request) Successful in 9s
CI / integration (pull_request) Successful in 12s
CI / ui (pull_request) Has been skipped
CI / gate (pull_request) Successful in 0s
CI / deploy (pull_request) Successful in 1m11s
CI / changes (pull_request) Successful in 1s
CI / unit (pull_request) Successful in 9s
CI / integration (pull_request) Successful in 12s
CI / ui (pull_request) Has been skipped
CI / gate (pull_request) Successful in 0s
CI / deploy (pull_request) Successful in 1m11s
Bumps the engine to scrabble-solver v1.1.1, where a single-word-per-turn play must form its word along its own line through an existing tile: a multi-tile play that touches the board only perpendicular to itself (the contour РЮМ/КЕД/ОР cases) no longer connects. For a single tile that abuts the board on both axes the engine now plays the higher-scoring legal orientation instead of the geometrically longer one (playDirection), so a real word is never rejected in favour of a non-word. Reworks the single-word solver/engine tests for the corrected rule (no longer a superset of standard play) and updates ARCHITECTURE/FUNCTIONAL/PRERELEASE.
This commit is contained in:
@@ -62,7 +62,7 @@ func (g *Game) SubmitPlay(tiles []TileRecord) (MoveRecord, error) {
|
||||
if err != nil {
|
||||
return MoveRecord{}, err
|
||||
}
|
||||
return g.Play(resolveDirection(g.board, placements), placements)
|
||||
return g.Play(g.playDirection(placements), placements)
|
||||
}
|
||||
|
||||
// SubmitPlayDir is SubmitPlay with the orientation supplied rather than inferred.
|
||||
@@ -106,7 +106,7 @@ func (g *Game) EvaluatePlay(tiles []TileRecord) (MoveRecord, error) {
|
||||
if err != nil {
|
||||
return MoveRecord{}, err
|
||||
}
|
||||
move, err := g.solver.ValidatePlayOpts(g.board, resolveDirection(g.board, placements), placements, g.playOpts())
|
||||
move, err := g.solver.ValidatePlayOpts(g.board, g.playDirection(placements), placements, g.playOpts())
|
||||
if err != nil {
|
||||
return MoveRecord{}, fmt.Errorf("%w: %v", ErrIllegalPlay, err)
|
||||
}
|
||||
@@ -169,6 +169,33 @@ func (g *Game) placements(tiles []TileRecord) ([]scrabble.Placement, error) {
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// playDirection resolves the orientation for a live play. resolveDirection infers it from
|
||||
// geometry alone, preferring the longer word when a single tile abuts the board on both
|
||||
// axes; under the single-word rule that can pick an orientation whose word is not in the
|
||||
// dictionary while the other orientation's is. So for a single tile under that rule the
|
||||
// engine tries both orientations through the solver and keeps the higher-scoring legal one
|
||||
// (horizontal breaks a tie). Multi-tile plays, and every play under the standard rule, keep
|
||||
// the geometric resolution: a multi-tile play's orientation is fixed by the line its tiles
|
||||
// share, and under the standard rule every word the play forms must be valid regardless of
|
||||
// which one is named the main word.
|
||||
func (g *Game) playDirection(placements []scrabble.Placement) scrabble.Direction {
|
||||
geo := resolveDirection(g.board, placements)
|
||||
if len(placements) != 1 || g.multipleWords {
|
||||
return geo
|
||||
}
|
||||
best, found, bestScore := geo, false, 0
|
||||
for _, dir := range [...]scrabble.Direction{scrabble.Horizontal, scrabble.Vertical} {
|
||||
m, err := g.solver.ValidatePlayOpts(g.board, dir, placements, g.playOpts())
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
if !found || m.Score > bestScore {
|
||||
best, found, bestScore = dir, true, m.Score
|
||||
}
|
||||
}
|
||||
return best
|
||||
}
|
||||
|
||||
// encodeTiles encodes decoded exchange tiles ("?" for a blank, otherwise a
|
||||
// concrete letter) into the internal byte form, wrapping a bad letter as
|
||||
// ErrTilesNotOnRack (the caller cannot hold a tile it cannot name).
|
||||
|
||||
@@ -33,9 +33,12 @@ func TestSingleWordRuleWiring(t *testing.T) {
|
||||
t.Error("single-word game must ignore cross-words")
|
||||
}
|
||||
|
||||
// Play the same opening (the standard game's top move) in both games, then compare
|
||||
// the next player's candidate moves. Both games share the seed, so the next rack is
|
||||
// identical; relaxed (single-word) generation never drops a legal standard move.
|
||||
// Play the same opening (the standard game's top move) in both games. Both share the
|
||||
// seed, so the next rack is identical and both still have legal replies. The single-word
|
||||
// rule is not a superset of the standard one — it forbids parallel plays the standard
|
||||
// rule allows and admits in-line plays whose cross-words are invalid — so here the two
|
||||
// move sets only need to be non-empty; their rule-specific differences are covered by the
|
||||
// cross-word and connectivity tests.
|
||||
hint, ok := std.HintView()
|
||||
if !ok {
|
||||
t.Fatal("opening game has no hint")
|
||||
@@ -46,9 +49,8 @@ func TestSingleWordRuleWiring(t *testing.T) {
|
||||
if _, err := single.SubmitPlay(hint.Tiles); err != nil {
|
||||
t.Fatalf("single-word opening: %v", err)
|
||||
}
|
||||
stdMoves, singleMoves := len(std.GenerateMoves()), len(single.GenerateMoves())
|
||||
if singleMoves < stdMoves {
|
||||
t.Errorf("single-word generation produced %d moves, want >= standard %d", singleMoves, stdMoves)
|
||||
if len(std.GenerateMoves()) == 0 || len(single.GenerateMoves()) == 0 {
|
||||
t.Error("both games should have legal replies after the opening")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -137,6 +139,145 @@ func TestEvaluatePlayHonorsSingleWordRule(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
// TestSingleWordRuleSingleTileDirection covers the single-tile half of the single-word
|
||||
// rule: when a lone tile abuts the board on both axes, the engine picks the orientation that
|
||||
// forms a real word, not the geometrically longer one. The lone 'о' spells the non-word
|
||||
// "фоф" across (length 3) but the real word "до" down (length 2); the geometric resolver
|
||||
// prefers the longer "фоф", so before the fix the play was wrongly rejected.
|
||||
func TestSingleWordRuleSingleTileDirection(t *testing.T) {
|
||||
if ok, err := testReg.Lookup(VariantErudit, testVersion, "до"); err != nil || !ok {
|
||||
t.Fatalf("precondition: \"до\" must be in the Erudit dictionary (ok=%v, err=%v)", ok, err)
|
||||
}
|
||||
if ok, _ := testReg.Lookup(VariantErudit, testVersion, "фоф"); ok {
|
||||
t.Fatal("precondition: \"фоф\" must not be a word")
|
||||
}
|
||||
|
||||
g, err := New(testReg, Options{
|
||||
Variant: VariantErudit, Version: testVersion, Players: 2, Seed: 1, MultipleWordsPerTurn: false,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("new erudit game: %v", err)
|
||||
}
|
||||
idx := func(s string) byte {
|
||||
i, err := g.rules.Alphabet.Index(s)
|
||||
if err != nil {
|
||||
t.Fatalf("index %q: %v", s, err)
|
||||
}
|
||||
return i
|
||||
}
|
||||
scrabble.Apply(g.board, scrabble.Move{Tiles: []scrabble.Placement{
|
||||
{Row: 4, Col: 8, Letter: idx("д")}, // above 'о': the vertical word "до"
|
||||
{Row: 5, Col: 7, Letter: idx("ф")}, // left of 'о': the across non-word "фоф"
|
||||
{Row: 5, Col: 9, Letter: idx("ф")}, // right of 'о'
|
||||
}})
|
||||
g.hands[0] = []byte{idx("о")}
|
||||
tiles := []TileRecord{{Row: 5, Col: 8, Letter: "о"}}
|
||||
|
||||
rec, err := g.EvaluatePlay(tiles)
|
||||
if err != nil {
|
||||
t.Fatalf("evaluate the single tile under the single-word rule: %v", err)
|
||||
}
|
||||
if rec.Dir != Vertical {
|
||||
t.Errorf("dir = %v, want Vertical (the real word \"до\")", rec.Dir)
|
||||
}
|
||||
if len(rec.Words) != 1 || rec.Words[0] != "до" {
|
||||
t.Errorf("words = %v, want [до]", rec.Words)
|
||||
}
|
||||
if _, err := g.SubmitPlay(tiles); err != nil {
|
||||
t.Errorf("submit the single tile under the single-word rule: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestSingleWordRuleSingleTileBestScore covers the rest of rule (2): when a single tile
|
||||
// forms a real word on BOTH axes, the engine keeps the higher-scoring orientation (and
|
||||
// horizontal on a tie), overriding the geometric resolver's tie preference. The lone 'с'
|
||||
// spells "ас" across and "юс" down; the engine must pick whichever scores more.
|
||||
func TestSingleWordRuleSingleTileBestScore(t *testing.T) {
|
||||
for _, w := range []string{"ас", "юс"} {
|
||||
if ok, err := testReg.Lookup(VariantErudit, testVersion, w); err != nil || !ok {
|
||||
t.Fatalf("precondition: %q must be in the Erudit dictionary (ok=%v, err=%v)", w, ok, err)
|
||||
}
|
||||
}
|
||||
g, err := New(testReg, Options{Variant: VariantErudit, Version: testVersion, Players: 2, Seed: 1})
|
||||
if err != nil {
|
||||
t.Fatalf("new erudit game: %v", err)
|
||||
}
|
||||
idx := func(s string) byte {
|
||||
i, err := g.rules.Alphabet.Index(s)
|
||||
if err != nil {
|
||||
t.Fatalf("index %q: %v", s, err)
|
||||
}
|
||||
return i
|
||||
}
|
||||
scrabble.Apply(g.board, scrabble.Move{Tiles: []scrabble.Placement{
|
||||
{Row: 5, Col: 7, Letter: idx("а")}, // left of 'с': the across word "ас"
|
||||
{Row: 4, Col: 8, Letter: idx("ю")}, // above 'с': the down word "юс"
|
||||
}})
|
||||
g.hands[0] = []byte{idx("с")}
|
||||
tiles := []TileRecord{{Row: 5, Col: 8, Letter: "с"}}
|
||||
ps := []scrabble.Placement{{Row: 5, Col: 8, Letter: idx("с")}}
|
||||
|
||||
across, aerr := g.solver.ValidatePlayOpts(g.board, scrabble.Horizontal, ps, g.playOpts())
|
||||
down, derr := g.solver.ValidatePlayOpts(g.board, scrabble.Vertical, ps, g.playOpts())
|
||||
if aerr != nil || derr != nil {
|
||||
t.Fatalf("both orientations should be legal: across=%v down=%v", aerr, derr)
|
||||
}
|
||||
wantDir, wantScore := Horizontal, across.Score
|
||||
if down.Score > across.Score {
|
||||
wantDir, wantScore = Vertical, down.Score
|
||||
}
|
||||
|
||||
rec, err := g.EvaluatePlay(tiles)
|
||||
if err != nil {
|
||||
t.Fatalf("evaluate the single tile: %v", err)
|
||||
}
|
||||
if rec.Dir != wantDir {
|
||||
t.Errorf("dir = %v, want %v (higher of \"ас\"=%d, \"юс\"=%d)", rec.Dir, wantDir, across.Score, down.Score)
|
||||
}
|
||||
if rec.Score != wantScore {
|
||||
t.Errorf("score = %d, want %d", rec.Score, wantScore)
|
||||
}
|
||||
}
|
||||
|
||||
// TestSingleWordRuleRejectsPerpendicularOnlyContour is the backend regression for the
|
||||
// reported contour bug: a multi-tile play whose main word is a real word but which touches
|
||||
// the board only perpendicular to its own line — forming a cross-word, not a word along that
|
||||
// line — does not connect under the single-word rule and is rejected by both the preview and
|
||||
// the submit path. Existing "до" sits down column 8; "кот" laid across row 6 is all-new along
|
||||
// its row and touches the board only through the 'о' below the existing 'о'.
|
||||
func TestSingleWordRuleRejectsPerpendicularOnlyContour(t *testing.T) {
|
||||
if ok, err := testReg.Lookup(VariantErudit, testVersion, "кот"); err != nil || !ok {
|
||||
t.Fatalf("precondition: \"кот\" must be a real word (ok=%v, err=%v)", ok, err)
|
||||
}
|
||||
g, err := New(testReg, Options{Variant: VariantErudit, Version: testVersion, Players: 2, Seed: 1})
|
||||
if err != nil {
|
||||
t.Fatalf("new erudit game: %v", err)
|
||||
}
|
||||
idx := func(s string) byte {
|
||||
i, err := g.rules.Alphabet.Index(s)
|
||||
if err != nil {
|
||||
t.Fatalf("index %q: %v", s, err)
|
||||
}
|
||||
return i
|
||||
}
|
||||
scrabble.Apply(g.board, scrabble.Move{Tiles: []scrabble.Placement{
|
||||
{Row: 4, Col: 8, Letter: idx("д")},
|
||||
{Row: 5, Col: 8, Letter: idx("о")},
|
||||
}})
|
||||
g.hands[0] = []byte{idx("к"), idx("о"), idx("т")}
|
||||
tiles := []TileRecord{
|
||||
{Row: 6, Col: 7, Letter: "к"},
|
||||
{Row: 6, Col: 8, Letter: "о"},
|
||||
{Row: 6, Col: 9, Letter: "т"},
|
||||
}
|
||||
if _, err := g.EvaluatePlay(tiles); !errors.Is(err, ErrIllegalPlay) {
|
||||
t.Errorf("EvaluatePlay = %v, want ErrIllegalPlay (connects only perpendicular)", err)
|
||||
}
|
||||
if _, err := g.SubmitPlay(tiles); !errors.Is(err, ErrIllegalPlay) {
|
||||
t.Errorf("SubmitPlay = %v, want ErrIllegalPlay (connects only perpendicular)", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestSingleWordRuleRobotCandidates proves the robot opponent never trips the same
|
||||
// cross-word check while searching for its move: its move source, Candidates ->
|
||||
// GenerateMovesOpts, already honours the rule. Under the single-word rule the bridged
|
||||
|
||||
Reference in New Issue
Block a user