Advertisement
SimoCode

tuki_turn.c

Mar 7th, 2019
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.94 KB | None | 0 0
  1. #include "player.h"
  2. #include <stdio.h>
  3. #include <string.h>
  4.  
  5. #define BOOL int
  6. #define TRUE 1
  7. #define FALSE 0
  8.  
  9. direction last = RIGHT;
  10.  
  11. direction where_go();                   // MASTER CONTROLLER
  12. BOOL      on_flw();                     // Sulla mia strada ci sono fiori?
  13. direction flw_dir();                    // Segui un fiore vicino
  14. BOOL      is_wall(direction dir);       // Davanti c'e` un muro?
  15. direction where_free();                 // La prima posizione senza muro
  16. // Non utilizzate
  17. BOOL      is_btf(btf_elm btf, direction dir);
  18. BOOL      is_there_btf(btf_elm btf);    // Guardo se c'e` un particolare simbolo
  19. direction where_is_btf(btf_elm btf);    // Guardo se intorno a me c'e` un simbolo
  20.  
  21.  
  22. BOOL is_wall(direction dir)
  23. {
  24.   btf_elm sym = read_battle_field(dir);
  25.   if(sym == VLN || sym == HLN || sym == TLC || sym == TRC ||
  26.       sym == BLC || sym == BRC || sym == TRC || sym == BRC)
  27.       return TRUE;
  28.   return FALSE;
  29. }
  30.  
  31. BOOL on_flw()
  32. {
  33.   if(read_battle_field(last) == FUTF)
  34.     return TRUE;
  35.   else FALSE;
  36. }
  37.  
  38. direction flw_dir()
  39. {
  40.   //FILE * stream = file_stream();
  41.   btf_elm r = read_battle_field(RIGHT);
  42.   btf_elm l = read_battle_field(LEFT);
  43.   btf_elm d = read_battle_field(DOWN);
  44.   btf_elm u = read_battle_field(UP);
  45.  
  46.   if(r == FUTF)
  47.     return RIGHT;
  48.   if(d == FUTF)
  49.     return DOWN;
  50.   if(l == FUTF)
  51.     return LEFT;
  52.   if(u == FUTF)
  53.     return UP;
  54.   return IN;
  55. }
  56.  
  57. direction where_free() // anti-loop 75% tested
  58. {
  59.   direction dirs[] = {RIGHT, DOWN, LEFT, UP};
  60.   int i = 0;
  61.  
  62.   if(last == UP)    i = 0;
  63.   if(last == RIGHT) i = 1;
  64.   if(last == DOWN)  i = 2;
  65.   if(last == LEFT)  i = 3;
  66.  
  67.   for(; i<4; (i++)%4 )
  68.     if(is_wall(dirs[i]) == FALSE)
  69.       return dirs[i];
  70. }
  71.  
  72. direction where_go()
  73. {
  74.   BOOL      right_flw   = on_flw();
  75.   direction new_flw_dir;
  76.  
  77.   // Inizio a guardare se riesco a seguire i fiori
  78.   if(right_flw == TRUE)
  79.     return last;
  80.   else if((new_flw_dir = flw_dir()) != IN)
  81.     return new_flw_dir;
  82.  
  83.   // Se no cerco di non andare a sbattere contro un muro
  84.   if(is_wall(last) == TRUE)
  85.     return where_free();
  86.   else return last;
  87. }
  88.  
  89. // Generiche (non solo per il fiore) usate per log/debug
  90. BOOL is_btf(btf_elm btf, direction dir)
  91. {
  92.   if(read_battle_field(dir) == btf)
  93.     return TRUE;
  94.   return FALSE;
  95. }
  96.  
  97. BOOL is_there_btf(btf_elm btf)
  98. {
  99.     return is_btf(btf, RIGHT) || is_btf(btf, DOWN) || is_btf(btf, LEFT) || is_btf(btf, UP);
  100. }
  101.  
  102. direction where_is_btf(btf_elm btf){
  103.   if(is_there_btf(btf) == TRUE){
  104.     if(is_btf(btf, RIGHT))
  105.       return RIGHT;
  106.     if(is_btf(btf, DOWN))
  107.       return DOWN;
  108.     if(is_btf(btf, LEFT))
  109.       return LEFT;
  110.     if(is_btf(btf, UP))
  111.       return UP;
  112.   }
  113. }
  114.  
  115. ////////////////////////////////
  116. // MY MOVE
  117. ////////////////////////////////
  118.  
  119. direction tuki_turn(){
  120.   direction choose;
  121.   btf_elm next_r = read_battle_field(RIGHT);
  122.   btf_elm next_d = read_battle_field(DOWN);
  123.  
  124.   choose = where_go();
  125.   last = choose;
  126.   return choose;
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement