Candy Crush type game in C

Started by
16 comments, last by shadowisadog 10 years, 5 months ago
I would also like to expand on what Lenny said with a small tip on scanf.
Typically it is dangerous to use scanf on a fixed size array as from what I remember scanf does no bounds checking at all. It would be safer to either a use a char** or to use the version that does bounds checking which I think is sscanf(). I would do a search first as the name of the function may be wrong off the top of my head.
Advertisement

Is there any website or any specific book that you recommend i read to be able to write the code for this game?

For C the K&R book is the go to book for the language. As for website the one lenny linked should have a reference of all the c standard headers with examples of use.

I would also like to expand on what Lenny said with a small tip on scanf.
Typically it is dangerous to use scanf on a fixed size array as from what I remember scanf does no bounds checking at all. It would be safer to either a use a char** or to use the version that does bounds checking which I think is sscanf(). I would do a search first as the name of the function may be wrong off the top of my head.


sscanf performs just like scanf except that it gets its input from a given string rather than from the stdin stream. While that does make it inherently safer as you can control the sizes of both the input and output strings it isn't useful in this scenario.

The better approach is to use:


fgets(name, sizeof(name), stdin);

You can get the same result using scanf, but with an extra line by doing:


scanf("%20s" &name);
name[19] = '\0';

But then you have to change two lines if you change the size of the name string. The fgets line would not need to be changed if the size of the string changed.

Possibly because



while(r=1)
is assigning 1 to r. Instead you might want to use:

while(r==1)

You need to change the 4th line to:


scanf("%s", &name);
Using %c will only read a single character, not a complete string.

You can use this page as a reference for format specifiers: http://www.cplusplus.com/reference/cstdio/printf/

I would also like to expand on what Lenny said with a small tip on scanf.
Typically it is dangerous to use scanf on a fixed size array as from what I remember scanf does no bounds checking at all. It would be safer to either a use a char** or to use the version that does bounds checking which I think is sscanf(). I would do a search first as the name of the function may be wrong off the top of my head.


sscanf performs just like scanf except that it gets its input from a given string rather than from the stdin stream. While that does make it inherently safer as you can control the sizes of both the input and output strings it isn't useful in this scenario.

The better approach is to use:

fgets(name, sizeof(name), stdin);

You can get the same result using scanf, but with an extra line by doing:

scanf("%20s" &name);
name[19] = '\0';

But then you have to change two lines if you change the size of the name string. The fgets line would not need to be changed if the size of the string changed.

Just my two cents, but I don't think just handing out the answers to his school assignment's really the thing to do here. Maybe tell him where the error is, rather than showing him the correct version--let him figure out WHY it's wrong on his own instead of handing him an A on the assignment...

Also, I'm curious if Mr. Saad hasn't been handed the answers through his Programming course, as some of these questions and errors would be addressed in any course before working with multi-dimensional arrays and functional games... Not criticizing his ability exactly, just saying he seems to be in a bit over his head, for having done his work on his own and grasping the material up to this point... Also


can you check the game out and assist me in writing the code for it?

seems more like "please tell me the answers" rather than "tell me how to get the answer"...

Aspirer

Saad is a beginner, so it might be best to assume positive intent. I think, at one point, every coder has a "what the heck" moment with some code. No, we're not supposed to write his assignment for him, but sometimes people just need a spark to ignite their inner flame.

Saad,

The code you were given is a framework.

#include <time.h>

#include <math.h>
#include <stdio.h>
#include <conio.h>
#define ROW 10
#define COL 10
int x,y;

Are include files and headers.

int main()

{
int r=1;
int arr[ROW][COL];
char name[20];
//Display a message/
printf("-------NUMBER CRUSH-------\n***************************\n\n");
//Ask the user for their name
printf("Enter your name: ");
scanf("%c", &name);
//Clear the screen.
system("cls");
// Assign the value 1 to r.
r=1;

Is the main loop that prints a message to the player saying. "Welcome to number crush" inside of the "main" function.

while(r=1)

{
printf("-------NUMBER CRUSH-------\n***************************\n\n");
printf("Welcome %c, LET'S PLAY!!\n\n", name);
initialize(arr);
printf("\n\n");
printf("to regenerate the array press 1: ");
scanf("%i", &r);
system("cls");
}
return 0;
}

Is the main loop. It waits for a key press, and clears the screen. I'm guessing here after " system("cls");" is a good place to begin your code. Before you start writing any code, it's a good idea to layout the code on paper, writing out each function, so it's easier to code. You should ask your instructor questions as well.


#include <time.h>
#include <math.h>
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>

#define ROW 9
#define COL 9
int x,y;

int main()
{
 int r=1;
 int arr[ROW][COL];
 char name[20];
 welcome(name);
 initialize(arr);
 printf("Thank you for playing %s!", name);
 return 0;
}

/*define grid and populate with random numbers and print to screen*/
int initialize(int grid[ROW][COL])
{
 int n, chk;
 srand(time(NULL));
 for(x=0;x<ROW;x++)
 {
  for(y=0;y<COL;y++)
  {
   do
   { 
    n=2+rand()%7;
    grid[x][y]=n;
	if (((grid[x][y]==grid[x-1][y]) && (grid[x][y]==grid[x-2][y])) || ((grid[x][y]==grid[x][y-1]) && (grid[x][y]==grid[x][y-2])))
	{
	 chk=1;
	 continue;;
	}
	else
	{
	 chk=0;
     printf("%d    ", n);
	}
   }
   while(chk==1);	 
  }
  printf("\n\n\n");
 }
}

/*playing menu*/ 
/*void menu(void)
{
 int drc;
 
 printf("\n***MENU***\n");
 printf("*Press 8 to move up: \n");
 printf("*Press 2 to move down: \n");
 printf("*Press 6 to move right: \n");
 printf("*Press 4 to move left: \n");
 printf("*Press 5 to switch: \n");
 printf("*Press Q to quit (case sensitive): \n");
 printf("Please enter your move: \n");
 
 getch()=drc;
 fflush(stdin); 
}*/

/*Welcome Message*/
int welcome(char name[50])
{
 int cnt;
 char c[100],s[100];
 FILE * txt;
 
 printf("----------NUMBER CRUSH----------\n*******************************\n");
 printf("----LIST OF PREVIOUS PLAYERS----\n");
 printf(" Name         Score\n");
 printf("------       -------\n");
 txt=fopen("txt.txt", "r");
 if(txt==NULL)
 {
  printf("No Scores Found\n");
  exit;
 }
 
/* while(fscanf(txt,"%s%i" ,&c &cnt)==2)
  printf("%s\t\t   %i\n",c,cnt);
 fclose(txt);*/
 
 printf("Enter your name: ");
 scanf("%s",name);
 system("cls");
 printf("Welcome %s, LET'S PLAY!!\n\n\n", name);
 fflush(stdin);
 printf("---------------NUMBER CRUSH---------------\n******************************************\n\n");
 return 0;
}

I've gotten this far but I'm still confused as to how to make a cursor move inside the grid which can be used to select a number and swap it with an adjacent number.
I know the cursor has to be a small array inside the main array but i'm confused as to how to go about writing the code for it.

Any help guys?

I have some general comments regarding your code:

1. I am not sure if it is the forum or if it is your code, but please make sure that you use proper indention. It will help make your code more readable.

2. If you get a line of code that is very long it is often a good idea to break it up into multiple lines for readability. (there is a line in your initialize function that is very long).

3. If you have a piece of code that is potentially difficult to understand, it is a good idea to add a comment explaining what the code does so that way others (and yourself!) can understand the code in the future.

4. You do not appear to be closing your file. I am fairly sure that you should be doing this (See: http://www.cprogramming.com/tutorial/cfileio.html ). I am also not sure why you commented out your menu code.

In answer to your actual question I am not going to provide a solution but I will provide some suggestions:

1. You have combined your initialize and print methods together. You should think about separating these so that you initialize the grid to the random configuration once, but that you can print the grid multiple times.

2. Think about how you can modify your print routine to add the "cursor". Look at your specification again. Do you see anything else that you need this functionality for?

3. How can you keep track of the cursor position?

4. How can you update the console and re print the grid when the cursor position updates?

Try to answer those questions and I think your solution will become apparent.

Good luck.

This topic is closed to new replies.

Advertisement