"""Sample policy for the Taffy Tractor API. Return a JSON-compatible action.""" SUITS = ("♠", "♥", "♣", "♦") def _rank(card): face = card % 54 return 15 if face == 52 else 16 if face == 53 else 2 + face // 4 def _suit(card): face = card % 54 return SUITS[face % 4] if face < 52 else None def choose_action(state): hand = state.get("hand", []) phase = state.get("phase") if phase == "trump": best = state.get("best_declaration") if best and best.get("seat") == state.get("my_seat"): return {"action": "pass_trump"} by_face = {} for card in hand: by_face.setdefault(card % 54, []).append(card) declarations = [] for card in hand: if _rank(card) == state.get("level"): declarations.append(((1, 0), [card])) for face, cards in by_face.items(): if len(cards) >= 2 and (_rank(cards[0]) == state.get("level") or _rank(cards[0]) in (15, 16)): power = (2, 0) if _rank(cards[0]) == state.get("level") else (3 if _rank(cards[0]) == 15 else 4, 0) declarations.append((power, cards[:2])) available = [item for item in declarations if not best or tuple(best.get("strength", [])) < item[0]] if available: return {"action": "declare_trump", "cards": max(available, key=lambda item: item[0])[1]} return {"action": "pass_trump"} if phase == "bury": level = int(state.get("level", 2)) trump = state.get("trump_suit") def discard_key(card): face = card % 54 rank = _rank(card) is_trump = face >= 52 or rank == level or _suit(card) == trump points = 0 if face >= 52 else 5 if rank == 5 else 10 if rank in (10, 13) else 0 return (points > 0, is_trump, rank) return {"action": "bury", "cards": sorted(hand, key=discard_key)[:8]} if not hand: return {"action": "play", "cards": []} level = int(state.get("level", 2)) trump = state.get("trump_suit") def category(card): face = card % 54 if face >= 52 or _rank(card) == level or _suit(card) == trump: return 4 return SUITS.index(_suit(card)) def strength(card): face = card % 54 rank = _rank(card) card_suit = _suit(card) if face == 53: return 500 if face == 52: return 490 if rank == level: return 480 if card_suit == trump else 470 + SUITS.index(card_suit) return 300 + rank if card_suit == trump else rank trick = state.get("trick", []) if not trick: return {"action": "play", "cards": [min(hand, key=lambda card: (_rank(card), card))]} shape = trick[0].get("shape", {}) lead = shape.get("category", trick[0].get("suit")) needed = int(shape.get("count", 1)) matching = [card for card in hand if category(card) == lead] by_face = {} for card in matching: by_face.setdefault(card % 54, []).append(card) pairs = [(face, cards[:2]) for face, cards in by_face.items() if len(cards) >= 2] if shape.get("kind") == "pair" and pairs: return {"action": "play", "cards": pairs[0][1]} if shape.get("kind") == "tractor": pair_count = int(shape.get("pair_count", needed // 2)) pairs.sort(key=lambda item: strength(item[0])) rank_order = [rank for rank in range(2, 15) if rank != level] for start in range(max(0, len(pairs) - pair_count + 1)): run = pairs[start:start + pair_count] if len(run) != pair_count: continue if lead == 4: trump_faces = sorted((face for face in range(54) if category(face) == 4), key=strength) consecutive = all(abs(trump_faces.index(a[0]) - trump_faces.index(b[0])) == 1 for a, b in zip(run, run[1:])) else: consecutive = all(abs(rank_order.index(_rank(a[0])) - rank_order.index(_rank(b[0]))) == 1 for a, b in zip(run, run[1:])) if consecutive: return {"action": "play", "cards": [card for _, pair in run for card in pair]} selected = [card for _, pair in pairs for card in pair][:needed] for card in matching: if len(selected) >= min(needed, len(matching)): break if card not in selected: selected.append(card) for card in sorted(hand, key=lambda value: (_rank(value), value)): if len(selected) >= needed: break if card not in matching and card not in selected: selected.append(card) return {"action": "play", "cards": selected[:needed]}