Meta (Facebook) AI-Enabled 面试真题 — 15点三张牌策略游戏 & 非法操作检测系统设计题

94次阅读
没有评论

Triplet Card Game — AI Strategy Task

You can see there is a 4×4 grid of cards, Ace through Nine.
Each turn consists of picking up 3 cards which add up to 15 (Aces count as 1);
these cards are then replaced from the deck.

If you can’t pick up exactly 3 cards that add up to 15, then the game is over.
In a perfect game, you would pick up all 36 cards.

Currently, pressing Return makes the game end immediately.
This is because MyStrategy is a skeleton that does not pick any cards.
You will implement that later.

Now run the unit tests in TripletGameTests. The game works if the Strategy plays fairly,
but the test shows that the game allows players to cheat by choosing a card that isn’t on the table.
Let’s fix that first!

Tasks:

  1. Fix the illegal play issue: prevent choosing a card that is not currently on the table.
  2. Ensure that the game ends immediately after an illegal play.
  3. Implement MyStrategy so that it selects valid moves (pick three cards whose sum = 15).
  4. The score should be calculated correctly based on legal triplets picked.

Unit Tests include:
testLegalPlaysAreAllowed()
testEndGameAfterIllegalPlay()

Card.java

class Card {
    public enum Rank {
        private final int value;
        private final String displayChar;

        Rank(int value, String displayChar) {
            this.value = value;
            this.displayChar = displayChar;
        }

        public int getValue() {return value;}

        public String getDisplayChar() {return displayChar;}
    }

    private final Suit suit;
    private final Rank rank;

    public Card(Suit suit, Rank rank) {
        this.suit = suit;
        this.rank = rank;
    }

    public Suit getSuit() {return suit;}

    public Rank getRank() {return rank;}

    public int getValue() {return rank.getValue();
    }

    public String toString() {return rank.getDisplayChar() + suit.getDisplayChar();}
}

TripletGameTests.java

@Test
public void testLegalPlaysAreAllowed() {Deck testDeck = stackDeckForTable(TABLE_ONE);
    TripletGame.Strategy strategy =
            new StaticStrategy(new Card[][] {{c(ACE, SPADES), c(FIVE, SPADES), c(NINE, SPADES)},
                            {c(ACE, HEARTS), c(FIVE, HEARTS), c(NINE, HEARTS)},
                            {c(ACE, DIAMONDS), c(FIVE, DIAMONDS), c(NINE, DIAMONDS)}
                    });

    TripletGame game = new TripletGame(testDeck, 4, strategy);
    assertFalse(game.playTurn());
    assertFalse(game.playTurn());
    assertTrue(game.playTurn());
    assertEquals(45, game.getScore());
}
@Test
public void testEndGameAfterIllegalPlay() {Deck testDeck = stackDeckForTable(TABLE_ONE);
    TripletGame.Strategy strategy =
            new StaticStrategy(new Card[][] {{c(TWO, SPADES), c(FIVE, SPADES), c(EIGHT, SPADES)},
                    });

    TripletGame game = new TripletGame(testDeck, 4, strategy);
    assertTrue(game.playTurn());
    assertEquals(0, game.getScore());
}

Main.java

public class Main {private static void updateDisplay(TripletGame game) {System.out.print("\u001b[H\u001b[2J");
        System.out.println("Current Game State:" + game.getScore());

        List<List<Card>> table = game.readOnlyTable();
        for (int rowIndex = 0; rowIndex < table.size(); rowIndex++) {List<Card> row = table.get(rowIndex);
            for (int cardIndex = 0; cardIndex < row.size(); cardIndex++) {String cardText = row.get(cardIndex) == null ? ".." : row.get(cardIndex).toString();
                System.out.print("" + cardText +" ");
            }
            System.out.println();}
    }

    public static void main(String[] args) {TripletGame game = new TripletGame(4, new MyStrategy());
        Scanner scanner = new Scanner(System.in);

        updateDisplay(game);
        while (!game.gameOver()) {System.out.println("Press enter to play a turn");
            scanner.nextLine();
            updateDisplay(game);
            game.playTurn();}

        System.out.println("Game Over! Final Score:" + game.getScore());
        scanner.close();}
}

MyStrategy.java

class MyStrategy implements TripletGame.Strategy {
    @Override
    public Card[] selectCards(List<List<Card>> table) {return new Card[0]; // TODO: choose 3 cards that sum to 15
    }
}

TripletGame.java

public boolean playTurn() {
    // TODO validate selected cards
    return false;
}

游戏棋盘是 4×4 的卡片矩阵,每次必须选 3 张卡且总和为 15。

当前代码漏洞:策略层允许选择不存在于桌面上的卡片,导致作弊。

修复方向:

检查所选卡片坐标是否存在于当前 table 中,否则立即结束游戏。

改写 playTurn() — 判断是否合法 → 合法则得分 + 替换牌 → 非法则结束游戏。

实现策略 MyStrategy:遍历所有组合找到 sum=15 的三张牌。

单元测试覆盖合法操作与非法终止逻辑。

VOprep 团队长期陪同学员实战各类大厂 OA 与 VO,包括 Meta、Google、Amazon 等,提供实时答案助攻、远程陪练与面试节奏提醒,帮助大家在关键时刻不卡壳。
如果你也在准备 Stripe 或类似工程向公司,可以了解一下我们的定制助攻方案——从编程面到系统设计,全程护航上岸。

正文完
 0