May 29, 2012

kitten kitten
Lab Rat
159 posts

having problem in creating tree

 

hi to everyone

i want to create a tree with lots of children
i write this code:

  1. class Tree
  2. {
  3.     friend class AIClass;
  4.     int v;
  5.     int **board;
  6.     QList<Tree> *children;
  7. public:
  8.  
  9.     Tree()
  10.     {
  11.         v = 0;
  12.         int n = 25;
  13.         board=(int**) malloc((n)*sizeof(int*));
  14.         for(int i=0;i<n;i++)
  15.         {
  16.             board[i]=(int*) malloc((n)*sizeof(int));
  17.             for(int j=0;j<n;j++)
  18.                 board[i][j]=0;
  19.         }
  20.         children = new QList<Tree>();
  21.     }
  22.  
  23.     Tree(int n)
  24.     {
  25.         v = 0;
  26.         board=(int**) malloc((n)*sizeof(int*));
  27.         for(int i=0;i<n;i++)
  28.         {
  29.             board[i]=(int*) malloc((n)*sizeof(int));
  30.             for(int j=0;j<n;j++)
  31.                 board[i][j]=0;
  32.         }
  33.         children = new QList<Tree>();
  34.     }
  35.     void setxy(int x,int y,int value)
  36.     {
  37.         board[x][y] = value;
  38.     }
  39.  
  40.     void setValue(int p)
  41.     {
  42.         v = p;
  43.     }
  44.  
  45. };
  46. class AIClass : public QObject
  47. {
  48. public:
  49.     int predict( Tree *item,int depth,int alpha,int beta,bool Player);
  50.  
  51. private:
  52.     Tree *root;
  53.     int board[25][25];
  54.     int count;
  55.     int k;
  56. };

and i wrote this:
  1. int AIClass::predict(Tree *item,int depth,int alpha,int beta,bool MaxPlayer)
  2. {
  3.         for(int i=0;i<count;i++)
  4.         {
  5.             for(int j=0;j<count;j++)
  6.             {
  7.                 if(item->board[i][j]==0) {
  8.                     temp = new Tree(count);
  9.                     for(int k=0;k<count;k++)
  10.                         for(int h=0;h<count;h++)
  11.                         {
  12.                             temp->setxy(k,h,item->board[k][h]);
  13.                             if((i==k)&&(j==h)) temp->setxy(k,h,2);
  14.                         }
  15.                     item->children->append(*temp);
  16.                }
  17.             }
  18.         }
  19.         for (int i = 0; i < item->children->size(); ++i) {
  20.             int temp = predict(&(item->children->value(i)),depth-1,alpha,beta,!MaxPlayer);
  21.             if(temp > alpha)
  22.                 alpha = temp;
  23.             if(beta < alpha) break;
  24.         }
  25.         item->setValue(alpha);
  26. }

( it is a part of alpha beta pruning algorithm)
but when it is compiled,the children does’nt get its value,after calling predict function,and again the v value in Tree class is 0 and does’nt get updated value
and i get this warning message:(i think the problem is):
  1. ..\dooz\src\AIClass.cpp: In member function 'int AIClass::predict(Tree*, int, int, int, bool)':
  2. ..\dooz\src\AIClass.cpp:98: warning: taking address of temporary

what do you think?

 Signature 

www.kitten.mihanblog.com

2 replies

May 29, 2012

DerManu DerManu
Robot Herder
451 posts

What line in your posted code does line AIClass.cpp:98 translate to?
And where does temp[m] come from in line 8 of AIClass::predict? Why do you redefine it to an int in line 21? And is temp[m] memory leaked (you deep-copy by value when appending * temp[m] to the list. Maybe you want a QList<Tree*> instead)?
Is children memory leaked? Where’s the dtor deleting children?
Why do you mix malloc and new?

May 29, 2012

kitten kitten
Lab Rat
159 posts

thanks alot i found the problem
i need QList<Tree *>

 Signature 

www.kitten.mihanblog.com

 
  ‹‹ Qt application running with high CPU usage on Mac OS X      qmake -project searching for fortran sources ››

You must log in to post a reply. Not a member yet? Register here!