-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame_node.c
More file actions
74 lines (58 loc) · 1.7 KB
/
game_node.c
File metadata and controls
74 lines (58 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <stdlib.h>
#include <assert.h>
#include "game_node.h"
#include "state.h"
#include "list.h"
#include "utility.h"
struct GameNode * new_game_node( struct State * state, struct GameNode * parent )
{
struct GameNode * node = Calloc( 1, sizeof( struct GameNode ) );
assert( node );
node->state = state;
node->parent = parent;
node->children = new_list();
return node;
}
void add_child_game_node( struct GameNode * parent, struct GameNode * child )
{
child->parent = parent;
add_front( &parent->children, child );
}
void delete_children_game_node( struct GameNode * parent )
{
struct List * children = parent->children;
struct ListNode * current;
if( parent == NULL )
return;
for( current = children->head;
current != NULL;
current = current->next )
{
delete_children_game_node( current->data );
}
delete_list( &children );
Free( parent, sizeof( struct GameNode ) );
}
void delete_game_node( struct GameNode * root )
{
if( root == NULL )
return;
/* for each child */
if( root->children != NULL )
{
struct ListNode * current = root->children->head;
struct ListNode * temp;
while( current != NULL )
{
struct GameNode * temp_node = current->data;
Free( temp_node->state, sizeof( struct State ) );
Free( temp_node->best_move, sizeof( struct Move ) );
delete_game_node( current->data );
temp = current;
current = current->next;
Free( temp, sizeof( struct ListNode ) );
}
delete_list( &root->children );
}
Free( root, sizeof( struct GameNode ) );
}