Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include<stdio.h>
- #include<stdlib.h>
- #include<time.h>
- int x;
- int ball_row=1, ball_col= 1;
- void ball_out_check(int scale, int ball_row, int ball_col){
- if (ball_row < 1) {
- printf("Error: Error: (Ball out of scale)\n");
- ball_row = 1;
- }
- if (ball_row > scale) {
- printf("Error: (Ball out of scale)\n");
- ball_row = scale ;
- }
- if (ball_col < 1) {
- printf("Error: Error: (Ball out of scale)\n");
- ball_row = 1 ;
- }
- if (ball_col > scale) {
- printf("Error: (Ball out of scale)\n");
- ball_col = scale;
- }
- }
- void process_game(int scale){
- srand((unsigned int)time (NULL));
- int hole_row= rand() % scale + 1;
- int hole_col= rand() % scale + 1;
- if (hole_row==1 && hole_col == 1){
- process_game(scale);
- }
- else {
- while(1){
- print_game(scale, ball_row, ball_col, hole_row, hole_col);
- printf("\nThe Ball Movement is as follows: \n");
- printf("Up: 1\nDown: 2\nLeft: 3\nRight: 4");
- do{
- printf("\nEnter Function: ");
- scanf("%d", &x);
- if(x<1 || x>5)
- printf("\nError: Wrong key.\n");
- }while(x<1||x>5);
- ball_out_check(scale, ball_row, ball_col);
- if(x == 1){
- ball_row--;
- }
- else if(x == 2){
- ball_row++;
- }
- else if(x == 3){
- ball_col--;
- }
- else if(x == 4){
- ball_col++;
- }
- if(ball_row == hole_row && ball_col == hole_col){
- printf("Congrats, you won\n\n");
- break;}
- }
- }
- }
- // print the game
- void print_game(int scale, int ball_row, int ball_col, int goal_row, int goal_col) {
- int i, j;
- printf("Ball position: <%d, %d>\n", ball_row, ball_col);
- printf("Goal position: <%d, %d>\n\n", goal_row, goal_col);
- for (i = 0; i < scale + 1; i++) {
- if (i == 0) {
- for (j = 1; j <= scale; j++) {
- printf("\t%d", j);
- }
- printf("\n\n");
- }
- else {
- printf("%d", i);
- for (j = 1; j <= scale; j++) {
- if ((i == goal_row) && (j == goal_col))
- printf("\tΒΆ");
- else if ((i == ball_row) && (j == ball_col))
- printf("\t*");
- else
- printf("\t");
- }
- printf("\n\n");
- }
- }
- }
- void main() {
- int scale, check;
- srand((unsigned int)time(NULL));
- do {
- check = 0;
- printf("Input Game Scale (x by x): ");
- scanf("%d", &scale);
- if (scale < 2) {
- printf("Error: The input scale shold be larger than 1\n\n");
- check = 1;
- }
- } while (check);
- process_game(scale);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement