Plan 9 from Bell Labs’s /usr/web/sources/contrib/akumar/igo/goban/move.c

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


#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, *next, *prev;

	if(!g)
		return nil;
	prev = nil;
	for(p = g; ; p = next){
		next = p->next;
		if(!next)
			break;
		prev = p;
	}
	free(p);
	if(!prev)
		return nil;

	prev->next = nil;
	return g;
}

/*Take out the last move from a group and return it*/
Move *
takelast(Move **grp)
{
	Move *p, *next, *prev,*last, *g;

	g = *grp;
	if(!g){
		*grp=nil;
		return nil;
	}
	prev = nil;
	for(p=g; ; p = next){
		next = p->next;
		if(!next)
			break;
		prev = p;
	}
	last = p;
	if(!prev){
		return nil;
	}
	prev->next = nil;
	*grp = g;
	return last;
}


Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to webmaster@9p.io.