Advertisement
Kenthris

1 of 4 directions

Apr 30th, 2017
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.13 KB | None | 0 0
  1. /*int k;
  2. //we already know turn
  3. k=1;
  4. do{
  5. if((wincon[fastest]==k) && (k!=wincon[turn])){
  6.  
  7. }
  8. k++;
  9. }while(k!=4;)
  10. */
  11. //your way ^, my way v
  12. int bloc=0;
  13. int uflag=dflag=rflag=lflag=aflag1=aflag2=1;//flags used to see if a direction has already been checked for an obstacle. aflag stops us from checking further directions
  14.  
  15. newflag=1;
  16. do{
  17. co1=pcoord[fastest][0];
  18. co2=pcoord[fastest][1];
  19. if(fastest == 0){
  20. if(array[pcoord[fastest][0]-1][pcoord[fastest][1]]=='.'){
  21. co1=pcoord[fastest][0]-1;
  22. aflag1=0;
  23. }
  24. else{
  25. bloc++;
  26. }
  27. spot = rand() % 4;
  28. do{
  29. else if((spot==1) && (rflag!=0)){
  30. if(array[co1][co2+1]=='.'){
  31. rflag=0;
  32. if(aflag1==0){//this if ensures that an open space for movement has already been found before setting a place to block
  33. aflag12=0;
  34. }
  35. else{
  36. aflag1=0;
  37. co2=co2+1;//a coordinate is set for an obstacle if there is no previously found spot for it. having placed one direction outside the loop gives that direction priority
  38. }
  39. }
  40. else{
  41. rflag=0;
  42. bloc++;
  43. }
  44. }
  45. else if((spot==2) && (dflag!=0)){
  46. if(array[co+1][co2]=='.'){
  47. dflag=0;
  48. if(aflag1==0){
  49. co1=co1+1;
  50. aflag12=0;
  51. }
  52. else{
  53. aflag1=0;
  54. }
  55. }
  56. else{
  57. dflag=0;
  58. bloc++;
  59. }
  60. }
  61. else if((spot==3) && (lflag!=0)){
  62. if(array[co1][co2-1]=='.'){
  63. lflag=0;
  64. if(aflag1==0){
  65. co2=co2-1;
  66. aflag12=0;
  67. }
  68. else{
  69. aflag1=0;
  70. }
  71. }
  72. else{
  73. lflag=0;
  74. bloc++;
  75. }
  76. }//with this we have checked all directions for an open space. If bloc becomes 3, we only have one open space, and thus cannot place further obstacles around the fastest
  77. //if, however, we can block him, co1 and co2 have become the coordinates of a free adjacent space. with this, we can look around that space for the next part of the obstacle
  78. //if we find that as well, we will set both locations on array (array[co1][co2] and array[co3][co4]) to become 'X'
  79. spot = rand() % 4;
  80. }while(!((aflag1==0) && (aflag2==0)) || (bloc==3));
  81. }
  82. }
  83. else if(turn == 1){
  84. if(array[pcoord[fastest][0]-1][pcoord[fastest][1]]=='.'){
  85. co2=pcoord[fastest][1]+1;
  86. }
  87. else{
  88. bloc++;
  89. spot = rand() % 4;
  90. do{
  91.  
  92. }while((aflag!=0) && (bloc!=4));
  93. }
  94. }
  95. else if(turn == 2){
  96. if(array[pcoord[fastest][0]-1][pcoord[fastest][1]]=='.'){
  97. co1=pcoord[fastest][0]+1;
  98. }
  99. else{
  100. bloc++;
  101. spot = rand() % 4;
  102. do{
  103.  
  104. }while((aflag!=0) && (bloc!=4));
  105. }
  106. }
  107. else if(turn == 3){
  108. if(array[pcoord[fastest][0]-1][pcoord[fastest][1]]=='.'){
  109. co2=pcoord[fastest][1]-1;
  110. }
  111. else{
  112. bloc++;
  113. spot = rand() % 4;
  114. do{
  115.  
  116. }while((aflag!=0) && (bloc!=4));
  117.  
  118. }
  119. }
  120. //once co1 and co2 have been found and an obstacle can be placed, we need co3 and co4 for the second piece
  121. //if an obstacle can be placed, we start by setting co3 and co4 to co1 and co2 respectively. if not, we set them to the coordinates of the fastest
  122. if((aflag1==0) && (aflag2==0)){
  123. co3=co1;
  124. co4=co2;
  125. spot = rand() % 4;
  126. if((spot == 0) && (array[co1+1][co2] == '.')){
  127. co3=co1+1;
  128. newflag=0;
  129. }
  130. else if((spot == 1) && (array[co1-1][co2] == '.')){
  131. co3=co1-1;
  132. newflag=0;
  133. }
  134. else if((spot == 2) && (array[co1][co2+1] == '.')){
  135. co4=co2+1;
  136. newflag=0;
  137. }
  138. else if((spot == 3) && (array[co1][co2-1] == '.')){
  139. co4=co2-1;
  140. newflag=0;
  141. }
  142. }
  143. //now we check their values. if no obstacle can be placed, we set newflag to 0 and call proceed instead
  144. if((co3!=co1) || (co4!=co2)){
  145. array[co1][co2]='X';
  146. array[co3][co4]='X';
  147. }
  148. else{
  149. aflag2=2;//outside the obstacles side of the code, check for this flag. if it is 2, call proceed instead
  150. }
  151. }while (newflag!=0);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement