Solve maze recursively using c++, cannot handle an exceptional case [closed] - c++

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I am using an algorithm that I found online to solve a maze.
FIND-PATH(x, y)
if (x,y outside maze) return false
if (x,y is goal) return true
if (x,y not open) return false
mark x,y as part of solution path
if (FIND-PATH(North of x,y) == true) return true
if (FIND-PATH(East of x,y) == true) return true
if (FIND-PATH(South of x,y) == true) return true
if (FIND-PATH(West of x,y) == true) return true
unmark x,y as part of solution path
return false
The algorithm works good on most mazes. But it cannot handle an exception:
######
#s. #
#.## #
#.####
#...f#
######
As shown in the picture, # is a wall, empty space is available path, s is the start and f is the finish.
And the '.' dotted line is the solution path marked after running the maze solving program.
In this case, the start point blocks the path into two way, and after running the program, there is one extra dot at the right side of the start point which cannot be unmarked.
I am wondering could anyone point out that why this happens to the above algorithm. And what extra check should I add to the solve() function? Thank you!

Probably the problem is occurring because you are revisiting already visited node.
Consider you are at the extra dot you are considering at beginning of function. Now on your west side you might have missed checking if it is a start point, and considering start point as not outside maze and available path. So now the program again starts checking from start and thus giving false positive result.
Without actual code its hard to debug though.
EDIT: It suffers from exact problem is was talking about above
Do you know where start point is. If so do following
Replace
if (ch =='.' || ch == '#')
return false;
with
if (ch =='.' || ch == '#' || ch=='s')
return false;

Related

switch statement ends up going through default, always [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
Hello everyone as the title says as I run through my code everything works fine, it loops correctly but somehow ends up always picking up the default case before restarting the loop.
I'll post my code through pastebin since it's quite long:
https://pastebin.com/TxvH3MhJ
while(myScore != "new" || myScore != "NEW" || myScore != "New"){
switch(s2){
case 1 :
switch(s3){
// code
}
default :
cout<<("\nSomething went wrong, please restart the program.");
break; //this statement works correctly.
}
default : //this statement is always brought up each time i type "new", i also added a cout s2 to check if actually somehow the variable was changing mid code but it prints out correctly.
cout<<("\nSomething went wrong please restart the program.");
cout<<s2;
break;`
p.s. I'm a beginner please bear in mind that probably the code is very rudimental although it is doing the job. at the moment the code just works if you enter 1s because i'm just working out the logic.
break; breaks out of the innermost switch (or loop) only. So you need to place it after inner switches too (unless you would need fall-through), like:
switch(s1) {
case 1:
switch(s2) {
...
}
break; // <== HERE
case 2:
...
break;
default:
....
break; // optional
}

I have a question about my battle system hp code [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
i've been coding a text based RPG pokemon battle and im trying to make a damage count of the wild pokemon. but i'm slightly stuck. i have the link to the running program so you can see what i mean.
https://repl.it/live/Ig6yy9UVHyxScw
ive tried using if the total hp of the wild pokemon is less than 90 but it still shows the first attacks damage. (damage = 20). i have the code i'm stuck on here.
if (pokemonSelect == 1 == move == 1) {//for charmander
wPhPtotal = wPhP;
wPhPtotal = wPhP - wPdamage;
}
cout<<"wild pokemon hp lost:" << red<<" "<<wPdamage<<" "<<def<< "hp:"<<green<<wPhPtotal<<endl;
NOTE: only use charmander and ember as i havent programmed the other moves yet.
is there A way to count it all up?
There's not enough information to really answer your question, but here's some stuff that might point you in the right direction.
Firstly, you can't chain ==, as you may be able to in other languages.
if (pokemonSelect == 1 == move == 1)
Should be
if (pokemonSelect == 1 && move == 1)
As a side note, you can write it the way you have it in this particular instance. However, that's mostly due to luck, and it won't work for any values
other than 1. This is because x == y returns 1 if true and 0 if false. So, for any value other than 1 in that conditional, it'll break.
Second,
wPhPtotal = wPhP;
wPhPtotal = wPhP - wPdamage;
Is redundant, since the second line will overwrite the first.

NVIDIA Cuda if-statement [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have a very strange error. I am writing a program in Cuda that emulates the Conway Game of Life. I transfered the 2D array to device and there is a if-case that check for the state's thread.
if(iam==-1)
{ //i am on
iam=0;
}
else if(iam==1)
{ //i am dying
iam=-1;
}
else //i am off
{
if(counter_alive==2)//two neighboors alive
{
iam=1; //i will be on
}
// iam = -999;
}
When the last line is in comment nothing works and the var "iam" has the first value. But if i drop the //, it will work. Of course, if the flow's code execute the else, the var "iam" will take the value -999.
Any ideas? Have i missed something?
Thanks in advance!
You do not show the initial value of the variable iam or counter_alive. Let us assume that the compiler has set it to 0. Of course even if the compiler just sets the space to a random value, this analysis would be the same.
if(iam==-1)
{ //i am on
iam=0;
}
Since the initial value is 0, then this fails and drops through
else if(iam==1)
{ //i am dying
iam=-1;
}
Again, since the initial value is 0, then it fails and drops through.
else //i am off
{
if(counter_alive==2)//two neighboors alive
{
iam=1; //i will be on
}
// iam = -999;
}
It enters here with a value of 0. However, since counter_alive has never been set, it is also 0 and the if fails.
Thus, the iam variable is never changed from 0. Note that since neither of the critical values changes, iam will never be reset from 0. If you uncomment the last line, it will always be explicitly set to -999 and will never change either. That is because you never test for 0 or 999. If you had it as -1 it would change to 0 and then never change unless you change counter_alive somewhere else to be 2.
Note that Explanation of CUDA C and C++ explains how the looping is handled as parallel processing. In that case, the reaction of the various items in the array may not be what you would expect in plain C (single stream) processing.

Arduino double command [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I'm trying to find a way for my Arduino to recognize some of the commands I'm attempting to send over serial (via Raspberry Pi)
I'm come as far as to detect 2 commands, but I'm stuck at my last.
To give a little bit more detail, I have my fingerprint scanner (GT-511C3) connected, and the library installed that will make it work.
That said, with the first 2 commands consist the verifying of fingerprints to ID (which works great) and the enrolling of fingerprints to new ID's (also work great) these parts of the script are triggered by the respective ASCII code "0" and "1".
My last, and problematic command "2" to remove an ID, is where I'm seeking help with.
Here's the code I'm currently working with:
if(ser == '2'){
while(val2 == 0){
char val3 = 0;
delay(10);
Serial.println("Type the ID to be deleted");
delay(2500);
fps.DeleteID(Serial.read());
val3 == Serial.read();
delay(10);
Serial.println("Deleted ID:");
Serial.print(val3);
delay(10);
val2 = 1;
}
}
I'm attempting to send the ASCII code "2" over Raspberry Pi through serial to the Arduino, followed by the ID that needs to be removed. It triggers after the command "2" is send, but refuses to read the ID and as such, I'm unable to finish it.
Any help or insight would really be appreciated.
val3 == Serial.read(); should be val3 = Serial.read();. You have an extra = which turns it into a useless comparison.

Ncurses: movement [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I have a problem with programming movement using C++ and Ncurses.
I'm programming a pacman and the main problem is, that I just want last pressed key in time period.
When I use usleep, it saves every key pressed during sleeping and then it's working with that in the order.
Thanks for ideas.
What did you try?
Something like this should work, if I understood correctly what you try to achieve:
int t = your_delay;
while (t --> 0)
{
sleep(1);
c = getch();
}
If you really need to time it down to the microsecond, this might not be the best approach, but if you can have some tolerance, this should be enough.
Are you in no-delay mode? If so, this might work:
usleep(your_delay);
last_key = ERR;
while ( (key=getch()) != ERR ) {
last_key = key;
}
// "last_key" now holds most recent key, if there was one, else ERR