?? “迷宮探險”游戲設計說明(4).txt
字號:
“迷宮探險”游戲設計詳細說明(4)
__________________________________________________
| |__☆★
| 上海財經大學圖書館 陳暢 | |_
| http://shchenchang.top263.net | ||
| 編程軟件,我的原創作品(含源碼和詳細說明) | ||
| mail:shufechenchang@263.net | ||
|_______QQ:7019717(歡迎編程愛好者加我為好友)_____| ||
|________________________________________________||
☆☆|_______________________________________________|
關于“迷宮探險”程序的一些問題:
******************************************************
Turbo C 中怎樣設置圖形顯示?
Turbo C 中常用圖形函數的用法?
怎樣獲取鍵盤輸入?
迷宮的數據表示法?
怎樣隨機生成一個從入口至出口只有唯一通路的迷宮?
你是怎樣知道生成的迷宮從入口至出口肯定只有唯一通路的?
入口和出口必須在左上角和右下角嗎?
迷宮通路的數據表示法?
怎樣才能找到從入口到出口的那條唯一通路?
對我程序的注解。
******************************************************
■對我程序的注解(一)
我的程序是用C++寫的,因此明白了里邊幾個類和結構的作用,整個程序就不難理
解了。
☆圖形類Graph,實際上是為了簡化設置圖形模式而設的。
class Graph
{
public:
Graph(); /* 圖形模式初始化 */
~Graph(); /* 關閉圖表模式,恢復文本模式 */
};
這個Graph類僅定義了一個全局實例G。
☆表示方向的枚舉類型direction:
enum direction {west,north,east,south};
☆用來表示坐標的類Point:
class Point
{
public:
int x,y; /* 橫、縱坐標 */
Point(); /* default constructor */
~Point(){} /* destructor */
Point(int,int); /* constructor */
Point & operator=(const Point &); /* 賦值運算符重載 */
};
☆一個可以動態申請內存的二維數組類TwoDimensionArray:
class TwoDimensionArray
{
private:
int width,height; /* 二維數組的寬度和高度 */
int **pM,*spM; /* 指向動態申請的內存的指針 */
void Clear(); /* 釋放動態申請的內存 */
public:
TwoDimensionArray(); /* default constructor */
TwoDimensionArray(int,int); /* destructor */
~TwoDimensionArray(); /* destructor */
int SetSize(int,int); /* 設置二維數組的寬度和高度 */
int Get(int x,int y) const; /* 得到二維數組(x,y)處的值 */
int Set(int x,int y,int v); /* 設置二維數組(x,y)處的值為v */
int GetWidth() const {return width;} /* 得到二維數組的寬度 */
int GetHeight() const {return height;} /* 得到二維數組的高度 */
};
二維數組在程序中多次用到。
☆迷宮類Maze。這個類是最復雜的,也是本程序的核心。
class Maze
{
private:
int width,height; /* 迷宮的寬度和高度 */
Point start,end; /* 迷宮的起點和終點 */
/* 表示迷宮和路線的二維數組 */
TwoDimensionArray maze,path;
/* 判斷在位置p處的格子是否可以向dir方向延伸 */
int ExtendableInDir(
const TwoDimensionArray & m,
const Point & p,
direction dir);
/* 判斷在位置p處的格子是否可以延伸 */
int Extendable(const TwoDimensionArray &,const Point &);
/* 在迷宮中“可延伸格子”中,隨機選一個格子 */
Point FindExtendablePosition(const TwoDimensionArray &);
/* 在向左、上、右、下可延伸的方向中隨機選一個方向 */
direction RandDirection(int,int,int,int);
/* 根據迷宮的寬度和高度,隨機生成一個新迷宮 */
void GenerateMaze();
/* 生成迷宮從入口到出口的路線 */
void GeneratePath();
/* 用深度優先算法探路 */
void DetectPath(TwoDimensionArray &,Point &,int &);
public:
Maze(); /* default constructor */
Maze(int,int); /* constructor */
int SetSize(int,int); /* 設置迷宮的寬度和高度 */
void SetStart(const Point &); /* 設置迷宮的起點 */
void SetEnd(const Point &); /* 設置迷宮的終點 */
const Point & GetStart() const; /* 返回迷宮的起點 */
const Point & GetEnd() const; /* 返回迷宮的終點 */
int GetMaze(int x,int y) const; /* 返回迷宮中(x,y)格子的狀態 */
int GetPath(int x,int y) const; /* 返回路線數組中(x,y)處的值 */
int GetWidth() const{return width;} /* 返回迷宮的寬度 */
int GetHeight()const{return height;} /* 返回迷宮的寬度 */
};
☆下面是幾個全局函數:
/* 繪制界面 */
void Draw()
/* 繪制迷宮的寬度和高度 */
void DrawSize(int width,int height)
/* 繪制迷宮m */
void DrawMaze(const Maze & m)
/* 繪制迷宮m的正確路線 */
void DrawPath(const Maze & m)
/* 在迷宮m的位置p處向方向dir走一步
tr為走過的路線標記
新走過的路和回頭路用不同的顏色標記 */
int TraceMaze(
TwoDimensionArray & tr,
Point & cur,
const Maze & m,
direction dir)
<未完>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -