Spaces:
Configuration error
Configuration error
Upload stratego/utils/game_move_tracker.py with huggingface_hub
Browse files
stratego/utils/game_move_tracker.py
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# stratego/memory_tracker.py
|
| 2 |
+
from __future__ import annotations
|
| 3 |
+
import datetime
|
| 4 |
+
from typing import List, Dict, Any, Optional
|
| 5 |
+
|
| 6 |
+
class GameMoveTracker:
|
| 7 |
+
"""
|
| 8 |
+
In-memory tracker of all moves (for both players).
|
| 9 |
+
Does NOT reveal piece identities, only events like capture/bomb/flag.
|
| 10 |
+
Produces a clean text summary suitable for LLM prompt injection.
|
| 11 |
+
"""
|
| 12 |
+
|
| 13 |
+
def __init__(self):
|
| 14 |
+
self.turn: int = 0
|
| 15 |
+
self.history: List[Dict[str, Any]] = [] # global chronological history
|
| 16 |
+
|
| 17 |
+
# ---------------------------------------------------------
|
| 18 |
+
# RECORDING MOVES
|
| 19 |
+
# ---------------------------------------------------------
|
| 20 |
+
def record(
|
| 21 |
+
self,
|
| 22 |
+
player: int,
|
| 23 |
+
move: str,
|
| 24 |
+
event: Optional[str] = None, # "capture", "bomb", "flag", etc.
|
| 25 |
+
extra: Optional[str] = None # captured piece? "explosion"? etc.
|
| 26 |
+
):
|
| 27 |
+
"""
|
| 28 |
+
Store one full move entry, but without revealing piece identities.
|
| 29 |
+
"""
|
| 30 |
+
entry = {
|
| 31 |
+
"turn": self.turn,
|
| 32 |
+
"player": player,
|
| 33 |
+
"move": move, # e.g. "E4 F4"
|
| 34 |
+
"event": event, # optional special event
|
| 35 |
+
"extra": extra, # optional detail
|
| 36 |
+
"timestamp": datetime.datetime.now().isoformat(timespec="seconds"),
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
self.history.append(entry)
|
| 40 |
+
self.turn += 1
|
| 41 |
+
|
| 42 |
+
# ---------------------------------------------------------
|
| 43 |
+
# ACCESSORS
|
| 44 |
+
# ---------------------------------------------------------
|
| 45 |
+
def get_player_moves(self, pid: int) -> List[Dict[str, Any]]:
|
| 46 |
+
"""Moves made by a specific player."""
|
| 47 |
+
return [h for h in self.history if h["player"] == pid]
|
| 48 |
+
|
| 49 |
+
def get_opponent_moves(self, pid: int) -> List[Dict[str, Any]]:
|
| 50 |
+
"""Moves made by the opponent."""
|
| 51 |
+
return [h for h in self.history if h["player"] != pid]
|
| 52 |
+
|
| 53 |
+
def last_move(self) -> Optional[Dict[str, Any]]:
|
| 54 |
+
"""Most recent move."""
|
| 55 |
+
return self.history[-1] if self.history else None
|
| 56 |
+
|
| 57 |
+
# ---------------------------------------------------------
|
| 58 |
+
# GENERATE STRING FOR PROMPT
|
| 59 |
+
# ---------------------------------------------------------
|
| 60 |
+
def to_prompt_string(self, player_id: int) -> str:
|
| 61 |
+
"""
|
| 62 |
+
Return a clean string that summarizes the match so far.
|
| 63 |
+
Keeps ONLY the last 20 moves to avoid loops.
|
| 64 |
+
"""
|
| 65 |
+
|
| 66 |
+
if not self.history:
|
| 67 |
+
return "No previous moves have been played.\n"
|
| 68 |
+
|
| 69 |
+
# ➤ KEEP ONLY LAST 20 ENTRIES
|
| 70 |
+
MAX_HISTORY = 20
|
| 71 |
+
if len(self.history) > MAX_HISTORY:
|
| 72 |
+
# delete oldest until only 20 remain
|
| 73 |
+
self.history = self.history[-MAX_HISTORY:]
|
| 74 |
+
|
| 75 |
+
lines = ["Game History (most recent last):"]
|
| 76 |
+
|
| 77 |
+
for entry in self.history:
|
| 78 |
+
turn = entry["turn"]
|
| 79 |
+
pid = entry["player"]
|
| 80 |
+
move = entry["move"]
|
| 81 |
+
event = entry["event"]
|
| 82 |
+
extra = entry["extra"]
|
| 83 |
+
|
| 84 |
+
if pid == player_id:
|
| 85 |
+
prefix = f"Turn {turn}: You played {move}"
|
| 86 |
+
else:
|
| 87 |
+
prefix = f"Turn {turn}: Opponent played {move}"
|
| 88 |
+
|
| 89 |
+
if event == "capture":
|
| 90 |
+
prefix += " (capture occurred)"
|
| 91 |
+
elif event == "bomb":
|
| 92 |
+
prefix += " (bomb explosion)"
|
| 93 |
+
elif event == "flag":
|
| 94 |
+
prefix += " (FLAG CAPTURE — game-ending)"
|
| 95 |
+
elif event == "invalid":
|
| 96 |
+
prefix += " (invalid move?)"
|
| 97 |
+
|
| 98 |
+
if extra:
|
| 99 |
+
prefix += f" — {extra}"
|
| 100 |
+
|
| 101 |
+
lines.append(prefix)
|
| 102 |
+
|
| 103 |
+
return "\n".join(lines) + "\n"
|
| 104 |
+
|