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 210. DrawBoard()11. ' Start the game12. continue = "True"13. While continue14. ' Play by player 115. player = 116. Play()17. If continue Then18. ' Play by player 219. player = 220. Play()21. EndIf22. EndWhile23. ' Game ending24. ' Ask next game25.EndWhileBoard Initialization
This subroutine initializes the game board. And calculates column and row positions of all squares.
27.Sub InitBoard28. colMax = 1129. rowMax = 1130. playerColor[1] = "Yellow"31. playerColor[2] = "Cyan"32. posMax = (colMax + rowMax - 2) * 233. For i = 1 To posMax34. If (1 <= i) And (i < rowMax) Then 35. colPos[i] = 136. rowPos[i] = rowMax + 1 - i37. ElseIf (rowMax <= i) And (i <= rowMax + colMax - 2) Then38. colPos[i] = i - rowMax + 139. rowPos[i] = 140. ElseIf (colMax + rowMax - 1 <= i) And (i <= 2 * rowMax + colMax - 2) Then41. colPos[i] = colMax42. rowPos[i] = i - rowMax - colMax + 243. ElseIf (2 * colMax + rowMax - 1 <= i) And (i <= posMax) Then44. colPos[i] = posMax - i + 245. rowPos[i] = rowMax46. EndIf47. EndFor48.EndSubDrawing a Board
This subroutine draws all squares.
50.Sub DrawBoard51. For pos = 1 To posMax52. DrawSquare()53. EndFor54.EndSubDrawing 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 + 1101.EndSubA 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 Play104. ' Input some key to roll the dice105. TextWindow.CursorLeft = 0106. TextWindow.CursorTop = rowMax * 2 + 1107. TextWindow.ForegroundColor = playerColor[player]108. TextWindow.Write("Player " + player +" --> ")109. TextWindow.ForegroundColor = "Gray"110. TextWindow.Write("Press enter...")111. TextWindow.Read()112. TextWindow.CursorLeft = 13113. TextWindow.CursorTop = TextWindow.CursorTop - 1114. ' Role the dice115. d = Math.GetRandomNumber(6)116. TextWindow.ForegroundColor = playerColor[player]117. TextWindow.WriteLine(d + " ")118. TextWindow.ForegroundColor = "Gray"119. For i = 1 To d120. MoveToNext()121. DrawBoard()122. Sound.PlayClickAndWait()123. EndFor124. ' Show the player's tactics125. ' If the new position is owned by the other player126. ' Try to deduct the rent127. ' If player has no rent128. ' Sell one of his properties129. ' Calculate the remainder130. ' Judge of game end131.EndSub A Move for a Piece in a Board
The position of a piece is expressed as an array playerPos.
133.Sub MoveToNext134. p = playerPos[player]135. p = p + 1136. If p > posMax Then137. p = 1138. EndIf139. playerPos[player] = p140.EndSubThe 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.