Gaming in c

This is a discussion on Gaming in c within the C Programming forums, part of the General Programming Boards category; I have learned basic c now.But i want to move further and i want to create simple games using C.But ...

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    21

    Gaming in c

    I have learned basic c now.But i want to move further and i want to create simple games using C.But i am not getting an idea where and how to start.

    If you guys can create a program of a sample game in c using simple functions and explain it, it would be a great help.

  2. #2
    Registered User gil_savir's Avatar
    Join Date
    Jan 2009
    Posts
    13
    This is not a state of the art code. I have it from previous assignment one of my students made. The comments are quiet explanatory.

    If what you mean is graphical games, then I suggest you take a special course in computer graphics.

    Code:
    / * ========================================================= 
     * This program implements the game Mines Sweeper. Its user  
     * can perform the following actions:
     * 1. choose board size
     * 2. place mines
     * 3. remove mines
     * 4. show mines
     * 5. start the game
     * 0. exit 
     * ========================================================= */ 
    
    // preprocessing
    #include <stdio.h>
    
    #define TRUE 1
    #define FALSE 0
    
    #define N 9
    
    // for uncoverMine() return value
    #define EMPTY 1
    #define MINE 0
    
    // for printType parameter in printBoard()
    #define MENU_PRINT 0	// main menu board print
    #define GAME_PRINT 1	// print board after square is checked
    #define WON_PRINT 2		// print board after win
    #define LOST_PRINT 3	// print board after mine hit - like MENU_PRINT?
    
    // global Var decl
    	//! not alowed !//
    
    // global function decl
    int mainPlayGame(char board[N][N]); 
    void mainProgram(char board[N][N]);
    void printBoard(char board[N][N],int size, int printType);
    void mainMenu();
    int insertMine(char board[N][N],char mines[],int size);
    int removeMine(char board[N][N],int size,int x,int y);
    int unCover(char board[N][N],int size,int x,int y);
    int checkEndOfGame(char board[N][N],int size);
    int playGame(char board[N][N],int size)	;
    
    int main()
    {
    	char board[N][N];
    
    	mainProgram	(board);
    	printf("press ENTER to EXIT\n");
    	getchar();
    	return 0;
    }
    
    void mainProgram(char board[N][N])
    {
    	//char* test_char_gil;
    
    	int size_defined = FALSE;
    	int size, i,  row, col, menu_choice, x, y, lost;
    	int lost_times = 0;
    	int won_times = 0;
    	char menu_choice_buf[346], mines[40];
    	char coordinates[346];
    	char nothing[346];
    	// 
    	while (TRUE)
    	{
    		mainMenu();
    
    		gets(menu_choice_buf);
    		// test input is ok
    		if (menu_choice_buf[0] < '0' || '5' < menu_choice_buf[0])
    			continue;
    		if (menu_choice_buf[1] != '\0')
    			continue;
    
    		// convert char to ints
    		menu_choice = menu_choice_buf[0] - 48;
    
    		// exit
    		if (menu_choice == 0)
    		{
    			if (won_times+lost_times == 0)
    				printf("you didn't play\n");
    			else
    			{
    				printf("you played %d game(s):\n", won_times+lost_times);
    				printf("you won %d\n", won_times);
    				printf("you lost %d\n", lost_times);
    			}
    			printf("bye!\n");
    			// print num of games
    			return;
    		}
    
    		// choose board size
    		if (menu_choice == 1)
    		{
    			size_defined = FALSE;
    			printf("enter board size [1..9] (no input control is needed):\n");
    			scanf ("%d", &size);
    			gets(nothing);	// consuming the rest of the stdin buffer
    			if (0 < size && size < 10)
    			{
    				// initializing board
    				for (row = 0; row < size; row++)
    					for (col = 0; col < size; col++)
    						board[row][col] = ' ';
    
    				size_defined = TRUE;
    			}
    			continue;
    		}
    
    		// place mines
    		if (menu_choice == 2)
    		{
    			if (size_defined == FALSE)
    			{
    				printf("you must choose board size first!\n");
    				continue;
    			}
    			printf ("enter x,y of the new mines (x1,y1)(x2,y2)...(xn,yn):\n");
    
    			// initialize mins[]
    			for (i=0; i<40; i++)
    				mines[i]=' ';
    
    			// user inputs mine/s coordinates
    			gets(mines);
    
    			// testing user input and applying it to the board array
    			if (insertMine (board, mines, size) == FALSE)
    				printf("wrong input\n");
    
    			continue;
    		}
    
    		// remove mines
    		if (menu_choice == 3)
    		{
    			if (size_defined == FALSE)
    			{
    				printf("you must choose board size first!\n");
    				continue;
    			}
    
    			printf("enter x,y of the mine to remove (no input control is needed):\n");
    
    			// get coordinates and test input
    			gets(coordinates);
    			if ((coordinates[0] < '0') || ('9' < coordinates[2]))
    				continue;
    			if (coordinates[1] != ',')
    				continue;
    			if ((coordinates[2] < '0') || ('9' < coordinates[2]))
    				continue;
    
    			x = coordinates[0]-48;
    			y = coordinates[2]-48;
    
    			// remove if possible
    			if(removeMine(board, size, x, y) == FALSE)
    				printf("wrong input or no mine\n");
    			else
    				printf("mine removed\n");
    
    			// if not, print err msg
    
    			continue;
    		}
    
    		// show mines
    		if (menu_choice == 4)
    		{
    			if (size_defined == FALSE)
    			{
    				printf("you must choose board size first!\n");
    				continue;
    			}
    			printBoard(board, size, MENU_PRINT);
    
    			continue;
    		}
    
    		// start the game
    		if (menu_choice == 5)
    		{
    			if (size_defined == FALSE)
    			{
    				printf("you must choose board size first!\n");
    				continue;
    			}
    			lost = playGame(board, size);
    			if (lost == TRUE)
    				lost_times++;
    			else
    				won_times++;
    
    			// make sure new game is to start
    			size_defined = FALSE;
    
    			continue;
    		}
    	}
    }
    
    
    void mainMenu()
    {
    	printf("--------------------------\n");
    	printf("welcome to the game\n"); 
    	printf("1. choose board size\n");
    	printf("2. place mines\n"); 
    	printf("3. remove mines\n"); 
    	printf("4. show mines\n"); 
    	printf("5. start the game\n"); 
    	printf("0. exit\n");
    	printf("please enter your choice (input control is needed):\n");
    }
    
    
    
    /* printType can have one of the following macros:
     * MENU_PRINT  - main menu board print
     * GAME_PRINT  - print board after square is checked
     * WON_PRINT   - print board after win
     * LOST_PRINT - print board after mine hit
     */
    void printBoard(char board[N][N],int size, int printType)
    {
    	char square;
    	int col, row;
    	// print top border first row
    	for (col = 0; col < size; col++)
    		printf("+-");
    	printf("+\n");
    
    	for (row = 0; row < size; row++)
    	{
    		for (col = 0; col < size; col++)
    		{
    			square = board[row][col];
    			if ((printType == MENU_PRINT) || (printType == WON_PRINT) || (printType == LOST_PRINT))
    			{
    				printf("|%c", square);
    			}
    			else
    				if (printType == GAME_PRINT)
    				{
    					if ((square == '*') || (square == ' '))
    						square = '?';
    					printf("|%c", square);
    				}
    				else
    					printf("something is damn wrong here\n");
    		}
    		printf("|\n");
    
    		for (col = 0; col < size; col++)
    			printf("+-");
    		printf("+\n");
    	}
    }
    
    
    int insertMine(char board[N][N],char mines[],int size)
    {
    	int i, input_ok, x, y, y_con, xy_to_input;
    	int test_type = 0;
    
    	// test for input ok
    	for (i=0; i<40; i++)
    	{
    		if (mines[i] == '\0')
    			break;
    		if (mines[i] == ' ')
    			continue;
    		// '(' test
    		if (test_type == 0)
    		{
    			if (mines[i]!='(')
    			{
    				input_ok = FALSE;
    				return input_ok;
    			}
    			else // !(mines[i]!='(')
    			{
    				test_type = (test_type + 1) % 5 ;
    				continue;
    			}
    		}
    
    		// number test
    		if ((test_type == 1) || (test_type == 3))
    		{
    			if ( mines[i] < 48 || 57 < mines[i] )
    			{
    				input_ok = FALSE;
    				return input_ok;
    			}
    			else 
    			{
    				test_type = (test_type + 1) % 5 ;
    				continue;
    			}
    		}
    
    		// ',' test
    		if (test_type == 2)
    		{
    			if (mines[i]!=',')
    			{
    				input_ok = FALSE;
    				return input_ok;
    			}
    			else 
    			{
    				test_type = (test_type + 1) % 5 ;
    				continue;
    			}
    		}
    
    		// ')' test
    		if (test_type == 4)
    		{
    			if (mines[i]!=')')
    			{
    				input_ok = FALSE;
    				return input_ok;
    			}
    			else 
    			{
    				test_type = (test_type + 1) % 5 ;
    				continue;
    			}
    		}
    	}
    
    	// test if input is complete
    	if (test_type != 0)
    	{
    		input_ok = FALSE;
    		return input_ok;
    	}
    
    	// input coordinates to board
    	y_con = FALSE;
    	xy_to_input = FALSE;
    	i = 0;
    	while  ( mines[i] != '\0')
    	{
    		if (48 < mines[i] && mines[i] < 57)
    		{
    			if (y_con == FALSE)
    			{
    				x = (mines[i]-48);
    				y_con = TRUE;
    			}
    			else
    			{
    				y = (mines[i]-48);
    				y_con = FALSE;
    				xy_to_input = TRUE;
    			}
    		}
    		
    		if (xy_to_input == TRUE)
    		{
    			printf ("(%d,%d)",x,y);
    			if ((x > size) || (y > size))
    				printf (" is out of range\n");
    			else
    			{
    				if (board[x-1][y-1] == '*')
    					printf (" already full\n");
    				else
    				{
    					board[x-1][y-1] = '*';
    					printf (" mine inserted\n");
    				}
    			}
    			xy_to_input = FALSE;
    		}
    		i++;
    	}
    
    	return TRUE;
    }
    
    int removeMine(char board[N][N],int size,int x,int y)
    {
    	// test x,y boundaries
    	if ( (x<1 || size<x) || (y<1 || size<y))
    		return FALSE;
    	// test if mine really exists at x,y
    	if (board[x][y] != '*')
    		return FALSE;
    
    	// remove mine
    	board[x][y] = ' ';
    
    	return TRUE;
    }
    
    
    int playGame(char board[N][N],int size)	
    {
    	int x, y, lost = FALSE;
    	char coordinates[346] = {0};
    
    	while ( (checkEndOfGame(board, size) == FALSE) && (lost == FALSE))
    	{
    		// enter x,y
    		printf("enter a squre x,y to uncover (no input control is needed):\n");
    		
    		// get coordinates and test input
    		gets(coordinates);
    		if ((coordinates[0] < '0') || ('9' < coordinates[2]))
    			continue;
    		if (coordinates[1] != ',')
    			continue;
    		if ((coordinates[2] < '0') || ('9' < coordinates[2]))
    			continue;
    
    		x = coordinates[0]-48;
    		y = coordinates[2]-48;
    
    		// test square
    		if (unCover(board, size, x, y) == MINE)
    			lost = TRUE;
    
    		// print result
    		if (lost == FALSE)
    			printBoard(board, size, GAME_PRINT);
    	}
    
    	// print win/loose
    	if (lost == TRUE)
    	{
    		printf("you lost\n");
    		printBoard(board, size, LOST_PRINT);
    	}
    	else
    	{
    		printf("all mines were uncovered - you won\n");
    		printBoard(board, size, WON_PRINT);
    	}
    
    	return lost;
    }
    
    int checkEndOfGame(char board[N][N],int size)
    {
    	int row, col;
    
    	// counting empty, non checked squares
    	int empty_square = 0;
    	for (row = 0; row < size; row++)
    		for (col = 0; col < size; col ++)
    			if (board[row][col] == ' ') 
    				empty_square++;
    
    	if (empty_square == 0)
    		return TRUE;
    	else
    		return FALSE;
    }
    
    int unCover(char board[N][N],int size,int x,int y)
    {
    	int sum, row, col;
    
    	// turning user input [1..9] to array index compatible [0..8]
    	x--; 
    	y--;
    
    	if (board[x][y] == '*')
    	{
    		board[x][y] = 'X';
    		return MINE;
    	}
    
    	// count neighbor squares for mines
    	sum = 0;
    	for (row = 0; row < size; row++)
    		for (col = 0; col < size; col ++)
    			if ((x-1 <= col) && (col <= x+1) && (y-1 <= col) && (col <= y+1))
    				if (board[row][col] == '*')
    					sum++;
    
    	// put in board
    	board[x][y] = sum+48;
    
    	return EMPTY;
    }

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,449
    Also note that for professional games, C++ is used, because it has a big advantage there.
    That is not to say C does not work for games, but if you are intending to learn writing big games or getting a job, you should concentrate on learning C++.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    21
    Thx for such quick help.I will definately go through the program.

    Elysia,Thx for advice.I am seriously considering learning c++.I have learned C but i want to move ahead.But i am not able to understand where to start from.Which book would be the best and the best way to learn c++?Also please answer how easy learning c++ will be after learning c.I donn't want to waste my holidays.

    Your advice is very valuable to me.Please suggest what to do...

  5. #5
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    A complete c++ reference by herbert schlidt is a good book to start with in my view.u can check other buks also on C++ board.after learning C it becomes a easy task to learn c++.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,436
    Quote Originally Posted by gil_savir
    This is not a state of the art code. I have it from previous assignment one of my students made. The comments are quiet explanatory.
    I hope that you are not the one teaching students to use unsafe functions like gets() and to have many rather long functions.

    Quote Originally Posted by ankitsinghal_89
    I am seriously considering learning c++.
    Okay, but note that if you do have more C++ specific questions, they should go in the C++ programming forum instead. If you have more specific game development questions, they should go in the game programming forum.

    Quote Originally Posted by ankitsinghal_89
    Which book would be the best and the best way to learn c++?
    There is no single book that is universally the best way to learn C++ for every person. Given that you already have some programming background I would suggest Accelerated C++ by Koenig and Moo.

    Quote Originally Posted by ankitsinghal_89
    Also please answer how easy learning c++ will be after learning c.
    For some people it will be more difficult, since in the words of Yoda, "you must unlearn what you have learned". For others it would be easier than learning C++ directly because they can make use of their knowledge of C syntax, and whatever general programming skills picked up while using C to solve problems.

    Quote Originally Posted by BEN10
    A complete c++ reference by herbert schlidt is a good book to start with in my view.
    I would caution against books by Herbert Schlidt as he as a poor track record of quality authorship with respect to the C and C++ standards. Of course, poor performance in the past does not mandate poor performance in the future, but it certainly does give me good vibes for a novice to read a book attributed to him.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User gil_savir's Avatar
    Join Date
    Jan 2009
    Posts
    13
    Quote Originally Posted by laserlight View Post
    I hope that you are not the one teaching students to use unsafe functions like gets() and to have many rather long functions.
    Fear not. I'm a private teacher who is bound to the constraints of a (to my opinion) not too sharp professor that teaches this subject to one of my students. gets() is long out of my own functions arsenal.
    I guess that what you call long functions refers to their verbosity, which in my opinion is crucial for students first programming assignments ever.

  8. #8
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    I use C to develop simple hobby games... it's fine for that. If I was more serious then I'd use C++, but it's just so much fun in C.

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Laserlight means "bad" vibes, or at least not good vibes regarding books by Herbert Schlidt.

    His books in the past have contained several rather blatant errors.

  10. #10
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    493
    GameDev.net -- How do I make games? A Path to Game Development
    have a look here , its a great help to you find your way.
    Highlight Your Codes
    The Boost C++ Libraries (online Reference)

    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.."
    Bill Bryson


  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,436
    Quote Originally Posted by gil_savir
    I guess that what you call long functions refers to their verbosity, which in my opinion is crucial for students first programming assignments ever.
    I think that it is more crucial for students to learn abstraction, and one way is to break up functions into smaller ones that perform well defined portions of the task of the given function (which itself should do one thing and do it well).

    Quote Originally Posted by Adak
    Laserlight means "bad" vibes, or at least not good vibes regarding books by Herbert Schlidt.
    Yes, for some reason I omitted the "not" during editing.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Feb 2008
    Posts
    19
    Quake 3 Arena seems all in C to me, not C++. If they could make such a successful game just using C means it's possible.
    Use C since you know it already, there are articles and ebooks on how to use C as a OO language on the net if you wish.
    Then again learning C++ won't hurt. I'm no expert.

  13. #13
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by Elysia View Post
    Also note that for professional games, C++ is used, because it has a big advantage there.
    That is not to say C does not work for games, but if you are intending to learn writing big games or getting a job, you should concentrate on learning C++.
    Could you care to provide arguments for that? In fact, I'm quite sure most games are made in C, not C++, because many of the C++ concepts (virtual functions, classes over functions) have some overhead, which takes some of the so-important processor power...

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,436
    Quote Originally Posted by EVOEx
    Could you care to provide arguments for that?
    I am not a game programmer and from what I understand the industry requirements is different from many other programming sub-fields, but I believe your question is at the heart of the language debate between C and C++. Arguments in favour of C++ include its native support for more modern programming techniques, but whether those really matter for game programming, I cannot say.

    Quote Originally Posted by EVOEx
    In fact, I'm quite sure most games are made in C, not C++
    This might be better settled by statistics. Of course, game programming is a wide topic, e.g., online games are more likely to use Java and web programming languages than C and/or C++.

    Quote Originally Posted by EVOEx
    because many of the C++ concepts (virtual functions, classes over functions) have some overhead, which takes some of the so-important processor power...
    I think that the additional cost implied by virtual functions and virtual calls would be the same if one were simulating such runtime polymorphism in C. I do not see how classes would introduce additional overhead in themselves: C has the same "problem" with structs.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,449
    Quote Originally Posted by 2112 View Post
    Quake 3 Arena seems all in C to me, not C++. If they could make such a successful game just using C means it's possible.
    Use C since you know it already, there are articles and ebooks on how to use C as a OO language on the net if you wish.
    Then again learning C++ won't hurt. I'm no expert.
    Indeed, it is possible, but C is an old language and thus for big projects, it gets a little harder to manage. Plus C is also a very low level language, so doing a lot of things in C just takes longer time than in other languages, and for games, time is money.
    So they would need some language that's fast (ie no C#), and yet something that is more powerful and flexible than C.
    It is no wonder that gaming companies usually choose C++ then.
    Note usually, of course. Not all consoles use C++ and not all companies code using C++.

    Quote Originally Posted by EVOEx View Post
    Could you care to provide arguments for that? In fact, I'm quite sure most games are made in C, not C++, because many of the C++ concepts (virtual functions, classes over functions) have some overhead, which takes some of the so-important processor power...
    Just as overhead is important, you have to ask yourself - where is the speed important? If it's a function called 10 times versus one called 10 000 times, the extra overhead really doesn't matter.
    And beside little extra speed loss (which typically means nothing on today's hardware), there are other important factors to consider - especially development time and the tools provided, ie the flexibility provided by the language.
    And then the fact of compability with tools and such. But since C++ is backwards compatible, they can use wherever tools and APIs use C interface without problems and still use a higher level language.

    Also, C++ usually lends itself very well to game development due to the flexibility it provides (especially OOP and generics), making developers able to create a powerful and flexible engine and basic components.

    Also, the typical "C runs faster than C++" comment is a myth. They are about equal in speed.
    Also there's a phrase known as "use the right tool for the right job." Bad design will lead to slow speed in both C and C++.
    Good design will lead to good speed in both C and C++.
    Last edited by Elysia; 01-23-2009 at 04:04 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

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

Similar Threads

  1. My view on the gaming world
    By swgh in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-25-2008, 09:41 AM
  2. What software is needed to write internet based gaming programs
    By grumpomatic in forum Game Programming
    Replies: 8
    Last Post: 11-26-2005, 09:49 PM
  3. Retro gaming purpose PC
    By Shadow in forum Tech Board
    Replies: 1
    Last Post: 11-08-2003, 05:09 PM
  4. Online Gaming
    By dP munky in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 04-05-2003, 02:51 PM
  5. online gaming places
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 02-19-2002, 11:26 PM

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