導航:首頁 > 編程語言 > 老鼠迷宮java

老鼠迷宮java

發布時間:2023-03-07 08:08:25

㈠ 跪求老鼠走迷宮游戲,必須用C++編寫,用棧來實現,因為是數據結構課程設計所以只要現成代碼,越快越好。

#include "stdafx.h"

#include <stack>

using namespace std;

const int rows = 8,cols = 8;

HINSTANCE hInst;

HBITMAP ball;

HDC hdc,mdc,bufdc;

HWND hWnd;

DWORD tPre,tNow;

char *str;

int nowPos,prePos;

bool find;

stack<int> path;


int mapIndex[rows*cols] = { 0,2,0,0,0,0,0,0, //材1&#59049;

0,1,0,1,1,1,1,0, //材2&#59049;

0,1,0,1,0,1,1,0, //材3&#59049;

0,1,0,0,0,1,1,0, //材4&#59049;

0,1,1,1,1,1,1,0, //材5&#59049;

0,1,0,0,0,0,1,0, //材6&#59049;

0,0,1,1,1,1,1,0, //材7&#59049;

0,0,0,0,0,0,3,0 }; //材8&#59049;

int record[rows*cols];

ATOM MyRegisterClass(HINSTANCE hInstance);

BOOL InitInstance(HINSTANCE, int);

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

void MyPaint(HDC hdc);


int APIENTRY WinMain(HINSTANCE hInstance,

HINSTANCE hPrevInstance,

LPSTR lpCmdLine,

int nCmdShow)

{

MSG msg;


MyRegisterClass(hInstance);



if (!InitInstance (hInstance, nCmdShow))

{

return FALSE;

}


while( msg.message!=WM_QUIT )

{

if( PeekMessage( &msg, NULL, 0,0 ,PM_REMOVE) )

{

TranslateMessage( &msg );

DispatchMessage( &msg );

}

else

{

tNow = GetTickCount();

if(tNow-tPre >= 100)

MyPaint(hdc);

}

}

return msg.wParam;

}


//****注冊窗口*************************

ATOM MyRegisterClass(HINSTANCE hInstance)

{

WNDCLASSEX wcex;


wcex.cbSize = sizeof(WNDCLASSEX);

wcex.style = CS_HREDRAW | CS_VREDRAW;

wcex.lpfnWndProc = (WNDPROC)WndProc;

wcex.cbClsExtra = 0;

wcex.cbWndExtra = 0;

wcex.hInstance = hInstance;

wcex.hIcon = NULL;

wcex.hCursor = NULL;

wcex.hCursor = LoadCursor(NULL, IDC_ARROW);

wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);

wcex.lpszMenuName = NULL;

wcex.lpszClassName = "canvas";

wcex.hIconSm = NULL;


return RegisterClassEx(&wcex);

}


//****初始化*************************************


BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)

{

HBITMAP bmp;

hInst = hInstance;


hWnd = CreateWindow("canvas", "迷宮" , WS_OVERLAPPEDWINDOW,

CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);


if (!hWnd)

{

return FALSE;

}


MoveWindow(hWnd,10,10,430,450,true);

ShowWindow(hWnd, nCmdShow);

UpdateWindow(hWnd);

hdc = GetDC(hWnd);

mdc = CreateCompatibleDC(hdc);

bufdc = CreateCompatibleDC(hdc);


bmp = CreateCompatibleBitmap(hdc,cols*50,rows*50);

SelectObject(mdc,bmp);


HBITMAP tile;

int rowNum,colNum;

int i,x,y;


tile = (HBITMAP)LoadImage(NULL,"tile.bmp",IMAGE_BITMAP,50,50,LR_LOADFROMFILE);

ball = (HBITMAP)LoadImage(NULL,"ball.bmp",IMAGE_BITMAP,50,50,LR_LOADFROMFILE);


for (i=0;i<rows*cols;i++)

{

record[i] = mapIndex[i];


rowNum = i / cols;

colNum = i % cols;

x = colNum * 50;

y = rowNum * 50;

SelectObject(bufdc,tile);


if(!mapIndex[i])

BitBlt(mdc,x,y,50,50,bufdc,0,0,SRCCOPY);

else

{

if(mapIndex[i] == 2)

{

nowPos = i;

path.push(i);

record[i] = 0;

}

BitBlt(mdc,x,y,50,50,bufdc,0,0,WHITENESS);

}

}

prePos = cols * rows + 1;


MyPaint(hdc);


return TRUE;

}


//****核心代碼*********************************

void MyPaint(HDC hdc)

{

int rowNum,colNum;

int x,y;

int up,down,left,right;



rowNum = prePos / cols;

colNum = prePos % cols;

x = colNum * 50;

y = rowNum * 50;


SelectObject(bufdc,ball);

BitBlt(mdc,x,y,50,50,bufdc,0,0, WHITENESS);



rowNum = nowPos / cols;

colNum = nowPos % cols;

x = colNum * 50;

y = rowNum * 50;


SelectObject(bufdc,ball);

BitBlt(mdc,x,y,50,50,bufdc,0,0, SRCCOPY);


if(!find)

{

str = "迷宮入口";


up = nowPos - cols;

down = nowPos + cols;

left = nowPos - 1;

right = nowPos + 1;


if(up>=0 && record[up])

{

path.push(up);

record[up] = 0;

prePos = nowPos;

nowPos = up;


if(mapIndex[nowPos] == 3)

find = true;

}

else if(down<=cols*rows-1 && record[down])

{

path.push(down);

record[down] = 0;

prePos = nowPos;

nowPos = down;


if(mapIndex[nowPos] == 3)

find = true;

}

else if(left>=rowNum*cols && record[left])

{

path.push(left);

record[left] = 0;

prePos = nowPos;

nowPos = left;


if(mapIndex[nowPos] == 3)

find = true;

}

else if(right<=(rowNum+1)*cols-1 && record[right])

{

path.push(right);

record[right] = 0;

prePos = nowPos;

nowPos = right;


if(mapIndex[nowPos] == 3)

find = true;

}

else

{

if(path.size() <= 1) //&#59076;&#59343;&#58864;&#58892;

str = "xxxxx";

else

{

path.pop();

prePos = nowPos;

nowPos = path.top();

}

}

}

else

{

str = "找到出口";

}


TextOut(mdc,0,0,str,strlen(str));

BitBlt(hdc,10,10,cols*50,rows*50,mdc,0,0,SRCCOPY);


tPre = GetTickCount();

}


//****消息函數***********************************

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)

{

switch (message)

{

case WM_KEYDOWN:

if(wParam==VK_ESCAPE)

PostQuitMessage(0);

break;

case WM_DESTROY:

DeleteDC(mdc);

DeleteDC(bufdc);

DeleteObject(ball);


ReleaseDC(hWnd,hdc);


PostQuitMessage(0);

break;

default:

return DefWindowProc(hWnd, message, wParam, lParam);

}

return 0;

}

㈡ 機器小老鼠走迷宮的工作原理是什麼

1977年,美國電氣與電子工程師協會舉辦了機器小老鼠走迷宮競賽。迷宮由許多圍牆構成走道,有不少死胡同。小老鼠從進口向里走,開始找路。它身上帶有感測器,能感覺出是否碰上圍牆。它是由電動機驅動身子下的輪子進行移動的,它身上有微電腦,由電腦計算出最短的路徑。電動機和感測器向電腦提供行走了多少路程,電腦根據這些數據和程序產生控制信號,控制輪子前進和轉彎,使小老鼠以最快速度走到終點。

java迷宮圖形界面

這是我之前課程設計做的一個迷宮,Swing做的,發在javaeye的博客上了,有打包成jar的,安裝了jdk可以直接雙擊運行,有源碼,還有我寫的一個說明文檔,網址如下:
http://zpsailor.javaeye.com/admin/blogs/651141
自己下載下吧,我就不給你發到郵箱了。

閱讀全文

與老鼠迷宮java相關的資料

熱點內容
如何登錄別人的伺服器 瀏覽:626
調度系統軟體python 瀏覽:205
微信大轉盤抽獎源碼 瀏覽:497
壓縮機損壞的表現 瀏覽:862
同步數據伺服器怎麼用 瀏覽:634
163郵箱伺服器的ip地址 瀏覽:50
伺服器跟域是什麼 瀏覽:128
rails啟動命令 瀏覽:465
logistic命令怎麼用 瀏覽:738
c語言點滴pdf 瀏覽:747
linuxrtc編程 瀏覽:258
linux打包並壓縮命令 瀏覽:644
aes加密的證書格式 瀏覽:99
oracledbcalinux 瀏覽:844
酬勤任務app怎麼被特邀 瀏覽:199
android應用文件夾 瀏覽:1002
平面設計法則pdf 瀏覽:339
3d圓角命令怎麼用 瀏覽:569
程序員買意外險還是重疾險 瀏覽:621
遼寧的dns伺服器地址雲空間 瀏覽:448