back to scratko.xyz
aboutsummaryrefslogtreecommitdiff
path: root/README.md
blob: 116b66f7d75b908e3cfef82301b6a4098650cd30 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# Pac-Man (Console Version with `ncurses`)

A terminal-based Pac-Man game written in C with `ncurses`.  It faithfully
replicates the original ghost AI from the classic 1980 arcade game, including
their behavioral modes, targeting rules, and unique pathfinding algorithms.

<img src = "pacman.png" />

This version contains one fixed level and implements core gameplay mechanics:
Pac-Man movement, ghost AI, lives, energizers (power pellets), frightened mode,
collision handling, and score tracking.

## Features

- **Classic Pac-Man logic** — core mechanics faithfully implemented
- **Ghost behavior inspired by the original game** — based on *The Pac-Man Dossier*
- **Mode switching** — scatter, chase, frightened
- **Unique AI logic per ghost** — each ghost uses different rules and pathfinding
- **Life system** — Pac-Man has limited lives
- **Power pellets** — enable Pac-Man to eat ghosts in frightened mode
- **Colored rendering** — `ncurses`-based graphics with different colors for each ghost and Pac-Man
- **Directional movement logic** — buffered input for responsive turns
- **Single static level** — pre-defined grid layout with maze walls and teleport tunnel
- **Maze collision and coin collection** — walls, tunnels, and dot eating logic
- **Pause support** — press spacebar to pause/resume the game

## Technologies Used

- **Language:** C (C99)
- **Graphics:** `ncurses`
- **Terminal:** ANSI-compatible
- **Manual memory management**, no external libraries beyond `ncurses`

## Controls

- Arrow keys — Move Pac-Man
- ESC — Quit the game
- Spacebar - Pause

## Ghost AI Logic

Each ghost behaves differently depending on the current game mode:

### Game Modes:

- **Chase** — Ghosts pursue targets based on individual AI
- **Scatter** — Ghosts retreat to their fixed corners
- **Frightened** — Ghosts move randomly after Pac-Man eats a power pellet
- **Returning** — Ghosts return to their house after being eaten

### Algorithms Used

- **BFS (Breadth-First Search)** — used for actual pathfinding on the map
- **Manhattan Distance** — used for heuristic target evaluation
- **Random Choice** — used when ghosts are frightened

### Ghost Behaviors:

#### Blinky (Red)
- **Behavior:** Always targets Pac-Man's current tile in chase mode
- **Algorithm:** BFS

#### Pinky (Pink)
- **Behavior:** Targets the tile 2 cells ahead of Pac-Man in his current direction
- **Algorithm:** Manhattan distance

#### Inky (Cyan)
- **Behavior:**

    1. Computes the tile two cells ahead of Pac-Man
    2. Calculates a vector from Blinky's position to that tile
    3. Doubles the vector to determine the target

- **Algorithm:** Manhattan distance

#### Clyde (Orange)
- **Behavior:**

    - If farther than 8 tiles from Pac-Man: behaves like Blinky (chases)
    - If closer: retreats to scatter corner

- **Algorithm:** Manhattan distance and BFS hybrid

## Build Instructions

Install `ncurses` (if needed):
```bash
sudo apt install libncurses5-dev libncursesw5-dev
```
Then:

```bash
git clone https://git.scratko.xyz/pacman
cd pacman
make
./pacman

```
Windows version: <a href="https://scratko.xyz/games/pacman.exe" target="_blank">Download</a>