MFC中国象棋程序悔棋功能的实现思路

2025-02-25 10:28:05
推荐回答(3个)
回答1:

建一个状态类,用于保存当前棋局信息
建一个状态类的堆栈,可以用stl库里的stack或者vector
每执行一步,就建立一个状态类的对象,压栈,悔棋的时候,就将栈顶对象弹出,恢复棋局。如果还需要有重做功能的话,就把弹出的栈顶对象压到另一个堆栈记录中,如果有新操作就将这个堆栈清零,如果没有,就可以用这个堆栈实现重做功能

回答2:

最近时间不多。
你需要一个双向链表:
typedef struct MOVE {
int chess; //象棋编号,比如正的1、2、3表示黑子的车、马、炮,负的-1、-2、-3表示红子的车、马、炮
int x; int y; //该子的落点
struct MOVE * prev; //上一步
struct MOVE * next; //下一步
} M;
用上面的链表记录每一步要走的内容,比如:
void append(M ** history, int chess, int x, int y)
{
if(!history || !chess) return ;
M * move = new M; memset(move, 0, sizeof(M));
move->chess = chess; move->x = x; move->y = y;
if(*history)
*history = move;
}
然后落子的时候你就这样写:
CChessDlg::OnMove(...)
{
extern M * history; //需要一局棋的全局变量
append(&history, chess, x, y);
}
回放的时候遍历这个链表即可,
这个链表也可以用于悔棋。

回答3:

你需要一个双向链表:
typedef struct MOVE {
int chess; //象棋编号,比如正的1、2、3表示黑子的车、马、炮,负的-1、-2、-3表示红子的车、马、炮
int x; int y; //该子的落点
struct MOVE * prev; //上一步
struct MOVE * next; //下一步
} M;
用上面的链表记录每一步要走的内容,比如:
void append(M ** history, int chess, int x, int y)
{
if(!history || !chess) return ;
M * move = new M; memset(move, 0, sizeof(M));
move->chess = chess; move->x = x; move->y = y;
if(*history)
*history = move;
}
然后落子的时候你就这样写:
CChessDlg::OnMove(...)
{
extern M * history; //需要一局棋的全局变量
append(&history, chess, x, y);
}
回放的时候遍历这个链表即可