ftp.nice.ch/pub/next/games/network/NeXTGo.2.7.NIHS.bs.gnutar.gz#/NeXTGo/smartgotree.c

This is smartgotree.c in view mode; [Download] [Up]

#include "comment.header"

/* $Id: smartgotree.c,v 1.3 1997/07/06 19:35:11 ergo Exp $ */

/*
 * $Log: smartgotree.c,v $
 * Revision 1.3  1997/07/06 19:35:11  ergo
 * actual version
 *
 * Revision 1.2  1997/05/04 18:57:15  ergo
 * added time control for moves
 *
 */

#include "smartgo.h"

/*  Define the following variable for debugging information.  */
/*  #define _DEBUG_ON_  */

#ifdef _DEBUG_ON_
#include <stdio.h>

void display_tree(node* root_node)
{
  node *tnode;

  tnode = root_node;

  while (tnode != NULL)
    {
      if (tnode->properties != NULL)
	printf(";");
      if (tnode->variants != NULL)
	{
	  printf("(");
	  display_tree(tnode->variants);
	  printf(")");
	}
      if (tnode->next == NULL)
	{
	  while (tnode->prev != NULL)
	    tnode = tnode->prev;
	  tnode = tnode->next_var;
	  if (tnode != NULL)
	    printf(")(");
	}
      else
	{
	  tnode = tnode->next;
	}
    }
}
#endif

node* forwardOneNode(node* currentNode)
{
  node *tnode;

  if (currentNode->variants != NULL)
    {
      currentNode->variants->recurse = 0;
      return currentNode->variants;
    }
  else if (currentNode->next != NULL)
    {
      currentNode->next->recurse = 0;
      return currentNode->next;
    }
  else
    {
      tnode = currentNode;
      while (tnode->prev != NULL)
	tnode = tnode->prev;
      if (tnode->next_var != NULL)
	{
	  tnode->next_var->recurse = 1;
	  return tnode->next_var;
	}
      else
	{
	  return forwardOneNode0(currentNode->parent);
	}
    }
}

node* forwardOneNode0(node* currentNode)
{
  node *tnode;

  if (currentNode->next != NULL)
    {
      currentNode->next->recurse = 1;
      return currentNode->next;
    }
  else
    {
      tnode = currentNode;
      while (tnode->prev != NULL)
	tnode = tnode->prev;
      if (tnode->next_var != NULL)
	{
	  tnode->next_var->recurse = 1;
	  return tnode->next_var;
	}
      else
	{
	  if (currentNode->parent != NULL)
	    {
	      return forwardOneNode0(currentNode->parent);
	    }
	  else
	    {
	      tnode->recurse = 1;
	      return tnode;
	    }
	}
    }
}

node* backOneNode(node* currentNode)
{
  if (currentNode->prev != NULL)
    {
      if (currentNode->prev->variants != NULL)
	{
	  return findLast(currentNode->prev->variants);
	}
      else
	{
	  currentNode->prev->recurse = 1;
	  return currentNode->prev;
	}
    }
  else
    {
      if (currentNode->prev_var != NULL)
	{
	  return findLast0(currentNode->prev_var);
	}
      else
	{
	  if (currentNode->parent != NULL)
	    {
	      currentNode->parent->recurse = 1;
	      return currentNode->parent;
	    }
	  else
	    {
	      return findLast(currentNode);
	    }
	}
    }
}

node* findLast(node* currentNode)
{
  node *tnode;

  tnode = currentNode;

  while (tnode->next_var != NULL)
    tnode = tnode->next_var;

  while (tnode->next != NULL)
    tnode = tnode->next;

  if (tnode->variants != NULL)
    {
      return findLast(tnode->variants);
    }
  else
    {
      tnode->recurse = 1;
      return tnode;
    }
}

node* findLast0(node* currentNode)
{
  node *tnode;

  tnode = currentNode;

  while (tnode->next != NULL)
    tnode = tnode->next;

  if (tnode->variants != NULL)
    {
      return findLast(tnode->variants);
    }
  else
    {
      tnode->recurse = 1;
      return tnode;
    }
}

node* forwardOneVariant(node* currentNode)
{
  node *tnode;

  tnode = currentNode;

  while (tnode->prev != NULL)
    tnode = tnode->prev;

  if (tnode->next_var != NULL)
    {
      tnode->next_var->recurse = 1;
      return tnode->next_var;
    }
  else
    {
      tnode->parent->variants->recurse = 1;
      return tnode->parent->variants;
    }
}

node* backOneVariant(node* currentNode)
{
  node *tnode;

  tnode = currentNode;

  while (tnode->prev != NULL)
    tnode = tnode->prev;

  if (tnode->prev_var != NULL)
    {
      tnode->prev_var->recurse = 1;
      return tnode->prev_var;
    }
  else
    {
      while (tnode->next_var != NULL)
	tnode = tnode->next_var;

      tnode->recurse = 1;
      return tnode;
    }
}

void clearNodeFlags(node* currentNode)
{
  node *r, *v;

  r = currentNode;
  while (r != NULL)
    {
      r->flag = 0;
      r->recurse = 0;
      v = r->variants;
      while (v != NULL)
	{
	  clearNodeFlags(v);
	  v = v->next_var;
	}
      r = r->next;
    }
}

int foundNode;

int evaluateSteps(node* currentNode, node* targetNode, unsigned char b[19][19])
{
  node *tnode, *vnodes;
  int i, j;
  extern int MAXX, MAXY;
  unsigned char b0[19][19];

  for (i = 0; i < 19; i++)
    for (j = 0; j < 19; j++)
      b0[i][j] = b[i][j];

  tnode = currentNode;
  while (tnode != targetNode)
    {
      if (tnode->properties != NULL)
	{
	  evaluateNode(tnode->properties, b0);
	  tnode->flag = 1;
	}

      vnodes = tnode->variants;
      while (vnodes != NULL)
	{
	  evaluateSteps(vnodes, targetNode, b0);
	  if (foundNode)
	    {
	      for (i = 0; i < MAXX; i++)
		for (j = 0; j < MAXY; j++)
		  b[i][j] = b0[i][j];

	      return 0;
	    }
	  vnodes = vnodes->next_var;
	}

      if (tnode->next == NULL)
	return 0;

      tnode = tnode->next;
    }

  if (tnode == targetNode)
    {
      foundNode = 1;
    }

  if ((tnode == targetNode) && (!tnode->flag) && (tnode->properties != NULL))
    {
      evaluateNode(tnode->properties, b0);
      tnode->flag = 1;

      for (i = 0; i < MAXX; i++)
	for (j = 0; j < MAXY; j++)
	  b[i][j] = b0[i][j];
    }

  return 0;
}

void buildToNode(node* targetNode)
{
  int i, j;
  extern int blackCaptured, whiteCaptured, lastMove, hist[19][19];
  extern unsigned char p[19][19];
  extern node *rootNode;

  for (i = 0; i < 19; i++)
    for (j = 0; j < 19; j++)
      p[i][j] = hist[i][j] = 0;
  blackCaptured = whiteCaptured = lastMove = 0;
  clearNodeFlags(rootNode);

  foundNode = 0;

  evaluateSteps(rootNode, targetNode, p);
}

node* stepForward(node* currentNode)
{
  node *tnode;
  extern node *rootNode;

  tnode = currentNode;
  do
    {
      tnode = forwardOneNode(tnode);
    }
  while ((tnode->properties == NULL) && (tnode != currentNode));

  if (tnode == currentNode)
    {
      tnode = rootNode;

      do
	{
	  tnode = forwardOneNode(tnode);
	}
      while (tnode->properties == NULL);
    }

  buildToNode(tnode);

  return tnode;
}

node* stepBackward(node* currentNode)
{
  node *tnode;
  extern node *rootNode;

  tnode = currentNode;
  do
    {
      tnode = backOneNode(tnode);
    }
  while ((tnode->properties == NULL) && (tnode != currentNode));

  if (tnode == currentNode)
    {
      tnode = rootNode;

      tnode = findLast(tnode);
    }

  buildToNode(tnode);

  return tnode;
}

node* jumpForward(node* currentNode)
{
  node *tnode;

  tnode = currentNode;

  tnode = forwardOneVariant(tnode);

  while (tnode->properties == NULL)
    {
      tnode = forwardOneNode(tnode);
    }

  buildToNode(tnode);

  return tnode;
}

node* jumpBackward(node* currentNode)
{
  node *tnode;

  tnode = currentNode;
  tnode = backOneVariant(tnode);

  while (tnode->properties == NULL)
    {
      tnode = forwardOneNode(tnode);
    }

  buildToNode(tnode);

  return tnode;
}

These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Marcel Waldvogel and Netfuture.ch.