Advertisement
pdaogu

HW8.2

Oct 30th, 2018
25
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.92 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <time.h>
  3. #include <stdlib.h>
  4. #include <conio.h>
  5.  
  6. #define P1 1
  7. #define P2 2
  8.  
  9. int main () {
  10.     int start, dest;    // start & destination of game
  11.     int turn;           // determine who has first turn
  12.     int total, jump;    // total point, jump of user
  13.     int isEven = -1,            // previous jump is even or not?
  14.         isInvalid,      // user input is invalid or not?
  15.         isOut = 0,      // End game?
  16.         Winner = 0;     // Save who win
  17.  
  18.     printf("..:: ICT GAME CENTER - SMART STRATEGY ::..\n\n");
  19.     printf("> Start: ");
  20.     scanf("%d", &start);
  21.     total = start;
  22.     do {
  23.         printf("> Destination: ");
  24.         scanf("%d", &dest);
  25.         if (dest <= start) {
  26.             printf("!!! Please enter destination bigger than start (%d)", start);
  27.         }
  28.     } while (dest <= start);
  29.     srand(time(NULL));
  30.     turn = rand() % 2 + 1;
  31.     printf("> Who play first (randomly): %d", turn);
  32.     putchar('\n');
  33.     do {
  34.         if (turn == P1) {
  35.             printf("\n+ P1 - turn:\n");
  36.         }
  37.         else {
  38.             printf("\n+ P2 - turn:\n");
  39.         }
  40.         do {
  41.             printf("  > Choose a number: ");
  42.             scanf("%d", &jump);
  43.             if (jump > 5) {
  44.                 printf("  ! invalid - greater than 5 !\n");
  45.                 isInvalid = 1;
  46.             } else if (jump < 1) {
  47.                 printf("  ! invalid - smaller than 1 !\n");
  48.                 isInvalid = 1;
  49.             } else if (isEven == 0 && (jump % 2)) {
  50.                 printf("  ! invalid - odd as previous selected !\n");
  51.                 isInvalid = 1;
  52.             } else if (isEven == 1 && !(jump % 2)) {
  53.                 printf("  ! invalid - even as previous selected !\n");
  54.                 isInvalid = 1;
  55.             } else {
  56.                 isInvalid = 0;
  57.                 isEven = !(jump % 2);
  58.             }
  59.         } while (isInvalid);
  60.         total += jump;
  61.         printf("-> Now the value is %d\n", total);
  62.         if (total >= dest) {
  63.             if (turn == P1) {
  64.                 printf("\n!!! P1 WIN. CONGRATULATION !!!\n");
  65.                 Winner = P1;
  66.             } else {
  67.                 printf("\n!!! P2 WIN. CONGRATULATION !!!\n");
  68.                 Winner = P2;
  69.             }
  70.             isOut = 1;
  71.         }
  72.         if (turn == P1) {
  73.             turn = P2;
  74.         }
  75.         else {
  76.             turn = P1;
  77.         }
  78.     } while (!isOut);
  79.     getch();
  80.     return 0;
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement