Jump to content

  • Log In with Google      Sign In   
  • Create Account

Candy Crush type game in C


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
17 replies to this topic

#1 saad7223   Members   -  Reputation: 98

Like
0Likes
Like

Posted 15 November 2013 - 04:24 AM

Hi guys,
I'm a first semester student of Programming and I have an assignment where I have to design a game somewhat like candy crush, called number crush where instead of candy, when 3 numbers are aligned, you get a point. and the numbers disappear.

I have to use C to design the game.
can anyone please help me?



Sponsor:

#2 jbadams   Senior Staff   -  Reputation: 18728

Like
5Likes
Like

Posted 15 November 2013 - 05:18 AM

Moving you to our For Beginners forum, as this questions seems to be more about developing (creating) the game than game-play design.

 

 

 

Have you learned how to write output to the command console, and do you know how to collect input from the user?

 

Have you been given any libraries or starting code to use?

 

What have you tried to start with?



#3 saad7223   Members   -  Reputation: 98

Like
-2Likes
Like

Posted 15 November 2013 - 07:19 AM

we were given an .exe file and were told to model our game on it.
can you check the game out and assist me in writing the code for it?
 



#4 saad7223   Members   -  Reputation: 98

Like
2Likes
Like

Posted 15 November 2013 - 07:24 AM

We were just given a compiled C code in .exe format and a word file explaining vaguely what to do and we were asked to model our game on it.
I've only gotten as far as the code i've shown below.

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

int main()
{
 int r=1;
 int arr[ROW][COL];
 char name[20];
 printf("-------NUMBER CRUSH-------\n***************************\n\n");
 printf("Enter your name: ");
 scanf("%c", &name);
 system("cls");
 r=1;
 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;
}

/*define grid and populate with random numbers and print to screen*/
int initialize(int grid[ROW][COL])
{
 int n;
 srand(time(NULL));
 for(x=0;x<10;x++)
 {
  for(y=0;y<10;y++)
  {
   n=2+rand()%7;
   grid[x][y]=n;
   printf("%d    ", n);
  }
  printf("\n\n\n");
 }
 return 0;
}

Attached Files



#5 blewisjr   Members   -  Reputation: 622

Like
6Likes
Like

Posted 15 November 2013 - 07:26 AM

Being that this is a school assignment we can not write the code. That is not how you learn.
How about instead you ask specific questions about issues you have and we try our best to better explain the concepts.

#6 saad7223   Members   -  Reputation: 98

Like
-4Likes
Like

Posted 15 November 2013 - 07:31 AM

i do not want you to write the entire code, just guide me in the right direction.
I will post new portions of the code as i'm done writing them and I would appreciate it if you could take a look and point me in the right direction.

can you do that?



#7 saad7223   Members   -  Reputation: 98

Like
1Likes
Like

Posted 15 November 2013 - 07:35 AM

 r=1;
 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");
 }

right now i'm having trouble with this portion of the code.
the loop is only supposed to work if i press 1, otherwise if i press anything else it should terminate but that is not happening.
can you tell me where i'm going wrong?



#8 Karsten_   Members   -  Reputation: 1611

Like
6Likes
Like

Posted 15 November 2013 - 07:37 AM

Possibly because

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

while(r==1)

Mutiny - Open-source C++ Unity re-implementation.
Defile of Eden 2 - FreeBSD and OpenBSD binaries of our latest game.


#9 saad7223   Members   -  Reputation: 98

Like
1Likes
Like

Posted 15 November 2013 - 07:48 AM

also i can not get the 1D array called name to store the complete string.
it is supposed to store the complete string which is entered using the scanf command and stored in the name array

it just displays some random ASCII character instead of the whole string.
what am i doing wrong here?

char name[20];
printf("-------NUMBER CRUSH-------\n***************************\n\n");
printf("Enter your name: ");
scanf("%c", &name);
system("cls");
r=1;
while(r=1)
{
printf("-------NUMBER CRUSH-------\n*************


#10 LennyLen   Crossbones+   -  Reputation: 3800

Like
2Likes
Like

Posted 15 November 2013 - 07:58 AM

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/

#11 blewisjr   Members   -  Reputation: 622

Like
1Likes
Like

Posted 15 November 2013 - 08:30 AM

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.

#12 saad7223   Members   -  Reputation: 98

Like
1Likes
Like

Posted 15 November 2013 - 08:59 AM

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



#13 blewisjr   Members   -  Reputation: 622

Like
0Likes
Like

Posted 15 November 2013 - 09:14 AM

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.

#14 LennyLen   Crossbones+   -  Reputation: 3800

Like
0Likes
Like

Posted 15 November 2013 - 08:48 PM

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.



#15 Aspirer   Members   -  Reputation: 544

Like
4Likes
Like

Posted 15 November 2013 - 09:11 PM

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"...



#16 Code_Grammer   Members   -  Reputation: 225

Like
1Likes
Like

Posted 17 November 2013 - 12:14 PM

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.


Edited by Code_Grammer, 17 November 2013 - 12:15 PM.


#17 saad7223   Members   -  Reputation: 98

Like
0Likes
Like

Posted 21 November 2013 - 01:00 PM

#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?



#18 shadowisadog   Crossbones+   -  Reputation: 2528

Like
1Likes
Like

Posted 24 November 2013 - 11:58 AM

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.


Edited by shadowisadog, 24 November 2013 - 12:15 PM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS