From 76c0ffb5d5a442f2ac6fb0876ef31d84d91097fa Mon Sep 17 00:00:00 2001 From: Inochi Amaoto Date: Fri, 16 Mar 2018 20:10:55 +0800 Subject: [PATCH] finish main support --- puzzle.gen.cpp | 42 +++---- puzzle.main.cpp | 301 +++++++++++++++++++++++++++++------------------- 2 files changed, 206 insertions(+), 137 deletions(-) diff --git a/puzzle.gen.cpp b/puzzle.gen.cpp index 7ad34b3..6444462 100644 --- a/puzzle.gen.cpp +++ b/puzzle.gen.cpp @@ -4,12 +4,12 @@ namespace { - void print_header(unsigned long __in) noexcept + void print_header(unsigned long in) noexcept { using std::printf; - printf("#ifndef __PUZZ_DEFAULT_MAP_%lu__\n", __in); - printf("#define __PUZZ_DEFAULT_MAP_%lu__\n", __in); + printf("#ifndef __PUZZ_DEFAULT_MAP_%lu__\n", in); + printf("#define __PUZZ_DEFAULT_MAP_%lu__\n", in); printf("\n"); printf("#include\"puzzle_fwd.hpp\"\n"); printf("\n"); @@ -18,32 +18,32 @@ namespace printf("{\n"); printf(" namespace build_in\n"); printf(" {\n"); - printf(" void map_%lu_data(matrix& __map);\n", __in); - printf(" countdown map_%lu_time() noexcept;\n", __in); - printf(" void map_%lu_mapping(std::map& __mapping);\n", __in); + printf(" void map_%lu_data(matrix& __map);\n", in); + printf(" countdown map_%lu_time() noexcept;\n", in); + printf(" void map_%lu_mapping(std::map& __mapping);\n", in); printf(" }\n"); printf("}\n"); printf("\n"); - printf("#endif // ! __PUZZ_DEFAULT_MAP_%lu__\n", __in); + printf("#endif // ! __PUZZ_DEFAULT_MAP_%lu__\n", in); printf("\n"); } - void print_map_data_header(unsigned long __in) noexcept + void print_map_data_header(unsigned long in) noexcept { using std::printf; - printf("#include\"map_%lu.h\"\n", __in); + printf("#include\"map_%lu.h\"\n", in); printf("\n\n"); } void print_map_data( - const game::matrix& __map, unsigned long __in + const game::matrix& __map, unsigned long in ) noexcept { using std::printf; printf( "void game::build_in::map_%lu_data(matrix& __map)\n", - __in + in ); printf("{\n"); printf(" __map.resize(%4lu, %4lu);\n", __map.row(), __map.col()); @@ -94,14 +94,14 @@ namespace } void print_map_time( - const game::countdown& __now, unsigned long __in + const game::countdown& __now, unsigned long in ) noexcept { using std::printf; printf( "game::countdown game::build_in::map_%lu_time() noexcept\n", - __in + in ); printf( "{ return game::countdown{%hu, %hu, %hu};}\n", @@ -111,14 +111,14 @@ namespace } void print_map_mapping( const std::map& __mapping, - unsigned long __in + unsigned long in ) noexcept { using std::printf; printf( "void game::build_in::map_%lu_mapping(std::map& __mapping)\n", - __in + in ); printf("{\n"); for(const std::pair& __tmp: __mapping) @@ -141,15 +141,15 @@ int main(int argc, char* argv[]) if(argc != 2) { return 0;} - unsigned long __in = 0; + unsigned long in = 0; game::puzzle puzz; if(puzz.load_file(argv[1])) { - print_header(__in); - print_map_data_header(__in); - print_map_data(puzz.map_data(), __in); - print_map_time(puzz.time_data(), __in); - print_map_mapping(puzz.mapping_data(), __in); + print_header(in); + print_map_data_header(in); + print_map_data(puzz.map_data(), in); + print_map_time(puzz.time_data(), in); + print_map_mapping(puzz.mapping_data(), in); } return 0; diff --git a/puzzle.main.cpp b/puzzle.main.cpp index 3847ced..b3fad7e 100644 --- a/puzzle.main.cpp +++ b/puzzle.main.cpp @@ -1,6 +1,8 @@ #include"build/puzzle.h" #include"build/defdata.h" #include +#include +#include #include // #include"./build/check.h" @@ -27,128 +29,195 @@ // #include"./buildin/map_0.cpp" // #include"./buildin/map_1.cpp" -// namespace -// { -// [[unused]] void puzz_help() noexcept -// { -// using ::std::printf; - -// printf("Usage:\n"); -// printf("puzzle.main [-p][-s][-e][-h] \n\n"); - -// printf(" -p []\tplay the game with map file, deafault with\n \t\t build-in maps.\n"); -// printf(" -s []\tsolve the game with map file, deafault with\n \t\t build-in maps.\n"); -// printf(" -e \tedit the map file.\n"); -// printf(" -h\t\tprint this help.\n"); -// printf(" -v\t\tprint the author information.\n"); -// } - -// [[unused]] void author_inf() noexcept -// { -// using ::std::printf; - -// printf("Created by Inochi Amaoto.\n"); -// } - - // void puzz_information() noexcept - // { - // using std::printf; - - // printf("This is the symbol information.\n"); - - // printf(" 0\t\tthe start postion.\n \t\tThe game starts from one of them.\n\n"); - // printf(" 1\t\tthe destination postion.\n \t\tThe game ends if you reach one of them.\n\n"); - // printf(" #\t\tthe wall.\n \t\tYou can't pass it at the most time, \n \t\tbut there are some special walls that you can pass\n\n"); - // printf(" -\t\tthe pass.\n \t\tYou can pass it at the most time,\n \t\tbut if it's a trap, you can't pass it.\n\n"); - // printf(" +\t\tthe transport postion.\n \t\tYou will be transported to a new postion when you reach.\n"); - // } - -// } +namespace +{ + struct puzz_args + { + bool play; + bool solve; + bool solve_all; + bool build_in; + bool edit; + bool help; + bool show; + unsigned long which; + const char* file; + const char* solve_file; + }; + + void puzz_help() noexcept + { + using ::std::printf; + + printf("Usage:\n"); + printf("puzzle.main [-b][-p][-s][-e][-h] \n\n"); + + printf(" -b [num]\tuse build in data.\n"); + printf(" -p []\tplay the game\n"); + printf(" -s []\tsolve the game\n"); + printf(" -e \tedit the map file.\n"); + printf(" -d []\tplay the game use trace files\n"); + printf(" -h\t\tprint this help.\n"); + printf("\nPlease type -b first if you what use build in data.\n"); + // printf(" -v\t\tprint the author information.\n"); + } + + void author_inf() noexcept + { + using std::printf; + printf("Created by Inochi Amaoto.\n"); + } + + void puzz_information() noexcept + { + using std::printf; + + printf("This is the symbol information.\n"); + + printf(" 0\t\tthe start postion.\n \t\tThe game starts from one of them.\n\n"); + printf(" 1\t\tthe destination postion.\n \t\tThe game ends if you reach one of them.\n\n"); + printf(" #\t\tthe wall.\n \t\tYou can't pass it at the most time, \n \t\tbut there are some special walls that you can pass\n\n"); + printf(" -\t\tthe pass.\n \t\tYou can pass it at the most time,\n \t\tbut if it's a trap, you can't pass it.\n\n"); + printf(" +\t\tthe transport postion.\n \t\tYou will be transported to a new postion when you reach.\n"); + } + + void analyze_args(int argc, char* argv[], puzz_args& args) + { + using std::fprintf; + using std::strtoul; + using std::exit; + + for(int __i = 1; __i < argc; ++__i) + { + if(argv[__i][0] == '-') + { + switch(argv[__i][1]) + { + case 'h': + args.help = true; + break; + case 'b': + args.build_in = true; + ++__i; + args.which = strtoul(argv[__i], nullptr, 10); + break; + case 'p': + args.play = true; + if(!args.build_in) + { + ++__i; + args.file = argv[__i]; + } + break; + case 's': + args.solve = true; + if(!args.build_in) + { + ++__i; + args.file = argv[__i]; + } + break; + case 'd': + args.show = true; + ++__i; + args.solve_file = argv[__i]; + if(!args.build_in) + { + ++__i; + args.file = argv[__i]; + } + break; + case 'e': + args.edit = true; + ++__i; + args.file = argv[__i]; + break; + default: + fprintf(stderr, "Unknown args - %c\n", argv[__i][1]); + puzz_help(); + printf("\n"); + puzz_information(); + printf("\n"); + author_inf(); + exit(EXIT_FAILURE); + } + } + } + } +} int main(int argc, char* argv[]) { using std::printf; + using std::memset; + + puzz_args args; + memset(&args, 0x00, sizeof(args)); + if(argc == 1) + { goto laber_help;} + analyze_args(argc, argv, args); + + if(args.help) + { + laber_help: + puzz_help(); + printf("\n"); + puzz_information(); + printf("\n"); + author_inf(); + return 0; + } + + if(args.edit) + { + if(args.file == nullptr) + { args.file = "default.map";} + game::puzzle::game_edit(args.file); + return 0; + } - // if(argc == 1 || argc > 3) - // { - // help_error: - // puzz_help(); - // printf("\n"); - // author_inf(); - // return 0; - // } - - // game::puzzle puzz; - - // if(argc == 2) - // { - // if(argv[1][0] != '-') - // { goto help_error;} - // switch(argv[1][1]) - // { - // case 'h': - // puzz_help(); - // printf("\n"); - // case 'v': - // author_inf(); - // break; - // case 'p': - // if(puzz.load_data(game::default_data(0)) && puzz.load_time(0, 1, 0)) - // { puzz.game_play();} - // break; - // case 's': - // if(puzz.load_data(game::default_data(0))) - // { puzz.game_solve();} - // break; - // case 'e': - // default: - // printf("Unknown options: -%c\n\n", argv[1][1]); - // goto help_error; - // } - // return 0; - // } - // if(argv[1][0] != '-') - // { goto help_error;} - // switch(argv[1][1]) - // { - // case 'h': - // puzz_help(); - // printf("\n"); - // case 'v': - // author_inf(); - // break; - // case 'p': - // if(puzz.load_data(argv[2])) - // { puzz.game_play();} - // break; - // case 's': - // if(puzz.load_data(argv[2])) - // { puzz.game_solve();} - // break; - // case 'e': - // game::puzzle::game_edit(argv[2]); - // break; - // default: - // printf("Unknown options: -%c\n\n", argv[1][1]); - // goto help_error; - // } - - // std::vector __po; - // game::matrix __map; - // std::map __mapping; - // game::build_in_data(__map, 0); - // game::build_in_mapping(__mapping, 0); - // game::countdown __time = game::build_in_time(0); game::puzzle puzz; - - if( - // puzz.load_data(std::move(__map)) && - // puzz.load_time(__time) && - // puzz.load_mapping(std::move(__mapping)) && - // puzz.load_select(-1) - puzz.load_file("/home/inochi/codes/vscode/Cpp/game-puzzle/output.map") - ) - { puzz.game_show("/home/inochi/codes/vscode/Cpp/game-puzzle/output");} + bool __load = false; + if(args.build_in) + { + std::vector __po; + game::matrix __map; + std::map __mapping; + game::build_in_data(__map, args.which); + game::build_in_mapping(__mapping, args.which); + __load = puzz.load_data(std::move(__map)) && + puzz.load_time(game::build_in_time(args.which)) && + puzz.load_mapping(std::move(__mapping)) && + puzz.load_select(-1); + } + else + { + if(args.file == nullptr) + { goto file_error;} + __load = puzz.load_file(args.file); + } + if(!__load) + { + file_error: + fprintf(stderr, "Error when load data.\n"); + return EXIT_FAILURE; + } + + if(args.play) + { + puzz.game_play(); + return 0; + } + if(args.solve) + { + puzz.game_solve(); + return 0; + } + if(args.show) + { + puzz.game_show(args.solve_file); + return 0; + } return 0; }