连连看小程序改进
为使数字更加醒目,将不同数字用不同颜色表示
// 使用ANSI转义码添加颜色 switch (board[i][j]) { case 1: cout << "\033[31m" << board[i][j] << "\033[0m "; // 红色 break; case 2: cout << "\033[32m" << board[i][j] << "\033[0m "; // 绿色 break; case 3: cout << "\033[33m" << board[i][j] << "\033[0m "; // 黄色 break; case 4: cout << "\033[34m" << board[i][j] << "\033[0m "; // 蓝色 break; case 5: cout << "\033[35m" << board[i][j] << "\033[0m "; // 紫色 break; default: cout << board[i][j] << " "; // 默认颜色 break;
为保证输入的数字有效,加入一个是否超范围的检测
bool isValidCoordinate(int x, int y) { return x >= 1 && x <= rows && y >= 1 && y <= cols; }
将所有数字全部配对之后,难度升级
增加两行,两列,时间增加20s
void increaseDifficulty() { ++level; INITIAL_ROWS += k; INITIAL_COLS += k; rows = INITIAL_ROWS; cols = INITIAL_COLS; refreshesLeft = MAX_REFRESHES; board.clear(); srand(time(0)); board = vector<vector<int>>(rows, vector<int>(cols)); vector<int> numbers; for (int i = 0; i < (rows * cols) / 2; ++i) { int num = rand() % 5 + 1; numbers.push_back(num); numbers.push_back(num); } random_shuffle(numbers.begin(), numbers.end()); int k = 0; for (int i = 0; i < rows; ++i) { for (int j = 0; j < cols; ++j) { board[i][j] = numbers[k++]; } } TIME_LIMIT += 30; startTime = chrono::steady_clock::now(); cout << "难度提升,等级 " << level << "!" << endl; play(); }
增加对用户输入数字合法性的检测
if (x1 == x2 && y1 == y2) { cout << "请连接两个点" << endl; justPaired = true; continue; } if (!isValidCoordinate(x1, y1) || !isValidCoordinate(x2, y2)) { cout << "无效坐标,请重新输入" << endl; justPaired = true; continue; }
完善对游戏玩法和提示的说明
cout << "欢迎来到胡管的地狱游戏!!!" << endl; cout << "你有30s时间阅读规则" << endl; cout << "规则如下:" << endl; cout << "1. 连接两个相等的数字。" << endl; cout << "2. 连接正确得分+10,连接错误得分-5。" << endl; cout << "3. 你现在有 " << TIME_LIMIT << " 秒去清除块。" << endl; cout << "4. 你目前有 " << MAX_REFRESHES << " 次机会刷新游戏,记住按1刷新游戏。" << endl; cout << "5. 输入坐标为(x y),行为x(从上到下),列为y(从左到右)。" << endl; cout << "6. 已经连接的数字用“·”替代" << endl; cout << "7. 通关当前关卡后难度升级。" << endl; cout << "==========================================" << endl;