Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 07:15 02 Aug 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : CMM2 / MMBasic equivalent to Python dictionary

Author Message
Nimue

Guru

Joined: 06/08/2020
Location: United Kingdom
Posts: 420
Posted: 11:44am 23 Aug 2020
Copy link to clipboard 
Print this post

Hi all

I am trying to port a Python script to MMBasic (on CMM2).

I use dictionaries to simulate a game of snakes and ladders:

Board_Layout = {2:16,7:21,22:11,24:35,30:43,31:28,33:17,53:69,58:41,62:98}

with the pairs of numbers representing the start / end of a snake or ladder.

So for example, 2:16 -- means if the player ends on square 2, this then returns 16 (ie a ladder).

position = position + dice

The players position is given by: position = Board_Layout.get(position, position), which finds position in the dictionary and returns the value from the pair.  If not found, position is just position -- ie the value just calculated.


My questions are:

How to do replicate the Board_layout ={} dictionary function?
How to I then replicate the .get method?


Any pointers greatly recieved.

Cheers
Nim
Entropy is not what it used to be
 
Nimue

Guru

Joined: 06/08/2020
Location: United Kingdom
Posts: 420
Posted: 11:51am 23 Aug 2020
Copy link to clipboard 
Print this post

Update 1

So I figure I can do this with a series of Case / Select statements   --  imagine that would work fine.

But that seems clumbersome as each pair would need to be a CASE = line...

I'll see how far I get with that and post the code (minus the visualisation which uses Matplotlib library).

Nim
Entropy is not what it used to be
 
realmnm

Newbie

Joined: 07/07/2020
Location: Australia
Posts: 34
Posted: 12:49pm 23 Aug 2020
Copy link to clipboard 
Print this post

Maybe set it up as an array, where the index is the square number, and the value is the square to go to. If it  doesn’t go anywhere, the value is the same as the index.

Martin
This signature intentionally left blank.
 
Nimue

Guru

Joined: 06/08/2020
Location: United Kingdom
Posts: 420
Posted: 12:52pm 23 Aug 2020
Copy link to clipboard 
Print this post

  realmnm said  Maybe set it up as an array, where the index is the square number, and the value is the square to go to. If it  doesn’t go anywhere, the value is the same as the index.

Martin


I'll have a look at that...

Here is the code using CASE / SELECT

Const DICE_SIZE = 6   ' number of faces on dice
Const NUM_GAMES = 100  ' how many games to simualte

Function position(x)

Select Case x
   Case 2
       position = 16
   Case 7
       position = 21
   Case 22
       position = 11
   Case 24
       position = 35
   Case 30
       position = 43
   Case 31
       position = 28
   Case 33
       position = 17
   Case 53
       position = 69
   Case 58
       position = 41
   Case 62
       position = 98
   Case 64
       position = 16
   Case 67
       position = 50
   Case 73
       position = 92
   Case 87
       position = 95
   Case 93
       position = 74
   Case 99
       position = 85
   Case Else
       position = x
End Select
End Function

'Print position(67)

Sub play_game()
   x = 0
   turns = 0

    Do While  x < 100
       turns = turns + 1
      'Print turns;
       roll = Int((Rnd()*DICE_SIZE)+1)
       'Print roll
       If x + roll > 100 Then Exit Sub
       x = x + roll
       x = position(x)
    Loop


End Sub

For y = 1 To NUM_GAMES
   play_game()
   game_total=game_total+turns
Next y

Print "Average number of turns, calculated over " NUM_GAMES " games = ",Int(game_total/NUM_GAMES)


Would have liked to use MATH - but it looks like this is not on the PC version of MMBasic.  When I push this to the CMM2, I will implement the MATH function.

Nim
Entropy is not what it used to be
 
Nimue

Guru

Joined: 06/08/2020
Location: United Kingdom
Posts: 420
Posted: 01:27pm 23 Aug 2020
Copy link to clipboard 
Print this post

  realmnm said  Maybe set it up as an array, where the index is the square number, and the value is the square to go to. If it  doesn’t go anywhere, the value is the same as the index.

Martin


Here we go as an array:

Const DICE_SIZE = 6   ' number of faces on dice
Const NUM_GAMES = 100  ' how many games to simualte

Dim GAMES(NUM_GAMES)

GAMES(2)=16
GAMES(7)=21
GAMES(22)=11
GAMES(24)=35
GAMES(30)=43
GAMES(31)=28
GAMES(33)=17
GAMES(53)=69
GAMES(58)=41
GAMES(62)=98
GAMES(64)=16
GAMES(67)=50
GAMES(73)=92
GAMES(87)=96
GAMES(93)=74
GAMES(99)=85


Function position(x)

position=GAMES(x)
If GAMES(x) = 0 Then position = x

End Function

Print position(19)

Sub play_game()
   x = 0
   turns = 0

    Do While  x < 100
       turns = turns + 1
      'Print turns;
       roll = Int((Rnd()*DICE_SIZE)+1)
       'Print roll
       If x + roll > 100 Then Exit Sub
       x = x + roll
       x = position(x)
    Loop



End Sub

For y = 1 To NUM_GAMES
   play_game()
   game_total=game_total+turns
Next y

Print "Average number of turns, calculated over " NUM_GAMES " games = ",Int(game_total/NUM_GAMES)


I assume this is more efficient as there is only one comparison inside the function this time.

Nice.

Really enjoying breaking things back to a "simpler" language and not relying on the Python stuff I take for granted.

Nim
Entropy is not what it used to be
 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025