Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 14:22 19 Nov 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 : Snakes and Ladders - or How to initialize arrays

Author Message
Nimue

Guru

Joined: 06/08/2020
Location: United Kingdom
Posts: 425
Posted: 10:58am 08 Aug 2021
Copy link to clipboard 
Print this post

Hi all

Apologies for lack of posting - been seriously ill for the last 4 months.  Back now  and 50% vaccinated to boot.

I am converting some Python code (don't hate me for mentioning the big P) that uses dictionary data types to store the positions and actions for landing on a snake or ladder in the board game.

Board_Layout = {10:33,16:37,21:41,23:2,34:15,35:54,44:76,52:31,62:43,80:99,89:68,95:74}


So you roll the dice and look up the value in the dictionary and go to that square.  For example, if you land on a 10 - this is a ladder and takes you to 33.  Quite simple.

To convert this to basic, I'm going to use an array dimensioned to be 100 long (for each square on the board. (Option Base 1) - DIM Board(100).

So in my example Board(10)=33 would simulate the dictionary.

In the cases where there is no snake or ladder, I want the array to store its index value.  For example Board(1)=1, Board(2)=2 etc.  In this case, when a player lands on 2, as there is no snake or ladder, it just returns the index.

My question is:  Can I initialize an array with the each element equal to it's index.  In C++ I can do: int list[4] = {2, 4, 6, 8};   to initialise with values.  

I know...
I could write a loop to fill the array with the indexes and then define the board

I could just check for array elements that are 0, and then do nothing.


Curious if there are any clever initialising tips.


Cheers
Nim
Entropy is not what it used to be
 
Mixtel90

Guru

Joined: 05/10/2019
Location: United Kingdom
Posts: 8297
Posted: 11:13am 08 Aug 2021
Copy link to clipboard 
Print this post

MMBasic supports this:

OPTION BASE 1
DIM board(5)=(1,2,3,4,5)

which would write the values 1,2,3,4,5 into board(1),board(2) etc.

There's no way to automatically initialise with its index value AFAIK.
Mick

Zilog Inside! nascom.info for Nascom & Gemini
Preliminary MMBasic docs & my PCB designs
 
Nimue

Guru

Joined: 06/08/2020
Location: United Kingdom
Posts: 425
Posted: 11:16am 08 Aug 2021
Copy link to clipboard 
Print this post

  Mixtel90 said  MMBasic supports this:

DIM board(5)=(1,2,3,4,5)



That's good to know.  

Verbose, but I could:

DIM board(100)=(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100)

Nice!

Thank you.
N
Entropy is not what it used to be
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 10610
Posted: 11:47am 08 Aug 2021
Copy link to clipboard 
Print this post

Here is a silly way


option base 1
dim integer a(100),board(100)
sort a(),board()
erase a()
 
Mixtel90

Guru

Joined: 05/10/2019
Location: United Kingdom
Posts: 8297
Posted: 11:47am 08 Aug 2021
Copy link to clipboard 
Print this post

I still prefer
For i=1 to 100:board(i)=i:next
It's hardly slow... and a hell of a lot less typing.  :)
Mick

Zilog Inside! nascom.info for Nascom & Gemini
Preliminary MMBasic docs & my PCB designs
 
Mixtel90

Guru

Joined: 05/10/2019
Location: United Kingdom
Posts: 8297
Posted: 11:49am 08 Aug 2021
Copy link to clipboard 
Print this post

That's neat, Peter. :)
I wish I understood it....   lol
Mick

Zilog Inside! nascom.info for Nascom & Gemini
Preliminary MMBasic docs & my PCB designs
 
Nimue

Guru

Joined: 06/08/2020
Location: United Kingdom
Posts: 425
Posted: 11:50am 08 Aug 2021
Copy link to clipboard 
Print this post

  matherp said  Here is a silly way


option base 1
dim integer a(100),board(100)
sort a(),board()
erase a()


Nice and elegant - I like and an interesting use case for sort and storing the index.

N
Entropy is not what it used to be
 
Nimue

Guru

Joined: 06/08/2020
Location: United Kingdom
Posts: 425
Posted: 11:52am 08 Aug 2021
Copy link to clipboard 
Print this post

  Mixtel90 said  I still prefer
For i=1 to 100:board(i)=i:next
It's hardly slow... and a hell of a lot less typing.  :)


Yup - this is the way I went with at the time - just seemed like initialising with the index might have been done somewhere.

The "beauty" of this way is that my students can instinctively see what is going on.

Cheers
N
Entropy is not what it used to be
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 10610
Posted: 11:52am 08 Aug 2021
Copy link to clipboard 
Print this post

With 5.07.01b4 you can also go


option base 1
dim board(100)
data 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20
data 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40
data  41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60
data 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80
data 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100
read board()


Not very useful for an index but very useful if the numbers are non-sequential
 
Lodovik

Regular Member

Joined: 17/05/2021
Location: Canada
Posts: 41
Posted: 02:00pm 08 Aug 2021
Copy link to clipboard 
Print this post

Very useful trick. I didn't knew that you could read an entire array at once. I've not seen this in the documentation and I've read most of it. Must have missed this somehow. I'll put this to good use, thanks.
 
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