This program is also from an article in Small Basic Forum Help with Small Basic program posted by Rahul Yedida. The question was "I am trying to make a Monopoly game in Small Basic TextWindow. I have declared the 2d arrays for the squares, chance and community chest. What should my game loop be like?" So I made a sample program for that.
Latest program is published as DFZ772-0. But this program doesn't work in browser. So, please import in Small Basic IDE. This program is not completed but has comments instead.
Main Loop
Main is structured with double loop. The outer loop is to repeat game. The inner loop is for each player's move.
1.
' Monopoly Like Game 0.2a
2.
' Program ID DFZ772-0
3.
nextGame
=
"True"
4.
' Declare arrays for the squares, chance and community chest
5.
InitBoard
(
)
6.
While
nextGame
7.
' Prepare the game
8.
playerPos
[
1
]
=
1
' start position for player 1
9.
playerPos
[
2
]
=
1
' start position for player 2
10.
DrawBoard
(
)
11.
' Start the game
12.
continue
=
"True"
13.
While
continue
14.
' Play by player 1
15.
player
=
1
16.
Play
(
)
17.
If
continue
Then
18.
' Play by player 2
19.
player
=
2
20.
Play
(
)
21.
EndIf
22.
EndWhile
23.
' Game ending
24.
' Ask next game
25.
EndWhile
Board Initialization
This subroutine initializes the game board. And calculates column and row positions of all squares.
27.
Sub
InitBoard
28.
colMax
=
11
29.
rowMax
=
11
30.
playerColor
[
1
]
=
"Yellow"
31.
playerColor
[
2
]
=
"Cyan"
32.
posMax
=
(
colMax
+
rowMax
-
2
)
*
2
33.
For
i
=
1
To
posMax
34.
If
(
1
<
=
i
)
And
(
i
<
rowMax
)
Then
35.
colPos
[
i
]
=
1
36.
rowPos
[
i
]
=
rowMax
+
1
-
i
37.
ElseIf
(
rowMax
<
=
i
)
And
(
i
<
=
rowMax
+
colMax
-
2
)
Then
38.
colPos
[
i
]
=
i
-
rowMax
+
1
39.
rowPos
[
i
]
=
1
40.
ElseIf
(
colMax
+
rowMax
-
1
<
=
i
)
And
(
i
<
=
2
*
rowMax
+
colMax
-
2
)
Then
41.
colPos
[
i
]
=
colMax
42.
rowPos
[
i
]
=
i
-
rowMax
-
colMax
+
2
43.
ElseIf
(
2
*
colMax
+
rowMax
-
1
<
=
i
)
And
(
i
<
=
posMax
)
Then
44.
colPos
[
i
]
=
posMax
-
i
+
2
45.
rowPos
[
i
]
=
rowMax
46.
EndIf
47.
EndFor
48.
EndSub
Drawing a Board
This subroutine draws all squares.
50.
Sub
DrawBoard
51.
For
pos
=
1
To
posMax
52.
DrawSquare
(
)
53.
EndFor
54.
EndSub
Drawing a Square
This subroutine draws a square. For drawing any place In TextWindow, you can use TextWindow.CursorLeft and TextWindow.CursorTop.
56.
Sub
DrawSquare
57.
' param pos
58.
col
=
colPos
[
pos
]
59.
row
=
rowPos
[
pos
]
60.
left
=
(
col
-
1
)
*
7
61.
top
=
(
row
-
1
)
*
2
62.
' Draw top row
63.
TextWindow
.
CursorLeft
=
left
64.
TextWindow
.
CursorTop
=
top
65.
TextWindow
.
Write
(
"+------+"
)
66.
top
=
top
+
1
67.
' Draw center row
68.
TextWindow
.
CursorLeft
=
left
69.
TextWindow
.
CursorTop
=
top
70.
TextWindow
.
Write
(
"|"
)
71.
If
pos
=
1
Then
72.
TextWindow
.
Write
(
"GO"
)
73.
Else
74.
If
pos
<
10
Then
75.
TextWindow
.
Write
(
" "
)
76.
EndIf
77.
TextWindow
.
Write
(
pos
)
78.
EndIf
79.
TextWindow
.
ForegroundColor
=
playerColor
[
1
]
80.
TextWindow
.
Write
(
" "
)
' reserved for player 1
81.
If
playerPos
[
1
]
=
pos
Then
82.
TextWindow
.
Write
(
"o"
)
83.
Else
84.
TextWindow
.
Write
(
" "
)
85.
EndIf
86.
TextWindow
.
ForegroundColor
=
playerColor
[
2
]
87.
TextWindow
.
Write
(
" "
)
' reserved for player 2
88.
If
playerPos
[
2
]
=
pos
Then
89.
TextWindow
.
Write
(
"o"
)
90.
Else
91.
TextWindow
.
Write
(
" "
)
92.
EndIf
93.
TextWindow
.
ForegroundColor
=
"Gray"
94.
TextWindow
.
Write
(
"|"
)
95.
top
=
top
+
1
96.
' Draw bottom row
97.
TextWindow
.
CursorLeft
=
left
98.
TextWindow
.
CursorTop
=
top
99.
TextWindow
.
Write
(
"+------+"
)
100.
top
=
top
+
1
101.
EndSub
A Move for a Player
This subroutine plays a move for a player. It rolls a dice and moves a piece as the same as the dice number. In nature, there are some events at a square but this time only comments are there.
103.
Sub
Play
104.
' Input some key to roll the dice
105.
TextWindow
.
CursorLeft
=
0
106.
TextWindow
.
CursorTop
=
rowMax
*
2
+
1
107.
TextWindow
.
ForegroundColor
=
playerColor
[
player
]
108.
TextWindow
.
Write
(
"Player "
+
player
+
" --> "
)
109.
TextWindow
.
ForegroundColor
=
"Gray"
110.
TextWindow
.
Write
(
"Press enter..."
)
111.
TextWindow
.
Read
(
)
112.
TextWindow
.
CursorLeft
=
13
113.
TextWindow
.
CursorTop
=
TextWindow
.
CursorTop
-
1
114.
' Role the dice
115.
d
=
Math
.
GetRandomNumber
(
6
)
116.
TextWindow
.
ForegroundColor
=
playerColor
[
player
]
117.
TextWindow
.
WriteLine
(
d
+
" "
)
118.
TextWindow
.
ForegroundColor
=
"Gray"
119.
For
i
=
1
To
d
120.
MoveToNext
(
)
121.
DrawBoard
(
)
122.
Sound
.
PlayClickAndWait
(
)
123.
EndFor
124.
' Show the player's tactics
125.
' If the new position is owned by the other player
126.
' Try to deduct the rent
127.
' If player has no rent
128.
' Sell one of his properties
129.
' Calculate the remainder
130.
' Judge of game end
131.
EndSub
A Move for a Piece in a Board
The position of a piece is expressed as an array playerPos.
133.
Sub
MoveToNext
134.
p
=
playerPos
[
player
]
135.
p
=
p
+
1
136.
If
p
>
posMax
Then
137.
p
=
1
138.
EndIf
139.
playerPos
[
player
]
=
p
140.
EndSub
The important point of today's program is using TextWindow.CursorLeft and TextWindow.CursorTop for movement. But this program doesn't work well in browser. Please import into Small Basic IDE and run.
With the manner above, you can create TextWindow games such like shooting or scrolling games.
Next week, I'm going to try games with GraphicsWindow. See you next week.