#include <u.h>
#include <libc.h>
#include <draw.h>
#include <igo.h>
/*
* operations over move datatype, grp is many moves
* in a linked list.
*/
void
fillmov(Move *m, Point pos, char type)
{
assert(m);
m->Point = pos;
m->type = type;
m->next = nil;
}
void
freegrp(Move *m)
{
Move *p,*next;
for(p = m; p != nil; p = next){
next = p->next;
free(p);
}
}
int
opgrp(Move *grp, char type, void (*op)(Move *m, char type))
{
int nmoves;
Move *p,*next;
nmoves = 0;
for(p = grp; p != nil; p = next){
next = p->next;
nmoves++;
if(op)
op(p, type);
}
return nmoves;
}
//Probably buggy, not tried yet..
Move *
clonegrp(Move *grp)
{
Move *p, *c, *clgrp;
if(!grp)
return nil;
clgrp = malloc(sizeof(Move));
c = clgrp;
for(p = grp; p != nil; p = p->next){
c->type = p->type;
c->Point = p->Point;
if(p->next == nil)
c->next = nil;
else{
c->next = malloc(sizeof(Move));
c = c->next;
}
}
return clgrp;
}
/*Adds at the end*/
Move *
addlast(Move *g, Move *m)
{
Move *p, *next;
if(verbose)
dprint("Addlast %P\n",m->Point);
if(!g)
return m;
for(p = g; ; p = next){
next = p->next;
if(!next)
break;
}
p->next = m;
return g;
}
Move *
pushmov(Move *g, Move *m)
{
if(!m)
return nil;
m->next = g;
return m;
}
Move *
popmov(Move **m, Move *g)
{
Move *ng;
if(!g)
return nil;
ng = g->next;
g->next = nil;
*m = g;
return ng;
}
void
opprint(Move *m, char)
{
if(!m){
dprint("nil\n");
return;
}
dprint("%c %d %d\n",m->type, m->Point.x, m->Point.y);
}
void
opdebug(Move *m, char)
{
if(!m){
dprint("nil\n");
return;
}
dprint("%p, %P, %c, [%p]->|||",m,m->Point, m->type, m->next);
}
void
opsettype(Move *m, char c)
{
if(!m){
dprint("nil\n");
return;
}
m->type = c;
}
void
opnop(Move *,char)
{
return;
}
Move *
freelast(Move *g)
{
Move *p, **pp;
pp = &g;
for(p = g; p != nil; p = p->next){
if(!p->next)
break;
pp = &p->next;
}
free(p);
if(!pp)
return nil;
*pp = nil;
return g;
}
/*Take out the last move from a group and return it*/
Move *
takelast(Move **grp)
{
Move *p, **pp, *g;
g = *grp;
pp = grp;
for(p=g; p != nil; p = p->next){
if(!p->next)
break;
pp = &p->next;
}
*pp = nil;
*grp = g;
return p;
}
|