Monday, 2 March 2015

C Programming - Treasure Hunt!

One of the challenges we were given to help us learn C was to write a treasure hunt game, in which the user tries to find the treasure and is assisted with hot and cold clues. The user has 10 turns to find the treasure and at the end of each turn must confirm they wish to continue. As an extension, the map should print to the screen and the treasure should be randomly place. Here's my solution:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

//tmap4.c - game tmap4

void print_gmap(char gmap[5][5]) {
                printf("\n--1--2--3--4--5-\n");
                printf("1[%c][%c][%c][%c][%c]\n", gmap[0][0], gmap[0][1], gmap[0][2], gmap[0][3], gmap[0][4]);
                printf("2[%c][%c][%c][%c][%c]\n", gmap[1][0], gmap[1][1], gmap[1][2], gmap[1][3], gmap[1][4]);
                printf("3[%c][%c][%c][%c][%c]\n", gmap[2][0], gmap[2][1], gmap[2][2], gmap[2][3], gmap[2][4]);
                printf("4[%c][%c][%c][%c][%c]\n", gmap[3][0], gmap[3][1], gmap[3][2], gmap[3][3], gmap[3][4]);
                printf("5[%c][%c][%c][%c][%c]\n\n", gmap[4][0], gmap[4][1], gmap[4][2], gmap[4][3], gmap[4][4]);
}

int main() {
                int valid_coord, temp, count, a, b;
                char carry_on;

                char gmap[5][5]={
                                {'-', '-', '-', '-', '-'},
                                {'-', '-', '-', '-', '-'},
                                {'-', '-', '-', '-', '-'},
                                {'-', '-', '-', '-', '-'},
                                {'-', '-', '-', '-', '-'}
                };
               
                int p, q, r, s, difference_r, difference_s;
                int map[5][5];

                srand(time(NULL));
                p=rand()%5;
                q=rand()%5;

                for(r=0; r<=4; r++){
                                difference_r = abs(p-r);
                                for (s=0; s<=4; s++){
                                                difference_s = abs(q-s);
                                                if(r==p && s==q){
                                                                map[r][s]=0;
                                                }else if(difference_r<=1 && difference_s<=1){
                                                                map[r][s]=1;
                                                }else if(difference_r<=2 && difference_s<=2){
                                                                map[r][s]=2;
                                                }else if(difference_r<=3 && difference_s<=3){
                                                                map[r][s]=3;
                                                }else{
                                                                map[r][s]=4;
                                                }
                                }
                }
               
                system("clear");
                printf("You've heard of a gemstone said to possess powers over hot and cold. ");
                printf("It was stolen and hidden many years ago by a vicious pirate lord. ");
                printf("There are rumours his descendents have learnt of its location, but you think you may have enough of a headstart. ");
                printf("After several days at sea, you arrive at the secret island.\n\n");
                printf("In the distance, you can see pirate sails.\n\n");
                printf("Can you find the treasure first?\n");

                print_gmap(gmap);
                for(count=1; count<=10; count++){
                                printf("Enter the x co-ordinate to search\n");
                                valid_coord=scanf("%d", &b); /* This + while loop is to ensure an integer is received */
                                while(valid_coord != 1){
                                                while((temp=getchar()) != EOF && temp != '\n');
                                                printf("Co-ordinates are numbers; please try again:\t");
                                                valid_coord=scanf("%d", &b);
                                }
                                b=b-1;
                                printf("Enter the y co-ordinate to search\n");
                                valid_coord=scanf("%d", &a);
                while(valid_coord != 1){
                        while((temp=getchar()) != EOF && temp != '\n');
                        printf("Co-ordinates are numbers; please try again:\t");
                        valid_coord=scanf("%d", &a);
                }
                                a=a-1;
                                if(a < 0 || a > 4 || b<0 || b>4) {
                                                printf("I think you're lost at sea\n");
                                } else if(map[a][b]==0){
                                                printf("Congratulations! You found the gem in");
                                                if(count==1){
                                                                printf(" %d turn.\n", count);
                                                } else {
                                                                printf(" %d turns.\n", count);
                                                }
                                                gmap[a][b]='X';
                                                print_gmap(gmap);
                                                printf("  /\\ \n");
                                                printf(" /  \\ \n");
                                                printf("/__\\_\\ \n");
                                                printf("\\  / / \n");
                                                printf(" \\  / \n");
                                                printf("  \\/ \n\n");
                                                break;
                                } else if(map[a][b]==1){
                                                printf("This part of the island feels unnaturally hot.\n");
                                                gmap[a][b]='H';
                                } else if(map[a][b]==2){
                                                printf("This part of the island is warmer than you'd expect.\n");
                                                gmap[a][b]='W';
                                } else if(map[a][b]==3){
                                                printf("This part of the island is a bit cool.\n");
                                                gmap[a][b]='C';
                                } else if(map[a][b]==4){
                                                printf("It's really, really cold here, as though the heat is being sucked elsewhere.\n");
                                                gmap[a][b]='F';
                                }
                                print_gmap(gmap);
                                if(count!=10){
                                                printf("Type 'Y' to try again or 'N' to exit.\t");
                                                scanf(" %c", &carry_on);
                                                if(toupper(carry_on)!='Y'){
                                                                printf("Are you sure? Type 'Q' to quit or 'P' to keep playing:\t");
                                                                scanf(" %c", &carry_on);
                                                                if(toupper(carry_on) == 'Q'){
                                                                                printf("Goodbye\n");
                                                                                break;
                                                                }
                                                }
                                printf("\n");
                                }
                                if(count==10){
                                                printf("You've taken too long - the pirates are here!\n");
                                }
                }

return 0;
}
The idea was to consolidate what we'd already learned and to understand multi-dimensional arrays. What resulted was a fun little game that a lot of people in the office ended up playing. 

I've since learnt how to do a lot of the bits in this much more neatly and, looking through it again, think that this isn't actually my final code - but it gives an idea of my thinking. Particularly, I've found a neater loop to print the map which I'll be sharing soon, because the code it's in has a few problems I can't solve. Then, I'll tidy up my Battleships game and show that off.

No comments:

Post a comment