Microsoft Small Basic
Program Listing:
Embed this in your website
<object id='sbapp' data='data:application/x-silverlight-2,' type='application/x-silverlight-2' width='640' height='480'> <param name='source' value='http://smallbasic.com/program/ClientBin/SBWeb.xap'/> <param name='onError' value='onSilverlightError' /> <param name='background' value='white' /> <param name='minRuntimeVersion' value='3.0.40624.0' /> <param name='autoUpgrade' value='true' /> <param name='initParams' value='programId=HANOI' /> </object>
Stack
.
PushValue
(
"a"
,
"a0"
)
Stack
.
PushValue
(
"c"
,
"c0"
)
Stack
.
PushValue
(
"b"
,
"b0"
)
Stack
.
PushValue
(
"ad"
,
"0"
)
Stack
.
PushValue
(
"cd"
,
"0"
)
Stack
.
PushValue
(
"bd"
,
"0"
)
'---------------------------------------------------------------------------------------------------------------------------------
GraphicsWindow
.
Show
(
)
GraphicsWindow
.
CanResize
=
0
GraphicsWindow
.
Width
=
800
GraphicsWindow
.
Height
=
600
GraphicsWindow
.
PenColor
=
"DarkSlateGray"
GraphicsWindow
.
PenWidth
=
10
GraphicsWindow
.
Title
=
"Game 'Tower of Hanoi. Question of Tonkin.'"
Path
=
"http://www.ac-acap.org/images/Hanoy/"
background
=
ImageList
.
LoadImage
(
Path
+
"fon.jpg"
)
GraphicsWindow
.
DrawImage
(
background
,
0
,
0
)
'---------------------------------------------------------------------------------------------------------------------------------
disk
=
0
GraphicsWindow
.
DrawLine
(
200
,
600
,
200
,
400
)
GraphicsWindow
.
DrawLine
(
400
,
600
,
400
,
400
)
GraphicsWindow
.
DrawLine
(
600
,
600
,
600
,
400
)
'---------------------------------------------------------------------------------------------------------------------------------
For
i
=
8
To
1
Step
-
1
GraphicsWindow
.
PenColor
=
GraphicsWindow
.
GetRandomColor
(
)
a
[
i
]
=
Shapes
.
AddEllipse
(
20
+
20
*
i
,
20
)
Shapes
.
Move
(
a
[
i
]
,
190
-
10
*
i
,
420
+
20
*
i
)
Stack
.
PushValue
(
"a"
,
a
[
i
]
)
Stack
.
PushValue
(
"ad"
,
i
)
EndFor
'---------------------------------------------------------------------------------------------------------------------------------
GraphicsWindow
.
ShowMessage
(
"The Tower of Hanoi is a mathematical game or puzzle. It consists of three rods, and a number of disks of different sizes which can slide onto any rod. The puzzle starts with the disks in a neat stack in ascending order of size on one rod, the smallest at the top, thus making a conical shape. The objective of the puzzle is to move the entire stack to another rod, obeying the following rules: Only one disk may be moved at a time. Each move consists of taking the upper disk from one of the rods and sliding it onto another rod, on top of the other disks that may already be present on that rod. No disk may be placed on top of a smaller disk."
,
"RULES"
)
Sound
.
PlayChime
(
)
GraphicsWindow
.
MouseDown
=
Down
GraphicsWindow
.
MouseMove
=
Move
GraphicsWindow
.
MouseUp
=
Up
'---------------------------------------------------------------------------------------------------------------------------------
Sub
Down
If
Mouse
.
IsLeftButtonDown
=
"true"
And
Mouse
.
IsRightButtonDown
<>
"true"
Then
If
Stack
.
GetCount
(
"a"
)
>
1
Then
If
GraphicsWindow
.
MouseX
>=
0
And
GraphicsWindow
.
MouseX
<
300
Then
s
=
1
disk
=
Stack
.
PopValue
(
"a"
)
nomer
=
Stack
.
PopValue
(
"ad"
)
Shapes
.
Move
(
disk
,
GraphicsWindow
.
MouseX
,
GraphicsWindow
.
MouseY
)
EndIf
EndIf
If
Stack
.
GetCount
(
"c"
)
>
1
Then
If
GraphicsWindow
.
MouseX
>=
300
And
GraphicsWindow
.
MouseX
<=
500
Then
s
=
2
disk
=
Stack
.
PopValue
(
"c"
)
nomer
=
Stack
.
PopValue
(
"cd"
)
Shapes
.
Move
(
disk
,
GraphicsWindow
.
MouseX
,
GraphicsWindow
.
MouseY
)
EndIf
EndIf
If
Stack
.
GetCount
(
"b"
)
>
1
Then
If
GraphicsWindow
.
MouseX
>
500
And
GraphicsWindow
.
MouseX
<=
800
Then
s
=
3
disk
=
Stack
.
PopValue
(
"b"
)
nomer
=
Stack
.
PopValue
(
"bd"
)
Shapes
.
Move
(
disk
,
GraphicsWindow
.
MouseX
,
GraphicsWindow
.
MouseY
)
EndIf
EndIf
EndIf
EndSub
'---------------------------------------------------------------------------------------------------------------------------------
Sub
Move
Shapes
.
Move
(
disk
,
GraphicsWindow
.
MouseX
,
GraphicsWindow
.
MouseY
)
EndSub
'---------------------------------------------------------------------------------------------------------------------------------
Sub
Up
If
disk
<>
0
Then
If
GraphicsWindow
.
MouseX
>=
0
And
GraphicsWindow
.
MouseX
<
300
Then
If
Stack
.
GetCount
(
"ad"
)
=
1
Then
Sound
.
PlayChimes
(
)
Stack
.
PushValue
(
"a"
,
disk
)
Stack
.
PushValue
(
"ad"
,
nomer
)
Shapes
.
Move
(
disk
,
190
-
10
*
nomer
,
620
-
Stack
.
GetCount
(
"a"
)
*
20
)
disk
=
0
nomer
=
0
Else
prov
=
Stack
.
PopValue
(
"ad"
)
If
prov
>
nomer
Then
Sound
.
PlayChimes
(
)
Stack
.
PushValue
(
"ad"
,
prov
)
Stack
.
PushValue
(
"a"
,
disk
)
Stack
.
PushValue
(
"ad"
,
nomer
)
Shapes
.
Move
(
disk
,
190
-
10
*
nomer
,
620
-
Stack
.
GetCount
(
"a"
)
*
20
)
disk
=
0
nomer
=
0
Else
Sound
.
PlayBellRing
(
)
GraphicsWindow
.
ShowMessage
(
"Так ходить нельзя"
,
"Ошибка"
)
If
s
=
1
Then
Stack
.
PushValue
(
"a"
,
disk
)
Stack
.
PushValue
(
"ad"
,
nomer
)
Shapes
.
Move
(
disk
,
190
-
10
*
nomer
,
620
-
Stack
.
GetCount
(
"a"
)
*
20
)
ElseIf
s
=
2
Then
Stack
.
PushValue
(
"c"
,
disk
)
Stack
.
PushValue
(
"cd"
,
nomer
)
Shapes
.
Move
(
disk
,
390
-
10
*
nomer
,
620
-
Stack
.
GetCount
(
"c"
)
*
20
)
ElseIf
s
=
3
Then
Stack
.
PushValue
(
"b"
,
disk
)
Stack
.
PushValue
(
"bd"
,
nomer
)
Shapes
.
Move
(
disk
,
590
-
10
*
nomer
,
620
-
Stack
.
GetCount
(
"b"
)
*
20
)
EndIf
Stack
.
PushValue
(
"ad"
,
prov
)
disk
=
0
nomer
=
0
EndIf
EndIf
EndIf
If
GraphicsWindow
.
MouseX
>=
300
And
GraphicsWindow
.
MouseX
<=
500
Then
If
Stack
.
GetCount
(
"cd"
)
=
1
Then
Sound
.
PlayChimes
(
)
Stack
.
PushValue
(
"c"
,
disk
)
Stack
.
PushValue
(
"cd"
,
nomer
)
Shapes
.
Move
(
disk
,
390
-
10
*
nomer
,
620
-
Stack
.
GetCount
(
"c"
)
*
20
)
disk
=
0
nomer
=
0
Else
prov
=
Stack
.
PopValue
(
"cd"
)
If
prov
>
nomer
Then
Sound
.
PlayChimes
(
)
Stack
.
PushValue
(
"cd"
,
prov
)
Stack
.
PushValue
(
"c"
,
disk
)
Stack
.
PushValue
(
"cd"
,
nomer
)
Shapes
.
Move
(
disk
,
390
-
10
*
nomer
,
620
-
Stack
.
GetCount
(
"c"
)
*
20
)
disk
=
0
nomer
=
0
Else
Sound
.
PlayBellRing
(
)
GraphicsWindow
.
ShowMessage
(
"Так ходить нельзя"
,
"Ошибка"
)
If
s
=
1
Then
Stack
.
PushValue
(
"a"
,
disk
)
Stack
.
PushValue
(
"ad"
,
nomer
)
Shapes
.
Move
(
disk
,
190
-
10
*
nomer
,
620
-
Stack
.
GetCount
(
"a"
)
*
20
)
ElseIf
s
=
2
Then
Stack
.
PushValue
(
"c"
,
disk
)
Stack
.
PushValue
(
"cd"
,
nomer
)
Shapes
.
Move
(
disk
,
390
-
10
*
nomer
,
620
-
Stack
.
GetCount
(
"c"
)
*
20
)
ElseIf
s
=
3
Then
Stack
.
PushValue
(
"b"
,
disk
)
Stack
.
PushValue
(
"bd"
,
nomer
)
Shapes
.
Move
(
disk
,
590
-
10
*
nomer
,
620
-
Stack
.
GetCount
(
"b"
)
*
20
)
EndIf
Stack
.
PushValue
(
"cd"
,
prov
)
disk
=
0
nomer
=
0
EndIf
EndIf
EndIf
If
GraphicsWindow
.
MouseX
>
500
And
GraphicsWindow
.
MouseX
<=
800
Then
If
Stack
.
GetCount
(
"bd"
)
=
1
Then
Sound
.
PlayChimes
(
)
Stack
.
PushValue
(
"b"
,
disk
)
Stack
.
PushValue
(
"bd"
,
nomer
)
Shapes
.
Move
(
disk
,
590
-
10
*
nomer
,
620
-
Stack
.
GetCount
(
"b"
)
*
20
)
disk
=
0
nomer
=
0
Else
prov
=
Stack
.
PopValue
(
"bd"
)
If
prov
>
nomer
Then
Sound
.
PlayChimes
(
)
Stack
.
PushValue
(
"bd"
,
prov
)
Stack
.
PushValue
(
"b"
,
disk
)
Stack
.
PushValue
(
"bd"
,
nomer
)
Shapes
.
Move
(
disk
,
590
-
10
*
nomer
,
620
-
Stack
.
GetCount
(
"b"
)
*
20
)
disk
=
0
nomer
=
0
Else
Sound
.
PlayBellRing
(
)
GraphicsWindow
.
ShowMessage
(
"Так ходить нельзя"
,
"Ошибка"
)
If
s
=
1
Then
Stack
.
PushValue
(
"a"
,
disk
)
Stack
.
PushValue
(
"ad"
,
nomer
)
Shapes
.
Move
(
disk
,
190
-
10
*
nomer
,
620
-
Stack
.
GetCount
(
"a"
)
*
20
)
ElseIf
s
=
2
Then
Stack
.
PushValue
(
"c"
,
disk
)
Stack
.
PushValue
(
"cd"
,
nomer
)
Shapes
.
Move
(
disk
,
390
-
10
*
nomer
,
620
-
Stack
.
GetCount
(
"c"
)
*
20
)
ElseIf
s
=
3
Then
Stack
.
PushValue
(
"b"
,
disk
)
Stack
.
PushValue
(
"bd"
,
nomer
)
Shapes
.
Move
(
disk
,
590
-
10
*
nomer
,
620
-
Stack
.
GetCount
(
"b"
)
*
20
)
EndIf
Stack
.
PushValue
(
"bd"
,
prov
)
disk
=
0
nomer
=
0
EndIf
EndIf
EndIf
If
Stack
.
GetCount
(
"b"
)
=
9
Then
GraphicsWindow
.
ShowMessage
(
"Congratulation! You Win!"
"Victory!"
)
EndIf
EndIf
EndSub
Copyright (c) Microsoft Corporation. All rights reserved.