View unanswered posts | View active topics It is currently Wed Apr 16, 2014 3:51 am






Reply to topic  [ 9 posts ] 
Need help with if/then statements 
Author Message
Rookie

Joined: Sat Jan 30, 2010 2:54 pm
Posts: 13
Post Need help with if/then statements
I'm new to robotc and programming, yet am trying to navigate a maze. The following portion of my code shows errors as listed. Any thoughts on what's wrong?

if (!Walls[0]) {
if (Map[MyPosX+RelPosX(LeftOf(MyDir))][MyPosY+RelPosY(LeftOf(MyDir))]==0)
NextMove = 0;
} else {
DecreaseOne(Map[MyPosX][MyPosY]);
DecreaseOne(Map[MyPosX+RelPosX(LeftOf(MyDir))][MyPosY+RelPosY(LeftOf(MyDir))]);
}

Errors Line 2:
Unexpected error token-> '?'
Unmatched left parenthesis ')'
Expected , found :
Unexpected error token :
Unexpected error token 0
Unexpected error token ,
Unexpected ; fund ,
Unexpected error token ==

other errors
The fifth and sixth lines (DecreaseOne) lines have the same types of errors as above. Thanks for any help you can provide.


Thu Feb 18, 2010 11:25 pm
Profile
Moderator
Moderator
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3103
Location: Rotterdam, The Netherlands
Post Re: Need help with if/then statements
Code:
if (!Walls[0]) {
  if (Map[MyPosX+RelPosX(LeftOf(MyDir))][MyPosY+RelPosY(LeftOf(MyDir))]==0) {
    NextMove = 0;
  } else {
    DecreaseOne(Map[MyPosX][MyPosY]);
    DecreaseOne(Map[MyPosX+RelPosX(LeftOf(MyDir))][MyPosY+RelPosY(LeftOf(MyDir))]);
  }
}


try that instead. You were missing a whole bunch of curly braces. If this is not the intended structure of your code, then you should learn to make better use of indentation.

- Xander

_________________
| Professional Conduit of Reasonableness
| (Title bestowed upon on the 8th day of November, 2013)
| My Blog: I'd Rather Be Building Robots
| ROBOTC 3rd Party Driver Suite: [Project Page]


Fri Feb 19, 2010 7:24 am
Profile WWW
Rookie

Joined: Sat Jan 30, 2010 2:54 pm
Posts: 13
Post Re: Need help with if/then statements
Thanks for the previous help. My maze navigation program is getting there, but I'm having one last error with my code. I get the following message in the line " MyDir = RightOf(MyDir);" in the switch statement.

**Severe*:Internal:: 'assign location' is same as "expression location' on simple assignment. Recursive proc call of 'cast" may cause this.



Code:
#define LeftOf(Dir) (tDirection)(((int)Dir+1) % 4)
#define RightOf(Dir) (Dir == North ? East : (tDirection)((int)Dir–1))

Code:
 switch (NextMove)
    {
        case 0:
            rotate_robot_left();
            MyDir = LeftOf(MyDir);
            break;
        case 2:
            rotate_robot_right();
            MyDir = RightOf(MyDir);
            break;
    }


Sat Feb 20, 2010 1:53 am
Profile
Moderator
Moderator
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3103
Location: Rotterdam, The Netherlands
Post Re: Need help with if/then statements
Well, without knowing what tDirection is, I can't really tell. Perhaps there is a better way to handle this multiple casting thing, it looks really nasty.

Xander

_________________
| Professional Conduit of Reasonableness
| (Title bestowed upon on the 8th day of November, 2013)
| My Blog: I'd Rather Be Building Robots
| ROBOTC 3rd Party Driver Suite: [Project Page]


Sat Feb 20, 2010 2:01 am
Profile WWW
Rookie

Joined: Sat Jan 30, 2010 2:54 pm
Posts: 13
Post Re: Need help with if/then statements
Here is tDirection. I define North to be the direction the robot is when it enters the maze.

Code:
typedef emun
{
    North = 0;
    West  = 1;
    South = 2;
    East = 3;
} tDirection;

tDirection MyDir = North;


Sat Feb 20, 2010 11:48 am
Profile
Moderator
Moderator
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3103
Location: Rotterdam, The Netherlands
Post Re: Need help with if/then statements
What is wrong with using something like:
Code:
#define North  0
#define West   1
#define South  2
#define East   3

#define LeftOf(Dir)   (Dir + 1) % 4
#define RightOf(Dir)  (Dir == North) ? East : (Dir - 1)

- and -
Code:
switch (NextMove)
{
   case 0:
     rotate_robot_left();
     MyDir = LeftOf(MyDir);
     break;
   case 2:
     rotate_robot_right();
     MyDir = RightOf(MyDir);
     break;
}



If you are not reusing RightOf and LeftOf, consider inserting the code there directly, it will make your code a lot more readable.

Xander

_________________
| Professional Conduit of Reasonableness
| (Title bestowed upon on the 8th day of November, 2013)
| My Blog: I'd Rather Be Building Robots
| ROBOTC 3rd Party Driver Suite: [Project Page]


Sat Feb 20, 2010 12:30 pm
Profile WWW
Rookie

Joined: Thu Feb 25, 2010 2:34 am
Posts: 2
Post Re: Need help with if/then statements
Just curious, but way wouldn't this work?
#define RightOf(Dir) (Dir - 1) % 4

if Dir=0, North it will return 3, East, etc.


Thu Feb 25, 2010 3:22 am
Profile
Moderator
Moderator
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3103
Location: Rotterdam, The Netherlands
Post Re: Need help with if/then statements
LennyB,

If Dir == 0, you will end up doing (0 - 1) % 4 which is -1, not 3.

- Xander

_________________
| Professional Conduit of Reasonableness
| (Title bestowed upon on the 8th day of November, 2013)
| My Blog: I'd Rather Be Building Robots
| ROBOTC 3rd Party Driver Suite: [Project Page]


Thu Feb 25, 2010 7:53 am
Profile WWW
Rookie

Joined: Wed Feb 24, 2010 11:43 pm
Posts: 34
Post Re: Need help with if/then statements
LennyB wrote:
Just curious, but way wouldn't this work?
#define RightOf(Dir) (Dir - 1) % 4


I think it would work better to add 3 instead of subtracting 1.

#define RightOf(Dir) (Dir + 3) % 4


Thu Feb 25, 2010 3:40 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 9 posts ] 

Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  



Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.