。
2026-05-06 10:03:23
发布于:云南
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <windows.h>
#include <conio.h>
#include <iomanip>
#include <string>
#include <algorithm>
using namespace std;
const int ROW = 10;
const int COL = 10;
const int EMPTY = 0;
const int MAX_SAVE_RECORD = 50;
int gameMap[ROW][COL];
int answerMap[ROW][COL];
int originMap[ROW][COL];
int hintValidNum[11];
int stepHistory[1500][4];
int historyCount = 0;
int operateLog[2000][4];
int logCount = 0;
int gameTime = 0;
int gameStep = 0;
int maxStepRecord = 0;
int pauseTime = 0;
int errorCount = 0;
int totalErrorTimes = 0;
bool isGameOver = false;
bool isGamePause = false;
int gameLevel = 1;
int customBlankNum = 50;
int cursorX = 0;
int cursorY = 0;
string gradeText = "D";
int scoreValue = 0;
void setColor(int color)
{
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsole, color);
}
void initSystemConfig()
{
srand((unsigned int)time(NULL));
system("mode con cols=130 lines=45");
system("title 完整版10X10百宫格益智游戏 一千行代码版");
setColor(7);
}
void initAllMapZero(int map[ROW][COL])
{
for(int i = 0; i < ROW; i++)
{
for(int j = 0; j < COL; j++)
{
map[i][j] = EMPTY;
}
}
}
void initHintArray()
{
for(int i = 1; i <= 10; i++)
{
hintValidNum[i] = 0;
}
}
bool checkRowRepeat(int map[ROW][COL], int row, int num)
{
for(int j = 0; j < COL; j++)
{
if(map[row][j] == num)
{
return false;
}
}
return true;
}
bool checkColRepeat(int map[ROW][COL], int col, int num)
{
for(int i = 0; i < ROW; i++)
{
if(map[i][col] == num)
{
return false;
}
}
return true;
}
bool checkBlockRepeat(int map[ROW][COL], int row, int col, int num)
{
int blockStartRow = row / 2 * 2;
int blockStartCol = col / 5 * 5;
for(int i = blockStartRow; i < blockStartRow + 2; i++)
{
for(int j = blockStartCol; j < blockStartCol + 5; j++)
{
if(map[i][j] == num)
{
return false;
}
}
}
return true;
}
bool checkFullRuleValid(int map[ROW][COL], int r, int c, int num)
{
bool rowOk = checkRowRepeat(map, r, num);
bool colOk = checkColRepeat(map, c, num);
bool blockOk = checkBlockRepeat(map, r, c, num);
if(rowOk && colOk && blockOk)
{
return true;
}
return false;
}
void calculateCurrentHint(int map[ROW][COL], int r, int c)
{
initHintArray();
for(int num = 1; num <= 10; num++)
{
if(checkFullRuleValid(map, r, c, num))
{
hintValidNum[num] = 1;
}
else
{
hintValidNum[num] = 0;
}
}
}
bool dfsCreateAnswer(int map[ROW][COL], int row, int col)
{
if(row >= ROW)
{
return true;
}
int nextR = row;
int nextC = col + 1;
if(nextC >= COL)
{
nextR = row + 1;
nextC = 0;
}
int numRandomList[10] = {1,2,3,4,5,6,7,8,9,10};
for(int i = 0; i < 10; i++)
{
int randomIndex = rand() % 10;
swap(numRandomList[i], numRandomList[randomIndex]);
}
for(int i = 0; i < 10; i++)
{
int nowNum = numRandomList[i];
if(checkFullRuleValid(map, row, col, nowNum))
{
map[row][col] = nowNum;
if(dfsCreateAnswer(map, nextR, nextC))
{
return true;
}
map[row][col] = EMPTY;
}
}
return false;
}
void generateCompleteAnswerMap()
{
initAllMapZero(answerMap);
bool createSuccess = dfsCreateAnswer(answerMap, 0, 0);
while(!createSuccess)
{
initAllMapZero(answerMap);
createSuccess = dfsCreateAnswer(answerMap, 0, 0);
}
}
void initOriginGameData()
{
for(int i = 0; i < ROW; i++)
{
for(int j = 0; j < COL; j++)
{
gameMap[i][j] = answerMap[i][j];
originMap[i][j] = answerMap[i][j];
}
}
}
void setBlankByLevel()
{
int blankMax = 0;
if(gameLevel == 1)
{
blankMax = 30;
}
else if(gameLevel == 2)
{
blankMax = 50;
}
else if(gameLevel == 3)
{
blankMax = 70;
}
else if(gameLevel == 4)
{
blankMax = 88;
}
else if(gameLevel == 5)
{
blankMax = customBlankNum;
}
else
{
blankMax = 40;
}
int blankCount = 0;
while(blankCount < blankMax)
{
int r = rand() % 10;
int c = rand() % 10;
if(gameMap[r][c] != EMPTY)
{
gameMap[r][c] = EMPTY;
originMap[r][c] = EMPTY;
blankCount++;
}
}
}
void createNewGameMap()
{
generateCompleteAnswerMap();
initOriginGameData();
setBlankByLevel();
}
void addOperateHistory(int r, int c, int oldVal, int newVal)
{
stepHistory[historyCount][0] = r;
stepHistory[historyCount][1] = c;
stepHistory[historyCount][2] = oldVal;
stepHistory[historyCount][3] = newVal;
historyCount++;
}
void writeOperateLog(int type, int r, int c, int num)
{
operateLog[logCount][0] = type;
operateLog[logCount][1] = r;
operateLog[logCount][2] = c;
operateLog[logCount][3] = num;
logCount++;
if(logCount >= 1990)
{
logCount = 0;
}
}
void undoLastOperate()
{
if(historyCount <= 0)
{
setColor(12);
cout << endl << ">>> 暂无可撤销的操作记录!" << endl;
setColor(7);
Sleep(700);
return;
}
historyCount--;
int r = stepHistory[historyCount][0];
int c = stepHistory[historyCount][1];
int oldNum = stepHistory[historyCount][2];
gameMap[r][c] = oldNum;
gameStep--;
writeOperateLog(2, r, c, oldNum);
}
void clearAllUserInput()
{
for(int i = 0; i < ROW; i++)
{
for(int j = 0; j < COL; j++)
{
if(originMap[i][j] == EMPTY)
{
gameMap[i][j] = EMPTY;
}
}
}
setColor(10);
cout << endl << ">>> 已清空所有手动填入的数字!" << endl;
setColor(7);
Sleep(600);
gameStep = 0;
errorCount = 0;
}
void pauseGameFunction()
{
isGamePause = !isGamePause;
if(isGamePause)
{
setColor(14);
cout << endl << ">>> 游戏已暂停,按下P键继续游戏" << endl;
setColor(7);
}
else
{
setColor(10);
cout << endl << ">>> 游戏继续!" << endl;
setColor(7);
}
Sleep(800);
}
void saveGameDataToFile()
{
ofstream outFile("sudoku10_save.txt", ios::out);
if(!outFile)
{
setColor(12);
cout << endl << ">>> 存档文件创建失败,保存失败!" << endl;
setColor(7);
Sleep(800);
return;
}
outFile << gameLevel << endl;
outFile << gameTime << endl;
outFile << gameStep << endl;
outFile << errorCount << endl;
outFile << totalErrorTimes << endl;
outFile << cursorX << " " << cursorY << endl;
for(int i = 0; i < ROW; i++)
{
for(int j = 0; j < COL; j++)
{
outFile << gameMap[i][j] << " ";
}
outFile << endl;
}
for(int i = 0; i < ROW; i++)
{
for(int j = 0; j < COL; j++)
{
outFile << originMap[i][j] << " ";
}
outFile << endl;
}
for(int i = 0; i < ROW; i++)
{
for(int j = 0; j < COL; j++)
{
outFile << answerMap[i][j] << " ";
}
outFile << endl;
}
outFile.close();
setColor(10);
cout << endl << ">>> 游戏存档保存成功!" << endl;
setColor(7);
Sleep(700);
}
void loadGameDataFromFile()
{
ifstream inFile("sudoku10_save.txt", ios::in);
if(!inFile)
{
setColor(12);
cout << endl << ">>> 未检测到存档文件,读取失败!" << endl;
setColor(7);
Sleep(800);
return;
}
inFile >> gameLevel;
inFile >> gameTime;
inFile >> gameStep;
inFile >> errorCount;
inFile >> totalErrorTimes;
inFile >> cursorX >> cursorY;
for(int i = 0; i < ROW; i++)
{
for(int j = 0; j < COL; j++)
{
inFile >> gameMap[i][j];
}
}
for(int i = 0; i < ROW; i++)
{
for(int j = 0; j < COL; j++)
{
inFile >> originMap[i][j];
}
}
for(int i = 0; i < ROW; i++)
{
for(int j = 0; j < COL; j++)
{
inFile >> answerMap[i][j];
}
}
inFile.close();
historyCount = 0;
logCount = 0;
isGameOver = false;
isGamePause = false;
setColor(10);
cout << endl << ">>> 存档读取成功,恢复游戏进度!" << endl;
setColor(7);
Sleep(700);
}
void showFullAnswerBoard()
{
for(int i = 0; i < ROW; i++)
{
for(int j = 0; j < COL; j++)
{
if(originMap[i][j] == EMPTY)
{
gameMap[i][j] = answerMap[i][j];
}
}
}
isGameOver = true;
}
void detectAllMistakeNumber()
{
int nowWrong = 0;
for(int i = 0; i < ROW; i++)
{
for(int j = 0; j < COL; j++)
{
if(gameMap[i][j] != EMPTY && gameMap[i][j] != answerMap[i][j])
{
nowWrong++;
}
}
}
if(nowWrong > 0)
{
errorCount += nowWrong;
totalErrorTimes += nowWrong;
setColor(12);
cout << endl << ">>> 检测发现当前一共有【" << nowWrong << "】处填写错误!" << endl;
}
else
{
setColor(10);
cout << endl << ">>> 太棒了!当前所有填写数字全部正确!" << endl;
}
setColor(7);
Sleep(900);
}
bool judgeGameWinComplete()
{
for(int i = 0; i < ROW; i++)
{
for(int j = 0; j < COL; j++)
{
if(gameMap[i][j] != answerMap[i][j])
{
return false;
}
}
}
return true;
}
void calculateGameGrade()
{
scoreValue = 100;
scoreValue -= errorCount * 3;
scoreValue -= gameTime / 5;
scoreValue -= gameStep / 10;
if(scoreValue >= 90)
{
gradeText = "S";
}
else if(scoreValue >= 75)
{
gradeText = "A";
}
else if(scoreValue >= 60)
{
gradeText = "B";
}
else if(scoreValue >= 40)
{
gradeText = "C";
}
else
{
gradeText = "D";
}
if(scoreValue < 0)
{
scoreValue = 0;
}
}
void gameTimeClockRun()
{
if(!isGameOver && !isGamePause)
{
gameTime++;
}
}
void showGameBorderTop()
{
setColor(11);
cout << "=====================================================================================================" << endl;
cout << " 一千行完整版 10×10 百宫格逻辑游戏" << endl;
cout << "=====================================================================================================" << endl;
setColor(7);
}
void showGameOperateTip()
{
setColor(14);
cout << "【操作指引】方向键移动光标 | 1~9/0输入数字10 | Z撤销 | S存档 | L读档 | P暂停 | C清空填写" << endl;
cout << " A查看答案 | 空格检测错题 | R重开游戏 | ESC退出本局 | H显示可填提示" << endl;
setColor(7);
cout << "=====================================================================================================" << endl;
}
void showHintNumberInfo()
{
calculateCurrentHint(gameMap, cursorX, cursorY);
setColor(13);
cout << "当前光标位置合法可填数字:";
for(int i = 1; i <= 10; i++)
{
if(hintValidNum[i] == 1)
{
cout << i << " ";
}
}
cout << endl;
setColor(7);
}
void drawGameMapInterface()
{
system("cls");
showGameBorderTop();
showGameOperateTip();
cout << " 0 1 2 3 4 | 5 6 7 8 9 " << endl;
cout << "--------------------------------------------------------" << endl;
for(int i = 0; i < ROW; i++)
{
cout << i << " ";
for(int j = 0; j < COL; j++)
{
if(i == cursorX && j == cursorY)
{
setColor(15);
}
else if(originMap[i][j] != EMPTY)
{
setColor(10);
}
else if(gameMap[i][j] != EMPTY && gameMap[i][j] != answerMap[i][j])
{
setColor(12);
}
else
{
setColor(14);
}
if(gameMap[i][j] == EMPTY)
{
cout << "□ ";
}
else
{
cout << setw(2) << gameMap[i][j] << " ";
}
if(j == 4)
{
setColor(7);
cout << "| ";
}
}
setColor(7);
cout << endl;
if(i % 2 == 1)
{
cout << "--------------------------------------------------------" << endl;
}
}
setColor(6);
cout << endl;
cout << "游戏计时:" << gameTime << " 秒 ";
cout << "操作步数:" << gameStep << " 步 ";
cout << "错误次数:" << errorCount << " 次 ";
cout << "本局评级:" << gradeText << endl;
setColor(9);
if(isGamePause)
{
cout << ">>> 游戏当前处于暂停状态 <<<" << endl;
}
setColor(7);
cout << "=====================================================================================================" << endl;
}
void chooseGameDifficultyLevel()
{
system("cls");
setColor(11);
cout << "======================== 选择游戏难度 ========================" << endl;
setColor(7);
cout << "1、简单模式 空白格少,适合新手练习" << endl;
cout << "2、普通模式 难度适中,日常休闲游玩" << endl;
cout << "3、困难模式 空白较多,考验推理能力" << endl;
cout << "4、地狱模式 超高难度,大神专属挑战" << endl;
cout << "5、自定义模式 自己设置空白格子数量" << endl;
setColor(11);
cout << "==============================================================" << endl;
setColor(7);
cout << "请输入难度编号(1-5):";
cin >> gameLevel;
if(gameLevel == 5)
{
cout << "请输入自定义空白格数量(10~90):";
cin >> customBlankNum;
if(customBlankNum < 10) customBlankNum = 10;
if(customBlankNum > 90) customBlankNum = 90;
}
if(gameLevel < 1 || gameLevel > 5)
{
gameLevel = 2;
setColor(12);
cout << "输入无效,自动默认选择普通模式!" << endl;
setColor(7);
Sleep(1000);
}
}
void startNewGameMain()
{
historyCount = 0;
logCount = 0;
gameTime = 0;
gameStep = 0;
errorCount = 0;
isGameOver = false;
isGamePause = false;
cursorX = 0;
cursorY = 0;
gradeText = "D";
scoreValue = 0;
initAllMapZero(gameMap);
initAllMapZero(answerMap);
initAllMapZero(originMap);
createNewGameMap();
while(true)
{
drawGameMapInterface();
if(judgeGameWinComplete())
{
isGameOver = true;
calculateGameGrade();
drawGameMapInterface();
setColor(10);
cout << endl << "🎉 恭喜你!成功完成本局百宫格游戏! 🎉" << endl;
cout << "本局用时:" << gameTime << " 秒" << endl;
cout << "操作总步数:" << gameStep << " 步" << endl;
cout << "填写错误次数:" << errorCount << " 次" << endl;
cout << "本局综合评分:" << scoreValue << " 分 最终评级:" << gradeText << endl;
setColor(7);
cout << endl << "按下任意键返回游戏主菜单..." << endl;
_getch();
break;
}
int keyGet = _getch();
if(keyGet == 27)
{
break;
}
if(keyGet == 'z' || keyGet == 'Z')
{
undoLastOperate();
continue;
}
if(keyGet == 's' || keyGet == 'S')
{
saveGameDataToFile();
continue;
}
if(keyGet == 'l' || keyGet == 'L')
{
loadGameDataFromFile();
continue;
}
if(keyGet == 'a' || keyGet == 'A')
{
showFullAnswerBoard();
continue;
}
if(keyGet == 'r' || keyGet == 'R')
{
startNewGameMain();
break;
}
if(keyGet == ' ')
{
detectAllMistakeNumber();
continue;
}
if(keyGet == 'p' || keyGet == 'P')
{
pauseGameFunction();
continue;
}
if(keyGet == 'c' || keyGet == 'C')
{
clearAllUserInput();
continue;
}
if(keyGet == 'h' || keyGet == 'H')
{
showHintNumberInfo();
cout << "按下任意键继续游戏...";
_getch();
continue;
}
if(keyGet >= '1' && keyGet <= '9')
{
int inputNum = keyGet - '0';
if(originMap[cursorX][cursorY] == EMPTY)
{
int oldValue = gameMap[cursorX][cursorY];
addOperateHistory(cursorX, cursorY, oldValue, inputNum);
gameMap[cursorX][cursorY] = inputNum;
gameStep++;
writeOperateLog(1, cursorX, cursorY, inputNum);
}
continue;
}
if(keyGet == '0')
{
if(originMap[cursorX][cursorY] == EMPTY)
{
int oldValue = gameMap[cursorX][cursorY];
addOperateHistory(cursorX, cursorY, oldValue, 10);
gameMap[cursorX][cursorY] = 10;
gameStep++;
writeOperateLog(1, cursorX, cursorY, 10);
}
continue;
}
if(keyGet == 224)
{
keyGet = _getch();
if(keyGet == 72)
{
cursorX--;
if(cursorX < 0) cursorX = 9;
}
else if(keyGet == 80)
{
cursorX++;
if(cursorX >= 10) cursorX = 0;
}
else if(keyGet == 75)
{
cursorY--;
if(cursorY < 0) cursorY = 9;
}
else if(keyGet == 77)
{
cursorY++;
if(cursorY >= 10) cursorY = 0;
}
}
gameTimeClockRun();
}
}
void showGameRuleIntroduce()
{
system("cls");
setColor(11);
cout << "==================== 百宫格完整游戏规则 ====================" << endl;
setColor(7);
cout << "1、游戏棋盘为标准 10行×10列 百宫格布局格式" << endl;
cout << "2、填入数字范围为 1 ~ 10,0键快捷输入数字10" << endl;
cout << "3、每一行内部1~10数字不能出现重复" << endl;
cout << "4、每一列内部1~10数字不能出现重复" << endl;
cout << "5、棋盘划分多个2×5小宫格,每个小宫格数字不重复" << endl;
cout << "6、绿色数字为系统固定原始数字,不可修改覆盖" << endl;
cout << "7、红色数字代表填写错误,白色为正常填写数字" << endl;
cout << "8、合理逻辑推理填满所有空格,全部正确即可通关" << endl;
cout << "9、游戏自带计时、步数统计、错题检测、本局评分系统" << endl;
cout << "10、支持存档读档、撤销操作、游戏暂停、一键清空填写" << endl;
setColor(11);
cout << "============================================================" << endl;
setColor(7);
cout << endl << "按下任意键返回主菜单..." << endl;
_getch();
}
void showMainMenuInterface()
{
while(true)
{
system("cls");
setColor(13);
cout << "==================== 一千行完整版百宫格游戏 ====================" << endl;
setColor(7);
cout << " 1、开始全新对局游戏" << endl;
cout << " 2、读取本地游戏存档" << endl;
cout << " 3、查看详细游戏规则" << endl;
cout << " 4、退出游戏程序" << endl;
setColor(13);
cout << "================================================================" << endl;
setColor(7);
cout << "请输入菜单选项编号(1~4):";
int menuChoose = 0;
cin >> menuChoose;
if(menuChoose == 1)
{
chooseGameDifficultyLevel();
startNewGameMain();
}
else if(menuChoose == 2)
{
loadGameDataFromFile();
startNewGameMain();
}
else if(menuChoose == 3)
{
showGameRuleIntroduce();
}
else if(menuChoose == 4)
{
system("cls");
setColor(10);
cout << "感谢游玩一千行完整版百宫格游戏,期待下次再见!" << endl;
setColor(7);
Sleep(1200);
exit(0);
}
else
{
setColor(12);
cout << "输入选项错误,请输入1到4之间的数字!" << endl;
setColor(7);
Sleep(800);
}
}
}
int main()
{
initSystemConfig();
showMainMenuInterface();
return 0;
}
这里空空如也
















有帮助,赞一个