控制台扫雷游戏
一個在控制臺下實現的簡易掃雷
鼠標控制代碼參照:http://blog.csdn.net/bnb45/article/details/8042819
c++代碼:
#include <windows.h> #include <stdio.h> #include <ctime> HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); HANDLE hIn = GetStdHandle(STD_INPUT_HANDLE); CONSOLE_SCREEN_BUFFER_INFO bInfo; INPUT_RECORD mouseRec; DWORD res; COORD crPos, crHome = { 0, 0 }; int s[51][51], fa = 0, sum = 81;//fa 為失敗標志,sum為計數器 int t = 1; clock_t t1, t2; void f3(int, int); void f4(int, int); void f1()//重新設置雷盤 {int n = 10, ti;int a, b;fa = 0;sum = 81;t = 1;for (int i = 0; i <= 50; i++){for (int j = 0; j <= 50; j++){s[i][j] = -1;}}HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);COORD pos;pos.X = 0;pos.Y = 13;SetConsoleCursorPosition(hOut, pos);for (int i = 1; i <= 64; i++){printf(" ");}pos.X = 0;pos.Y = 2;SetConsoleCursorPosition(hOut, pos);for (int i = 0; i <= 10; i++){printf("# ");}printf("\n");for (int i = 1; i < 10; i++){printf("# ");for (int j = 1; j < 10; j++){printf("O ");}printf("# \n");}for (int i = 0; i <= 10; i++){printf("# ");}printf("\n");srand(time(NULL));srand(time(NULL));//設置隨機函數種子while (n > 0){a = rand() % 10;b = rand() % 10;if ((s[a][b] != 9) && (a != 0) && (b != 0)){s[a][b] = 9;n--;}}for (int i = 1; i <= 9; i++)//設置整個棋盤的數字標號{for (int j = 1; j <= 9; j++){if (s[i][j] == -1){ti = 0;if (s[i - 1][j - 1] == 9){ti++;}if (s[i - 1][j] == 9){ti++;}if (s[i - 1][j + 1] == 9){ti++;}if (s[i][j - 1] == 9){ti++;}if (s[i][j + 1] == 9){ti++;}if (s[i + 1][j - 1] == 9){ti++;}if (s[i + 1][j] == 9){ti++;}if (s[i + 1][j + 1] == 9){ti++;}s[i][j] = ti;}}} } void f2(int x, int y)//對于點擊操作執行響應 {if (s[x][y] == 9){printf("您輸了,點擊重新開局再試一次吧! 花費時間:%3d秒", (t2 - t1) / CLOCKS_PER_SEC);fa = 1;t = 1;}if (s[x][y] == 0){f3(x, y);}else{if (s[x][y] == 9){FillConsoleOutputCharacter(hOut, '*', 1, crPos, &res);}else{if (s[x][y] == 1){FillConsoleOutputCharacter(hOut, '1', 1, crPos, &res);sum--;}if (s[x][y] == 2){FillConsoleOutputCharacter(hOut, '2', 1, crPos, &res);sum--;}if (s[x][y] == 3){FillConsoleOutputCharacter(hOut, '3', 1, crPos, &res);sum--;}if (s[x][y] == 4){FillConsoleOutputCharacter(hOut, '4', 1, crPos, &res);sum--;}if (s[x][y] == 5){FillConsoleOutputCharacter(hOut, '5', 1, crPos, &res);sum--;}if (s[x][y] == 6){FillConsoleOutputCharacter(hOut, '6', 1, crPos, &res);sum--;}if (s[x][y] == 7){FillConsoleOutputCharacter(hOut, '7', 1, crPos, &res);sum--;}if (s[x][y] == 8){FillConsoleOutputCharacter(hOut, '8', 1, crPos, &res);sum--;}s[x][y] = -1;}} } void f3(int x, int y) {HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);COORD pos;pos.X = 2 * x;pos.Y = y + 2;FillConsoleOutputCharacter(hOut, ' ', 1, pos, &res);s[x][y] = -1;sum--;for (int i = x - 1; i <= x + 1; i++){for (int j = y - 1; j <= y + 1; j++){if (x != i || y != j){if (s[i][j] == 0){f3(i, j);//若點到空白處,以遞歸方式翻開所有空白的地方;}else{f4(i, j);//若不是空白,則調用f4()顯示其數字}}}} } void f4(int x, int y) {HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);COORD pos;pos.X = 2 * x;pos.Y = y + 2;if (s[x][y] == 1){FillConsoleOutputCharacter(hOut, '1', 1, pos, &res);sum--;}if (s[x][y] == 2){FillConsoleOutputCharacter(hOut, '2', 1, pos, &res);sum--;}if (s[x][y] == 3){FillConsoleOutputCharacter(hOut, '3', 1, pos, &res);sum--;}if (s[x][y] == 4){FillConsoleOutputCharacter(hOut, '4', 1, pos, &res);sum--;}if (s[x][y] == 5){FillConsoleOutputCharacter(hOut, '5', 1, pos, &res);sum--;}if (s[x][y] == 6){FillConsoleOutputCharacter(hOut, '6', 1, pos, &res);sum--;}if (s[x][y] == 7){FillConsoleOutputCharacter(hOut, '7', 1, pos, &res);sum--;}if (s[x][y] == 8){FillConsoleOutputCharacter(hOut, '8', 1, pos, &res);sum--;}s[x][y] = -1; } int main(void) {printf("[掃雷][作者:huplion][光標位置] X: %2lu Y: %2lu\n", 0, 0);printf("重新開局 ");printf("退出\n");f1();int p = 1;while (1){ReadConsoleInput(hIn, &mouseRec, 1, &res);if (mouseRec.EventType == MOUSE_EVENT){crPos = mouseRec.Event.MouseEvent.dwMousePosition;GetConsoleScreenBufferInfo(hOut, &bInfo);SetConsoleCursorPosition(hOut, crHome);if (t == 0){t2 = clock();}printf("[掃雷][作者:huplion][光標位置] X: %2lu Y: %2lu 時間:%3d秒", crPos.X, crPos.Y, (t2 - t1) / CLOCKS_PER_SEC);SetConsoleCursorPosition(hOut, bInfo.dwCursorPosition);if (sum == 10){printf("恭喜您,獲得了勝利! 花費時間:%3d秒", (t2 - t1) / CLOCKS_PER_SEC);fa = 1;t = 1;sum--;}switch (mouseRec.Event.MouseEvent.dwButtonState){case FROM_LEFT_1ST_BUTTON_PRESSED:{if ((crPos.X >= 1 && crPos.X <= 7) && (crPos.Y == 1)){f1();}if ((crPos.X >= 10 && crPos.X <= 13) && (crPos.Y == 1)){return 1;}if (crPos.X >= 2 && crPos.Y >= 3 && crPos.X <= 18 && crPos.Y <= 11 && crPos.X % 2 == 0 && fa == 0){if (t == 1){t1 = clock();t = 0;}f2((crPos.X) / 2, crPos.Y - 2);}}break;case RIGHTMOST_BUTTON_PRESSED:{if (s[crPos.X / 2][crPos.Y - 2] != -1 && crPos.X % 2 == 0){if (p > 0){FillConsoleOutputCharacter(hOut, '?', 1, crPos, &res);p = -p;}else{FillConsoleOutputCharacter(hOut, 'O', 1, crPos, &res);p = -p;}}}break;default:break;}}}CloseHandle(hOut);CloseHandle(hIn);return 0; }注意:1,在VS2010,VS2013下測試可用,VC6.0無法運行;
2,只能實現鼠標左鍵右鍵操作,無法使用雙擊,無法實時刷新時間;
轉載請標明出處。
總結
- 上一篇: 3D扫雷
- 下一篇: 2022中国人才市场招聘趋势白皮书