WOW: Creating a finite state machine in BASIC and Lua
You can either use the recently released version of BASIC designer from Microsoft (which is quite different from the Visual Basic) or Lua. We’ll look at the Lua code in a later blog, here is the code, note this is NOT the Visual Studio Visual Basic, but rather the BASIC you download at: https://msdn.microsoft.com/en-us/devlabs/cc950524.aspx, this is a simpler type of BASIC programming language, but it appears that there are people who want to extend it already. For gosh sakes folks, just use Visual Basic, keep the designs in this BASIC simple and, well, basic.
An interesting feature of this BASIC is that you can publish the code to the web to share with others, so can simply import the code into your BASIC design environment by entering this code: RGW027 after pushing the “Import” button. Enter the Program ID, in this case RGW027 and the code magically appears in your design space.
I have documented the code, take a look at it, and like most completely autonomous programs it is boring. Now I challenge you to work a little with the code and make changes to it, to make it more interesting for others to use.
1: 'Create some variables so that you can store information
2: 'First number of items in the avatar's bag at the beginning
3: AvatarBag = 15
4: 'WeakEnemyBag, goodies that the Avatar gets from the Enemies bag from a fight
5: WeakEnemyBag = 15
6: StrongEnemyBag =15
7: 'Now we have to define the patrol area variable
8:
9: PatrolAreaType=0
10:
11: 'Now let's start the simplest types of fight, we will use if statements
12: While AvatarBag > 0
13: 'GetRandomNumber creates a random number between 1 and 3 inclusive
14: PatrolAreaType = Math.GetRandomNumber(6)
15: 'Now let's determine the contest, remember our avatar will either continue to
16: 'explore, where PatrolAreaType =1 or 2, fight a weaker enemy when it is 3 or 4,
17: 'and a strongenemy when the PatrolAreaType is 5 or 4
18: If (PatrolAreaType = 1 Or PatrolAreaType = 2) Then
19: RandomGoodies = Math.GetRandomNumber(6)
20: If (RandomGoodies = 6) Then
21: AvatarBag = AvatarBag +1
22: EndIf
23:
24: If (RandomGoodies < 6) Then
25: AvatarBag = AvatarBag -1
26: EndIf
27: EndIf
28:
29: If (PatrolAreaType = 3 Or PatrolAreaType = 4) Then
30: 'Battle with weak opponent
31: WeakEnemy = Math.GetRandomNumber(3)
32: Avatar = Math.GetRandomNumber(6)
33: If (Avatar >= WeakEnemy) Then
34: WeakEnemyBag= WeakEnemyBag -1
35: Avatar = Avatar + 1
36: EndIf
37: EndIf
38:
39: If (PatrolAreaType = 5 Or PatrolAreaType = 6) Then
40: 'Battle with weak opponent
41: StrongEnemy = Math.Abs(Math.GetRandomNumber(12)-2)
42: Avatar = Math.GetRandomNumber(12)
43: If (Avatar > StrongEnemy) Then
44: StrongEnemyBag= StrongEnemyBag -1
45: Avatar = Avatar + 1
46: EndIf
47: If (Avatar < StrongEnemy) Then
48: 'Battle with Strong opponent
49: StrongEnemyBag= StrongEnemyBag + 1
50: Avatar = Avatar - 1
51: EndIf
52:
53: EndIf
54:
55: NumberOfRounds = NumberOfRounds + 1
56:
57:
58: EndWhile
59: TextWindow.WriteLine("AvatarBag")
60: TextWindow.WriteLine(AvatarBag)
61: TextWindow.WriteLine("WeakEnemyBag")
62: TextWindow.WriteLine(WeakEnemyBag)
63: TextWindow.WriteLine("StrongEnemyBag")
64: TextWindow.WriteLine(StrongEnemyBag)
65: TextWindow.WriteLine("RandomGoodies")
66: TextWindow.WriteLine(RandomGoodies)
67: TextWindow.WriteLine("Number of Rounds")
68: TextWindow.WriteLine(NumberOfRounds)
Technorati Tags: Small Basic,Finite state machines,state machines,FUN