SRL Archive - First Scripts

Pegtoy - Simba edition


Lima Bean

2011-10-15 08:15:54

Simple:

1. Cut
2. Paste
3. Play

program PegtoySimba;
{Pegtoy Simba Edition 1.0.9 by Lima Bean}

{1.0 - 1.0.8}
{added times}
{move action now in Status}
{added failsafe for int input}
{Disc input added}
{min max added}
{commented out some code}
{some reordering done}
{Error pointed out by masterBB}

(*

Just uncomment the various lines in Pegtoy
to see differences in time.

Important to note, this is coded for Simba.exe
without any dlls, plugins, etc.

Enjoy.

Lima Bean

*)


var
m, n, stime, rtime, ftime: Integer;
value: string;

Procedure Pegtoy(Lfm, Lto, Lus, n: Integer);
begin
if n > 0 then
begin
Pegtoy(Lfm, Lus, Lto, n - 1);
m := m + 1;
//Status('move ' + ToStr(m) + ' ' + ToStr(LFm) + ' --> ' + ToStr(Lto));
//Writeln('move ' + ToStr(m) + ' ' + ToStr(LFm) + ' --> ' + ToStr(Lto));
Pegtoy(Lus, Lto, Lfm, n - 1);
end;
end;


Procedure GetDisks();
begin
InputQuery('Disk Count', 'Please Enter Number of Discs(31 or less)', value);
if value = '' then
getdisks;
if (chr(47) > value) or (chr(58) < value) then
showmessage('Numbers only. Thank you')
else
n := StrToInt(value);
if (n = 0) then GetDisks();
if (n >= 32) then
begin
Showmessage('That is not avaialbe at this time. Thank you');
n := 31;
end;
stime := GetSystemTime;
end;

begin
ClearDebug;
GetDisks;
writeln('Start Time : ' + ToStr(stime));
Writeln('Solution should take: ' + ToStr(pow(2.0,n)-1) + ' moves.'); // masterbb
Writeln('Solving for ' + ToStr(n) + ' discs with 3 pegs');
Pegtoy(1, 3, 2, n);
Writeln('Solution verified: ' + ToStr(m) + ' moves.');
ftime := GetSystemTime;
writeln('Finish Time : ' + ToStr(ftime));
rtime:= ftime - stime;
writeln('Total Run Time : ' + ToStr(rtime));
end.

Wizzup?

2011-10-15 18:57:14

How does this game work, I don't think I know it?

Lima Bean

2011-10-15 19:03:32

From: Wizzup?
How does this game work, I don't think I know it?


Only thing missing from it is the animation and user input, in Simba that is.

That is why the Comments in the "main" area.

1. http://www.mazeworks.com/hanoi/

2. http://www.animatedrecursion.com/intermediate/towersofhanoi.html

masterBB

2011-10-15 19:14:17

Critics:

Solution takes (2^n)-1 moves, otherwise it won't work.

Result := n; why?

Lima Bean

2011-10-15 19:24:54

From: masterBB
Critics:

Solution takes (2^n)-1 moves, otherwise it won't work.

Result := n; why?


*red* face, oooo… ouch ok it's fixed, kinda..

masterBB

2011-10-15 19:29:33

From: Lima Bean
This code does work, in Simba and a stand alone.

Know it before you blow it.


Result := n; is not used, thats why I asked why you used it. And you ask GetDisks before you run pegtoy, so if you want to be able to do it in that order, you will have to calculate the moves instead of counting them. I do not blow your code ^^.

Lima Bean

2011-10-15 19:35:21

Lol.. I have only been working with any kind of Pascal for the last few weeks.

I expect to make lots of mistakes …

I was rushing the last few minutes, I didn't back off of getting this written (the code).

Lol

masterBB

2011-10-15 19:38:35

If so, than please don't say things like this when an SRL member or anyone else with probably more experience gives some feedback…

From: Lima Bean
Look deeper into the Simba code and Pascal Script, you'll see the reason of result.


But here would be an example without that code, not yet optimized.

program PegtoySimba;
{Pegtoy 1.0 by Lima Bean}

var
m, n: Integer;

Procedure Pegtoy(Lfm, Lto, Lus, n: Integer);
begin
if n > 0 then
begin
Pegtoy(Lfm, Lus, Lto, n - 1);
m := m + 1;
Writeln('move ' + IntToStr(m) + ' ' + IntToStr(LFm) + ' --> ' + IntToStr(Lto));
Pegtoy(Lus, Lto, Lfm, n - 1);
end;
end;

Procedure GetDisks();
begin
Writeln('Solution takes: ' + ToStr(pow(2.0,n)-1) + ' moves.');
writeln('working ...' );
end;

begin
ClearDebug;
{----------------}
n := 3; // <------- change this number , be safe and stay at 31 or less
{----------------}
Getdisks;
Pegtoy(1, 3, 2, n);
end.

Lol at 31 as limit btw XD. On my computer 1000 moves takes 4.2 seconds of which 16 ms are used on the calculations and the other on the writeln calls. (2 ^ 31 -1) / 1000 * 4.2 is more than 9 million seconds which is more than 104 days.

Lima Bean

2011-10-15 20:19:54

Removed the Result.
Reordered the code.
Added your optimization code. :spot:

Home

2011-10-16 07:18:05

Sorry but. What is this :)?

Edit: Got it now :)


~Home

Lima Bean

2011-10-28 09:55:41

:):). I'm getting there …lol

E: small update, nothing special.

E2: added some basic refinements. Thx again masterbb :):)

onilika

2011-10-30 17:24:59

Lima bean, try not to spam. Edit your post, instead of creating a new one.

Lima Bean

2011-10-30 17:28:45

spam? By posting a small update change?