Advertisement
Guest User

Untitled

a guest
Feb 12th, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.86 KB | None | 0 0
  1. /// Yasmin Fakhoury
  2. /// COP 3502 Honors
  3. /// 2/14/17
  4. /// Program #3
  5. /// Outputs all the number of possible answer combinations for a partially
  6. /// completed game of Mastermind
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <math.h>
  11.  
  12. /// Global variables
  13. int n; /// Number of slots to fill
  14. int k; /// Number of possible colors
  15. int m; /// Number of moves made
  16. int b; /// Number of black pegs
  17. int w; /// Number of white pegs
  18. int total; /// Number of possible combinations
  19. int** combinations;
  20. int combopos = 0;
  21.  
  22. void printgame(int** game);
  23. void freegame(int** game);
  24. //void allCombos(int perm[], int guess[], int place);
  25. int black(int perm[], int guess[]);
  26. int white(int perm[], int guess[]);
  27. bool compare(int perm[], int guess[]);
  28. void allCombos2(int perm[], int place);
  29. void test(int** combinations, int** mm);
  30.  
  31. FILE* infile;
  32.  
  33.  
  34. int main(){
  35.  
  36. infile = fopen("test.in", "r");
  37.  
  38. int numcases; /// Total number of partially complete games to read in
  39. int h, i, j; /// Counters
  40.  
  41. /// Scan top of input for number of total cases
  42. fscanf(infile,"%d", &numcases);
  43.  
  44. /// Read in each game
  45. for(h=0; h<numcases; h++){
  46.  
  47.  
  48. /// Read in number of slots per guess, the number of possible
  49. /// colors to pick from, and the number of moves so far in the game
  50. fscanf(infile,"%d %d %d", &n, &k, &m);
  51.  
  52. combinations = (int**) malloc(pow(k,n)*sizeof(int*));
  53.  
  54. combopos=0;
  55.  
  56. for(i=0; i<pow(k,n); i++){
  57. combinations[i] = (int*) malloc(n*sizeof(int));
  58. }
  59.  
  60. /// Allocate space for rows of the fame in a 2d array
  61. int** mm = (int**) malloc(m * sizeof(int*));
  62. /// Array to hold each possible combination of guesses
  63. int* combo = (int*) malloc(n*sizeof(int));
  64. allCombos2(combo, 0);
  65. free(combo);
  66.  
  67. /// Loop through each move in the game
  68. for(i=0; i<m; i++){
  69.  
  70. /// Allocate space for columns of 2d array
  71. mm[i] = (int*)malloc((n+2) * sizeof(int));
  72.  
  73. /// Loop through n times
  74. for(j=0; j<n; j++){
  75. /// Scan in guesses from each turn in the game
  76. fscanf(infile,"%d", &mm[i][j]);
  77. }
  78.  
  79. /// Scan in the number of black and white pegs after each line
  80. fscanf(infile,"%d %d", &mm[i][n], &mm[i][n+1]);
  81.  
  82. /// Generate all the possible combinations for the game, and from
  83. /// there see which ones match the black and white peg values read in
  84. // allCombos(combo, mm[i], 0);
  85.  
  86.  
  87. }
  88.  
  89. /// Initialize total viable combinations at 0 before incrementation
  90. total = pow(k,n);
  91.  
  92. test(combinations, mm);
  93.  
  94. printf("%d\n", total);
  95. //printgame(mm);
  96.  
  97. // /// Free memory used to store game before moving on to the next one
  98. freegame(mm);
  99.  
  100. for(i=0; i<pow(k,n); i++)
  101. {
  102. free(combinations[i]);
  103. }
  104. free(combinations);
  105.  
  106. }
  107.  
  108. fclose(infile);
  109. return 0;
  110. }
  111.  
  112. /*
  113. /// Prints out all possible guess combinations for the given values of k and n
  114. void allCombos(int perm[], int guess[], int place){
  115.  
  116. /// If the place in the array is equal to the number of slots,
  117. /// then call compare function
  118. if(place == n){
  119. compare(perm, guess);
  120. }
  121.  
  122. /// Otherwise, keep going until an entire combination is filled in
  123. else{
  124. int i;
  125. for(i=0; i<k; i++){
  126. /// Set the slot in the array equal to one of the colors
  127. perm[place] = i;
  128. allCombos(perm, guess, place+1);
  129. }
  130. }
  131. }
  132. */
  133. bool compare(int perm[], int guess[]){
  134.  
  135. /// If both black and white peg functions return true,
  136. /// increment the total by 1
  137. return ((black(perm, guess) == 1) && (white(perm, guess) == 1));
  138. // printf("compare total = %d \n", total);
  139.  
  140. }
  141.  
  142.  
  143. int black(int perm[], int guess[]){
  144.  
  145. /// "totalblack" is the number of black pegs yielded through
  146. /// comparison of the guess and given combination
  147. int i, totalblack = 0;
  148.  
  149. for(i=0; i<n; i++){
  150. /// Each time slot i of the combination holds the same
  151. /// color as slot i of the guess, increase number of
  152. /// black pegs by 1
  153. if(perm[i] == guess[i])
  154. totalblack++;
  155. }
  156.  
  157. /// If the total number of black pegs found equals that which
  158. /// was given in the input, the function returns true
  159. /// Otherwise, return false
  160. if(totalblack == guess[n]) return 1;
  161. else return 0;
  162. }
  163.  
  164. int teststuff[4] = {0,1,2,1};
  165. int white(int combo[], int guess[])
  166. {
  167.  
  168.  
  169. int* freqguess = (int*)malloc(k*sizeof(int)); /// Frequency array for guess
  170. int* freqcombo = (int*)malloc(k*sizeof(int)); /// Frequency array for combination
  171. int i, j; /// Counters
  172. int totalwhite = 0; /// Number of white pegs found through
  173. /// comparing the guess and the combination
  174.  
  175. /// Initialize both frequency arrays at 0
  176. for(i=0; i<k; i++){
  177. freqguess[i] = 0;
  178. freqcombo[i] = 0;
  179. }
  180.  
  181. for(i=0; i<n; i++){
  182. freqguess[guess[i]]++;
  183. freqcombo[combo[i]]++;
  184. //
  185. // for(j=0; j<n; j++){
  186. // /// Each time that a slot in the guess or combo equals a particular color
  187. // /// increase the value in the slot of the frequency array for that
  188. // /// color by 1
  189. // if(guess[j] == i){
  190. // freqguess[i]++;
  191. // }
  192. // if(combo[j] == i){
  193. // freqcombo[i]++;
  194. // }
  195. // }
  196. }
  197.  
  198. /*
  199. int loop;
  200. printf("freqcombo: ");
  201. for(loop=0; loop<4; loop++){
  202. printf("%d ", freqcombo[loop]);
  203. }
  204. printf("\n");
  205.  
  206. printf("freqguess: ");
  207. for(loop=0; loop<4; loop++){
  208. printf("%d ", freqguess[loop]);
  209. }
  210. printf("\n");
  211. */
  212.  
  213. for(i=0; i<k; i++){
  214.  
  215. /// Compare the frequency array values of combo and guess
  216. /// for each color and increase the number of white pegs the
  217. /// lesser of the two values
  218. if(freqcombo[i] >= freqguess[i])
  219. totalwhite += freqguess[i];
  220.  
  221. else
  222. {
  223. totalwhite+=freqcombo[i];
  224. }
  225.  
  226. }
  227.  
  228. // printf("totalwhite = %d \n", totalwhite);
  229.  
  230. /// If the total number of white pegs calculated equals the number of
  231. /// white pegs given in the input, return true
  232.  
  233. free(freqcombo);
  234. free(freqguess);
  235. if(totalwhite == guess[n+1] + guess[n]){
  236. return 1;
  237. }else return 0;
  238. }
  239.  
  240.  
  241. /// Frees 2d array storing game
  242. void freegame(int** game){
  243.  
  244. int i;
  245. for(i=0; i<m; i++)
  246. free(game[i]);
  247.  
  248. free(game);
  249. }
  250.  
  251.  
  252. /*
  253. /// For testing, delete
  254. void printgame(int** game){
  255.  
  256. int i, j;
  257. for(i=0; i<m; i++){
  258. for(j=0; j<n; j++){
  259. printf("%d ", game[i][j]);
  260.  
  261. }
  262. printf("\n");
  263. }
  264. }
  265. */
  266.  
  267.  
  268. void test(int** combos, int** guesses){
  269. int i, j;
  270. int numpossible = pow(k,n);
  271.  
  272. for(i=0; i<numpossible; i++){
  273. for(j=0; j<m; j++)
  274. if(!compare(combos[i], guesses[j])){
  275. total--;
  276. break;
  277. }
  278. }
  279.  
  280. }
  281.  
  282. /// In main
  283. /// combos[i] = malloc(n*sizeof(int));
  284.  
  285. void allCombos2(int perm[], int place){
  286.  
  287. /// If the place in the array is equal to the number of slots,
  288. /// then call compare function
  289. if(place == n){
  290. int loop;
  291. for(loop=0; loop<n; loop++){
  292. combinations[combopos][loop] = perm[loop];
  293. }
  294. combopos++;
  295. }
  296.  
  297. /// Otherwise, keep going until an entire combination is filled in
  298. else{
  299. int i;
  300. for(i=0; i<k; i++){
  301. /// Set the slot in the array equal to one of the colors
  302. perm[place] = i;
  303. allCombos2(perm, place+1);
  304. }
  305. }
  306.  
  307.  
  308. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement