欧美极品高清xxxxhd,国产日产欧美最新,无码AV国产东京热AV无码,国产精品人与动性XXX,国产传媒亚洲综合一区二区,四库影院永久国产精品,毛片免费免费高清视频,福利所导航夜趣136
標題:
基于嵌入式C的游戲小程序2048代碼
[打印本頁]
作者:
972396891
時間:
2018-7-18 10:26
標題:
基于嵌入式C的游戲小程序2048代碼
#if defined(UNICODE) && !defined(_UNICODE)
#define _UNICODE
#elif defined(_UNICODE) && !defined(UNICODE)
#define UNICODE
#endif
#include <tchar.h>
#include <windows.h>
/* Declare Windows procedure */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
/* Make the class name into a global variable */
TCHAR szClassName[ ] = _T("CodeBlocksWindowsApp");
int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nCmdShow)
{
HWND hwnd; /* This is the handle for our window */
MSG messages; /* Here messages to the application are saved */
WNDCLASSEX wincl; /* Data structure for the windowclass */
/* The Window structure */
wincl.hInstance = hThisInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure; /* This function is called by windows */
wincl.style = CS_DBLCLKS; /* Catch double-clicks */
wincl.cbSize = sizeof (WNDCLASSEX);
/* Use default icon and mouse-pointer */
wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = NULL; /* No menu */
wincl.cbClsExtra = 0; /* No extra bytes after the window class */
wincl.cbWndExtra = 0; /* structure or the window instance */
/* Use Windows's default colour as the background of the window */
wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
/* Register the window class, and if it fails quit the program */
if (!RegisterClassEx (&wincl))
return 0;
/* The class is registered, let's create the program*/
hwnd = CreateWindowEx (
0, /* Extended possibilites for variation */
szClassName, /* Classname */
_T("Code::Blocks Template Windows App"), /* Title Text */
WS_OVERLAPPEDWINDOW, /* default window */
CW_USEDEFAULT, /* Windows decides the position */
CW_USEDEFAULT, /* where the window ends up on the screen */
544, /* The programs width */
375, /* and height in pixels */
HWND_DESKTOP, /* The window is a child-window to desktop */
NULL, /* No menu */
hThisInstance, /* Program Instance handler */
NULL /* No Window Creation data */
);
/* Make the window visible on the screen */
ShowWindow (hwnd, nCmdShow);
/* Run the message loop. It will run until GetMessage() returns 0 */
while (GetMessage (&messages, NULL, 0, 0))
{
/* Translate virtual-key messages into character messages */
TranslateMessage(&messages);
/* Send message to WindowProcedure */
DispatchMessage(&messages);
}
/* The program return-value is 0 - The value that PostQuitMessage() gave */
return messages.wParam;
}
/* This function is called by the Windows function DispatchMessage() */
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message) /* handle the messages */
{
case WM_DESTROY:
PostQuitMessage (0); /* send a WM_QUIT to the message queue */
break;
default: /* for messages that we don't deal with */
return DefWindowProc (hwnd, message, wParam, lParam);
}
return 0;
}
/*
* Copyright (C) Judge Young
* E-mail: [url=mailto:yjjtc@126.com]yjjtc@126.com[/url]
* Version: 1.0
*/
#include <stdio.h>
#include <time.h> /* 包含設定隨機數種子所需要的time()函數 */
#include <conio.h> /* 包含Windows平臺上完成輸入字符不帶回顯和回車確認的getch()函數 */
#include <windows.h> /* 包含Windows平臺上完成設定輸出光標位置達到清屏功能的函數 */
int word[19][60]=
{
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0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 */
void reset_game(); /* 重置游戲 */
/* 往左右上下四個方向移動 */
void move_left();
void move_right();
void move_up();
void move_down();
void refresh_show(); /* 刷新界面顯示 */
void add_rand_num(); /* 生成隨機數,本程序中僅生成2或4,概率之比設為2:1 */
void check_game_over(); /* 檢測是否輸掉游戲,設定游戲結束標志 */
int get_null_count(); /* 獲取游戲面板上空位置數量 */
int board[4][4]; /* 游戲數字面板,抽象為二維數組 */
int score; /* 游戲的分 */
int best; /* 游戲最高分 */
int if_need_add_num; /* 是否需要生成隨機數標志,1表示需要,0表示不需要 */
int if_game_over; /* 是否游戲結束標志,1表示游戲結束,0表示正常 */
/* main函數 函數定義 */
int main()
{
start_game();
}
void printwall()
{
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(handle, FOREGROUND_INTENSITY | FOREGROUND_RED);
printf("■");
}
void DrawMapstart()
{
//遍歷二維數組 //
int i,j;
for ( i = 0; i < 19; i++)
{
for (j = 0; j < 60; j++)
{
//if else switch
switch (word[i][j])
{
case 0:
printf(" ");
break;
case 1:
printwall();//紅
// printf("■");
break;
}
}
}
}
/* 開始游戲 函數定義 */
void start_game()
{
int id;
DrawMapstart();
scanf("%d",&id);
reset_game();
char cmd;
while (1)
{
cmd = getch(); /* 接收標準輸入流字符命令 */
if (if_game_over) /* 判斷是否需已經輸掉游戲 */
{
if (cmd == 'y' || cmd == 'Y') /* 重玩游戲 */
{
reset_game();
continue;
}
else if (cmd == 'n' || cmd == 'N') /* 退出 */
{
return;
}
else
{
continue;
}
}
if_need_add_num = 0; /* 先設定不默認需要生成隨機數,需要時再設定為1 */
switch (cmd) /* 命令解析,w,s,a,d字符代表上下左右命令 */
{
case 'a':
case 'A':
case 75 :
move_left();
break;
case 's':
case 'S':
case 80 :
move_down();
break;
case 'w':
case 'W':
case 72 :
move_up();
break;
case 'd':
case 'D':
case 77 :
move_right();
break;
}
score > best ? best = score : 1; /* 打破得分紀錄 */
if (if_need_add_num) /* 默認為需要生成隨機數時也同時需要刷新顯示,反之亦然 */
{
add_rand_num();
refresh_show();
}
}
}
/* 重置游戲 函數定義 */
void reset_game()
{
score = 0;
if_need_add_num = 1;
if_game_over = 0;
/* 了解到游戲初始化時出現的兩個數一定會有個2,所以先隨機生成一個2,其他均為0 */
int n = rand() % 16;
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
board[i][j] = (n-- == 0 ? 2 : 0);
}
}
/* 前面已經生成了一個2,這里再生成一個隨機的2或者4,且設定生成2的概率是4的兩倍 */
add_rand_num();
/* 在這里刷新界面并顯示的時候,界面上已經默認出現了兩個數字,其他的都為空(值為0) */
system("cls");
refresh_show();
}
/* 生成隨機數 函數定義 */
void add_rand_num()
{
srand(time(0));
int n = rand() % get_null_count();/* 確定在何處空位置生成隨機數 */
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
if (board[i][j] == 0 && n-- == 0) /* 定位待生成的位置 */
{
board[i][j] = (rand() % 3 ? 2 : 4);/* 確定生成何值,設定生成2的概率是4的概率的兩倍 */
return;
}
}
}
}
/* 獲取空位置數量 函數定義 */
int get_null_count()
{
int n = 0;
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
board[i][j] == 0 ? n++ : 1;
}
}
return n;
}
/* 檢查游戲是否結束 函數定義 */
void check_game_over()
{
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 3; j++)
{
/* 橫向和縱向比較挨著的兩個元素是否相等,若有相等則游戲不結束 */
if (board[i][j] == board[i][j+1] || board[j][i] == board[j+1][i])
{
if_game_over = 0;
return;
}
}
}
if_game_over = 1;
}
/*
* 如下四個函數,實現上下左右移動時數字面板的變化算法
* 左和右移動的本質一樣,區(qū)別僅僅是列項的遍歷方向相反
* 上和下移動的本質一樣,區(qū)別僅僅是行項的遍歷方向相反
* 左和上移動的本質也一樣,區(qū)別僅僅是遍歷時行和列互換
*/
/* 左移 函數定義 */
void move_left()
{
/* 變量i用來遍歷行項的下標,并且在移動時所有行相互獨立,互不影響 */
for (int i = 0; i < 4; i++)
{
/* 變量j為列下標,變量k為待比較(合并)項的下標,循環(huán)進入時k<j */
for (int j = 1, k = 0; j < 4; j++)
{
if (board[i][j] > 0) /* 找出k后面第一個不為空的項,下標為j,之后分三種情況 */
{
if (board[i][k] == board[i][j]) /* 情況1:k項和j項相等,此時合并方塊并計分 */
{
score += board[i][k++] <<= 1;
board[i][j] = 0;
if_need_add_num = 1; /* 需要生成隨機數和刷新界面 */
}
else if (board[i][k] == 0) /* 情況2:k項為空,則把j項賦值給k項,相當于j方塊移動到k方塊 */
{
board[i][k] = board[i][j];
board[i][j] = 0;
if_need_add_num = 1;
}
else /* 情況3:k項不為空,且和j項不相等,此時把j項賦值給k+1項,相當于移動到k+1的位置 */
{
board[i][++k] = board[i][j];
if (j != k) /* 判斷j項和k項是否原先就挨在一起,若不是則把j項賦值為空(值為0) */
{
board[i][j] = 0;
if_need_add_num = 1;
}
}
}
}
}
}
/* 右移 函數定義 */
void move_right()
{
/* 仿照左移操作,區(qū)別僅僅是j和k都反向遍歷 */
for (int i = 0; i < 4; i++)
{
for (int j = 2, k = 3; j >= 0; j--)
{
if (board[i][j] > 0)
{
if (board[i][k] == board[i][j])
{
score += board[i][k--] <<= 1;
board[i][j] = 0;
if_need_add_num = 1;
}
else if (board[i][k] == 0)
{
board[i][k] = board[i][j];
board[i][j] = 0;
if_need_add_num = 1;
}
else
{
board[i][--k] = board[i][j];
if (j != k)
{
board[i][j] = 0;
if_need_add_num = 1;
}
}
}
}
}
}
/* 上移 函數定義 */
void move_up()
{
/* 仿照左移操作,區(qū)別僅僅是行列互換后遍歷 */
for (int i = 0; i < 4; i++)
{
for (int j = 1, k = 0; j < 4; j++)
{
if (board[j][i] > 0)
{
if (board[k][i] == board[j][i])
{
score += board[k++][i] <<= 1;
board[j][i] = 0;
if_need_add_num = 1;
}
else if (board[k][i] == 0)
{
board[k][i] = board[j][i];
board[j][i] = 0;
if_need_add_num = 1;
}
else
{
board[++k][i] = board[j][i];
if (j != k)
{
board[j][i] = 0;
if_need_add_num = 1;
}
}
}
}
}
}
/* 下移 函數定義 */
void move_down()
{
/* 仿照左移操作,區(qū)別僅僅是行列互換后遍歷,且j和k都反向遍歷 */
for (int i = 0; i < 4; i++)
{
for (int j = 2, k = 3; j >= 0; j--)
{
if (board[j][i] > 0)
{
if (board[k][i] == board[j][i])
{
score += board[k--][i] <<= 1;
board[j][i] = 0;
if_need_add_num = 1;
}
else if (board[k][i] == 0)
{
board[k][i] = board[j][i];
board[j][i] = 0;
if_need_add_num = 1;
}
else
{
board[--k][i] = board[j][i];
if (j != k)
{
board[j][i] = 0;
if_need_add_num = 1;
}
}
}
}
}
}
/* 刷新界面 函數定義 */
void refresh_show()
{
/* 重設光標輸出位置方式清屏可以減少閃爍,system("cls")為備用清屏命令,均為Windows平臺相關*/
COORD pos = {0, 0};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
printf("\n\n\n\n");
printf(" GAME: 2048 SCORE: %06d BEST: %06d\n", score, best);
printf(" --------------------------------------------------\n\n");
/* 繪制表格和數字 */
printf(" ┌─--─┬─--─┬─--─┬─--─┐\n");
for (int i = 0; i < 4; i++)
{
printf(" │");
for (int j = 0; j < 4; j++)
{
if (board[i][j] != 0)
{
if (board[i][j] < 10)
{
printf(" %d │", board[i][j]);
}
else if (board[i][j] < 100)
{
printf(" %d │", board[i][j]);
}
else if (board[i][j] < 1000)
{
printf(" %d│", board[i][j]);
}
else if (board[i][j] < 10000)
{
printf("%4d│", board[i][j]);
}
else
{
int n = board[i][j];
for (int k = 1; k < 20; k++)
{
n >>= 1;
if (n == 1)
{
printf("2^%02d│", k); /* 超過四位的數字用2的冪形式表示,如2^13形式 */
break;
}
}
}
}
else printf(" │");
}
if (i < 3)
{
printf("\n ├─--─┼─--─┼─--─┼─--─┤\n");
}
else
{
printf("\n └──--┴─--─┴─--─┴──--┘\n");
}
}
printf("\n");
printf(" --------------------------------------------------\n");
printf(" W↑ A← →D ↓S");
if (get_null_count() == 0)
{
check_game_over();
if (if_game_over) /* 判斷是否輸掉游戲 */
{
printf("\r GAME OVER! TRY THE GAME AGAIN? [Y/N]");
}
}
}
復制代碼
作者:
admin
時間:
2018-7-19 03:31
此貼需要補全說明后才可獲取積分
歡迎光臨 (http://m.raoushi.com/bbs/)
Powered by Discuz! X3.1