Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 16th, 2006, 9:40 PM   #1
lilmul123
Newbie
 
Join Date: Mar 2006
Posts: 18
Rep Power: 0 lilmul123 is on a distinguished road
Turbo C++ Snake Problem

Hey guys, I'm in a high school programming class with a teacher that doesnt know how to teach it. (the other guy who used to teach it switched to the math department) so expect me to ask a lot of questions. currently, we are working on a snake game program (in turbo C++ 3 for DOS, dear god.) and i need to merge the following two programs that i've written in order for it to do something new. here's the directions from the top of the sheet:

"Delete the code that places the prize on the screen from program "snake5a.cpp" and insert the do..while loop from this program. This should prevent the prize showing up on a spot when the snake part is already there. Then, change the program so a number from 1 to 9 shows up instead of an X. If the snake reaches the prize, the snake should increase in length by the same number as the prize. The score should also increase by the number of the prize. Finally, change the program so that the program ends when the snake head hits a part of the snake."

Here's snake5a:
#include <conio.h>
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <dos.h>

void main()
{
	clrscr();
	int snakex[500],snakey[500],prizex,prizey;
	int timer,length,a;
	char dir,temp;

	_setcursortype(_NOCURSOR);
	timer=0;
	length=10;

	for(a=0;a<=length-1;a++)
	{
		snakex[a]=40;
		snakey[a]=10;
	}

	gotoxy(snakex[0],snakey[0]);
	printf("0");
	dir='2';

	for(a=19;a<=61;a++)
	{
		gotoxy(a,4);printf("*");
		gotoxy(a,21);printf("*");
	}
	for(a=4;a<=21;a++)
	{
		gotoxy(19,a);printf("*");
		gotoxy(61,a);printf("*");
	}	


	do
	{
		randomize();
		if(timer==0)
	{
	
		gotoxy(prizex,prizey);
		printf(" ");
		prizex=rand()%40+20;
		prizey=rand()%15+5;
		gotoxy(prizex,prizey);
		printf("X");
		timer=100000;
	}
	else timer=timer-1;

	delay(100);

	if(kbhit()!=0)
	{
	temp=getch();
	if(dir=='4'||dir=='6'||dir=='8'||dir=='2'||dir=='0') dir=temp;
	if(dir=='4'||dir=='6'||dir=='8'||dir=='2')
	{
	gotoxy(snakex[length-1],snakey[length=1]);
	printf(" ");
	for(a=length-1;a>=1;a--)
	{
		snakex[a]=snakex[a-1];
		snakey[a]=snakey[a-1];
	}
	}
	if(dir=='4')  snakex[0]--;
	if(dir=='6')  snakex[0]++;
	if(dir=='8')  snakey[0]--;
	if(dir=='2')  snakey[0]++;
	gotoxy(snakex[0],snakey[0]);
	printf("0");

	if(snakex[0]&&snakey[0]==prizex&&prizey)
	{
		length=length+5;
	}

	if(snakex[0]>=61||snakex[0]<=19||snakey[0]>=21||snakey[0]<=4)
		{
		dir = '0';
		gotoxy(36,12);
		printf("Game Over.");
		}

		}
		delay(1000);
	}while(dir!='0');
}

and heres snake6a:
#include <conio.h>
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <dos.h>

void main
{
	clrscr();

	int snakex[400],snakey[400],prizex,prizey,a,length,same;

	length=10;
	_setcursortype(_NOCURSOR);

	do
	{
	gotoxy{prizex,prizey);
	printf(" ");
	prizex=rand()%40 + 20;
	prizey=rand()%15 + 5;
	same=0;
	for(a=0;a<-length-1;a++)
	{
	if(prizex==snake[a]&&prizey==snakey[a]) same=1;
	}
	{while(same==1);

	gotoxy(prizex,prizey);
	printf("X");
	getch();
}

So...what do I do!? Please help mighty programming gods.
lilmul123 is offline   Reply With Quote
Old Mar 17th, 2006, 6:44 AM   #2
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
Welcome, I see you have been so kind to post in code tags, very good, but you should read the whole "how to post a question" thread.

You need to specify what works and what doesn't.

Also try to be concise. A small complete example is often better, I did not take a look at your code yet, so it might not be wrong of you to post it all.
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:24 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC