Skip to content

Commit 69821f2

Browse files
committed
推箱子v0.0.3
改进了fscanf函数,增强了安全🔐性 更新了README及目录 修复了一些bug
1 parent 56cf6f5 commit 69821f2

10 files changed

Lines changed: 28 additions & 120 deletions

File tree

CMakeLists.txt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,4 @@ include_directories(${CURSES_INCLUDE_DIR})
88

99
add_executable(PushBox main.c)
1010
target_link_libraries(PushBox ${CURSES_LIBRARY})
11-
add_executable(test test.c)
12-
target_link_libraries(test ${CURSES_LIBRARY})
13-
add_executable(test2 test2.c)
14-
target_link_libraries(test2 ${CURSES_LIBRARY})
11+

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
目前实现的功能有:
1818

1919
1. 已实现在无限步数下的撤回操作
20+
2021
~~基于链表的有限步数下的单次回滚(撤回)等操作 (通俗点说,就是不支持多次撤回,撤回的前一步不能是撤回步骤)~~
2122
2. 基于ncurses.h库而实现的窗口和方向键等功能 (本程序多次使用这个库里面的函数,建议了解并学习后使用它)
2223
3. 在玩过的关卡中可自由选择关卡
@@ -25,7 +26,8 @@
2526
6. 支持数据文件是否合法等的检查
2627
7. 有通过关卡到下一关的提示界面
2728
8. 有结束游戏开始保存数据的界面
28-
9. 等等…
29+
9. 关卡数和关卡游玩时间显示功能
30+
10. 等等…
2931

3032
## 如何运行?
3133

@@ -56,6 +58,5 @@ gcc main.c -o PushBox -lncurses
5658
2. 无限回滚操作 已完成✅--本人实现
5759
3. 自动生成地图文件
5860
4. 优化代码及其性能
59-
5. 修复fscanf安全🔐问题
60-
6. 等等…
61+
5. 等等…
6162

File renamed without changes.
File renamed without changes.
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
/Users/lanyun/CLionProjects/PushBox/cmake-build-debug/CMakeFiles/PushBox.dir
2-
/Users/lanyun/CLionProjects/PushBox/cmake-build-debug/CMakeFiles/test.dir
3-
/Users/lanyun/CLionProjects/PushBox/cmake-build-debug/CMakeFiles/test2.dir
42
/Users/lanyun/CLionProjects/PushBox/cmake-build-debug/CMakeFiles/edit_cache.dir
53
/Users/lanyun/CLionProjects/PushBox/cmake-build-debug/CMakeFiles/rebuild_cache.dir

cmake-build-debug/CMakeFiles/rules.ninja

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -30,44 +30,6 @@ rule C_EXECUTABLE_LINKER__PushBox_Debug
3030
restat = $RESTAT
3131

3232

33-
#############################################
34-
# Rule for compiling C files.
35-
36-
rule C_COMPILER__test_Debug
37-
depfile = $DEP_FILE
38-
deps = gcc
39-
command = /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc $DEFINES $INCLUDES $FLAGS -MD -MT $out -MF $DEP_FILE -o $out -c $in
40-
description = Building C object $out
41-
42-
43-
#############################################
44-
# Rule for linking C executable.
45-
46-
rule C_EXECUTABLE_LINKER__test_Debug
47-
command = $PRE_LINK && /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc $FLAGS -Wl,-search_paths_first -Wl,-headerpad_max_install_names $LINK_FLAGS $in -o $TARGET_FILE $LINK_PATH $LINK_LIBRARIES && $POST_BUILD
48-
description = Linking C executable $TARGET_FILE
49-
restat = $RESTAT
50-
51-
52-
#############################################
53-
# Rule for compiling C files.
54-
55-
rule C_COMPILER__test2_Debug
56-
depfile = $DEP_FILE
57-
deps = gcc
58-
command = /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc $DEFINES $INCLUDES $FLAGS -MD -MT $out -MF $DEP_FILE -o $out -c $in
59-
description = Building C object $out
60-
61-
62-
#############################################
63-
# Rule for linking C executable.
64-
65-
rule C_EXECUTABLE_LINKER__test2_Debug
66-
command = $PRE_LINK && /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc $FLAGS -Wl,-search_paths_first -Wl,-headerpad_max_install_names $LINK_FLAGS $in -o $TARGET_FILE $LINK_PATH $LINK_LIBRARIES && $POST_BUILD
67-
description = Linking C executable $TARGET_FILE
68-
restat = $RESTAT
69-
70-
7133
#############################################
7234
# Rule for running custom commands.
7335

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
Start testing: May 07 00:09 CST
1+
Start testing: May 07 11:12 CST
22
----------------------------------------------------------
3-
End testing: May 07 00:09 CST
3+
End testing: May 07 11:12 CST

cmake-build-debug/build.ninja

Lines changed: 1 addition & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -72,70 +72,6 @@ build PushBox: C_EXECUTABLE_LINKER__PushBox_Debug CMakeFiles/PushBox.dir/main.c.
7272
TARGET_FILE = PushBox
7373
TARGET_PDB = PushBox.dbg
7474

75-
# =============================================================================
76-
# Object build statements for EXECUTABLE target test
77-
78-
79-
#############################################
80-
# Order-only phony target for test
81-
82-
build cmake_object_order_depends_target_test: phony || CMakeFiles/test.dir
83-
84-
build CMakeFiles/test.dir/test.c.o: C_COMPILER__test_Debug /Users/lanyun/CLionProjects/PushBox/test.c || cmake_object_order_depends_target_test
85-
DEP_FILE = CMakeFiles/test.dir/test.c.o.d
86-
FLAGS = -g -arch arm64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk -std=gnu2x
87-
OBJECT_DIR = CMakeFiles/test.dir
88-
OBJECT_FILE_DIR = CMakeFiles/test.dir
89-
90-
91-
# =============================================================================
92-
# Link build statements for EXECUTABLE target test
93-
94-
95-
#############################################
96-
# Link the executable test
97-
98-
build test: C_EXECUTABLE_LINKER__test_Debug CMakeFiles/test.dir/test.c.o | /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/usr/lib/libcurses.tbd
99-
FLAGS = -g -arch arm64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk
100-
LINK_LIBRARIES = /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/usr/lib/libcurses.tbd
101-
OBJECT_DIR = CMakeFiles/test.dir
102-
POST_BUILD = :
103-
PRE_LINK = :
104-
TARGET_FILE = test
105-
TARGET_PDB = test.dbg
106-
107-
# =============================================================================
108-
# Object build statements for EXECUTABLE target test2
109-
110-
111-
#############################################
112-
# Order-only phony target for test2
113-
114-
build cmake_object_order_depends_target_test2: phony || CMakeFiles/test2.dir
115-
116-
build CMakeFiles/test2.dir/test2.c.o: C_COMPILER__test2_Debug /Users/lanyun/CLionProjects/PushBox/test2.c || cmake_object_order_depends_target_test2
117-
DEP_FILE = CMakeFiles/test2.dir/test2.c.o.d
118-
FLAGS = -g -arch arm64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk -std=gnu2x
119-
OBJECT_DIR = CMakeFiles/test2.dir
120-
OBJECT_FILE_DIR = CMakeFiles/test2.dir
121-
122-
123-
# =============================================================================
124-
# Link build statements for EXECUTABLE target test2
125-
126-
127-
#############################################
128-
# Link the executable test2
129-
130-
build test2: C_EXECUTABLE_LINKER__test2_Debug CMakeFiles/test2.dir/test2.c.o | /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/usr/lib/libcurses.tbd
131-
FLAGS = -g -arch arm64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk
132-
LINK_LIBRARIES = /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/usr/lib/libcurses.tbd
133-
OBJECT_DIR = CMakeFiles/test2.dir
134-
POST_BUILD = :
135-
PRE_LINK = :
136-
TARGET_FILE = test2
137-
TARGET_PDB = test2.dbg
138-
13975

14076
#############################################
14177
# Utility command for edit_cache
@@ -170,7 +106,7 @@ build rebuild_cache: phony CMakeFiles/rebuild_cache.util
170106
#############################################
171107
# Folder: /Users/lanyun/CLionProjects/PushBox/cmake-build-debug
172108

173-
build all: phony PushBox test test2
109+
build all: phony PushBox
174110

175111
# =============================================================================
176112
# Built-in targets

main.c

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
//禁用fscanf的警告.因为在文件无误的情况下不存在问题,所以不需要警告.
2+
#pragma clang diagnostic push
3+
#pragma ide diagnostic ignored "cert-err34-c"
14
//推箱子小游戏 Powered By LanYun
25
#include <ncurses.h>//对于代码内容关于ncurses.h库那些函数不太明白可以看看我写的[这篇文章](https://lanyundev.vercel.app/posts/a5945d21.html)噢
36
#include <stdlib.h>//声明了数值与字符串转换函数, 伪随机数生成函数, 动态内存分配函数, 进程控制函数等公共函数.
@@ -62,7 +65,7 @@ long Latest_Level = 1;//最新关卡
6265
long Total_Level = 14;//总关卡数
6366
long Remain_Box = 1;//剩余箱子数
6467
long Tmp = 0;//用于存储临时Level
65-
int check = 0;//退出标识的作用,及其检测是否有撤回操作
68+
int check;//退出标识的作用,及其检测是否有撤回操作
6669
int x, y;//玩家位置
6770
int ch;//获取输入内容
6871
time_t Level_start, Level_end;//记录关卡开始和结束时间
@@ -102,10 +105,10 @@ void Init() {
102105
FILE_FIX:
103106
sleep(0);//等待0秒
104107
FILE *fp = fopen("data.txt", "r");//只读打开data.txt文件📃
105-
if (fp == NULL) {//如果打开失败
108+
if (fp == NULL || check == 4) {//如果打开失败
106109
box(PushBox, 0, 0);//创建box窗口
107110
wrefresh(PushBox);//使box窗口生效
108-
mvprintw(offset_y + WORLD_HEIGHT / 2 - 6, offset_x + 8, "未能打开或不存在data.txt数据文件!");
111+
mvprintw(offset_y + WORLD_HEIGHT / 2 - 6, offset_x + 7, "未能打开或data.txt数据文件存在错误!");
109112
mvprintw(offset_y + WORLD_HEIGHT / 2 - 4, offset_x + 8, "尝试自动生成默认data.txt数据文件!");
110113
refresh(); //将虚拟屏幕上的内容写到显示屏上,并且刷新窗口
111114
fclose(fp);//关闭文件📃
@@ -116,9 +119,15 @@ void Init() {
116119
sleep(1);//等待1秒
117120
goto FILE_FIX;//重新回到只读打开
118121
}
119-
fscanf(fp, "Top_Level:%ld Latest_Level:%ld", &Top_Level, &Latest_Level);//读取文件.⚠️:此处,处在一处安全🔐性警告⚠️,但因无能修复而不得不使用。
122+
if (fscanf(fp, "Top_Level:%ld Latest_Level:%ld", &Top_Level, &Latest_Level) !=
123+
2) { //读取文件.⚠️:此处,处在一处安全🔐性警告⚠️,但因无能修复而不得不使用。
124+
check = 4;//标志为文件📃需重新生成
125+
goto FILE_FIX;
126+
}
120127
fclose(fp);//关闭文件📃
121128

129+
check = 0;//check标志置零
130+
122131
//检查最高数据是否异常
123132
if (Top_Level < 1 || Top_Level > Total_Level) {
124133
box(PushBox, 0, 0);//创建box窗口
@@ -278,17 +287,20 @@ Position *Draw(Position *p) {
278287
//读入地图,并将地图中的数字转换为字符,存入地图数组中,方便绘制。
279288
FILE *fp = fopen("map.txt", "r");//打开文件
280289
if (fp == NULL) {
290+
FILE_Error:
281291
box(PushBox, 0, 0);//创建box窗口
282292
wrefresh(PushBox);//使box窗口生效
283-
mvprintw(offset_y + WORLD_HEIGHT / 2 - 6, offset_x + 10, "未能打开或不存在map.txt地图文件!");
293+
mvprintw(offset_y + WORLD_HEIGHT / 2 - 6, offset_x + 7, "未能打开或map.txt地图文件存在错误!");
284294
refresh(); //将虚拟屏幕上的内容写到显示屏上,并且刷新窗口
285295
sleep(1);//等待1秒
286296
exit(1);
287297
}
288298
fseek(fp, (16L * 8 + 1) * (Level - 1), SEEK_SET);//移动文件指针
289299
for (int i = 0; i < HEIGHT; i++) {
290300
for (int j = 0; j < WIDTH; j++) {
291-
fscanf(fp, "%1d", &map[i][j]); //读取文件.⚠️:此处,处在一处安全🔐性警告⚠️,但因无能修复而不得不使用。
301+
if (fscanf(fp, "%1d", &map[i][j]) != 1) { //读取文件.⚠️:此处,处在一处安全🔐性警告⚠️,但因无能修复而不得不使用。
302+
goto FILE_Error;
303+
}
292304
}
293305
}
294306
fclose(fp);//关闭文件
@@ -849,3 +861,5 @@ Position *moveBack(Position *head, Position *p) {
849861

850862

851863

864+
865+
#pragma clang diagnostic pop

0 commit comments

Comments
 (0)