Exemple #1
0
/* Imprime na tela o tabuleiro atual. */
void CRUELprint() {
    int i;
    for( i = 0; i < 4; i++ ) {
        if( STACKempty(suitStack[i]) )
            printf("     ");
        else
            CARDprint(STACKpeek(suitStack[i]));
    }
    printf("\n------------------\n");
    for( i = 0; i < 12; i++ ) {
        if( STACKempty(cardStack[i]) )
            printf("     ");
        else
            CARDprint(STACKpeek(cardStack[i]));
        if( i % 4 == 3 )
            printf("\n");
    }
}
Exemple #2
0
/* --- Function: void pop_node(Stack stk) --- */
void pop_node(Stack stk)
{
  int tmp, *pi, *ptmp;
  char mess[BUFSIZ], ans;

  /* Initialize 'tmp'... */
  tmp = 0;

  do
    {
      if (tmp == -1)
        break;

      my_clearscrn();
      printf("--- POP NODE FROM STACK ---\n");
      printf("\nCurrent stack status(%d nodes): ", STACKsize(stk));
      SLISTtraverse(stk, print, SLIST_FWD);

      ptmp = (int *)STACKpeek(stk);

      if (ptmp  == NULL)
        {
          prompt_and_pause("\n\nStack is EMPTY - nothing to do..!");
          tmp = -1;
        }
      else
        {
          sprintf(mess, "\nAbout to pop node %d.. - Continue? (y/n): ", *ptmp);
          ans = read_char(mess, 0, 0, my_chkch);

          if (ans == 'y' || ans == 'Y')
            {
              if ((STACKpop(stk, (void **)&pi)) != OK)
                {
                  printf("\nFatal error popping data - exiting...!");
                  STACKdestroy(stk);
                  exit(-1);
                }
              else
                {
                  sprintf(mess, "Node %d will be popped!", *pi);
                  prompt_and_pause(mess);
                  my_destroy(pi);
                }
            }
          else
            tmp = -1;
        }
    } while (TRUE);
}
Exemple #3
0
/* Analisa o jogo atual e devolve uma jogada. Se a jogada devolvida
for invalida, significa que nao ha mais jogadas possiveis. */
action CRUELsugestaction() {
    card c;
    int suitVal[4], i, k;
    if( lastSugest.to != -1 )
        return lastSugest;
    
    for( i = 0; i < 4; i++ ) {
        c = STACKpeek( suitStack[suitPosition[i]] );
        suitVal[i] = CARDval(c);
    }
    /* primeiro verifica se da pra mover alguma carta para uma 
        pilha de naipes, iniciando de uma pilha qualquer. */
    for( k = rand(), i = 0; i < 12; i++, k++ ) {
        k = k % 12;
        if( STACKempty( cardStack[k] ) )
            continue;
        c = STACKpeek( cardStack[k] );
        if( CARDval(c) == suitVal[CARDsuit(c)] + 1 ) {
            return lastSugest = ACTIONcreateTargetless(k);
        }
    }
    
    for( i = 0; i < 11; i++ ) {
        if( STACKempty( cardStack[i] ) )
            continue;
        c = STACKpeek( cardStack[i] );
        for( k = i + 1; k < 12; k++ ) {
            if( STACKempty( cardStack[k] ) )
                continue;
            if( CARDsuit(c) == CARDsuit( STACKpeek( cardStack[k] ) )
                && CARDval(c) == CARDval( STACKpeek( cardStack[k] ) ) - 1 )
                return lastSugest = ACTIONcreate( i, k, 1 );
        }
    }
    return ACTIONcreate( -1, -1, -1 );
}
Exemple #4
0
int CRUELvalid(action a) {
    stack from, to;
    from = cardStack[a.from];
    to = ( a.suit == 0 ) ? suitStack[a.to] : cardStack[a.to];
    if( STACKempty(from) || STACKempty(to) ) return 0; /* Pilhas vazias nao sao validas. */
    if( CARDsuit( STACKpeek(from) ) != CARDsuit( STACKpeek(to) ) ) return 0;
    
    if( a.suit == 0 )
        /* Para mover para a pilha de naipe, a carta tem que ter um valor 1 maior. */
        return ( CARDval( STACKpeek(from) ) - 1 == CARDval( STACKpeek(to) ) );
    else
        /* Para mover para outra pilha de cartas, a carta tem que ter um valor 1 menor. */
        return ( CARDval( STACKpeek(from) ) + 1 == CARDval( STACKpeek(to) ) );
}
Exemple #5
0
int main(void)
{
  /* Declare YOUR variables here ! */
  Stack mystk;
  Queue myqueue;
  char mess[BUFSIZ];
  int i, nr;

  srand((unsigned int)time(NULL));
  my_clearscrn();

  printf("--- INITIALIZING A QUEUE, %d ELEMENTS, RANDOM INTEGER DATA ---", NR_OF_ITEMS);
  if ((myqueue = QUEUEinit(my_destroy)) == NULL) /* Initialize the queue... */
    {
      printf("\nFatal error - bailing out...!");
      exit(-1);
    }

  queue_elements(myqueue, NR_OF_ITEMS); /* Populate the queue... */

  nr = QUEUEsize(myqueue)/2;  /* Save half the size of the queue... */
  sprintf(mess, "\nNext - let's DEQUEUE %d elements from our queue...", nr);
  prompt_and_pause(mess);
  prompt_and_pause("...and now PUSH them - on a brand, new STACK...!!");

  if ((mystk = STACKinit(my_destroy)) == NULL) /* Set up a new stack... */
    {
      printf("\nFatal error - bailing out...!");
      exit(-1);
    }

  for (i = 0; i < nr; ++i)
    {
      void *piq, *pis;
      int retval;

      retval = QUEUEdequeue(myqueue, &piq);
      assert(retval == OK);

      sprintf(mess, "QUEUE: Dequeued: %02d (new frontvalue: %02d)", *(int *)piq, *(int *)QUEUEpeek(myqueue));
      prompt_and_pause(mess);

      /* Check current stack top... */
      pis = STACKpeek(mystk);
      /* Push the value just dequeued - from our queue... */
      retval = STACKpush(mystk, piq);
      assert(retval == OK);

      if (pis == NULL) /* If this is the FIRST stack push... */
	sprintf(mess, "STACK: Pushed  : %02d (old stacktop  : none)", *(int *)STACKpeek(mystk));
      else
	sprintf(mess, "STACK: Pushed  : %02d (old stacktop  : %02d)", *(int *)STACKpeek(mystk), *(int *)pis);

      /* Print the message assembled above... */
      prompt_and_pause(mess);
    }

  printf("\n--- CURRENT STATUS OF STACK AND QUEUE ---");
  printf("\nStack: ");
  SLISTtraverse(mystk, print, SLIST_FWD);
  printf("\nQueue: ");
  SLISTtraverse(myqueue, print, SLIST_FWD);
  
  prompt_and_pause("\n\nLet's tidy up (destroy queue/stack) - Bye!");

  SLISTdestroy(mystk);
  SLISTdestroy(myqueue);

  return 0;
}
Exemple #6
0
action ACTIONcreateTargetless(unsigned int from) {
    /* A posicao de uma pilha de naipes eh aleatoria, mas foi 
        armazenada no vetor suitPosition. */
    return ACTIONcreate( from, suitPosition[ CARDsuit( STACKpeek( cardStack[from] ) ) ], 0 );
}