hello so this is a long code whit multypule files so i will try to explain:
this is a porject nad this is my q3: make A func that returns a tree that symblosie all the possible moves a knight can take whit out stepping on the same sqaure twice(so it wont be infinit).
assume i have this 2 funcs (from q1 , q2 my partener does and its isnt neccery for q3)
chessPosArray*** validKnightMoves();
// this returns a pointer of a 2d _chessPosArray of all the possible moves a knight can take in every location on the board (so in A2 he can move to xy,xz,yz
locations)
why its 3(actuly 4) layers of pointers? i dont know i should as my teachers why they hate us.
and void freeCPA(chessPosArray*** possArr);
// this one free the 2d array.
and this 2 typedef chessPos[2]
(0 is letters and 1 is numbers)
typedef struct _chessPosArray {
unsigned int size;
chessPos* positions;
} chessPosArray;
in q3 a tree node has 2 pieces of info. a chesspos and a special list of pointers to the next tree nodes.
here all the type def i use for q3 :
typedef int chessboard[B_SIZE][B_SIZE]// a map of the board . if its a 0 the tree nod exist in the p
here is the code for q3:
#ifndef B_SIZE
#define B_SIZE 5
#endif // !B_SIZE
#include <iostream>
#include<stdbool.h>
#include<stdio.h>
#include<limits.h>
#include<string.h>
#include <stdlib.h>
#include<math.h>
#include "Node.h"
#include "q1.h"
typedef int chessboard[B_SIZE][B_SIZE];//a hash map of the board 0 there is no pos 1 there is)
pathTree findAllPossibleKnightPaths(chessPos* startingPosition);//the main func of the q
void FAPLKP_Rec(treeNode* cur, chessboard* checkBoard, chessPosArray*** TheposArr);// the rec of that func
void buildListFormPosArr(List* lst, chessPos pos, chessPosArray*** TheposArr);//builds list from an array of knigh possible move in X pos
void RemuveRepitingPos(List* lst, chessboard* checkBoard);// remuve nodes that there pos all ready apears on the hash map
void deletNodeFromMidList(ListNode* deleted, ListNode* pre, list lst);//the func that actually delet them
int main()
{
chessPos pos;
pos[0] = 'B'; pos[1] = '2';
pathTree path = findAllPossibleKnightPaths(&pos);
freeTree(path);
}
pathTree findAllPossibleKnightPaths(chessPos* startingPosition) {
pathTree thePaths;
chessboard theBoard;
memset(theBoard, 0, sizeof(theBoard));
theBoard[*startingPosition[0] - 'A'][*startingPosition[1] - '1'] = 1;
chessPosArray*** TheposArr = validKnightMoves();
thePaths.root = createNewTreeNode(*startingPosition);
FAPLKP_Rec(thePaths.root, &theBoard, TheposArr);
freeCPA(TheposArr);
return thePaths;
}
void FAPLKP_Rec(treeNode* cur, chessboard* checkBoard, chessPosArray*** TheposArr) {
List lst;
makeEmpthyList(&lst);
buildListFormPosArr(&lst, cur->position, TheposArr);
printList(lst);
RemuveRepitingPos(&lst, checkBoard);
P_insert_LIST_intoTNL(&cur->next_possible_positions, &lst);//makes the Tlist. makes so all the tree its pointed to have the pos in pos and have empthy Tlist
freeList(lst);
treeNodeListCell* runner = cur->next_possible_positions.head;
while (runner != NULL)
{
*checkBoard[runner->node->position[0] - 'A'][runner->node->position[1] - '1'] = 1;
FAPLKP_Rec(runner->node, checkBoard, TheposArr);
runner = runner->next;
}
*checkBoard[cur->position[0] - 'A'][cur->position[1] - '1'] = 0;
}
void RemuveRepitingPos(List* lst, chessboard* checkBoard) {
ListNode* cur = lst->head;
ListNode* deleted = NULL;
ListNode* pre=NULL;
while (cur!=NULL) {
printf("checking pos (%c,%c): ", cur->pos[0], cur->pos[1]);
if (*checkBoard[cur->pos[0]-'A'][cur->pos[1]-'1'] == 1) {
printf("Pos invalidn");
deleted = cur;
cur = cur->next;
deletNodeFromMidList(deleted, pre, *lst);
}
else {
printf("Pos validen");
pre = cur;
cur = cur->next;
}
}
}
void deletNodeFromMidList(ListNode* deleted, ListNode* pre, list lst) {
if (pre == NULL) {//you remuve head
lst.head = deleted->next;
}
else if (deleted==lst.tail)
{
lst.tail = pre;
}
else
{
pre->next = deleted->next;
}
free(deleted);
}
void buildListFormPosArr(List* lst, chessPos pos, chessPosArray*** TheposArr) {
int row = pos[0] - 'A';
int col = pos[1] - '1';
int len = TheposArr[row][col]->size;
chessPos* runner = TheposArr[row][col]->positions;
for (int i = 0; i < len; i++) {
insertDataToEndList(lst, *runner);
runner++;
}
}
type here
here is the code for the node.s a file whit all the trees and lists and the func on them:
the type deff;
typedef struct _treeNode treeNode;
typedef struct _treeNodeListCell {
treeNode *node;
struct _treeNodeListCell* next;
} treeNodeListCell;
typedef struct _treeNodeList {
struct _treeNodeListCell* head;
struct _treeNodeListCell* tail;
} treeNodeList;
//tree
typedef struct _treeNode {
chessPos position;
treeNodeList next_possible_positions; // the pointers
} treeNode;
typedef struct _pathTree {
treeNode* root;
} pathTree;
//regular node
typedef struct listNode {
chessPos pos;
struct listNode* next;
} ListNode;
typedef struct list {
ListNode* head;
ListNode* tail;
}List;
yes the names are confusing i didnt choose them they where givin to us and yes i want to kill them for that.
here is the actual code:
void P_insert_LIST_intoTNL(treeNodeList* t_lst, List* lst) {
t_lst->head = NULL;
t_lst->tail = NULL;
ListNode* cur = lst->head;
while (cur!=NULL) {
P_insertDataToEndList(t_lst, cur);
cur = cur->next;
}
}
void P_insertDataToEndList(treeNodeList* lst, ListNode* node) {
treeNodeListCell* newTail;
newTail = P_createNewListNode(node, NULL); P_insertNodeToEndList(lst, newTail);
}
void P_insertNodeToEndList(treeNodeList* lst, treeNodeListCell* newTail) {
if (lst != NULL)
lst->head = lst->tail = newTail;
else
{
lst->tail->next = newTail;
lst->tail = newTail;
}
}
treeNodeListCell* P_createNewListNode(ListNode* node, treeNodeListCell* next) {
treeNodeListCell* newN = (treeNodeListCell*)malloc(sizeof(treeNodeListCell));
if (!newN)
exit(1);
newN->node = createNewTreeNode(node->pos);
newN->next = next;
return newN;
}
treeNode* createNewTreeNode(chessPos pos) {
treeNode* node = (treeNode*)malloc(sizeof(treeNode));
if (!node)
exit(0);
transferPosData(&node->position, pos);
node->next_possible_positions.head = NULL;
node->next_possible_positions.tail = NULL;
return node;
}
void transferPosData(chessPos* resiver, chessPos trasferer) {
(*resiver)[0] = trasferer[0];
(*resiver)[1] = trasferer[1];
}
void freeTree(pathTree pt) {
if (pt.root != NULL)
freeTreeRec(pt.root);
}
void freeTreeRec(treeNode* cur) {
if (!cur->next_possible_positions.head) {
free(cur);
return;
}
treeNodeListCell* runner = cur->next_possible_positions.head;
while (runner!=NULL)
{
treeNodeListCell* temp = runner;
freeTreeRec(temp->node);
runner = runner->next;
free(temp);
}
free(cur);
}
void P_freeList(treeNodeList lst) {
if (!lst.head)
return;
P_freeListRec(lst.head);
}
void P_freeListRec(treeNodeListCell* head) {
treeNodeListCell* temp;
while (head!=NULL)
{
temp = head;
head=head->next;
free(temp);
}
}
void insertDataToEndList(List* lst, chessPos pos) {
ListNode* newTail;
newTail = createNewListNode(pos, NULL); insertNodeToEndList(lst, newTail);
}
void insertNodeToEndList(List* lst, ListNode* newTail) {
if (lst != NULL)
lst->head = lst->tail = newTail;
else
{
lst->tail->next = newTail;
lst->tail = newTail;
}
}
ListNode* createNewListNode(chessPos pos, ListNode* next) {
ListNode* node = (ListNode*)malloc(sizeof(ListNode));
if (!node)
exit(1);
transferPosData(&node->pos, pos);
node->next = next;
return node;
}
void makeEmpthyList(list* list) {
list->head = NULL;
list->tail = NULL;
}
void freeList(List lst) {
freeListRec(lst.head);
}
void freeListRec(ListNode* head) {
ListNode* temp;
while (head!=NULL)
{
temp = head;
head = head->next;
free(temp);
}
}
void printList(List lst) {
if (!lst.head) {
printf("lst is empthyn");
}
printListRec(lst.head);
}
void printListRec(ListNode* head) {
while (!head)
{
printf("(%d,%d) ", head->pos[0], head->pos[1]);
}
printf("n");
}
the algorithem seppous to work like that:
1)i creat a list of pos
2) i insert in the list all of the possible knight moves from the current tree node pos
3) i check whit the boardmap to see if there is somthing reapiting . remuve if does
4) ubdate the boardmap
5) go to the next tree node in the list.
6) when finishing whit this treenode i ubdate the boardmap again.
now the problem i have (for the people who halped the prevoius problem thanks its halp but now a new problem arouse )
is its in here (*checkBoard)[runner->node->position[0] - 'A'][runner->node->position[1] - '1'] = 1;
runner->node doasnt exists and i dont know why, in my func creating a by the end of creating of the tree node i have a list of pointers who point into trees whit pos and emthpy lists. and its seems that pointers are pointing into empthy lists and i dont know why, im trying to check in the code but i can finde where the creation of the tree nodes happen
also in deletNodeFromMidList its says temp is undceclard identfire and i have now idea why its started to do that.
mybe to undarstand what i did worng, where the logic crush. why temp says its doasnt exist when it does
on what i tried i follow the code step by step , i did allot of prints, i checked whit chat. even did f11(its was long kill me)
on what i tried i follow the code step by step , i did allot of prints, i checked whit chat. even did f11(its was long kill me) and i allready ask for halp for the Q but the stack overlords removed it (i dont know why)