//数字华容道 #include <iostream> #include <vector> #include <cstdlib> #include <ctime> #include <iomanip> #include <cmath> using namespace std; int SIZE; int MAX_NUM; const int EMPTY = 0; void setDifficulty(int level) { switch(level) { case 1: SIZE = 3; MAX_NUM = 8; break; case 2: SIZE = 4; MAX_NUM = 15; break; case 3: SIZE = 6; MAX_NUM = 35; break; case 4: SIZE = 10; MAX_NUM = 99; break; } } void initializeBoard(vector<vector<int> >& board) { board.clear(); int num = 1; for (int i = 0; i < SIZE; i++) { vector<int> row; for (int j = 0; j < SIZE; j++) { row.push_back(num++); } board.push_back(row); } board[SIZE-1][SIZE-1] = EMPTY; } void shuffleBoard(vector<vector<int> >& board) { srand(time(0)); int emptyRow = SIZE-1; int emptyCol = SIZE-1; int moves = SIZE * SIZE * 10; for (int i = 0; i < moves; i++) { int dir = rand() % 4; int newRow = emptyRow; int newCol = emptyCol; switch(dir) { case 0: newRow--; break; case 1: newRow++; break; case 2: newCol--; break; case 3: newCol++; break; } if (newRow >= 0 && newRow < SIZE && newCol >= 0 && newCol < SIZE) { swap(board[emptyRow][emptyCol], board[newRow][newCol]); emptyRow = newRow; emptyCol = newCol; } } } void printBoard(const vector<vector<int> >& board) { cout << "\n===== 数字推盘游戏(" << SIZE << "×" << SIZE << ")=====\n"; cout << "操作:输入要移动的数字,将数字推到空白位置(0表示空白)\n"; cout << "目标:将数字按1-" << MAX_NUM << "顺序排列,空白在右下角\n"; cout << "输入0可退出游戏\n\n"; int displaySize = (SIZE > 8) ? 8 : SIZE; int startRow = (SIZE > 8) ? (SIZE - 8) / 2 : 0; int startCol = (SIZE > 8) ? (SIZE - 8) / 2 : 0; if (SIZE > 8) { cout << "注:仅显示中间8×8区域,输入仍需符合1-" << MAX_NUM << "范围\n"; } for (int i = startRow; i < startRow + displaySize; i++) { for (int j = startCol; j < startCol + displaySize; j++) { if (board[i][j] == EMPTY) { cout << setw(4) << " "; } else { cout << setw(4) << board[i][j]; } } cout << "\n"; } cout << "\n"; } bool findEmpty(const vector<vector<int> >& board, int& row, int& col) { for (int i = 0; i < SIZE; i++) { for (int j = 0; j < SIZE; j++) { if (board[i][j] == EMPTY) { row = i; col = j; return true; } } } return false; } bool moveTile(vector<vector<int> >& board, int num) { int emptyRow, emptyCol; findEmpty(board, emptyRow, emptyCol); int tileRow = -1, tileCol = -1; for (int i = 0; i < SIZE; i++) { for (int j = 0; j < SIZE; j++) { if (board[i][j] == num) { tileRow = i; tileCol = j; break; } } if (tileRow != -1) break; } if (tileRow == -1) return false; bool isAdjacent = (abs(tileRow - emptyRow) == 1 && tileCol == emptyCol) || (abs(tileCol - emptyCol) == 1 && tileRow == emptyRow); if (isAdjacent) { swap(board[tileRow][tileCol], board[emptyRow][emptyCol]); return true; } return false; } bool checkWin(const vector<vector<int> >& board) { int num = 1; for (int i = 0; i < SIZE; i++) { for (int j = 0; j < SIZE; j++) { if (i == SIZE-1 && j == SIZE-1) { return board[i][j] == EMPTY; } if (board[i][j] != num++) { return false; } } } return true; } int main() { int difficulty; vector<vector<int> > board; bool gameOver = false; int moveCount = 0; cout << "===== 多难度数字推盘游戏 =====\n"; cout << "请选择难度:\n"; cout << "1. 简单 (3×3,数字1-8)\n"; cout << "2. 普通 (4×4,数字1-15)\n"; cout << "3. 困难 (6×6,数字1-35)\n"; cout << "4. 地狱 (10×10,数字1-99)\n"; cout << "输入难度编号(1-4):"; cin >> difficulty; while (difficulty < 1 || difficulty > 4) { cout << "无效选择,请输入1-4之间的数字:"; cin >> difficulty; } setDifficulty(difficulty); initializeBoard(board); shuffleBoard(board); while (!gameOver) { printBoard(board); int num; cout << "请输入要移动的数字(输入0退出游戏):"; cin >> num; if (num == 0) { cout << "游戏退出!\n"; gameOver = true; continue; } if (num < 1 || num > MAX_NUM) { cout << "无效数字!请输入1-" << MAX_NUM << "之间的数字\n"; continue; } if (moveTile(board, num)) { moveCount++; if (checkWin(board)) { printBoard(board); cout << "恭喜你!游戏胜利!\n"; cout << "你总共移动了 " << moveCount << " 步\n"; gameOver = true; } } else { cout << "无法移动该数字!它不在空白格旁边\n"; } } return 0; }
Note.ms
/kxpi