C programming game problem

This is a discussion on C programming game problem within the C Programming forums, part of the General Programming Boards category; I'm having a problem with my code. It's is a game where you have to control the turtle without seeing ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    9

    C programming game problem

    I'm having a problem with my code.
    It's is a game where you have to control the turtle without seeing where it is. But it won't control, it's stuck where it randomly starts. I've been scanning the code for hours, but I can't find the problem

    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <windows.h>
    #include <time.h>
    
    #define ROW 25
    #define COL 30
    
    void title()
    {
    	printf("\nWELCOME TO...\n\n");
    	printf("\t\t88888888888               888    888      \n");    
    	printf("\t\t    888                   888    888          \n");
    	printf("\t\t    888  888  888 888d888 888888 888  .d88b.  \n");
    	printf("\t\t    888  888  888 888P*   888    888 d8P  Y8b \n");
    	printf("\t\t    888  888  888 888     888    888 88888888 \n");
    	printf("\t\t    888  Y88b 888 888     Y88b.  888 Y8b.     \n");
    	printf("\t\t    888   *Y88888 888      *Y888 888  *Y8888  \n\n");
                                                  
                                                  
                                                  
    	printf("\t\t\t .d8888b.          d8b          \n");
    	printf("\t\t\td88P  Y88b         Y8P          \n");
    	printf("\t\t\t888    888                      \n");
    	printf("\t\t\t888        888d888 888 88888b.  \n");
    	printf("\t\t\t888  88888 888P*   888 888 *88b \n");
    	printf("\t\t\t888    888 888     888 888  888 \n");
    	printf("\t\t\tY88b  d88P 888     888 888 d88P \n");
    	printf("\t\t\t *Y8888P88 888     888 88888P*  \n");
    	printf("\t\t\t                       888      \n");
    	printf("\t\t\t                       888      \n");
    	printf("\t\t\t                       888   \n\n");
    }
    
    void instructions()
    {
    	printf(" Task:\n");
    	printf(" The turtle is having his time swimming around the sea.\n He wants your help in guiding him under the sea and on the surface.\n");
    	printf("\n Controls:\n");
    	printf(" 8 - Up \n");
    	printf(" 2 - Down \n");
    	printf(" 4 - Left \n");
    	printf(" 6 - Right \n");
    	printf(" 7 - Pen Down \n");	
    	printf(" 5 - Pen Up \n");
    }
    
    void menu()
    {
    	printf("\t\t\t  Press 1 to play the game.\n");
    	printf("\t\t\tPress 2 to view instructions.\n");
    	printf("\t\t\t     Press 3 to to quit.\n");
    }
    
    int print(char a[ROW][COL])
    {
    	int x,y;
    
    	system("cls");
    
    	for(x=0;x<ROW;x++)
    	{
    		printf("\t\t\t");
    		for(y=0;y<COL;y++)
    		{
    			printf("%c",a[x][y]);
    	
    		}
    
    	printf("\n");
    	}
    	
    	return a[ROW][COL];
    }
    
    int clear(char a[ROW][COL])
    {
    	int x,y;
    
    	for(x=1;x<=ROW;x++)
    	{
    		for(y=1;y<=COL;y++)
    		{
    			a[x][y]=NULL;
    		}
    	}
    
    	for(x=0;x<COL;x++)
    	{	
    		a[0][x]=178;
    	}
    
    	for(x=0;x<ROW;x++)
    	{
    		a[x][0]=178;
    	}
    
    	for(x=0;x<ROW;x++)
    	{
    		a[x][29]=178;
    	}
    
    	for(x=0;x<COL;x++)
    	{
    		a[24][x]=178; 
    	}
    	
    	return a[ROW][COL];
    }
    
    int controls (char a[ROW][COL],int row,int col)
    {
    	int i=0,
    	int row2=row;
    	int col2=col;
    	int move;
    
    	do
    	{
    
    		move=getch();
    		clear(a);
    		
    		switch(move)
    		{
    		case 2:	row2=row2+1;
    				if(row2==24 || row2==0)
    				{
    					system("cls");
    					printf("\n\n\t\t BOOOM!");
    					printf("\n\n\t\t Game Over!:P");
    					i++;
    				}
    				else
    				{	
    					a[row2][col2]='O';
    					print(a);
    
    					printf("\n\t\t\t\tControls:\n\t\t 8 - up // 4 - Left // 6 - Right // 2 - Down // 5 - Pen Down // 7 - Pen Up ");
    				}break;
    
    		case 4: col2=col2-1;
    				if(col2==29 || col2==0)
    				{ 
    					system("cls");
    					printf("\n\n\t\t BOOOM!");
    					printf("\n\n\t\t Game Over!:P");
    					i++;
    				}
    				else
    				{
    					a[row2][col2]='O';
    					print(a);
    					printf("\n\t\t\t\tControls:\n\t\t 8 - up // 4 - Left // 6 - Right // 2 - Down // 5 - Pen Down // 7 - Pen Up ");
    				}break;
    		case 5:a[row2][col2]=' ';
    				print(a);break;
    		case 6: col2=col2+1;
    				if(col2==29 || col2==0)
    				{ 
    					system("cls");
    					printf("\n\n\t\t BOOOM!");
    					printf("\n\n\t\t Game Over!:P");
    					i++;
    				}
    				else
    				{
    
    					a[row2][col2]='O';
    					print(a);
    					printf("\n\t\t\t\tControls:\n\t\t 8 - up // 4 - Left // 6 - Right // 2 - Down // 5 - Pen Down // 7 - Pen Up ");
    				} break;
    		case 7: a[row2][col2]='O';
    				print(a);break;
    		case 8: row2=row2-1;
    				if(row2==24 || row2==0)
    				{ 
    					system("cls");
    					printf("\n\n\t\t BOOOM!");
    					printf("\n\n\t\t Game Over!:P");
    					i++;
    				}
    
    				else
    				{
    					a[row2][col2]='O';
    					print(a);
    
    					printf("\n\t\t\t\tControls:\n\t\t 8 - up // 4 - Left // 6 - Right // 2 - Down // 5 - Pen Down // 7 - Pen Up ");
    				}break;
    		}
    	}while(i==0);
    	
    	return 0;
    }
    
    
    main()
    {
    	int ans,row,col;
    	char a[ROW][COL];
    	time_t t;
    	system("color 0A");
    
    start:
    	title();
    	menu();	
    	printf("\t\t\t\tYour option: ");
    	scanf("%d",&ans);
    
    	switch(ans)
    	{
    	case 1: system("cls");
    			clear(a);
    			srand((unsigned) time (&t));
    			row = rand()%25 +1;
    			col = rand()%30 +1;
    			a[row][col]=' ';
    			print(a);
    			controls(a,row,col);
    			
    			//printf("bleh\n");
    			break;
    	case 2: system("cls");
    			instructions();
    			printf("\n Press enter to return to the menu.");
    			getch();
    			system("cls");
    			goto start;break;
    	case 3: system("cls");
    			printf("\t\t\t    \nGoodbye!:D\n\n\n\n");break;
    			
    	}
    
    
    	return 0;
    
    
    }

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    2,706
    I'd first like to say that I really like the layout of the game - or at least the title screen and help menu, because that's all I'm seeing.

    I don't understand how you're supposed to control something you can't see, or at least, troubleshoot something that you can't see. Is there a way to make it visible for debugging purposes? Could you clarify the game control and what to expect a little bit more?

    And the code didn't quite compile on my machine (though I'm not at my own machine right now and am using a very outdated compiler).

    In the mean time, some notes:

    - int main(void) is better than main()
    - In your "controls()" function, your "int i" declaration is appended with a comma, not a semi-colon
    - In your "controls()" function, there's no need to declare the variables "row2" and "col2" as they're redundant. By defining variables "row" and "col" in the function prototype, they're already available within that function. There's no need to reassign them to another locally declared variable.
    - In your "clear()" function, you might have better luck assigning the "null character" ('\0') to your char array rather than "NULL."

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    9
    Well, you have to control the turtle without seeing it. If you click 7 or Pen Up, you can see the turtle, and 5 or Pen Down, it'll hide again. It will make you estimate where the turtle is and controling it without seeing it. It's a bit of a challenge. That's how I understood about the game.

    My only problem is controling the turtle.

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    2,706
    Found it. In your "control()" function, you are receiving the user input with "getch()" into an integer. This is exactly correct, as "getch()" indeed has a return type of integer.

    However, it is an integer representation of the character you entered. Therefore, when you enter '8' for example, the "move" variable does NOT equal 8 but the ASCII representation of 8 (which is decimal 56).

    To remedy this, change your cases in the switch statement from integers to characters:

    Code:
    Change from:
        case 8:  //...
    To:
        case '8':  //...
    Last edited by Matticus; 07-11-2011 at 10:48 AM. Reason: fixed "getch()" references

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    9
    It worked, finally! Thank you very much!

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    2,706
    You're welcome, glad to be of help. Looks like it will be a fun game, good work!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need help with game programming
    By alice_y in forum C Programming
    Replies: 2
    Last Post: 04-04-2007, 09:25 AM
  2. Game Programming FAQ
    By TechWins in forum Game Programming
    Replies: 5
    Last Post: 09-29-2004, 03:00 AM
  3. It seems that all use c++ for game programming
    By nai in forum Game Programming
    Replies: 21
    Last Post: 07-10-2004, 07:32 PM
  4. Game Programming Help
    By Krak in forum Game Programming
    Replies: 10
    Last Post: 07-03-2003, 09:55 PM
  5. Game programming with SDL
    By cozman in forum Game Programming
    Replies: 9
    Last Post: 08-23-2001, 10:18 AM

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