Guest User

Untitled

a guest
Jan 12th, 2013
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.46 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdbool.h>
  4.  
  5.  
  6. void userEnter(int*pattern, int n);
  7. void print( int * s, int n);
  8. void recurs( int * s, int * a, int n, int wpegs, int bpegs);
  9. bool Done (int*s);
  10. bool bPegs(int*a ,int*s, int bpegs, int wpegs, int n);
  11. bool wPegs(int* modcom, int* modoriginal, int*s, int wpegs, int w);
  12. void change(int*modoriginal, int*modcom, int i, int k, int w);
  13.  
  14. int main(void)
  15. {
  16. int i, n, bpegs, wpegs;
  17.  
  18. printf("Enter the pattern length: ");
  19. scanf("%d",&n);
  20. int *a = (int*)malloc((n)*(sizeof(int)));
  21. printf("Input the guess pattern: ");
  22. int pattern[n];
  23. userEnter(pattern, n);
  24. printf("Enter the number of black pegs in the feedback: ");
  25. scanf("%d",&bpegs);
  26. printf("Enter the number of white pegs in the feedback: ");
  27. scanf("%d",&wpegs);
  28. printf("The possible key patterns are: ");
  29. for(i=0; i<=n-1; i++)
  30. {
  31. a[i]=0;
  32. }
  33. print(a, n);
  34. recurs(a, pattern, n, wpegs, bpegs);
  35.  
  36. }
  37.  
  38. void userEnter(int*pattern, int n)
  39. {
  40. char input[n];
  41. scanf("%s",&input);
  42.  
  43. int i;
  44. for(i = 0; i < n-1; i++)
  45. {
  46. pattern[i] = input[i]-65;
  47. }
  48. }
  49.  
  50. void print( int * s, int n)
  51. {
  52. int i;
  53. printf( "n" );
  54. for( i = n-1; i >= 0; i-- )
  55. {
  56. printf( "%c", ( s[ i ] + 65 ) );
  57. }
  58. }
  59.  
  60. void recurs( int * s, int * a, int n, int wpegs, int bpegs)
  61. {
  62.  
  63. int i;
  64.  
  65. if(Done(s))
  66. {
  67. print( s, n);
  68. printf( "nAccomplisshed!n" );
  69. }
  70.  
  71. else{
  72. s[ 0 ] += 1;
  73. for( i = 0; i < n-1; i++ )
  74. {
  75. if( s[ i ] == 6 ){
  76. s[ i ] = 0;
  77. s[ i + 1 ] += 1;
  78. }
  79. }
  80. if(bPegs(a ,s, bpegs, wpegs, n))
  81. {
  82. print( s, n);
  83. }
  84. recurs(s, a, n, wpegs, bpegs);
  85. }
  86. }
  87.  
  88. bool Done (int*s)
  89. {
  90. int i;
  91. bool done=true;
  92. for (i=0;i<=11;i++)
  93. {
  94. if(s[i]!=5)
  95. {
  96. done=false;
  97. }
  98. }
  99. return done;
  100. }
  101.  
  102.  
  103. bool bPegs(int*a ,int*s, int bpegs, int wpegs, int n)
  104. {
  105. int i,j,c=0;
  106. bool d = false;
  107. for(i=0; i<n-1; i++)
  108. {
  109. if(a[i]==s[i])
  110. {
  111. c++;
  112. }
  113. }
  114. int x =n-c;
  115. int* modcom;
  116. int*modoriginal;
  117. modcom=(int*)malloc((x)*(sizeof(int)));
  118. modoriginal=(int*)malloc((x)*(sizeof(int)));
  119. int w=0;
  120. for(j=0; j<n-1; j++)
  121. {
  122. if(a[j]!=s[j])
  123. {
  124. modcom[w]=s[j];
  125. modoriginal[w]=a[j];
  126. w++;
  127. }
  128. }
  129. if(c==bpegs)
  130. {
  131. d = wPegs(modcom, modoriginal, s, wpegs, w);
  132. }
  133.  
  134. return d;
  135.  
  136. }
  137.  
  138. bool wPegs(int*modcom, int*modoriginal, int*s, int wpegs, int w)
  139. {
  140. int i, k, count=0;
  141. for(i=0; i<=w; i++)
  142. {
  143. for(k=0; k<=w; k++)
  144. {
  145. if (modoriginal[i]==modcom[k])
  146. {
  147. count++;
  148. change(modoriginal, modcom, i, k, w);
  149. }
  150. }
  151. }
  152. if(wpegs==count)
  153. {
  154. return true;
  155. }
  156. else
  157. {
  158. return false;
  159. }
  160.  
  161. }
  162.  
  163. void change(int*modoriginal, int*modcom, int i, int k, int w)
  164. {
  165. int c, o;
  166. for(c=i-1; c<w-1; c++)
  167. {
  168. modoriginal[c]=modoriginal[c+1];
  169. }
  170. for(o=k-1;o<w-1;o++)
  171. {
  172. modcom[o]=modcom[o+1];
  173. }
  174. }
  175.  
  176. 13421173.c:25: warning: format ‘%d’ expects type ‘int *’, but argument 2 has type ‘int’
  177. 13421173.c:25: warning: format ‘%d’ expects type ‘int *’, but argument 2 has type ‘int’
  178. 13421173.c:27: warning: format ‘%d’ expects type ‘int *’, but argument 2 has type ‘int’
  179. 13421173.c:27: warning: format ‘%d’ expects type ‘int *’, but argument 2 has type ‘int’
  180.  
  181. scanf("%d", &bpegs);
  182.  
  183. scanf("%d",bpegs);
  184. printf("Enter the number of white pegs in the feedback: ");
  185. scanf("%d",wpegs);
  186.  
  187. scanf("%d",&bpegs);
  188. printf("Enter the number of white pegs in the feedback: ");
  189. scanf("%d",&wpegs);
  190.  
  191. scanf("%s",input);
  192.  
  193. scanf("%d",bpegs);
Add Comment
Please, Sign In to add comment