• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Saad Zafar

Candy Crush type game in C

17 posts in this topic

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?

0

Share this post


Link to post
Share on other sites

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?
 

-2

Share this post


Link to post
Share on other sites

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;
}
2

Share this post


Link to post
Share on other sites

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?

-4

Share this post


Link to post
Share on other sites
 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?

1

Share this post


Link to post
Share on other sites

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*************
1

Share this post


Link to post
Share on other sites
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.
1

Share this post


Link to post
Share on other sites

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

1

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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
1

Share this post


Link to post
Share on other sites
#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?

0

Share this post


Link to post
Share on other sites

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
1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0