Snakes and ladders

Markov Chain to study the game: Snakes and ladders, What is the best way to complete this code :

 size = 100
 first = 0
 last = 99
 board = np.zeros((size, size), dtype = float)


 board[last, last] = 1.0
 for i in range(0,size):
 for j in range(0,min(6, last-i)):
 board[i+j+1,i] = 1.0

 colsum = np.dot(allone, board)

 allone = np.ones((1,size))[0]

 for i in range(size):
 board[:,i] /= colsum[i]

 def addShortCut(board, i, j):
  board[j, :] += board[i, :]
  board[i, :] = np.zeros(size, dtype = float)

It’s easier to get help if your code is formatted correctly. Check the About the Users category.

Your question is to broad.

What quality standards do you want to fulfil (what do you mean by “best”)? What is your goal for this question?

Some advice:

Fix your syntax errors:

>>> for i in range(0,size):
... for j in range(0,min(6, last-i)):
  File "<stdin>", line 2
    for j in range(0,min(6, last-i)):
      ^
IndentationError: expected an indented block

There are more and other syntax errors, make sure code you post can be run.

Check your results

After that, your code should run and produce a board, if I am not mistaking. Check that the board is like you expect.

Use numpy?

You do not show your imports, but it looks like you want to be using numpy (np.zeros etc.). Is that required? It adds to the learning curve, I think this problem is easier to solve using “plain Python” and the Standard Library.

1 Like

Thanks for reply,
here is what I want to do

We have board is going to be 10x10, which means we have 100 tiles in total. The game starts at tile 0 and ends at tile 99. We are going to use a Markov Chain to study the game, and so we need a 100x100 stochastic matrix. We start by creating the matrix for the Markov chain graph of an empty board:
source code:

    size = 100
    first = 0
     last = 99
      board = np.zeros((size, size), dtype = float)


      board[last, last] = 1.0
       for i in range(0,size):
       for j in range(0,min(6, last-i)):
       board[i+j+1,i] = 1.0

      colsum = np.dot(allone, board)

      allone = np.ones((1,size))[0]

      for i in range(size):
       board[:,i] /= colsum[i]

   def addShortCut(board, i, j):
        board[j, :] += board[i, :]
       board[i, :] = np.zeros(size, dtype = float)

now:

Add 5 snakes and 5 ladders to the game using “addShortCut”

I need python code which answers the following questions:

For one player playing the game:

  1. What is the minimal number of rolls needed to finish the game

  2. What is the average number of rolls needed to finish the game.

  3. What percentage of games are finished after 30 rolls.

  4. What percentage of games are finished after 50 rolls.

  5. How many rolls are needed for 90% of games to be finished.

The indentation on the code you are showing now, is even more broken. Please fix that.

It will reveal other errors to fix.

Afterwards, start by adding these. Sorry, but we are not going to write your code.

1 Like