VisualStudio2010 C#怎么做迷宫啊(纯小白求全过程和代码)

VisualStudio2010 C#怎么做迷宫啊(纯小白求全过程和代码),第1张

你好,

软糖花时间使用C#控制台做了个演示程序,见下图:

迷宫这个程序还是有些复杂的,初学者可能有点难懂。不懂再问吧。

一、定义结构 --> 点,表示坐标X和Y

public struct 点 {

    public 点(int vX, int vY) { X = vX; Y = vY; }

    public int X { get; set; }

    public int Y { get; set; }

}

二、定义抽象迷宫类,提供一个二维数组

public abstract class 迷宫 {

    internal int[,] _map;

    internal 点 _起始点;

    internal int _高;

    internal int _宽;

    public int[,] map {

        get {

            return _map;

        }

    }

    public 点 originPos {

        get {

            return _起始点;

        }

    }

    public int 高 {

        get {

            return _高;

        }

    }

    public int 宽 {

        get {

            return _宽;

        }

    }

    public abstract void 生成迷宫(int width, int height, 点 originPos);

}

三、定义阻挡墙和阻挡方向

[Flags]

public enum 阻挡方向 {

    无 = 0,

    上 = 1,

    下 = 2,

    左 = 4,

    右 = 8,

}

public class 迷宫墙 {

    public 迷宫墙(int x, int y, 阻挡方向 方向) {

        thisx = x;

        thisy = y;

        this阻挡方向 = 方向;

    }

    public int x;

    public int y;

    public 阻挡方向 阻挡方向;

}

四、迷宫程序 --> 输出到控制台

using System;

namespace 迷宫生成 {

    class Program {

        static void Main(string[] args) {

            var maze = new 普利姆迷宫();

            ConsoleBackgroundColor = ConsoleColorWhite;

            ConsoleForegroundColor = ConsoleColorBlack;

            ConsoleWriteLine("生成迷宫 21 X 21 ");

            ConsoleReadKey();

            //重新开始生成迷宫

            重新生成:

            ConsoleCursorVisible = false;

            ConsoleCursorLeft = 0;

            ConsoleCursorTop = 1;

            int startX = 9;

            int startY = 9;

            int 宽 = 21;

            int 高 = 21;

            ConsoleSetWindowSize(60, 30);

            ConsoleSetWindowPosition(0, 0);

            maze生成迷宫(宽, 高, new 点(startX, startY));

            for (int y = 0; y < maze高; y++) {

                for (int x = 0; x < maze宽; x++) {

                    var k = mazemap[y, x];

                    if (k == 0) {

                        //绘制墙

                        ConsoleBackgroundColor = ConsoleColorGray;

                        ConsoleWrite("  ");

                    } else {

                        //绘制通路

                        ConsoleBackgroundColor = ConsoleColorDarkCyan;

                        ConsoleWrite("  ");

                    }

                }

                ConsoleWriteLine();

            }

            //退出

            ConsoleBackgroundColor = ConsoleColorBlack;

            ConsoleWriteLine();

            ConsoleBackgroundColor = ConsoleColorBlack;

            ConsoleForegroundColor = ConsoleColorWhite;

            ConsoleWriteLine("按空格键重新生成迷宫,其他任意键退出");

            //绘制开始点

            ConsoleCursorLeft = startX;

            ConsoleCursorTop = startY + 1;

            ConsoleBackgroundColor = ConsoleColorDarkGray;

            ConsoleForegroundColor = ConsoleColorYellow;

            ConsoleWrite("始");

            var j = ConsoleReadKey();

            if (jKey == ConsoleKeySpacebar) {

                goto 重新生成;

            }

        }

    }

}

具体有什么不懂的再问把,代码拿走~~慢慢研究把。

记得点赞、采纳、好评哟~~

#include "stdafxh"

#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[rowscols] = { 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[rowscols];

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( msgmessage!=WM_QUIT )

  {

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

      {

          TranslateMessage( &msg );

          DispatchMessage( &msg );

      }

else

{

tNow = GetTickCount();

if(tNow-tPre >= 100)

MyPaint(hdc);

}

  }

return msgwParam;

}

//注册窗口

ATOM MyRegisterClass(HINSTANCE hInstance)

{

WNDCLASSEX wcex;

wcexcbSize = sizeof(WNDCLASSEX);

wcexstyle = CS_HREDRAW | CS_VREDRAW;

wcexlpfnWndProc = (WNDPROC)WndProc;

wcexcbClsExtra = 0;

wcexcbWndExtra = 0;

wcexhInstance = hInstance;

wcexhIcon = NULL;

wcexhCursor = NULL;

wcexhCursor = LoadCursor(NULL, IDC_ARROW);

wcexhbrBackground = (HBRUSH)(COLOR_WINDOW+1);

wcexlpszMenuName = NULL;

wcexlpszClassName = "canvas";

wcexhIconSm = 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,cols50,rows50);

SelectObject(mdc,bmp);

HBITMAP tile;

int rowNum,colNum;

int i,x,y;

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

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

for (i=0;i<rowscols;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;

pathpush(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])

{

pathpush(up);

record[up] = 0;

prePos = nowPos;

nowPos = up;

      if(mapIndex[nowPos] == 3)

find = true;

}

else if(down<=colsrows-1 && record[down])

{

pathpush(down);

record[down] = 0;

prePos = nowPos;

nowPos = down;

if(mapIndex[nowPos] == 3)

find = true;

}

else if(left>=rowNumcols && record[left])

{

pathpush(left);

record[left] = 0;

prePos = nowPos;

nowPos = left;

if(mapIndex[nowPos] == 3)

find = true;

}

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

{

pathpush(right);

record[right] = 0;

prePos = nowPos;

nowPos = right;

if(mapIndex[nowPos] == 3)

find = true;

}

else

{

if(pathsize() <= 1) //&#59076;&#59343;&#58864;&#58892;

str = "xxxxx";

else

{

pathpop();

prePos = nowPos;

nowPos = pathtop();

}

}

}

else

{

str = "找到出口";

}

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

BitBlt(hdc,10,10,cols50,rows50,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;

}

//  可以运行   请采纳

有不懂的可以联系我

这个可是标准c++的   这是结果

这是源代码 

#include<iostreamh>

void search(int ,int);

void out();

int check(int ,int ,int);

static int maze[8][8]={{0,0,0,0,0,0,0,0},{0,1,1,1,1,0,1,0},{0,0,0,0,1,0,1,0},

{0,1,0,0,0,0,1,0},{0,1,0,1,1,0,1,0},{0,1,0,0,0,0,1,1},{0,1,0,0,1,0,0,0},{0,1,1,1,1,1,1,0}};

static int fx[4]={1,-1,0,0},fy[4]={0,0,-1,1};

static int i,j,k,total;

void main()

{

int total=0;

maze[1][1]=3;//入口坐标设置已走标志

search(1,1);

cout<<"Total is "<<total<<endl;//统计总步数

}

void search(int i,int j)

{

int k,newi,newj;

for(k=1;k<=4;k++)//搜索可达方格

{

if(check(i,j,k)==1)

{

newi=i+fx[k];

newj=j+fy[k];

maze[newi][newj]=3;//来到新位置后,设置已走过标志

if(newi==8&&newj==8)//如到出口则输出,否则下一步递归

out();

else

search(newi,newj);

}

maze[i][j]=2;//某一方格只能走入死胡同

}

}

void out()

{

int i,j;

for(i=1;i<=8;i++)

{

cout<<endl;

for(j=1;j<=8;j++)

{

if(maze[i][j]==3)

{

cout<<'V';

total++;//统计总步数

}

else

cout<<'';

}

}

}

int check(int i,int j,int k)

{

int flag=1;

i+=fx[k];

j+=fy[k];

if(i<1||i>8||j<1||j>8)//是否在迷宫内

flag=0;

else if(maze[i][j]!=0)//是否可行

flag=0;

return flag;

}

血族树属性最强阵容鼎力推荐

>

在程序的最后加一个条件语句:

若之前pr过程(也就是你的路径输出过程)没被执行过,那么 输出"NO"

当然,在pr过程中要加入一个标记记录这个过程是否被执行了。

还有,你的程序对于题目的输入是不能正常读入的,输出也不符合标准,需要修改。

另外,宽搜尽量用UNTIL语句写,比较好调试。

祝你早日成为编程高手!

以上就是关于VisualStudio2010 C#怎么做迷宫啊(纯小白求全过程和代码)全部的内容,包括:VisualStudio2010 C#怎么做迷宫啊(纯小白求全过程和代码)、跪求老鼠走迷宫游戏,必须用C++编写,用栈来实现,因为是数据结构课程设计所以只要现成代码,越快越好。、用C++求迷宫求解(要求8个方向)等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zz/10085879.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-05
下一篇 2023-05-05

发表评论

登录后才能评论

评论列表(0条)

保存