Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# sudoku
C++ 实现的跨平台数独游戏,命令行操作易上手,可以在开发间隙用来放松身心。数百行代码,初学者也可以轻松掌握。
C++ 实现的跨平台数独游戏和扫雷游戏(fork了大佬的数独游戏,然后使用相同的代码风和操作界面,添加了扫雷玩法),命令行操作易上手,可以在开发间隙用来放松身心。数百行代码,初学者也可以轻松掌握。
欢迎通过pull request的方式来添加功能或修复缺陷。

## 感谢贡献者
Expand Down
2 changes: 1 addition & 1 deletion src/input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ int inputDifficulty()
break;
}
}
catch(...)
catch(...) //catch all types of exceptions
{
need_erase_grids = 0;
}
Expand Down
100 changes: 75 additions & 25 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,46 +4,96 @@
#include "i18n.h"
#include "input.h"
#include "scene.h"
#include "minesweeper.h"
#include "system_env.hpp"
#include "utility.inl"

static void printHelp() {
std::cout << std::endl;
std::cout << "sudoku - a little game in command line" << std::endl
<< std::endl;
std::cout << "Game Menu - Choose your game:" << std::endl << std::endl;
std::cout << "Usage:" << std::endl;
std::cout << "\t sudoku [-l <progressFile>]" << std::endl << std::endl;
std::cout << "\t game [-l <progressFile>] [-m <mode>]" << std::endl << std::endl;
std::cout << "Options:" << std::endl;
std::cout << "\t -l <path> \t specify path of progress file to load, optional." << std::endl
<< std::endl;
std::cout << "\t -l <path> \t specify path of progress file to load, optional." << std::endl;
std::cout << "\t -m <mode> \t specify game mode: 1 for Sudoku, 2 for Minesweeper" << std::endl << std::endl;
}

// 选择游戏模式
int selectGameMode() {
std::string mode;
do {
std::cout << "选择游戏模式 / Select game mode:" << std::endl;
std::cout << "1. 数独 / Sudoku" << std::endl;
std::cout << "2. 扫雷 / Minesweeper" << std::endl;
std::cout << "请选择 / Please choose (1-2): ";

std::cin >> mode;
if(mode == "1" || mode == "2") {
return std::stoi(mode);
}
message(I18n::Instance().Get(I18n::Key::INPUT_ERROR));
} while(true);
}

int main(int argc, char **argv)
{
SetSystemEnv();
SetSystemEnv();
InputLanguage();

CScene scene;
int gameMode = 1; // 默认为数独模式
const char* loadFile = nullptr;

if (argc == 1) {
InputLanguage();
int eraseGridNumber = inputDifficulty();
scene.generate();
scene.eraseRandomGrids(eraseGridNumber);
} else if (argc == 3 && !strcmp(argv[1], "-l")) {
// load saved game progress
if (!scene.load(argv[2])) {
message(I18n::Instance().Get(I18n::Key::LOAD_PROGRESS_FAIL));
return 0;
// 处理命令行参数
for(int i = 1; i < argc; i++) {
if(strcmp(argv[i], "-l") == 0 && i + 1 < argc) {
loadFile = argv[++i];
}
else if(strcmp(argv[i], "-m") == 0 && i + 1 < argc) {
gameMode = std::stoi(argv[++i]);
}
else {
printHelp();
return 0;
}
}
InputLanguage();
} else {
printHelp();
return 0;
}

scene.setMode(inputKeyMode());
// 如果没有通过命令行指定游戏模式,则提示用户选择
if(argc == 1 || (argc == 3 && loadFile != nullptr)) {
gameMode = selectGameMode();
}

scene.play();
KeyMode keyMode = inputKeyMode();

return 0;
if(gameMode == 1) { // 数独模式
CScene scene;
scene.setMode(keyMode);

if(loadFile) {
if(!scene.load(loadFile)) {
message(I18n::Instance().Get(I18n::Key::LOAD_PROGRESS_FAIL));
return 0;
}
} else {
int eraseGridNumber = inputDifficulty();
scene.generate();
scene.eraseRandomGrids(eraseGridNumber);
}

scene.play();
}
else if(gameMode == 2) { // 扫雷模式
CMinesweeper minesweeper(9, 9, 10); // 默认9x9,10个地雷
minesweeper.setMode(keyMode);

if(loadFile) {
if(!minesweeper.load(loadFile)) {
message(I18n::Instance().Get(I18n::Key::LOAD_PROGRESS_FAIL));
return 0;
}
}

minesweeper.play();
}

return 0;
}
Loading