我正在尝试为我的电路板创建一个撤消功能,以返回到上一个 董事会状态。 我还建立了一个临时保存功能,该功能创建一个包含所有先前游戏状态的列表。但是,当我撤消直到棋盘达到初始游戏状态(全部为空白)然后进行移动时,在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;
}
}
最后是undo函数,该函数返回将在main函数中正确打印的表,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'。
答案 0 :(得分:0)
由于示例不完整,我假设table
和last
是全局的,并且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;
}