Mar. 14, 2025
CS61c Proj1 Snake
Overview
游戏的所有对象都由一个或多个特定字符来表示
##############
# #
# dv #
# v # #
# v # #
# s >>D # #
# v # #
# *A< * # #
# #
##############
main() {
Read board from file or stdin or create a default board
update game
write updated board to file or stdout
}
struct
typedef struct snake_t {
unsigned int tail_row;
unsigned int tail_col;
unsigned int head_row;
unsigned int head_col;
bool live;
} snake_t;
typedef struct game_t {
unsigned int num_rows;
char **board;
unsigned int num_snakes;
snake_t *snakes;
} game_t;
update_game()
board file预设了所有对象的位置:蛇,水果,墙
在加载board之后,一时刻的游戏运行逻辑:
蛇的移动实际上只需要更新头部和尾部的位置,即
update_head(), update_tail()
两个函数根据下一刻的位置是空地,墙还是水果做不同的更新
而这两个函数的实现有许多helper完成,大概来说有: