Guest User

ParticleAI

a guest
Jan 17th, 2014
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.90 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <stdlib.h>
  4. #include <time.h>
  5.  
  6. #define numMoves 14
  7. #define numParticles 5
  8. #define goalX 10
  9. #define goalY 2
  10.  
  11. typedef struct{
  12.  
  13.     int pX;
  14.     int pY;
  15.     char *moves[numMoves];
  16.    
  17. } Particle;
  18.  
  19.  
  20. typedef enum{
  21.     LEFT,RIGHT,UP,DOWN
  22. } Direction;
  23.  
  24.  
  25. Particle makeMoveList(Particle p){
  26.  
  27.     int i;
  28.    
  29.     for(i=0;i<numMoves;i++){
  30.    
  31.        
  32.        
  33.     }
  34.    
  35.     return p;
  36. }
  37.  
  38. void execMoves( Particle p ){ //use either randMove or doNothing if the correct params don't pop up
  39.  
  40. }
  41.  
  42. Particle teleport(int x, int y, Particle p){
  43.  
  44.     p.pX=x;
  45.     p.pY=y;
  46.    
  47.     return p;
  48.  
  49. }
  50.  
  51. Particle move(Direction dir, int steps, Particle p){
  52.  
  53.     switch(dir){
  54.    
  55.     case LEFT:
  56.         p.pX-=steps;
  57.         break;
  58.        
  59.     case RIGHT:
  60.         p.pX+=steps;
  61.         break;
  62.        
  63.     case UP:
  64.         p.pY+=steps;
  65.         break;
  66.        
  67.     case DOWN:
  68.         p.pY-=steps;
  69.         break;
  70.    
  71.     }
  72.     return p;
  73. }
  74.  
  75. Particle randMove(Particle p){
  76.  
  77.     Direction dir = genDirRand();
  78.     int steps = genStepsRand();
  79.    
  80.     p = move(dir,steps,p);
  81.    
  82.     return p;
  83. }
  84.  
  85. Direction genDirRand(void){
  86.  
  87.     Direction dir;
  88.     int intDir=  rand()%4+1;
  89.    
  90.     switch(intDir){
  91.    
  92.         case 1:
  93.             dir = LEFT;
  94.             break;
  95.            
  96.         case 2:
  97.             dir = RIGHT;
  98.             break;
  99.            
  100.         case 3:
  101.             dir = UP;
  102.             break;
  103.            
  104.         case 4:
  105.             dir = DOWN;
  106.             break;
  107.            
  108.     }
  109.    
  110.     return dir;
  111.  
  112. }
  113.  
  114. int genStepsRand(void){
  115.  
  116.     return rand()%10+1;
  117.  
  118. }
  119.  
  120. Direction genDirByGoal(Particle p){
  121.  
  122.     int biggestGap = abs((goalX-p.pX));
  123.     Direction dir;
  124.    
  125.     if( (abs(goalY-p.pY)) > biggestGap ){
  126.    
  127.         if( (goalY-p.pY) < 0){
  128.        
  129.             dir = DOWN;
  130.  
  131.         }
  132.        
  133.         else{
  134.        
  135.             dir = UP;
  136.        
  137.         }
  138.     }
  139.  
  140.     else{
  141.    
  142.         if( (goalX-p.pX) < 0){
  143.        
  144.             dir = LEFT;
  145.        
  146.         }
  147.        
  148.         else{
  149.        
  150.             dir = RIGHT;
  151.        
  152.         }
  153.        
  154.     }
  155.    
  156.     return dir;
  157. }
  158.  
  159. int genStepsByDist(Particle p){
  160.  
  161.     Direction dir = genDirByGoal(p);
  162.    
  163.     if( dir == LEFT || dir == RIGHT ){
  164.         return abs(goalX - p.pX);
  165.     }
  166.    
  167.     else{
  168.         return abs(goalY - p.pY);
  169.     }
  170. }
  171.  
  172. void doNothing( void ){ }// an option to waste your turn or can be forced if the move to execute does not have the correct params
  173.  
  174.  
  175. float calcDist(Particle p){
  176.    
  177.     return sqrt( pow( p.pX-goalX,2 ) + pow( p.pY-goalY,2 ) ); //distance formula
  178.  
  179. }
  180.  
  181. void PrintMoveSet(Particle p){
  182.  
  183.     int i;
  184.     for(i=0;i<numMoves;i++){
  185.        
  186.     }
  187.    
  188.     printf("\n");
  189.  
  190. }
  191.  
  192. int main(int argc, char *argv[]){   //things to repair:: Main, printMoveSet, makeMoveList, and execMoves;
  193.  
  194.     srand (time(NULL));
  195.    
  196.     Particle particles[numParticles];
  197.     float smallestDist,tempDist;
  198.     int genCount, i;
  199.    
  200.    
  201.     for(i=0;i<numParticles;i++){ //initialize starting position of all particles
  202.    
  203.         particles[i].pX=0;
  204.         particles[i].pY=0;
  205.        
  206.     }
  207.    
  208.     if(argc == 2){
  209.    
  210.         genCount = atoi(argv[1]);
  211.    
  212.         printf("\n");
  213.    
  214.         for(i=0;i<genCount;i++){
  215.        
  216.             printf("\n\n====================Generation %d====================\n\n",i+1);
  217.        
  218.         }
  219.    
  220.         printf("Closest Dist: %f",smallestDist);
  221.        
  222.     }
  223.    
  224.     else{
  225.         printf("Bad input");
  226.     }
  227.    
  228.    
  229.     return 0;
  230. }
Advertisement
Add Comment
Please, Sign In to add comment