c - 撤消功能在达到游戏初始状态后无法正常工作

标签 c pointers linked-list stack undo

我正在尝试为我的电路板创建一个撤消功能,以返回到上一个
董事会状态。
我还建立了一个临时保存功能,该功能创建一个包含所有先前游戏状态的列表。但是,当我撤消直到棋盘达到初始游戏状态(全部为空白)然后进行移动时,在4x4棋盘中说A3时,撤消功能无法返回到初始状态(全部为空白),并且保存了当前作为板的第一个状态移动,因此初始空白状态消失了...

如果有人对我可能做错了什么,请发表评论。
任何帮助表示赞赏。

我是一名初学者程序员,但我希望我对该问题的解释能提供很多信息。在此之后,我将把tempSave函数,Undo Function和我称为Board State的结构放置:

这是董事会状态的结构:

typedef struct state *stateptr;
struct state {
char **table;
stateptr prev;
};

extern stateptr last=NULL;


遵循tmpSave函数:

void tmpSave(char **table,int dim)
{
//dim is the dimension of the dim x dim board and i have a main 
//function that calls tmpSave after each move with the above parameters
int i,j;
stateptr newstate;
newstate=malloc(sizeof(struct state));

newstate->table=malloc(dim*sizeof(char*));
for(i=0; i<dim; i++)
{
    newstate->table[i]=malloc(dim*sizeof(char));
}

for(i=0; i<dim; i++)
{
    for(j=0; j<dim; j++)
    {
        newstate->table[i][j]=table[i][j];
    }
}
    if(last!=NULL)
    {
        newstate->prev=last;
        last=newstate;
    }
    else
    {
        printf("\nHi!!\n");
        last=newstate;
        last->prev=NULL;
    }
}


最后,撤消函数返回一个将在主函数中正确打印的表,START标志确定我们是否处于初始(空白)状态:

char **undo(short int *START)
{
stateptr prev=last->prev;
if(last->prev==NULL)
{
    printf("\nCan't further undo\n");
}
else
{
    free(last);
    last=prev;

}
if(last->prev==NULL) *START=1;

return last->table;
}


例:
1.首先板是空白的
2.移动
  0 0 0 0 w 0 0 0 0 0 0 0 0 0 0 0
3.另一招:
  0 0 w 0 w 0 0 0 0 0 0 0 0 0 0 0
4.撤消到初始状态(空白状态)
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 此时,'last'指针等于初始状态,因此last-> prev = NULL且* START变为1,该指针用于定义机器人玩家执行的移动(不要不要认真考虑)

5,另一招
0 0 0 w 0 0 0 0 0 0 0 0 0 0 0 0
6.Undo状态(此处出现描述的问题):
0 0 0 w 0 0 0 0 0 0 0 0 0 0 0 0
而应该是:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 (空白状态)

之后,初始状态为(1,4)上带有“ w”的错误状态。

最佳答案

由于示例不完整,因此我假设tablelast是全局的,并且DIM是常量。
首先调用tmpSave将存储空白板的副本。
在该结构中,将table作为类型char (*table)[DIM]使得可以通过一次调用分配和释放内存。它还使复制和复制table内容与存储的副本更加容易。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#define DIM 4

struct state {
    char (*table)[DIM];
    struct state *prev;
};

struct state *last = NULL;
char table[DIM][DIM];

void tmpSave ( )
{
    struct state *newstate = NULL;

    newstate = malloc ( sizeof ( *newstate));//allocate structure

    newstate->table = malloc ( sizeof table);//allocate table

    memmove (newstate->table, table, sizeof table);//copy table

    if ( last != NULL)
    {
        newstate->prev = last;
        last = newstate;
    }
    else
    {
        printf ( "\nHi!!\n");
        last = newstate;
        last->prev = NULL;
    }
}

void undo ( void)
{
    struct state *prev = last->prev;

    if ( last->prev == NULL)
    {
        printf ( "\nCan't further undo\n");
    }
    else
    {
        free ( last->table);//free table
        free ( last);//free structure
        last = prev;
        memmove ( table, last->table, sizeof table);//copy saved table back to table
    }
}

void showboard ( void)
{
    int i = 0;
    for ( i = 0; i < DIM; ++i)
    {
        printf ( "%.*s\n", DIM, table[i]);//no zero terminator so print only DIM number of characters
    }
}

struct state *freestate ( struct state *all)
{
    //make sure all structures are free
    while ( all) {
        struct state *tmp = all;
        free ( all->table);//free table
        free ( all);//free structure
        all = tmp->prev;
    }
    return NULL;
}

int main( void) {

    srand ( time ( NULL));//seed random number generator

    memset ( table, '0', sizeof table);//set table to all 0
    showboard ( );
    tmpSave ( last);//make sure empty table is saved

    for ( int each = 0; each < DIM; ++each) {//DIM moves
        int row = rand ( ) % DIM;
        int col = rand ( ) % DIM;

        table[col][row] = each + '1';
        tmpSave ( last);//save table

        printf ( "\tMove %d\n", each + 1);
        showboard ( );
    }
    for ( int each = 0; each < DIM + 1; ++each) {//undo DIM + 1
        printf ( "\tundo %d\n", each + 1);
        undo ( );
        showboard ( );
    }

    last = freestate ( last);

    return 0;
}


要在运行时输入dim,请使用char **table并根据需要进行分配。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

typedef struct State state;
struct State {
    char **table;
    state *prev;
};

state *tmpSave ( state *last, int dim, char **table) {
    state *newstate = NULL;

    if ( NULL == ( newstate = malloc ( sizeof ( *newstate)))) {//allocate structure
        fprintf ( stderr, "malloc newstate problem\n");
        return last;
    }

    if ( NULL == ( newstate->table = malloc ( sizeof *newstate->table * dim))) {//allocate table pointers
        fprintf ( stderr, "malloc newstate->table problem\n");
        free ( newstate);
        return last;
    }

    for ( int each = 0; each < dim; ++each) {
        if ( NULL == ( newstate->table[each] = malloc ( sizeof **newstate->table * dim))) {//chars to pointers
            fprintf ( stderr, "malloc newstate->table[each] problem\n");
            while ( each--) {
                free ( newstate->table[each]);
            }
            free ( newstate->table);
            free ( newstate);
            return last;
        }
        memmove ( newstate->table[each], table[each], dim);//copy table
    }

    if ( last != NULL) {
        newstate->prev = last;
        last = newstate;
    }
    else {
        printf ( "\nHi!!\n");
        last = newstate;
        last->prev = NULL;
    }

    return last;
}

state *undo ( state *last, int dim, char **table) {
    state *prev = last->prev;

    if ( last->prev == NULL) {
        printf ( "Can't further undo\n");
    }
    else {
        for ( int each = 0; each < dim; ++each) {
            free ( last->table[each]);//free chars
        }
        free ( last->table);//free table pointers
        free ( last);//free structure
        last = prev;
        for ( int each = 0; each < dim; ++each) {
            memmove ( table[each], last->table[each], dim);//copy saved table back to table
        }
    }

    return last;
}

void showboard ( int dim, char **table) {
    int show = 0;
    for ( show = 0; show < dim; ++show) {
        printf ( "%.*s\n", dim, table[show]);//no zero terminator so print only dim number of characters
    }
}

state *freeall ( state *all, int dim) {
    //make sure all structures are free
    while ( all) {
        state *tmp = all;
        for ( int each = 0; each < dim; ++each) {
            free ( all->table[each]);//free chars
        }
        free ( all->table);//free pointers
        free ( all);//free structure
        all = tmp->prev;
    }
    return NULL;
}

int main( void) {
    char input[100] = "";
    int dim = 0;
    int scanned = 0;
    char extra = 0;
    char **table = NULL;
    state *last = NULL;

    srand ( time ( NULL));//seed random number generator

    do {
        if ( 1 != scanned) {
            printf ( "enter a number only. try again\n");
        }
        printf ( "enter a number\n");
        if ( fgets ( input, sizeof input, stdin)) {
            scanned = sscanf ( input, "%d %c", &dim, &extra);
        }
        else {
            fprintf ( stderr, "fgets problem\n");
            return 0;
        }
    } while ( 1 != scanned);

    if ( NULL == ( table = malloc ( sizeof *table * dim))) {//allocate pointers
        fprintf ( stderr, "problem malloc table\n");
        return 0;
    }
    for ( int each = 0; each < dim; ++each) {
        if ( NULL == ( table[each] = malloc ( sizeof **table * dim))) {//allocate chars to pointers
            fprintf ( stderr, "problem malloc table[each]\n");
            while ( each--) {
                free ( table[each]);
            }
            free ( table);
            return 0;
        }
        memset ( table[each], '-', dim);//set chars to all -
    }

    showboard ( dim, table);
    last = tmpSave ( last, dim, table);//make sure empty table is saved

    for ( int move = 0; move < dim; ++move) {//dim number of random moves
        int row = rand ( ) % dim;
        int col = rand ( ) % dim;

        table[col][row] = move + 'A';
        last = tmpSave ( last, dim, table);//save table

        printf ( "\tMove %d of %d\n", move + 1, dim);
        showboard ( dim, table);
        printf ( "press enter\n");
        if ( ! fgets ( input, sizeof input, stdin)) {
            fprintf ( stderr, "fgets problem\n");
            last = freeall ( last, dim);
            return 0;
        }
    }
    for ( int move = 0; move < dim + 1; ++move) {//try to undo dim + 1 moves
        printf ( "\tundo %d of %d\n", move + 1, dim + 1);
        last = undo ( last, dim, table);
        showboard ( dim, table);
        printf ( "press enter\n");
        if ( ! fgets ( input, sizeof input, stdin)) {
            fprintf ( stderr, "fgets problem\n");
            last = freeall ( last, dim);
            return 0;
        }
    }

    last = freeall ( last, dim);

    return 0;
}

关于c - 撤消功能在达到游戏初始状态后无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54722467/

相关文章:

c - 程序进入无限循环而不是调用函数?

复制指针数组并打印副本

c - 从 pulseaudio 获取音量值

iphone - C 中分配内存的问题

C++ 类持有并返回指向另一个类的指针

c++ - 为什么我应该使用引用变量?

java - 交换线性链表中的值并递归返回副本

c - AND 运算符应用于链表中的指针时是什么意思?

C 的 printf 和 fprintf(stdout,) 不打印

c - 指向整数数组的指针