C是一個結構化語言,它的重點在於算法和數據結構。C程序的設計首要考慮的是如何通過一個過程,對輸入(或環境條件)進行運算處理得到輸出(或實現過程(事務)控制)。
C++,首要考慮的是如何構造一個對象模型,讓這個模型能夠契合與之對應的問題域,這樣就可以通過獲取對象的狀態信息得到輸出或實現過程(事務)控制。 所以C與C++的最大區別在於它們的用於解決問題的思想方法不一樣。之所以說C++比C更先進,是因為“ 設計這個概念已經被融入到C++之中 ”。
C與C++的最大區別:在於它們的用於解決問題的思想方法不一樣。之所以說C++比C更先進,是因為“ 設計這個概念已經被融入到C++之中 ”,而就語言本身而言,在C中更多的是算法的概念。那麼是不是C就不重要了,錯!算法是程序設計的基礎,好的設計如果沒有好的算法,一樣不行。而且,“C加上好的設計”也能寫出非常好的東西。
今天給大家帶來的是一個智力小遊戲-《點燈遊戲》。點燈遊戲是一個十分有趣的智力遊戲:有一行N行N列的燈,開始時全部是滅的,當你點擊其中一盞燈時他的上下左右(若存在的話)狀態全部改變,現在要求你將全部的燈點亮。可能聽起來很簡單,當格子逐漸增加之後,就會變得更加困難。
簡單點來說就是通過點擊,讓屏幕上的黑色方塊全部編程綠色的方塊就可以通關了。
第一關很簡單,下圖中只差最後一步就通關了:
(小編推薦一個學C語言/C++的學習群:639912742,入群即送C/C++全套學習資料,滿滿的乾貨!)
第二關就比較難了,大家可以自己玩玩.
(小編推薦一個學C語言/C++的學習群:639912742,入群即送C/C++全套學習資料,滿滿的乾貨!)
然後看下代碼吧:
/******************************************************************************
項目名稱:《點燈遊戲》
******************************************************************************/
#include
#include
#defineMaxNum14//單邊最大格子數
#defineG_length50//格子邊長
#defineUSER_LBUTTONDOWN101
#defineUSER_RBUTTONDOWN102
#defineUSER_MBUTTONDOWN103
///////////////////////////////////////////////
void PaintGrid(int Mid_x, int Mid_y, int num, int color);// 繪製遊戲格子,初始化格子
void OnLButtonDown(MOUSEMSG m, int num);// 左鍵按下
void OnRButtonDown(int num);// 右鍵按下
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.xgrid.top && m.y
{
// 計算位置
nx = (int)(m.x - grid.left) / G_length;
ny = (int)(m.y - grid.top) / G_length;
// 轉換格子狀態
grid.array[nx][ny] = -grid.array[nx][ny];
if (nx >= 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
for (ny = 0; ny
{
if (grid.array[nx][ny] == 1)
setfillcolor(GREEN);
else
setfillcolor(BLACK);
x = nx * G_length + grid.left;
y = ny * G_length + grid.top;
solidrectangle(x + 1, y + 1, x + G_length - 1, y + G_length - 1);
}
}
}
///////////////////////////////////////////////
// 右鍵按下清空
void OnRButtonDown(int num)
{
int x, y, nx, ny;
for (x = 0; x
for (y = 0; y
grid.array[x][y] = -1;
for (nx = 0; nx
for (ny = 0; ny
{
setfillcolor(BLACK);
x = nx*G_length + grid.left;
y = ny*G_length + grid.top;
solidrectangle(x + 1, y + 1, x + G_length - 1, y + G_length - 1);
}
}
///////////////////////////////////////////////
// 顯示下一關
// 參數:
//num:下一關的邊界格子數
void NextLevel(int num)
{
// 清屏
BeginBatchDraw();
for (int y = 0; y <= 480; y += 5)
{
setlinecolor(RGB(0, 255, 0));
settextcolor(RGB(0, 255, 0));
line(0, y, 640, y);
line(0, 480 - y, 640, 480 - y);
outtextxy(300, y, L"下一關");
FlushBatchDraw();
Sleep(16);
setfillcolor(BLACK);
solidrectangle(0, y + 4, 640, y - 5);
solidrectangle(0, 480 - y, 640, 480 - y + 5);
}
EndBatchDraw();
// 繪製下一關格子
PaintGrid(320, 240, num, RGB(0, 255, 0));
}
///////////////////////////////////////////////
// 判斷格子是否填滿
// 參數:
//num:單邊格子數目
//array:生成數組接收實參
int JudgeFull(int num, int array[MaxNum][MaxNum])
{
int c = -1;
int nx = 0, ny = 0;
while (nx
{
for (nx = 0; nx
for (ny = 0; ny
if (array[nx][ny] == 1)
continue;
else
return c;
}
c = 1;
return c;
}
///////////////////////////////////////////////
// 繪製遊戲格子,初始化格子
// 參數:
//Mid_x:屏幕中心 x 座標
//Mid_y:屏幕中心 y 座標
//num:單邊格子數目
//color:格子線條顏色
void PaintGrid(int Mid_x, int Mid_y, int num, int color)
{
int x, y, nx, ny;
// 遊戲區域大小
grid.left = Mid_x - num*G_length / 2;
grid.right = Mid_x + num*G_length / 2;
grid.top = Mid_y - num*G_length / 2;
grid.bottom = Mid_y + num*G_length / 2;
// 繪製格子
setlinecolor(color);
for (x = grid.left; x <= grid.right; x += G_length)
{
line(x, grid.top, x, grid.bottom);
Sleep(10);
}
for (y = grid.top; y <= grid.bottom; y += G_length)
{
line(grid.left, y, grid.right, y);
Sleep(10);
}
// 外邊框
for (x = 20; x>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
for (y = 0; y
grid.array[x][y] = -1;
for (nx = 0; nx
for (ny = 0; ny
{
setfillcolor(BLACK);
x = nx * G_length + grid.left;
y = ny * G_length + grid.top;
solidrectangle(x + 1, y + 1, x + G_length - 1, y + G_length - 1);
}
}
///////////////////////////////////////////////
// 顯示遊戲主界面
void welcome()
{
// 初始化窗口
initgraph(640, 480);
// 輸出屏幕提示
cleardevice();
settextcolor(RGB(0, 255, 0));
settextstyle(64, 0, L"華文彩雲");
outtextxy(70, 50, L"點燈遊戲");
settextcolor(YELLOW);
settextstyle(16, 0, L"華文彩雲");
outtextxy(100, 200, L"每點一個格子,上下左右的格子也會做出於現狀相反的動作");
outtextxy(100, 240, L"總共11關,左鍵填色,右鍵重來,中鍵退出");
settextstyle(16, 0, L"華文彩雲");
// 實現閃爍的"按任意鍵繼續"
int c = 255;
while (!_kbhit())
{
settextcolor(RGB(0, c, 0));
outtextxy(280, 400, L"按任意鍵繼續");
c -= 8;
if (c < 0) c = 255;
Sleep(20);
}
_getch();
cleardevice();
}
///////////////////////////////////////////////
// 顯示結束畫面
void goodbye(int num)
{
int c = 255;
// 清屏
BeginBatchDraw();
for (int y = 0; y <= 480; y += 5)
{
setlinecolor(RGB(0, 255, 0));
line(0, y, 640, y);
line(0, 480 - y, 640, 480 - y);
FlushBatchDraw();
Sleep(16);
setfillcolor(BLACK);
solidrectangle(0, y + 4, 640, y - 5);
solidrectangle(0, 480 - y, 640, 480 - y + 5);
}
EndBatchDraw();
//判斷顯示文字
if (num == MaxNum + 1)
{
settextcolor(RGB(0, c, 0));
settextstyle(48, 0, L"華文彩雲");
outtextxy(280, 200, L"通關");
settextstyle(20, 0, L"華文彩雲");
}
else
{
settextcolor(RGB(0, c, 0));
settextstyle(48, 0, L"華文彩雲");
outtextxy(200, 200, L"再接再厲");
settextstyle(20, 0, L"華文彩雲");
}
while (!_kbhit())
{
settextcolor(RGB(0, c, 0));
c -= 8;
if (c < 0) c = 255;
Sleep(20);
}
_getch();
}
閱讀更多 IT程序員小輝 的文章