Guest User

Untitled

a guest
Jun 25th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.23 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.  
  74. int player()
  75. {
  76.        
  77.     int row_number;
  78.     int how_many;
  79.    
  80.     printf("\n       Your turn. Chose the row and the number of matches you want to take.\n");
  81.    
  82.     do
  83.     {  
  84.         do
  85.         {
  86.             printf("\n                                   Row: ");
  87.             scanf("%d",&row_number);
  88.             if(row_number < 1 || row_number > 4)
  89.             {
  90.                 printf("\n                  ERROR: invalid row number. Please try again.");
  91.                 display();
  92.             }
  93.         }while(row_number < 1 || row_number > 4);
  94.        
  95.             printf("                                 Matches: ");
  96.             scanf("%d",&how_many);
  97.        
  98.         if(how_many > *(row+row_number-1) || how_many < 1)
  99.         {
  100.             printf("\n               ERROR: invalid number of matches. Please try again.");
  101.             display();
  102.         }
  103.     }while(how_many > *(row+row_number-1) || how_many < 1);
  104.    
  105.     *(row+row_number-1)-= how_many;
  106.    
  107.     display();
  108.    
  109.     return computer();
  110. }
  111.  
  112. int computer()
  113. {
  114.     int random;
  115.     srand(time(NULL));
  116.     random = rand() % 4;
  117.    
  118.     if(is_win()==0)
  119.         winning_move(random);
  120.     else
  121.         losing_move(random);
  122.    
  123.     display();
  124.    
  125.     if(is_win() == 2)
  126.         return 1;
  127.     else if(is_win() == 3)
  128.         return 0;
  129.     else
  130.         return player();  
  131. }
  132.  
  133. int is_win()                                        
  134. {
  135.    
  136.     initializetemp();
  137.    
  138.     int fours = countfours();
  139.     int twos = counttwos();
  140.     int ones = countones();
  141.    
  142.     if(fours==0 && twos==0)
  143.     {
  144.         if(ones == 0)
  145.             return 2;
  146.         else if(ones == 1)
  147.             return 3;
  148.         else if(ones == 3)
  149.             return 1;    
  150.         else
  151.             return 0;
  152.     }
  153.     else if(fours%2 || twos%2 || ones%2)
  154.         return 0;
  155.     else
  156.         return 1;
  157. }
  158.  
  159. void initializetemp()
  160. {
  161.     for(int i=0;i<4;i++)
  162.         *(temp_row+i) = *(row+i);
  163.    
  164. }
  165.  
  166. int countfours()
  167. {
  168.     int fours = 0;
  169.    
  170.     for(int i=0;i<4;i++)
  171.     {   counter = 0;
  172.         fours += checkrow(i,4);
  173.     }
  174.     return fours;
  175. }
  176.  
  177. int counttwos()
  178. {
  179.     int twos = 0;
  180.    
  181.     for(int i=0;i<4;i++)
  182.     {counter=0;
  183.         twos += checkrow(i,2);
  184.     }
  185.    
  186.     return twos;
  187. }
  188.  
  189. int countones()
  190. {
  191.     int ones = 0;
  192.    
  193.     for(int i=0;i<4;i++)
  194.     {counter=0;
  195.         ones += checkrow(i,1);
  196.     }
  197.     return ones;
  198. }
  199.  
  200. int checkrow(int row_number, int matches)
  201. {  
  202.     if(TEMP_MATS>=matches)
  203.     {
  204.         TEMP_MATS-= matches;
  205.         counter++;
  206.         return checkrow(row_number, matches);
  207.     }
  208.     else
  209.         return counter;
  210. }
  211.  
  212. void winning_move(int row_number)
  213. {
  214.     int matches_taken = 0;
  215.    
  216.     while(MATS_IN_ROW && is_win() == 0)
  217.     {        
  218.         MATS_IN_ROW--;
  219.         matches_taken++;
  220.     }
  221.    
  222.     if(is_win() == 0)
  223.     {
  224.         MATS_IN_ROW = matches_taken;
  225.         winning_move((row_number+1) % 4);
  226.     }
  227.     else
  228.         printf("\n                     Computer takes %d match(es) from row %d",matches_taken,row_number+1);
  229. }
  230. void losing_move(int row_number)
  231. {
  232.     if(MATS_IN_ROW)
  233.     {
  234.         MATS_IN_ROW--;
  235.         printf("\n                     Computer takes 1 match(es) from row %d",row_number+1);
  236.     }
  237.     else
  238.         losing_move((row_number+1) % 4);
  239. }
  240.  
  241. void display()
  242. {
  243.     printf("\n");
  244.     for(int i=0;i<4;i++)
  245.     {
  246.         printf("\n                        Row %d:  ",i+1);
  247.         for(int j=0;j<i;j++)
  248.                 printf("  ");
  249.         for(int j=0;j<*(row+i);j++)
  250.             printf("| ");
  251.     }
  252.     printf("\n");
  253. }
Add Comment
Please, Sign In to add comment