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.
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 NimEntropy is not what it used to be
Nimue Guru Joined: 06/08/2020 Location: United KingdomPosts: 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).
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.
MartinThis signature intentionally left blank.
Nimue Guru Joined: 06/08/2020 Location: United KingdomPosts: 420
Posted: 12:52pm 23 Aug 2020
Copy link to clipboard
Print this post
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.
NimEntropy is not what it used to be
Nimue Guru Joined: 06/08/2020 Location: United KingdomPosts: 420
Posted: 01:27pm 23 Aug 2020
Copy link to clipboard
Print this post
Here we go as an array:
Const DICE_SIZE = 6 ' number of faces on dice Const NUM_GAMES = 100 ' how many games to simualte
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.