點燈游戲是一個十分有趣的智力游戲:有一行N行N列的燈,開始時全部是滅的,當你點擊其中一盞燈時他的上下左右(若存在的話)狀態全部改變,現在要求你在限定的時間內以最少地步數,將全部的燈點亮。
例如一開始有5×5共25盞燈,都處于關的狀態,現在要想辦法把25盞燈全打開,每次只能開/關一盞燈,但由于電路原因,和它相鄰的四盞燈也會改變開/關狀態,于是想把25盞燈全打開就有一定難度。
(本期代碼有兩個版本)
編譯環境:Visual C++ 6.0 / VS2019/2022,EasyX插件
代碼版本一:
?
#include#include #define MaxNum 14 //單邊最大格子數 #define G_length 30 //格子邊長 #define USER_LBUTTONDOWN 101 #define USER_RBUTTONDOWN 102 #define USER_MBUTTONDOWN 103 /////////////////////////////////////////////// void PaintGrid(int Mid_x, int Mid_y, int num, int color); // 繪制游戲格子,初始化格子 void OnLButtonDown(MOUSEMSG m, int num); // 左鍵按下 void OnRButtonDown(int num); // 右鍵按下 void OnMButtonDown(); // 中鍵按下*暫無定義* void welcome(); // 顯示游戲主界面 void goodbye(int num); // 顯示結束畫面 void NextLevel(int num); // 下一關 int GetMessage(MOUSEMSG m); // 獲取鼠標信息 int DispatchMessage(MOUSEMSG m, int opt); // 分發鼠標信息 int JudgeFull(int num, int array[MaxNum][MaxNum]); // 格子是否填滿 /////////////////////////////////////////////// // 定義游戲格子結構體 struct Grid { int left; // 游戲區域邊界 int right; int top; int bottom; int array[MaxNum][MaxNum]; // 記錄格子狀態 int num; // 記錄邊界格子數目 }grid; /////////////////////////////////////////////// void main() { int opt, end=0; grid.num=4; welcome(); PaintGrid(320, 240, grid.num, RGB(0,255,0)); MOUSEMSG m; while(end!=1) { m = GetMouseMsg(); opt = GetMessage(m); end=DispatchMessage(m, opt); } goodbye(grid.num); closegraph(); } /////////////////////////////////////////////// // 獲取鼠標信息 int GetMessage(MOUSEMSG m) { //鼠標循環 switch(m.uMsg) { case WM_LBUTTONDOWN: return USER_LBUTTONDOWN; case WM_RBUTTONDOWN: return USER_RBUTTONDOWN; case WM_MBUTTONDOWN: return USER_MBUTTONDOWN; } return 0; } /////////////////////////////////////////////// // 分發消息 int DispatchMessage(MOUSEMSG m, int opt) { switch(opt) { case USER_LBUTTONDOWN: // 左鍵填色 OnLButtonDown(m, grid.num); // 判斷是否填滿 if(JudgeFull(grid.num, grid.array)==1) { grid.num++; // 格子數目超過最大值通關 if(grid.num>MaxNum) { return 1; break; } else NextLevel(grid.num); } break; case USER_RBUTTONDOWN: // 右鍵清除 OnRButtonDown(grid.num); break; case USER_MBUTTONDOWN: return 1; break; } return 0; } /////////////////////////////////////////////// // 左鍵按下 void OnLButtonDown(MOUSEMSG m, int num) { int nx, ny, x, y; if(m.x>grid.left && m.x grid.top && m.y = 0 && nx < num-1) grid.array[nx+1][ny] = -grid.array[nx+1][ny]; if(nx > 0 && nx <= num-1) grid.array[nx-1][ny] = -grid.array[nx-1][ny]; if(ny >= 0 && ny < num-1) grid.array[nx][ny+1] = -grid.array[nx][ny+1]; if(ny > 0 && ny <= num-1) grid.array[nx][ny-1] = -grid.array[nx][ny-1]; // 掃描填色 for(nx=0; nx 10;x--) { line(grid.left-x, grid.top-x, grid.right+x, grid.top-x); line(grid.left-x, grid.bottom+x, grid.right+x, grid.bottom+x); line(grid.left-x, grid.top-x, grid.left-x, grid.bottom+x); line(grid.right+x, grid.top-x, grid.right+x, grid.bottom+x); Sleep(5); } // 清空單元格 for(x=0; x ?
代碼版本二:
?
#include#include //easyx圖形庫 #include #include //包含躲媒體1設備接口頭文件 #pragma comment(lib,"winmm.lib") #define WIN_WIDTH 640 #define WIN_HEIGHT 480 #define GRID_WIDTH 30//格子寬度 #define GRID_NUM 4//每一行格子的數量 struct Grid //格子結構,結構體,類首字母大寫 { int top;//上面一條線的y int down; int left; int right; int foot;//玩家走了多少步 int map[GRID_NUM][GRID_NUM]; int x;//保存鼠標點擊的數組下標 int y; }grid; MOUSEMSG msg; //int a=0; IMAGE img;//保存背景圖片 //歡迎界面 void Welcome() { /* 錯誤 1 error C2665: “loadimage”: 2 個重載中沒有一個可以轉換所有參數類型 什么問題?字符集問題1,在字符串前面加上L 2,在字符串前面加上_T(str) 推薦 3,修改項目字符集屬性 */ mciSendString("open ./森林幻想曲.mp3 alias BGM",0,0,0);//向多媒體設備接口發送一個字符串 mciSendString("play BGM repeat", 0, 0, 0);//重復播放音樂 //Sleep(10000); //mciSendString("close BGM", 0, 0, 0);//關閉音樂 loadimage(&img, "./bk.jpg", WIN_WIDTH, WIN_HEIGHT);//加載圖片 putimage(0, 0, &img); setbkmode(TRANSPARENT);//設置文字背景顏色透明 settextcolor(GREEN); settextstyle(50, 0, "楷體");//設置字體大小,類型 char str[] = "涂格子游戲(點燈游戲)"; int twidth = textwidth(str)/2;//讓文字居中顯示 outtextxy(WIN_WIDTH / 2 - twidth, 20, str); while (!_kbhit())//如果沒有按鍵就一直循環 { settextcolor(RGB(rand() % 256, rand() % 256,rand() % 256)); outtextxy(180, 300, "按任意鍵繼續..."); Sleep(300); } _getch();//等待按鍵,沒有按鍵,就一直等待 } //初始化數據 void GameInit() { grid.top = WIN_HEIGHT / 2 - GRID_NUM*GRID_WIDTH / 2;//獲取上面的y坐標 grid.down = WIN_HEIGHT / 2 + GRID_NUM*GRID_WIDTH / 2;//獲取下面的y坐標 //理解不? grid.left = WIN_WIDTH / 2 - GRID_NUM*GRID_WIDTH / 2; grid.right = WIN_WIDTH / 2 + GRID_NUM*GRID_WIDTH / 2; grid.foot = 0; //雙重循環初始化二維數組 for (int i = 0; i < GRID_NUM; i++) { for (int k = 0; k < GRID_NUM; k++) { grid.map[i][k] = 1;//黑色為1,綠色為-1 } } } //把數據通過圖形界面的方式展示出來 void GameDraw() { cleardevice(); putimage(0, 0, &img); //繪制格子 setlinecolor(RGB(0, 200, 0)); //循環繪制格子線條 for (int x = grid.left; x <= grid.right; x+=GRID_WIDTH) { line(x, grid.top, x, grid.down);//畫豎線 } for (int y = grid.top; y <= grid.down; y += GRID_WIDTH) { line(grid.left, y, grid.right,y); } //繪制界面邊框 for (int x = 20; x > 10; x--) { line(grid.left - x, grid.top - x, grid.right + x, grid.top - x); line(grid.left - x, grid.down + x, grid.right + x, grid.down + x); line(grid.left - x, grid.top - x, grid.left - x, grid.down + x); line(grid.right + x, grid.top - x, grid.right + x, grid.down + x); } grid.map[2][2] = -1; //填充游戲格子為黑色 int x, y; for (int i = 0; i < GRID_NUM; i++) { for (int k = 0; k < GRID_NUM; k++) { y = i*GRID_WIDTH + grid.top;//得到的是什么 x = k*GRID_WIDTH + grid.left;//得到的是什么 if (grid.map[i][k] == 1) { setfillcolor(BLACK); } else if (grid.map[i][k] == -1) { setfillcolor(GREEN); } solidrectangle(x + 1, y + 1, x + GRID_WIDTH - 1, y + GRID_WIDTH - 1); } } } //游戲控制,鼠標點擊 void GameControl() { if (MouseHit()) { msg = GetMouseMsg(); //如果是鼠標左鍵點擊,而且點擊在游戲區域,格子區域 if (msg.uMsg == WM_LBUTTONDOWN && msg.x>grid.left && msg.x grid.top && msg.y>grid.down) { grid.y = (msg.x - grid.left) / GRID_WIDTH; grid.x = (msg.y - grid.top) / GRID_WIDTH; grid.map[grid.x][grid.y] = -grid.map[grid.x][grid.y];//改變顏色 if (grid.x >= 0 && grid.x < GRID_NUM - 1) { grid.map[grid.x+1][grid.y] = -grid.map[grid.x+1][grid.y]; } if (grid.x > 0 && grid.x <= GRID_NUM - 1) { grid.map[grid.x - 1][grid.y] = -grid.map[grid.x - 1][grid.y]; } if (grid.y >= 0 && grid.y < GRID_NUM - 1) { grid.map[grid.x][grid.y + 1] = -grid.map[grid.x ][grid.y + 1]; } if (grid.y >= 0 && grid.y <= GRID_NUM - 1) { grid.map[grid.x][grid.y-1] = -grid.map[grid.x][grid.y-1]; } } } } int main() { initgraph(WIN_WIDTH, WIN_HEIGHT); Welcome(); GameInit(); BeginBatchDraw(); while (1) { GameDraw(); FlushBatchDraw(); GameControl(); } return 0; } ?
大家趕緊去動手試試吧!
審核編輯:湯梓紅
評論
查看更多