thanks for guidance

Aug 1, 2008 at 11:37pm
thanks
Last edited on Aug 4, 2008 at 3:09pm
Aug 2, 2008 at 3:21am
Ok. let's see if I can help here. First I notice a few things wrong with what you have thus far.

Let's make certain you understand this. You are making a traffic light class, from which you will make 4 traffic lights.
You need to understand that the class is for 1 traffic light. It's a blue print, from which the 4 traffic lights will be built. But each traffic light is only interested in itself, not the other three, so the blue print needs to be designed around just 1. I bring this up, because I see:
1
2
3
4
5
void TrafficSignal::printState(){
   cout << "Signal " << Id << " is GREEN." << endl;
   cout << "Signal " << Id << " is Yellow." << endl;
   cout << "Signal " << Id << " is Red." << endl;
}
which is rather confusing.

Next step, I also see this code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void TrafficSignal::cycle(){ 

   if (Red == true){
      Red = false;
      Green = true;
   }
   if (Green == true){
      Green = false;
      Yellow = true;
   }
   if (Yellow == true){
      Yellow = false;
      Red = true;
   }
}


This is going to fail miserably, because the logic behind it is self defeating.
If red is true, then we set red to false, and set green to true.
If green is true, well it is, cause we just set it, so let's set green to false and yellow to true
If yellow is true, well, we just set it, so yes it will be, so we set it to false and set red to true

The outcome of that code is that it will always have red = true.

You need to fix that by using else if like thus
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void TrafficSignal::cycle() {
   if (Red == true) {
      Red = false;
      Green = true;
   }

   else if (Green == true) {
      Green = false;
      Yellow = true;
   }

   else if (Yellow == true) {
      Yellow = false;
      Red = true;
   }
}


However, I would recommend scrapping the whole bool for each color anyway. I see nothing in the instructions that say they have to be bools. Since a traffic light can be one color at a time, e.g. it can be red, or green, or yellow, but never red and green, or yellow and red, then probably the better bet is to use a single variable to hold it's current state.

1
2
enum color = { Red, Green, Yellow };
color light;


Or you can use an integer which may score you an extra point for originality if used like thus:
1
2
3
4
5
6
7
8
int light; // 0 = Red, 1 = Green, 2 = Yellow

...

void TrafficSignal::cycle() {
   light++; // add 1 to light
   light%= 3; // and modulo 3 to keep it from getting too high
}


So, as is, your current class needs a bit more work. You're gonna need one variable to hold current state (which light is lit up), three variables (or an array of 3 ints could easily be accessed using the light variable as index) to hold the duration for each light. Another variable to hold elapsed time from last cycle. (the seconds since light first started I don't see as being needed, but can be included if you want it.)

Then for the tick function, you need to increase your elapsed time variable, get current state, use that to get duration of current state, and compare that against elapsed time to see if a cycle is needed. There is a need to be able to set the durations, but i see no reason to retrieve them. You need to set an ID (another variable you will need), but can probably live without retrieving it. You can use the elapsed time from within the tick function, should be no need to get at it from outside the class, or to set it from outside either.

You really need to re-think what the traffic light is supposed to do, and what you need to make available to the outside world (the remainder of the program). Also, remember, to design the class based around 1 traffic light. the class is a blue print, from which you will be making 4 exact copies (objects) based on the 1 blueprint. That will be done in the main function.

There's lots more, but I don't want to overwhealm you in a single post. So see if you can figgure out what i've said thus far and then we'll move on.
Aug 2, 2008 at 5:18pm
thanks
Last edited on Aug 4, 2008 at 3:10pm
Aug 2, 2008 at 5:26pm
ok.. so bools it is then...

again, the class should be centered around one traffic light. It has an id so we can print which one is which, however we won't need the id to tell the difference from within the program itself.

In the main function of the program you will create four traffic lights, either by using four different variables:
TrafficSignal traffic1, traffic2, traffic3, traffic4;

Or, if you want the full points, using an array:
TrafficSignal traffic[4];
You can refference each one with an index, 0 through 3.

Edit: I just noticed the main function. hadn't seen it last night.
It would be much easier to read if you would be so kind as to add [code] [/code] tags around it.

Ok, so your tick function should take no parameters and return a bool, true if you had to call cycle, and false if not.

What it needs to do is increase your elapsed time variable, and perhaps your tick variable if you're using that.. get the current state (red, green, or yellow) and get the duration of that state, then compare if the time since last cycle is equal to or greater than that duration. If so, then call cycle, reset the time since last cycle, and return true. If not, just return false.

then in the main function add your for loop to count from 0 to 999, (that is 1000 total) and call the tick function for each light in the array.
Last edited on Aug 2, 2008 at 5:36pm
Aug 2, 2008 at 6:02pm
hello Aakanaar (73),
thanks
Last edited on Aug 4, 2008 at 3:11pm
Aug 2, 2008 at 7:18pm
I look at the code and I see this:
//TrafficSignal tick()
//if (Green){

//}

But if you say it's done, then fine, i'll accept that. And hope it works correctly as I can't fix anything I can't see.

So let's move on to the main function.

As I posted before, you need to make a loop to count from 1 to 1000, (or actually from 0 to 999: for (int i = 0; i < 1000; i++) ) and on each iteration of the loop, call each signal's tick function.
1
2
3
4
for (j = 0; j < 4; j++) {
   if (signal[j].tick())
      signal[j].printTrafficSignal();
}


I generally don't give code, but this is one small part. This needs to be included in the loop that's counting ticks.

Also, before getting into that tick counting loop, you still have to set each signals' durations. Just remember that the array is 0 index, so signal 1 is signal[0], signal 2 is signal[1] etc..

Edit: Unfortunately, I gotta dissapear for a few, running errands. But I'll check back into this thread just as soon as I get back.
Last edited on Aug 2, 2008 at 7:19pm
Aug 2, 2008 at 7:46pm
Hello AaKanaar(74),
thanks
Last edited on Aug 4, 2008 at 3:12pm
Aug 2, 2008 at 8:05pm
He means that you should go back to your old post, highlight it all in edit mode, and click the "#" symbol on the right.
Aug 2, 2008 at 9:59pm
not all, just the code sniplettes.. ok.. so we are still on the tick function.. I'll say same thing as above.. the tick function should return a bool and take no arguments.. so

bool TrafficSignal::tick ()
{
}

then, what do we do inside the tick function..

You have two variables that need to be increased
1
2
3
int Elapsed_Time; //int variable indicating # of seconds since signal started
int Tick; 
//this is my extra time variable to support the "time since the last cycle change" functionality 


So, increase both by 1.

Then we need to determin which state the traffic light is in. red, green or yellow, and get the duration of that state. Then compare the duration to the tick variable to see if it's time to cycle or not.

If (Tick >= <duration>)

If it is time to cycle, then call the cycle function, reset your Tick variable to 0, and return true. If it isn't time to cycle, then just return False.

Edit: logical error fixed.
Last edited on Aug 2, 2008 at 10:01pm
Aug 2, 2008 at 10:06pm
thanks
Last edited on Aug 4, 2008 at 3:10pm
Aug 2, 2008 at 11:18pm
Hello Aakanaar (76),
thanks
Last edited on Aug 4, 2008 at 3:11pm
Aug 3, 2008 at 1:31am
not quite.. you don't want to count 1 to 1000 inside the tick function. You're gonna do that inside the main function.

and you can increase Tick with just Tick++

then check if Red is true, and Tick >= Red_Duration
etc..
Topic archived. No new replies allowed.