Advertisement
Guest User

Untitled

a guest
Oct 20th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.56 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include<stdio.h>
  3. #include<stdlib.h>
  4. #include<time.h>
  5.  
  6. int x;
  7.  
  8. int ball_row=1, ball_col= 1;
  9.  
  10. void ball_out_check(int scale, int ball_row, int ball_col){
  11.     if (ball_row < 1) {
  12.                 printf("Error: Error: (Ball out of scale)\n");
  13.                 ball_row = 1;
  14.         }
  15.  
  16.     if (ball_row > scale) {
  17.                 printf("Error: (Ball out of scale)\n");
  18.                 ball_row = scale ;
  19.         }
  20.        
  21.     if (ball_col < 1) {
  22.                 printf("Error: Error: (Ball out of scale)\n");
  23.                 ball_row = 1 ;
  24.         }
  25.        
  26.     if (ball_col > scale) {
  27.                 printf("Error: (Ball out of scale)\n");
  28.                 ball_col = scale;
  29.            
  30.         }
  31.  
  32.  
  33. }
  34.    
  35.    
  36.    
  37. void process_game(int scale){
  38.    
  39.     srand((unsigned int)time (NULL));
  40.    
  41.         int hole_row= rand() % scale + 1;
  42.             int hole_col= rand() % scale + 1;
  43.  
  44.         if (hole_row==1 && hole_col == 1){
  45.            
  46.              process_game(scale);
  47.         }
  48.        
  49. else {
  50.            
  51.         while(1){  
  52.            
  53.  
  54.         print_game(scale, ball_row, ball_col, hole_row, hole_col);
  55.        
  56.         printf("\nThe Ball Movement is as follows: \n");
  57.        
  58.         printf("Up: 1\nDown: 2\nLeft: 3\nRight: 4");
  59.        
  60.         do{
  61.         printf("\nEnter Function: ");
  62.         scanf("%d", &x);
  63.         if(x<1 || x>5)
  64.             printf("\nError: Wrong key.\n");
  65.         }while(x<1||x>5);
  66.        
  67.         ball_out_check(scale, ball_row, ball_col);
  68.        
  69.        
  70.             if(x == 1){
  71.                 ball_row--;
  72.             }
  73.             else if(x == 2){
  74.                 ball_row++;
  75.             }
  76.             else if(x == 3){
  77.                 ball_col--;
  78.             }
  79.             else if(x == 4){
  80.                 ball_col++;
  81.             }
  82.                         if(ball_row == hole_row && ball_col == hole_col){
  83.                         printf("Congrats, you won\n\n");
  84.                         break;}
  85.  
  86.        
  87.     }  
  88.        
  89.             }
  90.         }
  91.  
  92.  
  93.  
  94. // print the game
  95. void print_game(int scale, int ball_row, int ball_col, int goal_row, int goal_col) {
  96.    
  97.     int i, j;
  98.  
  99.     printf("Ball position: <%d, %d>\n", ball_row, ball_col);
  100.     printf("Goal position: <%d, %d>\n\n", goal_row, goal_col);
  101.  
  102.     for (i = 0; i < scale + 1; i++) {
  103.         if (i == 0) {
  104.             for (j = 1; j <= scale; j++) {
  105.                 printf("\t%d", j);
  106.             }
  107.             printf("\n\n");
  108.         }
  109.         else {
  110.             printf("%d", i);
  111.             for (j = 1; j <= scale; j++) {
  112.                 if ((i == goal_row) && (j == goal_col))
  113.                     printf("\tΒΆ");
  114.                 else if ((i == ball_row) && (j == ball_col))
  115.                     printf("\t*");
  116.                 else
  117.                     printf("\t");
  118.             }
  119.             printf("\n\n");
  120.         }
  121.     }
  122. }
  123.  
  124.  
  125. void main() {
  126.     int scale, check;
  127.  
  128.     srand((unsigned int)time(NULL));
  129.  
  130.     do {
  131.         check = 0;
  132.  
  133.         printf("Input Game Scale (x by x): ");
  134.         scanf("%d", &scale);
  135.  
  136.         if (scale < 2) {
  137.             printf("Error: The input scale shold be larger than 1\n\n");
  138.             check = 1;
  139.         }
  140.     } while (check);
  141.  
  142.     process_game(scale);
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement