2023 交大電機營 程式遊戲實作 – 踩地雷
聽完前面兩個講師的課,相信大家也都變超強的啦~
現在就來挑戰自己,完成下面的踩地雷小遊戲吧!
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
被挖空的程式碼
將下面程式碼的 11題空格填入正確的答案
#include <assert.h>
#include <string.h>
#include <conio.h>
#include <unistd.h>
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <queue>
#include <iomanip>
using namespace std;
void CleanScreen();
void output(int graph[8][8], int vis[8][8], int, int, int, int);
int check_position(int graph[8][8], int, int);
int bomb_around(int graph[8][8], int, int);
void CleanScreen() {
#ifdef _WIN32
system("cls");
#else
system("clear");
#endif
}
void output(int graph[8][8], int vis[8][8], int mined_cnt, int flag_cnt,
int cursor_row, int cursor_col) {
cout << "\033[1;32;45m ";
for (int j = 0; j < 8; j++) cout << setw(3) << j;
cout << "\033[0m\n";
for (int i = 0; i < 8; i++) {
cout << "\033[1;32;45m" << setw(3) << i << "\033[0m";
for (int j = 0; j < 8; j++) {
if (___1___)
cout << "\033[5;44m";
if (vis[i][j] == 0) cout << " - " << "\033[0m";
else if (vis[i][j] == 1) {
if (___2___) cout << "\033[1;31m" << " * " << "\033[0m";
else cout << ' ' << graph[i][j] << ' ' << "\033[0m";
}
else if (vis[i][j] == 2) cout << "\033[1;36m" << " $ " << "\033[0m";
cout << "\033[0m";
}
cout << '\n';
}
cout << "\033[1;33m\n"
<< "mined places: " << mined_cnt << " /" << 8 * 8 - 10 << '\t'
<< "flagged bombs: " << flag_cnt << " /" << 10 << "\033[0m\n";
return;
}
int check_position(int graph[8][8], int row, int col) {
if (___3___)
return 0;
if (graph[row][col] == -1) return 1;
if (graph[row][col] == 0) return 2;
return 3;
}
int bomb_around(int graph[8][8], int row, int col) {
int bomb_num = 0;
for (___4.1___) {
for (___4.2___) {
if (i == 0 && j == 0) continue;
if (___5___) bomb_num++;
}
}
return bomb_num;
}
int main() {
int graph[8][8] = {0}, vis[8][8] = {0};
int mined_cnt = 0, flag_cnt = 0;
int cursor_row = 0, cursor_col = 0;
srand(time(NULL));
CleanScreen();
output(graph, vis, mined_cnt, flag_cnt, cursor_row, cursor_col);
bool fail = false;
while (___6___) {
char ch = '\0';
while (ch == '\0') ch = getch();
if ((ch == 'w' || ch == 72) && check_position(graph, cursor_row - 1, cursor_col)) ___7.1___;
else if ((ch == 's' || ch == 80) && check_position(graph, cursor_row + 1, cursor_col)) ___7.2___;
else if ((ch == 'a' || ch == 75) && check_position(graph, cursor_row, cursor_col - 1)) ___7.3___;
else if ((ch == 'd' || ch == 77) && check_position(graph, cursor_row, cursor_col + 1)) ___7.4___;
else if (ch == 'j') {
if (mined_cnt == 0) {
for (int i = 0; i < 10; i++) {
int pos = ___8___;
bool clr_cursor = (pos/8-cursor_row <= 1 && pos/8-cursor_row >= -1);
clr_cursor &= (pos%8-cursor_col <= 1) && (pos%8-cursor_col >= -1);
while (graph[pos/8][pos%8] == -1 || clr_cursor) {
pos = ___8___;
clr_cursor = (pos/8-cursor_row <= 1 && pos/8-cursor_row >= -1);
clr_cursor &= (pos%8-cursor_col <= 1) && (pos%8-cursor_col >= -1);
}
graph[pos/8][pos%8] = -1;
}
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
if (graph[i][j] == 0)
graph[i][j] = ___9___;
}
}
}
if (vis[cursor_row][cursor_col]) continue;
else if (graph[cursor_row][cursor_col] == -1) {
for (int t = 0; t < 8 * 8; t++) vis[t/8][t%8] = 1;
fail = true;
}
else if (graph[cursor_row][cursor_col] > 0) {
vis[cursor_row][cursor_col] = 1;
___10___;
}
else {
queue<int> save;
save.push(cursor_row * 8 + cursor_col);
while (save.size()) {
int cur = save.front();
int cur_row = cur / 8, cur_col = cur % 8;
save.pop();
if (vis[cur_row][cur_col]) continue;
cursor_row = cur_row;
cursor_col = cur_col;
vis[cur_row][cur_col] = 1;
mined_cnt++;
if (check_position(graph, cur_row, cur_col) == 2) {
for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j++) {
if (i == 0 && j == 0) continue;
if (check_position(graph, cur_row+i, cur_col+j) > 1 && !vis[cur_row+i][cur_col+j])
save.push((cur_row+i) * 8 + (cur_col + j));
}
}
}
}
}
}
else if (ch == 'k') {
if (vis[cursor_row][cursor_col] == 0) {
vis[cursor_row][cursor_col] = 2, flag_cnt++;
}
else if (vis[cursor_row][cursor_col] == 2) {
vis[cursor_row][cursor_col] = 0, flag_cnt--;
}
}
CleanScreen();
output(graph, vis, mined_cnt, flag_cnt, cursor_row, cursor_col);
if (fail) {
cout << "\033[1;5;31m\n\t\t"
<< "Stepped on bomb!!"
<< "\033[0m\n";
break;
}
}
if (!fail) cout << "\033[1;5;32m\n\t\t" << ___11___ << "\033[0m\n";
cout << "\033[0m" << "\n\nPress 'c' to leave\n";
char get_end;
while (get_end = getch()) {
if (get_end == 'c') break;
}
return 0;
}
題目 Part 1. 選填題
作答的過程中,記得把選了甚麼選項記下來唷 !
就算你超有自信,覺得自己選的一定是對的直接填進就沒問題也一樣,等等需要對答案 !
Problem 1. 判斷目前要輸出的區塊是不是游標所在的位置
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
選項 >> 只有一個是對的喔!
A ) i == cursor_row && j == cursor_col
B ) j == cursor_row && i == cursor_col
C ) i == cursor_row || j == cursor_col
D ) j == cursor_row || i == cursor_col
Problem 2. 判斷是不是地雷
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
選項 >> 一樣只有一個對的喔~
A ) graph[i][j] == 1
B ) graph[i][j] == 0
C ) graph[i][j] == -1
D ) vis[i][j] == -1
Problem 3. 檢查位置的合法性
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Hint:[row][col] 代表的位置不可以超出地圖的範圍
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
選項 >> 不用懷疑,還是只有一個對的
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
A ) (row < 0 && row >= 8) || (col < 0 && col >= 8)
B ) (row < 0 || row >= 8) && (col < 0 || col >= 8)
C ) row < 0 && row >= 8 && col < 0 && col >= 8
D ) row < 0 || row >= 8 || col < 0 || col >= 8
Problem 4. 計算自己周圍九宮格內的地雷數
如上面表格的情形,自己是C位,要計算周遭標示O的區塊總共有幾個地雷
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
選項 >> 這題水一點,有兩個選項正確
_4.1_ / _4.2_
A ) int i = -1; i <= 1; i++
/ int j = -1; j <= 1; j++
B ) int i = 0; i <= 2; i++
/ int j = 0; j <= 1; j++
C ) int i = 1; i >= -1; i--
/ int j = 1; j >= -1; j--
D ) int i = 2; i >= 0; i--
/ int j = 2; j >= 0; j--
Problem 5. 檢查是不是地雷
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
選項 >> 這題送你,只有一個選項 (絕對不是因為課程組掰不出來)
A ) check_position (graph, row+i, col+j) == 1
Problem 6. while loop 中止條件(成功結束時)
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Hint: 當 點開區塊數 = 總區塊數 - 總炸彈數 >> 成功結束
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
選項 >> 只有一個是正確的喔
A ) 1
B ) 0
C ) mined_cnt < 8 * 8 - 10
D ) mined_cnt < 10
Problem 7. 上下左右鍵
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
選項 >> 四個子題共用下列的選項
作答形式: _7.1 / _7.2_ / _7.3_ / _7.4_
A ) cursor_col++
B ) cursor_row++
C ) cursor_col--
D ) cursor_row--
Problem 8. 隨機變數產生
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Hint: 隨機產生一個 0 ~ 63 的整數
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Hint: 常數 RAND_MAX 為函數 rand() 能產出的最大值
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
選項 >> 有兩個是對的喔 (程式裡面兩個標示 _ 8 _ 的地方是一樣的東西
A ) (int) ((double) rand()%RAND_MAX / 64)
B ) (int) ((double) rand()/RAND_MAX * 8 * 8)
C ) rand() % 64
D ) rand() / 64
Problem 9. 呼叫函式計算周遭的地雷數
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
選項 >> 只有一個是正確的喔
A ) output (graph, vis, mined_cnt, flag_cnt, cursor_row, cursor_col)
B ) check_position (graph, cursor_row, cursor_col)
C ) bomb_around (graph, cursor_row, cursor_col)
D ) bomb_around (graph, i, j)
Problem 10. 紀錄挖開的區塊數
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
選項 >> 有兩個是正確的喔
A ) flag_cnt++
B ) mined_cnt++
C ) flag_cnt += 1
D ) mined_cnt = mined_cnt+1
Problem 11. 超難題
題目敘述:
恭喜玩家終於成功解開整張地圖都沒踩到地雷!
請在螢幕上輸出 "\\ You win ! //" 的字樣 (輸出在螢幕上時沒有雙引號)
_ - _ - _ - _ - _ - _ - _ - _ - _ - _ - _ - _ - _ - _ - _ - _ - _ - _ - _ - _ - _ - _ - _ - _ - _ - _ - _
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
注意: 輸出 \\ You win ! // 完不用自己加換行
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Hint: 可以自己開一個 cpp 檔案試著輸出上面的字串喔!
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
選項 >> 因為是超難題所以就沒有選項啦
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
直接寫出 __ 11 __ 內該寫入什麼程式碼喔
作答完成
寫完上面11題的答案之後,記得給學長姐們確認過答案,再把答案打入你的程式檔喔!

題目 Part 2. 程式改寫
能做到這真的已經超強了!
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
接下來試著把踩地雷程式做出以下改變:
- 8*8的地圖 變成 16*16的地圖
- 10顆地雷 變成 40顆地雷
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Hint: 附上一張貓貓給你能量
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
如果你真的卡住了的話,可以看一下下面巨有料的影片喔!!
- 影片 1 -
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →