新年快乐 .
这个问题源于一个在C中编写一个名为Laby-chiffres的游戏的项目 . 要在这个链接中看到第三批游戏中的游戏:http://www.rci-jeux.com/jeux/labychiffres/laby.swf .
Context :我使用链接列表来存储玩家通过数字网格所采用的路径 . 正确记录每个号码的玩家's aim is to find a path from the departure point to the arrival point, with a given length and given total of the numbers in the path. There is a switch statement for a menu and then one to accept the direction the player wants to move in. This works as I have code that shows that the '段落'
Problem :当我尝试打印路径时(这是作业的要求 - 对玩家来说很有用)我按照正确的顺序打印路径,除了最后一个元素,即玩家最近搬到的地方,显示在列表的开头和结尾 .
示例:玩家通过具有如下值的位置进行移动 . 从出发点值5 - > 8 - > 4->开始1.每个阶段打印的内容是:
5->
8->8->
4->8->4->
1->8->4->1->
What I have tried 我查看了其他几个链表问题,但我所看到的都没有与我有相同的问题 . 我试过改变很多东西,但说实话现在比实际更多 . 例如,更改 empiler
函数以在错误的末尾添加元素以错误的顺序打印路径(如预期的那样)但仍然打印最近达到的数字两次,只是在路径的开头而不是在开始时的一个和一个在最后 .
Relevant functions 讲师明确表示打印需要颠倒顺序,因此这些是打印功能:
void affiche_chemin (PLATEAU jeu, PILE pile){
afficher_pile_inverse(&jeu , jeu.chemin.P_sommet);
printf("\n");
}
void afficher_pile_inverse(PLATEAU *P_jeu, ELEMENT *P_cur){
if(P_cur != NULL){
afficher_pile_inverse(P_jeu,P_cur->P_suivant);
printf("%d->",lire_valeur_a(P_jeu,&P_cur->valeur));
}
}
lire_valeur_a
读取网格中的值,该值是下面给出的 PLATEAU
结构的一部分,并从文本文件中读入 . 这似乎有效,所以我不包括它试图保持问题的长度 .
添加元素的功能是:
ELEMENT* empiler(PILE *P_liste, ELEMENT *P_elt_ajoute){
P_elt_ajoute ->P_suivant = P_liste->P_sommet;
P_liste->P_sommet = P_elt_ajoute;
return P_elt_ajoute;
}
鉴于讲师评论需要颠倒正确打印的顺序,我想我在列表的右端添加元素 .
这些函数初始化路径,并分配一个新的 ELEMENT
,仅供参考,如下一个提取中所用 .
void initialiser_pile(PILE *P_pile){
P_pile->P_sommet = NULL;
}
ELEMENT *nouvel_element (POSITION nouvelle_valeur){
ELEMENT *P_elt;
P_elt =(ELEMENT*) malloc(sizeof(ELEMENT));
if(P_elt ) { /* NULL equivalent to false like 0 */
P_elt->valeur = nouvelle_valeur;
P_elt->P_suivant = NULL;
}
return P_elt;
}
此代码在读取游戏的文本文件时首次设置路径,因此这是第一次使用 empiler
将元素添加到路径中 . (此摘录来自多次使用 fscanf
的长函数来读取游戏文本文件,并且似乎正常工作 . )
ELEMENT *P_sommet = nouvel_element(PLAT->dep);
if (P_sommet == NULL){
printf("Erreur d'allocation\n");
return 0;
}
initialiser_pile(&PLAT->chemin);
empiler (&PLAT->chemin,P_sommet);
PLAT->longcur = 1;
PLAT->sumcur=PLAT->grille[PLAT->dep.indl][PLAT->dep.indc];
以下函数用于在游戏过程中添加元素 .
int choix_indep_jeu_update(PLATEAU *jeu, POSITION *new_pos, int pas, int dir){
ELEMENT *new = nouvel_element(*new_pos);//1 Make new element
if (new == NULL) return 0;
empiler( &jeu->chemin, new );//should add new element
jeu->longcur++;
jeu->sumcur = jeu->sumcur + lire_valeur_a(jeu, new_pos);
affiche_grille(*jeu);
affiche_chemin(*jeu,jeu->chemin);
return 1;
}
Data Structures typedef struct position_st {int indl; // indice of ligne int indc; // indice of colonne} POSITION;
typedef struct element_st{
POSITION valeur;
struct element_st *P_suivant;
}ELEMENT;
typedef struct pile_st{
ELEMENT * P_sommet;
}PILE;
typedef struct plat_st{
//########## GAME FILE INFORMATION
int nl; //number of lines in grid
int nc; //number of columns in grid
POSITION dep; //position du depart: dep.indl and dep.indc
POSITION arr; //position d'arrive: arr.indl and arr.indc
int longdem; //length demanded
int sumdem; //total demanded
int ** grille; //Playing grid
//#######INFO re GAME IN PROGRESS ########
int longcur; //longueur courant
int sumcur; //totale courant
PILE chemin; //The path
}PLATEAU;
1 回答
我简化了你的代码并且它似乎正在工作,假设你希望empiler()将元素插入到列表的前面,所以它们以相反的顺序结束 . 我添加了afficher_pile_inverse1(),以便最后一个值后面没有“ - >” . 因此,错误在于创建列表,或者您没有显示的网格功能存在问题 .