Like Tree1Likes

High Low Game with High Scores

This is a discussion on High Low Game with High Scores within the C Programming forums, part of the General Programming Boards category; Hi. I'm hoping someone can help me with a project I've been working on. I need to create a game ...

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    12

    High Low Game with High Scores

    Hi. I'm hoping someone can help me with a project I've been working on. I need to create a game a user enters a name though a menu option and then guesses a number, then the lowest number of tries to guess the correct number needs to be recorded in a high scores list. The username should be able to be changed to allow for multiple users in the high scores list. I have the actual game working, but for some reason I can't get both the name and the score to be recorded at the same time. I'm not asking for anyone to do my work for me, but I'm completely stumped and could use someone to point me in the right direction. This is what I have so far:

    Code:
    #include <stdio.h>			
    #include <stdlib.h>			
    #include <conio.h>			
    #include <string.h>			
    #include <ctype.h>
    #include <time.h>
    #include <math.h>
    
    #define MAX 5
    #define MAX2 10
    
    struct player
    {
     char name[30];
     int score;
    };
    void init_array(struct player *top5);
    void get_name(struct player *local);
    void print_array(struct player *top5);
    int setpt(struct player *local,int hld_it);
    void load_array(struct player *local,int size);
    void save_array(struct player *local,int size);
    char make_choice();
    void display_menu();
    void choice_a();
    int choice_b(struct player *local, struct player *top5);
    void printsummary(int ct, int hlt, int t, char n[30]);
    
    
    void main()
    {
    	int HL_Total= 0;
    	int HL_Tries=0;
    	int	CC_Total= 0;
    	int tries= 0;
    	char choice;
    	char hld_name[30];
    	
    	struct player top5[MAX2];
    	struct player local[MAX2];
    	load_array(top5,sizeof(top5));
    	print_array(top5);
    	get_name(top5);
    
    	do
    	{
    	display_menu();
    	choice = make_choice();
    		switch(choice)
    			{
    			case 'A':
    			 choice_a();
    			 CC_Total++;
    			 break;
    			case 'B':
    			 HL_Tries += choice_b(local, top5);
    			 HL_Total++;
    
    			 break;
    			case 'C':
    				printsummary(CC_Total, HL_Total, HL_Tries, hld_name);
    				break;
    			case 'N':
    				get_name(top5);				
    				break;
    			case 'S':
    				print_array(top5);				
    				break;	
    			}
    	}while(choice != 'Q'); 
    
    	save_array(top5,sizeof(top5));
    }
    
    void init_array(struct player *local)
    {
     int cur_row;
     for(cur_row=0;cur_row<MAX2;cur_row++)
     {
      local[cur_row].score=0;
      strcpy(local[cur_row].name,"init");
     }
    }
    
    void print_array(struct player *local)
    {
     int cur_row;
     printf("\n");
     for(cur_row=0;cur_row<MAX2;cur_row++)
     {
      printf("Element #%d is:%30s    %6d\n"
    		,cur_row,local[cur_row].name,local[cur_row].score);
     }
     printf("\n\nPress any key");
     getch();
    }
    
    void get_name(struct player *local)
    {
     int cur_row;
     int hld_row=0;
     char hld_name[30];
     printf("\nEnter a name\n");
      flushall();
      gets(hld_name);
    
    }
    
    
    
    
    int setpt(struct player *local,int hld_it)
    {
     int set_pt,incr;
    
     for(set_pt=0;set_pt < MAX2 && hld_it > local[set_pt].score
    			&& local[set_pt].score!=0;set_pt++); //this for loop will locate the row that the new data is to be inserted
    
     for(incr=MAX2 -1;incr >set_pt;incr--)  //this for loop will make room in the array for the new data insertion
     {
      local[incr] = local[incr-1];
      local[incr].score=local[incr-1].score;
      strcpy(local[incr].name,local[incr-1].name);
     } 
     return set_pt;
    
    }
    
    void save_array(struct player *local,int size)
    {
     FILE *fptr;
    
     if ((fptr = fopen("topten.dat","wb"))==NULL)
     {
      system("CLS");
      printf("File write error...Press any key to terminate");
      getch();
      exit(0);
     }
     else
     {
      fwrite(local,size,1,fptr);
      fclose(fptr);
     }
    }
    
    void load_array(struct player *local,int size)
    {
     FILE *fptr;
    
     if ((fptr = fopen("topten.dat","rb"))==NULL)
     {
      system("CLS");
      printf("File does not exists...initializing new array...Press any key to continue");
      getch();
      init_array(local);
     }
     else
     {
      fread(local,size,1,fptr);
      fclose(fptr);
     }
    }
    
    
    char make_choice()
    {
    	char ch;
    	do{
    	ch = toupper(_getch());
    	}while (!strchr("ABCNSQ",ch));
    	return ch;
    }
    
    void display_menu()
    {
    	system("cls");
    
    	printf( "Please select one of these options. \n");
    	printf( "A. Change Count \n");
    	printf( "B. Play High / Low Game \n");
    	printf( "C. Print Summary \n");
    	printf( "N. Change Name \n");
    	printf( "S. High Scores \n");
    	printf( "Q. Quit Program \n");
    }
    
    
    void choice_a()
    {
    	int hd,
    		q,
    		d,
    		n,
    		p;
    system("cls");
    printf("You have chosen Change Count \n\nEnter number of half dollars: ");
    scanf_s("%i", &hd);
    printf("Enter number of quarters: ");
    scanf_s("%i", &q);
    printf("Enter number of dimes: ");
    scanf_s("%i", &d);
    printf("Enter number of nickles: ");
    scanf_s("%i", &n);
    printf("Enter number of pennies: ");
    scanf_s("%i", &p);
    int tc= hd+q+d+n+p;
    double hd_v= hd*0.5,
    	  q_v= q*0.25,
    	  d_v= d*0.10,
    	  n_v= n*0.05,
    	  p_v= p*0.01;
    printf("\nYou have entered %i half dollars: $", hd);
    printf("%.2f",hd * 0.5);
    printf("\nYou have entered %i quarters: $", q);
    printf("%.2f", q * .25);
    printf("\nYou have entered %i dimes: $", d);
    printf("%.2f", d * .10);
    printf("\nYou have entered %i nickles: $", n);
    printf("%.2f", n * .05);
    printf("\nYou have entered %i pennies: $", p);
    printf("%.2f", p * .01);
    double tm= hd_v+q_v+d_v+n_v+p_v;
    printf("\n\nYou have entered %i coins worth a total of %.2f", tc, tm);
    printf("\n\nPress any key to return to the main menu");
    _getch();
    }
    
    int choice_b(struct player *local, struct player *top5) 
    {
    	int magic, 
    		guess,
    		hld_row=0,
    		cur_row,
    		tries=0;
    	char hld_name[30];
    
    	srand((unsigned)time(NULL));
    	magic = rand()%MAX;
    	system("cls");
    
    	printf("\n\nGuess the random number from 0 to %d\n\n",MAX);
    
    	do
    	{
    		printf("guess: ");
    		scanf_s("%d",&guess);
    		if(guess == magic)
    		{
    			printf("** Right **");
    			printf(" %d is the magic number\n", magic);
    		}
    		else 
    		if (guess > magic)
    			printf(".. Wrong .. Too High\n");
    		else
    			printf(".. Wrong .. Too Low\n");
    			tries++;
    	}while (guess != magic);
    	hld_row=setpt(local,tries);
    	local[hld_row].score=tries;
    	strcpy(local[hld_row].name, hld_name);
    	getch();
     printf("You took %d tries.\n", tries);
    
     printf("\n\n\n\nPress any key to return to the main menu");
     getch();
     return tries; 
    }
    
    void printsummary(int ct, int hlt, int t, char n[30])
    {
    	system("cls");
    	printf("Hello %10s\n\n", n);
    	printf("You have counted change %d times\n", ct);
    	printf("You have played High / Low %d times\n", hlt);
    	printf("Your average tries for High / Low are %.2f\n", (float) t/hlt);
    	getch();
    }

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,498
    As for your problems, I think there are a few:
    • It's int main(void) and return an integer at the end, usually 0.
    • gets is bad.
    • 'local' in main should probably be a single struct rather than an array of MAX2 elements.
    • choice_a and choice_b, local and top5 (hey, MAX2 is 10) are horrible names. Why not call them something like count_change, guess_number, player and top_scores?
    • You need to pass in both player and top_scores to guess_number, then pass both of them to setpt.
    • setpt never writes the new player data to the top5 array (which is erroneously named, since there are 10 entries in it). It should only do this if it found a place in the top scores list, i.e. if set_pt is < MAX2.


    That should get you going for a bit.

  3. #3
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Also, avoid using conio and obsolete DOS libraries and step into the 21st century .
    anduril462 likes this.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  4. #4
    Registered User
    Join Date
    May 2011
    Posts
    12
    How do I write the new data to the array? I tried using strcpy but it didn't work.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    What "didn't work" about it?

  6. #6
    Registered User
    Join Date
    May 2011
    Posts
    12
    Here's what I added to setpt: for(set_pt=0;set_pt < MAX2;strcpy(Hld_it, top_scores);
    it gave me this error: Error 2 error C2664: 'strcpy' : cannot convert parameter 1 from 'int' to 'char *'

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You can't use strcpy on things that aren't strings. Hld_it isn't a string.

  8. #8
    Registered User
    Join Date
    May 2011
    Posts
    12
    Oh right. Sorry. So if I want to write an integer into an array, what would I use?

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Using =.

  10. #10
    Registered User
    Join Date
    May 2011
    Posts
    12
    when I put for(;set_pt < MAX2;Gnd_it = player[set_pt].score); it compiles, but only saves one high score. If I put for(set_pt=0;set_pt < MAX2;Gnd_it = player[set_pt].score); it refuses to go back to the menu from the guess the number game.

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why are you making the incrementation portion of your loop assign a value instead of incrementing the loop?


    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    Registered User
    Join Date
    May 2011
    Posts
    12
    I need to write the new high scores to the array after it is incremented and I'm not sure how to do it. I'm pretty new to this stuff.

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    int array[ 5 ];
    int x;
    for( x = 0; x < 5; x++ )
        array[ x ] = 1 * x;

    Quzah.
    Hope is the first step on the road to disappointment.

  14. #14
    Registered User
    Join Date
    May 2011
    Posts
    12
    I can get the score into the array, but my problem is that I can't get a player name associated with the score.

  15. #15
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Bradley Buck View Post
    I can get the score into the array, but my problem is that I can't get a player name associated with the score.
    That's why your struct has two (count 'em, two (2)) parts to it. You need to set them both.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. High Scores - A Dilemma
    By CrazyNorman in forum Game Programming
    Replies: 5
    Last Post: 12-28-2006, 08:01 PM
  2. Solitaire High Scores
    By sean in forum A Brief History of Cprogramming.com
    Replies: 17
    Last Post: 07-03-2005, 07:15 PM
  3. Help on high scores
    By Gnoober in forum C++ Programming
    Replies: 0
    Last Post: 02-17-2003, 06:28 PM
  4. Working on High Scores...
    By Gnoober in forum C++ Programming
    Replies: 4
    Last Post: 02-16-2003, 11:50 AM
  5. High Scores
    By Gnoober in forum C++ Programming
    Replies: 2
    Last Post: 02-13-2003, 12:08 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21