Skip to content

Commit

Permalink
推箱子v0.0.3
Browse files Browse the repository at this point in the history
改进了fscanf函数,增强了安全🔐性
更新了README及目录
修复了一些bug
  • Loading branch information
LanYunDev committed May 7, 2022
1 parent 56cf6f5 commit 69821f2
Show file tree
Hide file tree
Showing 10 changed files with 28 additions and 120 deletions.
5 changes: 1 addition & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,4 @@ include_directories(${CURSES_INCLUDE_DIR})

add_executable(PushBox main.c)
target_link_libraries(PushBox ${CURSES_LIBRARY})
add_executable(test test.c)
target_link_libraries(test ${CURSES_LIBRARY})
add_executable(test2 test2.c)
target_link_libraries(test2 ${CURSES_LIBRARY})

7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
目前实现的功能有:

1. 已实现在无限步数下的撤回操作

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

## 如何运行?

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

File renamed without changes
File renamed without changes.
File renamed without changes.
2 changes: 0 additions & 2 deletions cmake-build-debug/CMakeFiles/TargetDirectories.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
/Users/lanyun/CLionProjects/PushBox/cmake-build-debug/CMakeFiles/PushBox.dir
/Users/lanyun/CLionProjects/PushBox/cmake-build-debug/CMakeFiles/test.dir
/Users/lanyun/CLionProjects/PushBox/cmake-build-debug/CMakeFiles/test2.dir
/Users/lanyun/CLionProjects/PushBox/cmake-build-debug/CMakeFiles/edit_cache.dir
/Users/lanyun/CLionProjects/PushBox/cmake-build-debug/CMakeFiles/rebuild_cache.dir
38 changes: 0 additions & 38 deletions cmake-build-debug/CMakeFiles/rules.ninja
Original file line number Diff line number Diff line change
Expand Up @@ -30,44 +30,6 @@ rule C_EXECUTABLE_LINKER__PushBox_Debug
restat = $RESTAT


#############################################
# Rule for compiling C files.

rule C_COMPILER__test_Debug
depfile = $DEP_FILE
deps = gcc
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
description = Building C object $out


#############################################
# Rule for linking C executable.

rule C_EXECUTABLE_LINKER__test_Debug
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
description = Linking C executable $TARGET_FILE
restat = $RESTAT


#############################################
# Rule for compiling C files.

rule C_COMPILER__test2_Debug
depfile = $DEP_FILE
deps = gcc
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
description = Building C object $out


#############################################
# Rule for linking C executable.

rule C_EXECUTABLE_LINKER__test2_Debug
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
description = Linking C executable $TARGET_FILE
restat = $RESTAT


#############################################
# Rule for running custom commands.

Expand Down
4 changes: 2 additions & 2 deletions cmake-build-debug/Testing/Temporary/LastTest.log
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Start testing: May 07 00:09 CST
Start testing: May 07 11:12 CST
----------------------------------------------------------
End testing: May 07 00:09 CST
End testing: May 07 11:12 CST
66 changes: 1 addition & 65 deletions cmake-build-debug/build.ninja
Original file line number Diff line number Diff line change
Expand Up @@ -72,70 +72,6 @@ build PushBox: C_EXECUTABLE_LINKER__PushBox_Debug CMakeFiles/PushBox.dir/main.c.
TARGET_FILE = PushBox
TARGET_PDB = PushBox.dbg

# =============================================================================
# Object build statements for EXECUTABLE target test


#############################################
# Order-only phony target for test

build cmake_object_order_depends_target_test: phony || CMakeFiles/test.dir

build CMakeFiles/test.dir/test.c.o: C_COMPILER__test_Debug /Users/lanyun/CLionProjects/PushBox/test.c || cmake_object_order_depends_target_test
DEP_FILE = CMakeFiles/test.dir/test.c.o.d
FLAGS = -g -arch arm64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk -std=gnu2x
OBJECT_DIR = CMakeFiles/test.dir
OBJECT_FILE_DIR = CMakeFiles/test.dir


# =============================================================================
# Link build statements for EXECUTABLE target test


#############################################
# Link the executable test

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
FLAGS = -g -arch arm64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk
LINK_LIBRARIES = /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/usr/lib/libcurses.tbd
OBJECT_DIR = CMakeFiles/test.dir
POST_BUILD = :
PRE_LINK = :
TARGET_FILE = test
TARGET_PDB = test.dbg

# =============================================================================
# Object build statements for EXECUTABLE target test2


#############################################
# Order-only phony target for test2

build cmake_object_order_depends_target_test2: phony || CMakeFiles/test2.dir

build CMakeFiles/test2.dir/test2.c.o: C_COMPILER__test2_Debug /Users/lanyun/CLionProjects/PushBox/test2.c || cmake_object_order_depends_target_test2
DEP_FILE = CMakeFiles/test2.dir/test2.c.o.d
FLAGS = -g -arch arm64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk -std=gnu2x
OBJECT_DIR = CMakeFiles/test2.dir
OBJECT_FILE_DIR = CMakeFiles/test2.dir


# =============================================================================
# Link build statements for EXECUTABLE target test2


#############################################
# Link the executable test2

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
FLAGS = -g -arch arm64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk
LINK_LIBRARIES = /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/usr/lib/libcurses.tbd
OBJECT_DIR = CMakeFiles/test2.dir
POST_BUILD = :
PRE_LINK = :
TARGET_FILE = test2
TARGET_PDB = test2.dbg


#############################################
# Utility command for edit_cache
Expand Down Expand Up @@ -170,7 +106,7 @@ build rebuild_cache: phony CMakeFiles/rebuild_cache.util
#############################################
# Folder: /Users/lanyun/CLionProjects/PushBox/cmake-build-debug

build all: phony PushBox test test2
build all: phony PushBox

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

check = 0;//check标志置零

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




#pragma clang diagnostic pop

0 comments on commit 69821f2

Please sign in to comment.