Skip to main content
GameDev.net gamedev.net
🔒 Locked

a strange logic error

Started by doc_holliday69 Oct 29, 2004 at 4:37 PM 11 replies 1.1k views
Original Post
doc_holliday69
doc_holliday69
when I run this, it doesn't do what it's supposed to. It's an ascii space invaders game(my first game programming project). It's supposed to go left when you press a, and right when you press d. but when i run it, it only goes right.
#include <stdio.h>			// for printf
#include <conio.h>			// for getch
#include <string.h>
#include <iostream.h>



char espace_vaisseau[76];
char left_right;
int espace = 39;
int count;
int loop;

main ()
{
	strcpy(espace_vaisseau, "                                       ");		// graphics pour le vaisseau

	

	printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");

	printf("%s^W^", espace_vaisseau); // afficher vaisseau

        for(;;)		// infinite loop
	{
		if((left_right = getch()) == 'a')
		{
			espace--;
		}
		else if ((left_right = getch()) == 'd')
		{
			espace++;
		}
		if (espace < 0)
		{
			espace++;
		}
		else if (espace > 76)
		{
			espace--;
		}

		for (count = 0; count <= espace; count++)
		{
			espace_vaisseau[count]=' ';
		}

		printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");

		printf("%s^W^", espace_vaisseau); // afficher vaisseau
	}

return (0);
}
[Edited by - doc_holliday69 on October 29, 2004 7:43:55 PM]
"the only laws we have to obey are the laws of physics"
nmi
nmi
What does it do instead ?
nmi
nmi
Maybe you want to change this from

if (espace < 0){  espace++;}else if (espace > 76){  espace--;}


to

if (espace < 0){  espace++;}if (espace > 76){  espace--;}

doc_holliday69
doc_holliday69
nmi, what's the difference?
"the only laws we have to obey are the laws of physics"
Enigma
Enigma
There is no difference. I think I can see one problem. I think you should add espace_vaisseau[espace] = '\0'; before your last printf statement. Other wise the number of spaces printed never changes. I can't tell for certain if this is the only problem because you haven't given enough information. We need to know what it's supposed to do, what it does do and what you've done to try and fix it.

Enigma
Eric Burnett
Eric Burnett
I can see a few possible things.

1) You go from using getch() (for char a) to fetch() (for char d). Typing mistake?

2) Also, that second getch() (if that is what it is supposed to be) should not be there. In the if statement for a you set left_right to be the recieved character. Then, on the else, you look like you are trying to get it again. Why would you do that? Then every other character would be checked against a, and the others against d. Since you have put it to a variable, the d line should just look like
else if (left_right == 'd')


3) To do an infinite for loop, you do not need all the extra garbage and a wasted variable. Just use
for(;;) {


4) I use c++ not c, but shouldn't it be
printf(espace_vaisseau, "%s^W^"); 

instead of
printf("%s^W^", espace_vaisseau); 


5) If you are going to use an infinite loop, it should have some way of exiting. After the 'd' line you should add
 else if (left_right == 'q')		{			return;		}
or something similar.

That get the problem?
--Eric Burnett I know I have mnemophobia, but I can't remember what it is.
doc_holliday69
doc_holliday69
updated code in first post

I corrected all the mistakes, and fixed the bad coding (for (;;;))
"the only laws we have to obey are the laws of physics"
doc_holliday69
doc_holliday69
fixed the problem. first, the screen is not 77 characters as the first version, but 80. Also, I kept a last array element for the "end of string" value.

it was all because of the "end of string"

#include <stdio.h>			// for printf#include <conio.h>			// for getch#include <string.h>#include <iostream.h>char espace_vaisseau[81];char left_right;int espace = 39;int count;int loop;main (){	strcpy(espace_vaisseau, "                                       ");		// graphics pour le vaisseau		printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");	printf("%s^W^\0", espace_vaisseau); // afficher vaisseau	for(;;)		// infinite loop	{		if((left_right = getch()) == 'a')		{			espace--;		}		else if ((left_right = getch()) == 'd')		{			espace++;		}		if (espace < 0)		{			espace++;		}		else if (espace > 76)		{			espace--;		}		for (count = 0; count <= espace; count++)		{			espace_vaisseau[count]=' ';		}		espace_vaisseau[count] = '\0';		printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");		printf("%s^W^", espace_vaisseau); // afficher vaisseau	}return (0);} 
"the only laws we have to obey are the laws of physics"
iMalc
iMalc
But you're still doing
left_right = getch()
twice in the for loop. Only do it once.
doc_holliday69
doc_holliday69
oh right


when the getch was there, going to the right was slower than going left. An if can be so slow? geez
"the only laws we have to obey are the laws of physics"
Eric Burnett
Eric Burnett
Quote:
@Eric Burnett:
"4) I use c++ not c, but shouldn't it be

printf(espace_vaisseau, "%s^W^");"

No, the way he has it is right.

Thanks! Now I should go look up the function I guess ;)

Quote:
when the getch was there, going to the right was slower than going left. An if can be so slow? geez

It is not the if statement that is slow, it is the way it is programmed. If you want to move left, you press 'a'. In the if statement it takes that character, then moves left. But with a second getch() for moving right, it would only catch every second letter, hence the half speed. Essentially:
a --> get character, then check if for a. Yep, now move lefta --> get character, then check if for a. Yep, now move lefta --> get character, then check if for a. Yep, now move lefta --> get character, then check if for a. Yep, now move leftd --> get character, then check if for a. Nope.d --> get character, then check if for d. Yep, now move rightd --> get character, then check if for a. Nope.d --> get character, then check if for d. Yep, now move right


as you can see, it has to check twice as often for every move right.

One other thing to point out is that the way it works now, the program will wait for every letter you press. Works great for this, but if you add enemies, they will only move when you do. Just something to think about.
--Eric Burnett I know I have mnemophobia, but I can't remember what it is.
zaidgs
zaidgs
this works perfectly i guess:

#include <stdio.h>			// for printf#include <conio.h>			// for getch#include <string.h>#include <iostream.h>char espace_vaisseau[76];char left_right;int espace = 39;int count;int loop;main (){	strcpy(espace_vaisseau, "                                       ");		// graphics pour le vaisseau		printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");	printf("%s^W^", espace_vaisseau); // afficher vaisseau        for(;;)		// infinite loop	{		if((left_right = getch()) == 'a')		{			espace--;		}		else if (left_right  == 'd')	//dont ask for a character two times		{			espace++;		}		if (espace < 0)		{			espace++;		}		else if (espace > 75)		//one less		{			espace--;		}		for (count = 0; count <= espace; count++)		{			espace_vaisseau[count]=' ';		}		espace_vaisseau[count]='\0';	//dont forget nulling !!!!!		printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");		printf("%s^W^", espace_vaisseau); // afficher vaisseau	}return (0);}

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.