Advertisement
Le_BuG63

AI MasterMind

Sep 26th, 2013
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.76 KB | None | 0 0
  1. /*
  2.  * File:   main.c
  3.  * Author: Raphael
  4.  *
  5.  * Created on 25 septembre 2013, 21:04
  6.  */
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <time.h>
  11.  
  12. #define FALSE   0
  13. #define TRUE    1
  14.  
  15. #define SIZE_GRID       4
  16.  
  17. #define NCOLOR          7
  18.  
  19. int gagner = 0,
  20.     defaite = 0;
  21.  
  22. enum {
  23.     RED,
  24.     YELLOW,
  25.     GREEN,
  26.     BLUE,
  27.     ORANGE,
  28.     WHITE,
  29.     PURPLE,
  30.     FUCHSIA
  31. };
  32.  
  33. int ball_color[SIZE_GRID] = {-1};
  34. int player_color[SIZE_GRID] = {-1};
  35.  
  36. inline int
  37. _rand(int max) {
  38.     return (rand() % max);
  39. }
  40.  
  41. void
  42. rand_color(void) {
  43.     int i;
  44.  
  45.     for (i = 0; i < SIZE_GRID; i++) {
  46.         ball_color[i] = _rand(NCOLOR);
  47.     }
  48. }
  49.  
  50. void
  51. resolve(void) {
  52.     int i = 0,
  53.             j = 0;
  54.  
  55.     while (player_color[0] != ball_color[0] || player_color[1] != ball_color[1] || player_color[2] != ball_color[2] || player_color[3] != ball_color[3]) {
  56.         if (player_color[0] != ball_color[0])
  57.             player_color[0] = j;
  58.  
  59.         if (player_color[1] != ball_color[1])
  60.             player_color[1] = j;
  61.  
  62.         if (player_color[2] != ball_color[2])
  63.             player_color[2] = j;
  64.  
  65.         if (player_color[3] != ball_color[3])
  66.             player_color[3] = j;
  67.  
  68.         j++;
  69.     }
  70.  
  71.     if (j <= 12)
  72.         gagner++;
  73.     else
  74.         defaite++;
  75.  
  76.     printf("%d %d %d %d\n", ball_color[0], ball_color[1], ball_color[2], ball_color[3]);
  77.     printf("%d %d %d %d -> %d\n\n", player_color[0], player_color[1], player_color[2], player_color[3], j);
  78. }
  79.  
  80. int main(int argc, char** argv) {
  81.     int i = 0;
  82.  
  83.     (void)argc;
  84.     (void)argv;
  85.    
  86.     srand(time(NULL));
  87.  
  88.     for (i = 0; i < 1000; i++) {
  89.         rand_color();
  90.         resolve();
  91.     }
  92.     printf("Gagnes: %d\nPerdues: %d", gagner, defaite);
  93.     return (EXIT_SUCCESS);
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement