資源簡介
華容道基本功能,簡單的c++代碼實現,適合初學者,需要學過數組等內容,以及一點點對結構的了解。主要是提供一個編程思路。
代碼片段和文件信息
#include?
#include?
using?namespace?std;
//位置類型
struct?Position
{
int?x?y; //坐標位置(xy)
};
//棋子形狀類型
struct?Shape
{
int?h?w; //棋子的高度和寬度,以棋盤格度量
};
//棋子類型
struct?Chessman
{
char?*cmd_name; //棋子的控制命令
char?*surface; //棋子的顯示內容
struct?Shape?cur_size; //當前關卡棋子的形狀
struct?Position?cur_pos; //當前關卡棋子的當前位置
};
//棋子ID
enum?ChessID?{?CC/*曹操*/?GY/*關羽*/?ZF/*張飛*/?MC/*馬超*/?HZ/*黃忠*/?ZY/*趙云*/?
Z1/*卒1*/?Z2/*卒2*/?Z3/*卒3*/?Z4/*卒4*/?EMPTY?CHESS_NUM?=?EMPTY?};
//棋子
struct?Chessman?chessmen[CHESS_NUM]?=
{
{“CC““曹操曹操“}{“GY““關羽“}{“ZF““張飛“}{“MC““馬超“}{“HZ““黃忠“}{“ZY““趙云“}
{“Z1““Z1“}{“Z2““Z2“}{“Z3““Z3“}{“Z4““Z4“}
};
//移動ID
enum?MoveOp?{?UP?DOWN?LEFT?RIGHT?OP_NUM?};
//移動類型
struct?Move
{
char?name;
int?x;
int?y;
};
//移動方向
struct?Move?move_dir[]?=?{
{?‘U‘?-1?0?}
{?‘D‘?1?0?}
{?‘L‘?0?-1?}
{?‘R‘?0?1?}
};
//棋盤格類型
struct?Cell
{
enum?ChessID?id; //所放棋子ID
int?part; //所屬棋子局部位置
};
//關卡類型
struct?Level
{
char?*name; //關卡名稱
struct?Shape?size[CHESS_NUM]; //關卡棋子形狀
struct?Position?org[CHESS_NUM]; //關卡棋子初始位置
int?step; //當前步數
};
struct?Cell?map[5][4]; //棋盤布局
struct?Level?levels[]?= //游戲關卡
{
{?“橫刀立馬“?
/*曹操?關羽?張飛?馬超?黃忠?趙云?卒1??卒2??卒3??卒4?*/
{{22}{12}{21}{21}{21}{21}{11}{11}{11}{11}}
{{01}{21}{23}{20}{00}{03}{31}{32}{40}{43}}}
};
struct?Level?*pcur_level; //當前關卡
void?ClearMap();
void?ShowMap();
void?ShowChessMan(?enum?ChessID?id?int?part?);
bool?PlaceChessMan(?enum?ChessID?id?struct?Shape?&s?struct?Position?&pos?);
//開始游戲
bool?Start(?struct?Level?*lv?)
{
int?i;
ClearMap();
for(?i?=?0;?i? PlaceChessMan(?(enum?ChessID)i?lv->size[i]?lv->org[i]?);
lv->step?=?0;
ShowMap();
return?true;
}
//清空棋盤
void?ClearMap()
{
int?i?j;
for(?i?=?0;?i?5;?i++?)
for(?j?=?0;?j?4;?j++?)
map[i][j].id?=?EMPTY;
return;
}
//顯示棋盤布局
void?ShowMap()
{
int?i?=?0?j;
system(“cls“);
cout?<step?<“?steps:“?< for(?i?=?0;?i?5;?i++?)
{
for(?j?=?0;?j?4;?j++?)
ShowChessMan(?map[i][j].id?map[i][j].part?);
cout?<‘\n‘;
}
cout?<‘\n‘;
return;
}
//顯示棋子
void?ShowChessMan(?enum?ChessID?id?int?part?)
{
struct?Chessman?*p?=?&chessmen[id];
int?pos?=?part*2;
if(?id?!=?EMPTY?)
{
cout?<surface[pos];
cout?<surface[pos+1];
}
else
cout?<“??“;
return;
}
//放置棋子
bool?PlaceChessMan(?enum?ChessID?id?struct?Shape?&s?struct?Position?&pos?)
{
int?i?j?part?=?0;
chessmen[id].cur_size?=?s;
chessmen[id].cur_pos?=?pos;
for(?i?=?0;?i? {
int?x?=?pos.x?+?i;
for(?j?=?0;?j? {
int?y?=?pos.y?+?j;
map[x][y].id?=?id;
map[x][y].part?=?part++;
}
}
return?true;
}
//移動棋子
bool?MoveChessMan(?enum?ChessID?id?enum?MoveOp?op?)
{
struct?Chessman?*p?=?&chessmen[id];
struct?Move?*q?=?&move_dir[op];
int?i?j;
if(?q->x?!=?0?)
{
- 上一篇:基于暗通道和導向濾波的圖像去霧算法C++實現
- 下一篇:基于二叉堆優化的A星算法
評論
共有 條評論