RBE104TC C语言 吃豆人[2025-10-21]

小组项目 - 吃豆人(文本版,仅使用标准C语言)
占总成绩比例:35%
发布日期:
提交截止日期:(待定)

项目概述

使用标准C语言开发一款可在终端运行的文本版吃豆人游戏。游戏采用ASCII字符显示迷宫、吃豆人、幽灵和豆子,除标准C库外,无需任何图形库或外部库。该项目以小组形式完成,需体现合理的模块化设计、清晰的规则以及全面的测试。


核心要求

1. 地图与渲染

  • 以二维字符数组形式表示迷宫(例如,最大尺寸为25×40)。使用以下ASCII符号:# 代表墙壁,. 代表普通豆子,O 代表能量豆(可选,详见“扩展功能”),P 代表吃豆人,G 代表幽灵,空格代表空白区域。
  • 每一轮游戏循环(tick)都将游戏面板渲染到控制台,显示方式保持简洁,按从上到下的顺序打印各行内容。
  • 在代码中嵌入一个固定地图即可满足要求(可选开发单独的地图加载器)。

2. 控制与游戏循环

  • 采用回合制/轮次制循环:每一轮中,读取玩家输入(W/A/S/D键控制移动,Q键退出游戏)。
  • 若玩家意图移动的方向是墙壁,吃豆人保持原地不动(不执行移动操作)。
  • 吃豆人移动后,先执行“吃豆”操作(增加分数),随后每个幽灵各移动一次。
  • 打印更新后的游戏面板以及简要状态信息(分数、剩余生命数、剩余豆子数量、当前轮次)。

3. 幽灵(简易人工智能)

初始设置1-4个幽灵(G ∈{1,2,3,4})。每一轮游戏循环中,每个幽灵会从上下左右四个方向中,随机选择一个不撞墙的有效方向移动。若存在多个有效方向,采用均匀随机的方式选择。

  • 可选优化规则:除非无其他选择,否则避免反向移动(减少幽灵的抖动现象)。

4. 碰撞、分数与生命

  • 豆子机制:吃豆人每吃到一颗普通豆子,得10分,并从地图中移除该豆子。
  • 幽灵碰撞机制:当幽灵处于正常状态时,若幽灵移动到吃豆人所在位置(或反之),吃豆人损失一条生命并在初始位置重生,幽灵返回各自初始位置,已被吃掉的豆子保持消失状态。
  • 生命机制:初始拥有3条生命,当生命数降至0时,游戏结束(玩家失败)。
  • 胜利条件:玩家吃掉所有豆子即获胜。

5. 计时与确定性

  • 游戏基于轮次运行,每一轮可仅等待玩家输入(无需实时计时功能)。
  • 随机性元素(如幽灵移动、可选的水果生成等)需支持可复现,允许用户通过指定种子(例如,命令行参数)控制。

推荐数据结构

  • struct Pos { int r, c; };(用于表示位置,r为行号,c为列号)
  • struct Entity { struct Pos pos; struct Pos start; int dr, dc; };(用于表示吃豆人和每个幽灵,pos为当前位置,start为初始位置,dr和dc为方向增量)
  • struct Game { int rows, cols; char board[25][40]; struct Entity pac; struct Entity ghosts[4]; int ghostCount; int score; int lives; int pelletsRemaining; unsigned int rng seed; };(用于表示游戏整体状态,rows和cols为地图行列数,board为地图数组,pac为吃豆人实体,ghosts为幽灵数组,ghostCount为幽灵数量,score为分数,lives为剩余生命数,pelletsRemaining为剩余豆子数,rng seed为随机数种子)

示例最小地图(嵌入为字符数组)

########################
#..........##..........#
#.####.###.##.###.####.#
#O#   #.#   #.#   #O.#
#.####.#.#####.#.#####.#
#......#...P...#...... #
#.####.#.#####.#.#####.#
#.#   #.#   G   G   #.# #.#
#.####.###.##.###.####.#
########################

(P和G的位置在运行时会被实体覆盖,空格代表通道。可根据数组边界调整地图的行列数。)


推荐团队分工

角色 职责
地图与渲染 负责游戏面板的表示、ASCII字符渲染以及豆子数量统计
玩家控制与规则 负责输入处理、移动逻辑、吃豆机制以及胜负判定逻辑
幽灵逻辑 负责幽灵的随机移动、有效移动方向选择以及与吃豆人的碰撞检测
游戏状态与测试 负责分数与生命的管理、种子与配置设置、测试场景设计以及模块集成

约束与说明

  • 仅可使用实验室机器上可用的标准C语言头文件(例如,stdio.h、stdlib.h、string.h、ctype.h、time.h)。
  • 简化编译过程,例如可使用命令 gcc *.c -o pacman 进行编译。
  • 清屏功能为可选,每一轮仅打印更新后的面板也符合要求。
  • 确保输入验证功能(忽略无效按键,避免因遇到文件结束符(EOF)而崩溃)。

可选扩展功能(用于获取更高分数)

  • 能量豆:吃到 O 后,幽灵在T轮内进入“恐惧”状态,此时吃豆人可吃掉幽灵并获得额外分数,被吃掉的幽灵延迟一段时间后在初始位置重生。
  • 幽灵简易模式:交替执行N轮“追逐”模式(倾向于向吃豆人方向移动)和M轮“分散”模式(倾向于向角落移动),移动逻辑采用启发式算法(仍需使用标准C语言实现)。
  • 水果奖励:偶尔生成奖励物品,吃掉可获得额外分数;若未在指定轮次内吃掉,奖励物品会消失。
  • 多关卡:吃掉当前关卡所有豆子后,加载第二个预先编写的地图,该地图可设置更多幽灵或更密集的墙壁。
  • 地图加载器:从纯文本文件中读取地图(是否允许需遵从小组成员意见)。

测试与报告

至少提供两次确定性运行测试(使用固定种子),分别展示:(1)胜利条件达成;(2)因幽灵碰撞损失生命;(3)豆子数量统计准确性。

  • 报告需包含:数据结构说明、状态图/流程图、碰撞处理逻辑,以及测试证明(截图或控制台日志)。

提交指南

  • 以单个压缩文件(.zip格式)提交。
  • 压缩文件需包含:报告(PDF格式)、源代码,以及简短的编译/运行指南。
  • 文件命名格式:小组ID_PacmanC.zip

评分标准(摘要)

  • 设计:15%
  • 代码实现:45%
  • 健壮性:10%
  • 测试:25%
  • 报告质量:5%

要不要我帮你整理一份项目核心要求与分工对照表?这样你在分配任务时能更清晰地对应每个职责和具体开发内容。

RBE104TC C Programming Language

Group Project — Pac-Man (Text-Based, Standard C Only)

  • Contribution to the Overall Marks: 35%
  • Issue Date:
  • Submission Deadline: (to be announced)

Assignment Overview

Develop a text-based Pac-Man game in standard C, playable in the terminal. The game uses ASCII characters to display the maze, Pac-Man, ghosts, and pellets. No graphics or external libraries are required beyond the standard C library. The project is group-based and should demonstrate sensible modular design, clear rules, and thorough testing.

Core Requirements

1) Map & Rendering

  • Represent the maze as a 2D char array (e.g. up to 25 × 40). Use ASCII symbols: (# =) wall, . = pellet, O = power pellet (optional, see Extensions), P = Pac-Man, G = ghost, space = empty.
  • Render the board to the console each tick. Keep the display simple (print lines top to bottom).
  • A single fixed map embedded in code is sufficient. (A separate map loader is optional.)

2) Controls & Game Loop

  • Turn/tick based loop: each tick, read player input (W/A/S/D to move; Q to quit).
  • If the intended move hits a wall, Pac-Man stays in place (no move).
  • After Pac-Man moves: eat pellet (increase score), then each ghost moves once.
  • Print updated board and brief status (score, lives, remaining pellets, tick).

3) Ghosts (Simple AI)

Start with (G \in{1,2,3,4}) ghosts. Each tick, every ghost chooses a random valid direction among {up,down,left,right} that is not a wall. If multiple are valid, pick uniformly at random.

  • Optional rule of thumb: avoid reversing direction unless no alternative (reduces jitter).

4) Collisions, Score, Lives

  • Pellets: Pac-Man earns +10 points per pellet. (Remove the pellet from the map.)
  • Ghost collision: If a ghost moves onto Pac-Man (or vice versa) while ghosts are normal, Pac-Man loses one life and respawns at the starting cell; ghosts return to their starting cells. The current pellets remain eaten.
  • Lives: Start with 3 lives. When lives reach 0, the game ends (lose).
  • Win: The player wins when all pellets are eaten.

5) Timing & Determinism

  • The game is tick-based; you may simply wait for user input each tick (no real-time timing needed).
  • Randomness (ghost movement, optional fruit, etc.) should be reproducible by allowing a user-specified seed (e.g. CLI argument).

Suggested Data Structures

  • struct Pos { int r, c; };
  • struct Entity { struct Pos pos; struct Pos start; int dr, dc; }; (for Pac-Man and each ghost)
  • struct Game { int rows, cols; char board[25][40]; struct Entity pac; struct Entity ghosts[4]; int ghostCount; int score; int lives; int pelletsRemaining; unsigned int rng seed; };

Example Minimal Map (embed as char array)

######################## 
#..........##..........# 
#.####.###.##.###.####.# 
#O#   #.#   #.#   #O.#   
#.####.#.#####.#.#####.# 
#......#...P...#...... # 
#.####.#.#####.#.#####.# 
#.#   #.#   G   G   #.# #.# 
#.####.###.##.###.####.# 
########################

(P and G positions will be overwritten at runtime by entities; spaces represent corridors. You may adjust rows/cols to fit your array bounds.)

Teamwork Structure (Recommended)

Role Responsibilities
Map & Rendering Board representation; ASCII rendering; pellet counting
Player Control & Rules Input handling; movement; pellet eating; win/lose logic
Ghost Logic Random movement; valid move selection; collision with Pac-Man
Game State & Testing Score/lives; seeds/config; test scenarios; integration

Constraints & Notes

  • Use only standard C headers available on the lab machines (e.g. stdio.h, stdlib.h, string.h, ctype.h, time.h).
  • Keep builds simple: e.g. gcc *.c -o pacman.
  • Clear screen is optional; a simple print-each-tick is acceptable.
  • Ensure input validation (ignore invalid keys; do not crash on EOF).

Optional Extensions (for higher marks)

  • Power Pellets: O turns ghosts “scared” for T ticks; Pac-Man can eat ghosts for bonus points. Eaten ghosts respawn at start after a delay.
  • Simple Ghost Modes: Alternate N ticks of “chase” (bias towards Pac-Man) and M ticks of “scatter” (bias towards corners) using heuristic moves (still standard C).
  • Fruit Bonus: Occasionally spawn a bonus item for extra points; expires after a few ticks if not eaten.
  • Multiple Levels: After clearing pellets, load a second hardcoded map with more ghosts or denser walls.
  • Map Loader: Read a map from a plain text file (optional if allowed by staff).

Testing & Reporting

Provide at least two deterministic runs (fixed seeds) demonstrating: (1) win condition, (2) losing a life to a ghost, (3) pellet counting accuracy.

  • Report should include: data structures, state diagram/flow, how collisions are handled, and evidence of testing (screenshots or console logs).

Submission Guidelines

  • Submit as a single .zip.
  • Include: Report (PDF), Source Code, and a short build/run guide.
  • File naming: GroupID PacmanC.zip

Marking Scheme (Summary)

  • Design: 15%
  • Coding Implementation: 45%
  • Robustness: 10%
  • Testing: 25%
  • Report Quality: 5%

源码联系UP主 -> https://space.bilibili.com/329101171

Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐