PDA

View Full Version : [Delphi] interupting loops



edg3
11-10-2007, 07:35 PM
I have a question not exactly to do with game development (it is a game of sorts, but more of an IT project).

the loops we have to use are as follows:


repeat
inc(iCount,5);
redOut.lines.add(inttostr(iCount));
sleep(100);

{ insert the line/lines to interrupt here
application.processmessages; doesnt work }

until (iCount = iMax);




while (iCount < iMax) begin
inc(iCount,5);
redOut.lines.add(inttostr(iCount));
sleep(100);

{ insert the line/lines to interrupt here
and app.processmsgs doesnt work here either }

end;


We need to be able to end the loop or at least make note of where the person tried to stop it (eg when iCount is 45) and then work out certain things afterwards. The only issue I have is that not even our IT teacher can give us a solution to this problem!

We tried looking for a solution, and came up with using this:

application.processmessages;
but unfortunately it only runs the event I try to run (eg if I click on the stop button) after the loop ends, when its too late to work out where/when they clicked it.

The program/"game" is to try be a challenge for people to choose a number, and try stop on the number they chose. Its simple enough to do with a timer, but we have been specifically told to use loops.

UntouchableOne
11-10-2007, 08:33 PM
bEndLoop:=false;
while (iCount<iMax)and(bEndLoop=false) do
begin
application.processmessages;
inc(iCount,5);
redOutput.lines.add(inttostr(iCount);
sleep(100);
end;

set bEndLoop to true when you want to stop the loop. Better make it global.

edg3
11-10-2007, 08:42 PM
thats the problem, If I try to run another procedure (ie. clicking a button) it runs the buttons code after the whole loop has finished...

UntouchableOne
11-10-2007, 08:47 PM
It shouldn't. When the button is clicked, you set bEndLoop to true. This will terminate the loop. Then you can have the rest of the required statements after that.
Edit : Or you could make a procedure with the statements you need then call that procedure after the loop. So the loop will be ended by the button then the required procedure will be run after the loop.

cairnswm
11-10-2007, 08:47 PM
Make a timer, on each execution of the timer, up the value you are displaying. When the guy presses the button, disable the timer.

Dont make a loop. Once the loop starts it needs to end, a delphi application by default is a single thread, to do what you want to do you'd need multiple threads (and all the sycronisation problems that come with them) - actually IO dont think that would work anyway.

UntouchableOne
11-10-2007, 08:53 PM
Or you can listen to the master, Mr Cairns. Best bet! But cairnswm, edg3 needs to use loops in this application.

edg3
11-10-2007, 08:55 PM
I must have had a logical error somewhere, I tried again and it worked. thanks :)

EDIT:
I wasnt allowed timers at all :)

UntouchableOne
11-10-2007, 09:04 PM
Great stuff. Good luck with the project!

edg3
11-10-2007, 09:18 PM
thanks :)
this is pretty much it: http://www.gamedev.za.net/filecloset/data/files/245/Loops.rar
Thanks :)
My teacher was looking for an "impossible problem", and I was sure there was a way. Ive been trying all afternoon! :D

The Challenge was:
Create a program that does the following:
- The user chooses a number, and then clicks a button, one for a "while" loop, the other for a "repeat" loop
- the loops count through to a number further on than the chosen number, but the user has to stop it by means on clicking a button, and try to get their number
Conditions:
- no timers, no "for" loop
- The player must be told if he got his number

Our IT teacher said this should be "unsolveable", so I cant wait till tomorrow!! :D
Thanks to: cairnswm, UntouchableOne, and Goraan who helped me

UntouchableOne
11-10-2007, 09:36 PM
Neat. I also enjoyed doing stuff like this when I had the time. Now I hardly have time for anything but all shall be well soon.