Overview
The Mouse and Cats game is implemented as a single CLIPS expert system file (raton_y_gatos.clp) that uses rule-based programming to control game logic, AI strategy, and board rendering.
File Structure
The entire game is contained in a single.clp file organized into the following sections:
Single File Architecture
Unlike traditional procedural programs, this CLIPS implementation uses a declarative approach where:- All game state is stored as facts in working memory
- All behavior is defined as rules that pattern-match against facts
- Control flow emerges from rule activation and salience priorities
Control Flow
Game Initialization
The game starts when a user calls the(jugar) function:
raton_y_gatos.clp:21-26
Fact-Based Game State
The game uses two primary templates to represent state: 1. Casilla (Board Square) -raton_y_gatos.clp:76-87
raton_y_gatos.clp:89-104
Rule Activation and Firing
Rule Priority System
CLIPS uses salience to control rule execution order:| Salience | Rule | Purpose |
|---|---|---|
| 10 | encerrar-raton | Check if mouse is trapped (highest priority) |
| 0 (default) | Most game rules | Normal AI strategy |
| -10 | mover-gato-mas-alejado-raton | Fallback move when no strategy matches |
raton_y_gatos.clp:1325 (salience 10) and raton_y_gatos.clp:990 (salience -10)
Pattern Matching Process
Facts are asserted
When
(reset) is called, all facts from deffacts hechos-iniciales are loaded into working memory.Rules pattern-match
CLIPS examines all rules to find which patterns match the current facts in working memory.
Conflict resolution
If multiple rules match, CLIPS uses salience, recency, and specificity to choose which rule fires first.
Rule fires
The selected rule’s RHS (right-hand side) executes, which may:
- Assert new facts
- Retract existing facts
- Modify facts
- Print output
Game Loop Flow
Board Rendering System
Three-Part Rendering
The board rendering system (actualizar-tablero rule, lines 262-363) uses a clever 3-pass approach:
Each cell is rendered in three parts:
raton_y_gatos.clp:262-363
Rendering Algorithm
Initialize print state
Fact
(imprime 1 1) tracks current row/column being rendered.
Fact (parteImprimir 1) tracks which part (1, 2, or 3) to print.Match casilla with pieza
The rule matches
casilla facts with their corresponding pieza definition based on the valor field.Print part 2 of all columns
Retracts
(parteImprimir 1), asserts (parteImprimir 2), repeats for parte2.Print part 3 and advance row
Prints
parte3, then increments row counter and resets to (parteImprimir 1).Key Rendering Logic
AI Strategy Architecture
Strategy Rule Hierarchy
The cats’ AI uses multiple rules with different priorities:- Endgame (salience 10):
encerrar-raton- Trap the mouse if it has only one escape - Block formation:
ejecutar-movimiento-completar-fila-gatos- Advance cats as a wall - Semi-trap:
semi-encerrar-raton- Create pincer movements - Pattern-based:
cubrir-posible-avance-raton- Counter specific mouse positions - Fallback (salience -10):
mover-gato-mas-alejado-raton- Move furthest cat toward mouse
(fila-columna-mover-gatos ?row ?col ?catFact) which triggers the execution rule.
Control Facts
The system uses control facts to orchestrate rule firing:(ingresar-tablero)→ Triggers board initialization(actualizar-tablero)→ Triggers board rendering(pedir-movimiento-raton)→ Triggers player input(buscar-diagonales-gatos)→ Triggers cat diagonal calculation(finalizar-juego)→ Triggers game over
Key Design Patterns
Pattern: Control Fact
Rules use control facts to trigger specific behavior:Pattern: Fact Modification
Piece movement works by modifying casilla facts:raton_y_gatos.clp:530-536
Pattern: Fact Indexing
Cats are identified by their valor multislot:raton_y_gatos.clp:246-255
Performance Considerations
- 64 casilla facts are created at initialization (8x8 board)
- Pattern matching happens on every rule cycle
- Rendering iterates 8 rows × 8 columns × 3 parts = 192 rule firings per board display
- AI evaluation may test multiple strategies before finding a match