Guest User

Untitled

a guest
Jun 25th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.27 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. #define TEMP_MATS (*(temp_row+row_number))
  6. #define MATS_IN_ROW (*(row+row_number))
  7. void initializetemp();
  8. int checkrow(int, int);
  9. int countfours();
  10. int counttwos();
  11. int countones();
  12. int is_win();
  13. void display();
  14. void winning_move(int);
  15. void losing_move(int);
  16. int player();
  17. int computer();
  18.  
  19. int row[4] = {7,5,3,1};
  20. int temp_row[4];
  21. int counter;
  22. main()
  23. {
  24.     printf("\n\n  O))       O))               O))            O))                      O))     "
  25.            "\n  O) O))   O)))       O)      O))            O))                      O))     "
  26.            "\n  O)) O)) O O))               O))  O))       O))         O))          O))  O))"
  27.            "\n  O))  O))  O))      O))      O)) O))        O))       O))  O))       O)) O)) "
  28.            "\n  O))   O)  O))      O))      O)O))          O))      O))   O))       O)O))   "
  29.            "\n  O))       O))      O))      O)) O))        O))      O))   O))       O)) O)) "
  30.            "\n  O))       O))      O))      O))  O))      O)))        O)) O)))      O))  O))");
  31.     printf("\n\n                          Welcome to my nim game");
  32.    
  33.     printf("\n                                  The rules:"
  34.            "\n                      1. Matches are laid out like this:\n"
  35.            "\n                                | | | | | | |"
  36.            "\n                                  | | | | |"
  37.            "\n                                    | | |"
  38.            "\n                                      |\n"
  39.            "\n                     2. Players take matches in turns"
  40.            "\n                         3. As many as they want"
  41.            "\n                    4. But only from one row at a time"
  42.            "\n                    5. Whoever takes the last one loses");
  43.  
  44.    
  45.     char flag;
  46.     do
  47.     {
  48.         printf("\n\n           Press 'y' if you want computer to make the first move... ");
  49.         flag = (char)getchar();
  50.        
  51.         if(flag == 'Y' || flag == 'y')
  52.         {
  53.             if(computer())
  54.                 printf("\n\n                                 YOU WIN!\n\n");
  55.             else
  56.                 printf("\n\n                                 YOU LOSE!\n\n");
  57.         }
  58.         else
  59.         {
  60.             display();
  61.            
  62.             if(player())
  63.                 printf("\n\n                                 YOU WIN!\n\n");
  64.             else
  65.                 printf("\n\n                                 YOU LOSE!\n\n");
  66.         }  
  67.        
  68.         printf("                       Press 'y' to play again... ");
  69.         scanf("%1s",&flag);
  70.        
  71.     }while(flag=='y' || flag=='Y');
  72.    
  73.     getchar();
  74.     getchar();    
  75. }
  76.  
  77. int player()
  78. {
  79.        
  80.     int row_number;
  81.     int how_many;
  82.    
  83.     printf("\n       Your turn. Chose the row and the number of matches you want to take.\n");
  84.    
  85.     do
  86.     {  
  87.         do
  88.         {
  89.             printf("\n                                   Row: ");
  90.             scanf("%d",&row_number);
  91.             if(row_number < 1 || row_number > 4)
  92.             {
  93.                 printf("\n                  ERROR: invalid row number. Please try again.");
  94.                 display();
  95.             }
  96.         }while(row_number < 1 || row_number > 4);
  97.        
  98.             printf("                                 Matches: ");
  99.             scanf("%d",&how_many);
  100.        
  101.         if(how_many > *(row+row_number-1) || how_many < 1)
  102.         {
  103.             printf("\n               ERROR: invalid number of matches. Please try again.");
  104.             display();
  105.         }
  106.     }while(how_many > *(row+row_number-1) || how_many < 1);
  107.    
  108.     *(row+row_number-1)-= how_many;
  109.    
  110.     display();
  111.    
  112.     return computer();
  113. }
  114.  
  115. int computer()
  116. {
  117.     int random;
  118.     srand(time(NULL));
  119.     random = rand() % 4;
  120.    
  121.     if(is_win()==0)
  122.         winning_move(random);
  123.     else
  124.         losing_move(random);
  125.    
  126.     display();
  127.    
  128.     if(is_win() == 2)
  129.         return 1;
  130.     else if(is_win() == 3)
  131.         return 0;
  132.     else
  133.         return player();  
  134. }
  135.  
  136. int is_win()                                        
  137. {
  138.    
  139.     initializetemp();
  140.    
  141.     int fours = countfours();
  142.     int twos = counttwos();
  143.     int ones = countones();
  144.    
  145.     if(fours==0 && twos==0)
  146.     {
  147.         if(ones == 0)
  148.             return 2;
  149.         else if(ones == 1)
  150.             return 3;
  151.         else if(ones == 3)
  152.             return 1;    
  153.         else
  154.             return 0;
  155.     }
  156.     else if(fours%2 || twos%2 || ones%2)
  157.         return 0;
  158.     else
  159.         return 1;
  160. }
  161.  
  162. void initializetemp()
  163. {
  164.     for(int i=0;i<4;i++)
  165.         *(temp_row+i) = *(row+i);
  166.    
  167. }
  168.  
  169. int countfours()
  170. {
  171.     int fours = 0;
  172.    
  173.     for(int i=0;i<4;i++)
  174.     {   counter = 0;
  175.         fours += checkrow(i,4);
  176.     }
  177.     return fours;
  178. }
  179.  
  180. int counttwos()
  181. {
  182.     int twos = 0;
  183.    
  184.     for(int i=0;i<4;i++)
  185.     {counter=0;
  186.         twos += checkrow(i,2);
  187.     }
  188.    
  189.     return twos;
  190. }
  191.  
  192. int countones()
  193. {
  194.     int ones = 0;
  195.    
  196.     for(int i=0;i<4;i++)
  197.     {counter=0;
  198.         ones += checkrow(i,1);
  199.     }
  200.     return ones;
  201. }
  202.  
  203. int checkrow(int row_number, int matches)
  204. {  
  205.     if(TEMP_MATS>=matches)
  206.     {
  207.         TEMP_MATS-= matches;
  208.         counter++;
  209.         return checkrow(row_number, matches);
  210.     }
  211.     else
  212.         return counter;
  213. }
  214.  
  215. void winning_move(int row_number)
  216. {
  217.     int matches_taken = 0;
  218.    
  219.     while(MATS_IN_ROW && is_win() == 0)
  220.     {        
  221.         MATS_IN_ROW--;
  222.         matches_taken++;
  223.     }
  224.    
  225.     if(is_win() == 0)
  226.     {
  227.         MATS_IN_ROW = matches_taken;
  228.         winning_move((row_number+1) % 4);
  229.     }
  230.     else
  231.         printf("\n                     Computer takes %d match(es) from row %d",matches_taken,row_number+1);
  232. }
  233. void losing_move(int row_number)
  234. {
  235.     if(MATS_IN_ROW)
  236.     {
  237.         MATS_IN_ROW--;
  238.         printf("\n                     Computer takes 1 match(es) from row %d",row_number+1);
  239.     }
  240.     else
  241.         losing_move((row_number+1) % 4);
  242. }
  243.  
  244. void display()
  245. {
  246.     printf("\n");
  247.     for(int i=0;i<4;i++)
  248.     {
  249.         printf("\n                        Row %d:  ",i+1);
  250.         for(int j=0;j<i;j++)
  251.                 printf("  ");
  252.         for(int j=0;j<*(row+i);j++)
  253.             printf("| ");
  254.     }
  255.     printf("\n");
  256. }
Add Comment
Please, Sign In to add comment