目錄表

國立屏東大學 資訊工程學系 程式設計

23. numAB2: 1A2B猜數字遊戲之二(換電腦來猜)


23.1 Project Description

23.2 Source Code

#include <stdio.h>
#include <stdlib.h>
#include <time.h>


#define numlong 4
#define maxAnswers 10000

int guess[numlong];
int answer[numlong];

int possibleAnswers[maxAnswers][numlong];

int pivot= (maxAnswers-1);

int QWQ=1;
int A, B;
int checkA, checkB;
int cur=0;

void initial();

void getUserGuess();
int checkAGuess();
void showAnAnswer(int);
int checkDup(int);
void swap2PossibleAnswers(int, int);
void moveAPossibleAnswer(int, int);
void makeAGuess();
void updatePossibleAnswers();

int main()
{
   int quit =0;
   int qq=0;
   int count = 0;

   initial();

   while(!quit)
   {

       cur = makeAGuess();

       printf("A=?");
       scanf("%d", &A);

       printf("B=?");
       scanf("%d", &B);

       if(A==numlong)
       {
         printf("....^_^....\n");
         quit=1;
       }

       updatePossibleAnswers();

   }

  printf("GoodBye\n!");

}


void updatePossibleAnswers()
{
  i=0;
  while(i < pivot)
  {
    if(i!=cur)
    {
      checkAB(cur,i);
      if ((A != checkA )||(B != checkB))
      {
        moveAPossibleAnswer(pivot, i);
        pivot--;
      }
      else
      {
        i++;
      }

    }

  }
}


void makeAGuess()
{
  int r;
  srand(time(NULL));
  r = rand() % pivot;
  printf("pivot=%d rand=%d\n", pivot,r);
  printf("I guess the answer is ");
  showAnAnswer(r);
}

void showAnAnswer(int x)
{
  int i=0;
  for(i=0; i< numlong; i++)
  {
    printf("%d", possibleAnswers[x][i]);
  }
  printf("\n");
  }

void initial()
{

  int i;


  for(i=0;i<maxAnswers;i++)
  {
      possibleAnswers[i][3] = ((int)(i / 1)) % 10;
      possibleAnswers[i][2] = ((int)(i / 10)) % 10;
      possibleAnswers[i][1] = ((int)(i / 100)) % 10;
      possibleAnswers[i][0] = ((int)(i / 1000)) % 10;
  }


  i=0;
  while( i < pivot)
  {


    if( checkDup(i) )
    {
      moveAPossibleAnswer(pivot, i);
      pivot--;
    }
    else
    {
      i++;
    }

  }

}

void moveAPossibleAnswer(int f, int t)
{
  int i;
  for(i=0;i<numlong;i++)
  {
    possibleAnswers[t][i]=possibleAnswers[f][i];
  }
}

int checkDup(int x)
{
  int i,j=0;

  for(i = 0;i< numlong-1;i++)
  {
    for(j = i+1;j<numlong;j++)
    {
      if (possibleAnswers[x][i] == possibleAnswers[x][j])
            {
        return 1;
      }
    }
  }
  return 0;
}


void getUserGuess()

{
 int i,q,w;
 int ck=0;
 while(!ck)
 {
  printf("input number:");
  for(i=0;i<numlong;i++)
  {
   scanf(" %c", &guess[i] );
  }

  for(i=0;i<numlong;i++)
  {
   guess[i]-=48;
  }
     if((guess[0]==guess[1])||(guess[0]==guess[2])||(guess[0]==guess[3]
    {
    printf("retry!!\n");
    QWQ=0;

    }
   else
    {
     QWQ=1;
     ck=1;
    }
 }
}

int checkAGuess()

{
 int a=0,b=0,i,j;

  for(i=0;i<numlong;i++)
  {

    for(j=0;j<numlong;j++)

    {
      if((guess[i]==answer[j])&&(i==j))
      {
        a++;
        break;
      }
      else if(guess[i]==answer[j])
      {
       b++;
       break;
      }
    }

  }
  printf("....%dA %dB\n",a,b);

  if(a==4)
    return 1;
  else
   return 0;
}