# Snakes and ladders
from random import *
nb_cells = 62
id_finish_cell = nb_cells - 1
# Array describing the board. Values different from 0 indicate special cells.
# If your pawn would end on the cell i and special_cells[i] != 0, your pawn
# ends on the cell special_cells[i] instead.
special_cells = [0, 0, 0, 20, 0, 0, 0, 0, 0, 0, 6, 17, 0, 2, 0, 0, 49, 0, 0, 0, 0, 0, 37, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 59, 0, 0, 0, 0, 21, 0, 0, 0, 0, 0, 0, 26, 0, 0, 0, 0, 0, 55, 38, 0, 0, 43, 0, 0, 31, 0]
#-----------------------------------------------------------------------------#
#-------------------------------------Q1--------------------------------------#
#-----------------------------------------------------------------------------#
# This function counts the number of ladders on a board given as parameter.
# @param[in] board : the array explaining how is the board (special_cells
# is an example of one such boards)
# @return the number of ladders in the board.
def ladder_count(board):
nb_ladders = 0
for i in range(nb_cells):
if (board[i] > i):
nb_ladders = nb_ladders + 1
return nb_ladders
# This function counts the number of cells that can be skipped thanks to
# ladders on a board given as parameter.
# @param[in] board : the array explaining how is the board (special_cells
# is an example of one such boards)
# @return the number of cells skipped on ladders in the board (e.g. a ladder
# from cell 3 to cell 20 skips 17 cells).
def skipped_cells_count(board):
nb_skipped_cells = 0
for i in range(nb_cells):
if (board[i] > i):
nb_cells_ladder = board[i] - i
nb_skipped_cells = nb_skipped_cells + nb_cells_ladder
return nb_skipped_cells
# This function counts the number of cells that can be lost because of
# snakes on a board given as parameter.
# @param[in] board : the array explaining how is the board (special_cells
# is an example of one such boards)
# @return the number of cells lost on snakes in the board (e.g. a snake
# from cell 10 to cell 6 loses 4 cells).
def lost_cells_count(board):
nb_lost_cells = 0
for i in range(nb_cells):
if (board[i] < i and board[i] != 0):
nb_cells_snake = i - board[i]
nb_lost_cells = nb_lost_cells + nb_cells_snake
return nb_lost_cells
# This function takes a board as parameter and answers whether the board
# is correct or not. A correct board verifies:
# * it has 62 cells
# * it has 6 ladders
# (you can use function ladder_count already given)
# * it has 7 snakes
# * the total number of cells ``going up'' on ladders must be equal to 97
# (you can use function skipped_cells_count already given)
# * the total number of cells ``going down'' on snakes must be equal to 114
# (you can use function lost_cells_count already given)
# @param[in] board : the array explaining how is the board (special_cells
# is an example of one such boards)
# @return True if the board is correct; False otherwise.
# (you can verify that is_correct(special_cells) returns True !)
def is_correct(board):
### your code here
pass
#-----------------------------------------------------------------------------#
#-------------------------------------Q2--------------------------------------#
#-----------------------------------------------------------------------------#
# This function simulates a single-player game, and counts the number of die
# rolls needed to win.
# @param[in] board : the array explaining how is the board (special_cells
# is an example of one such boards)
# @return the number of die rolls needed to finish the game.
def single_player_nb_die_rolls(board):
nb_die_rolls = 0
pawn_cell = 0
while (True):
die_roll = randint(1, 6)
nb_die_rolls = nb_die_rolls + 1
pawn_cell = pawn_cell + die_roll
if (pawn_cell >= id_finish_cell):
break
if (board[pawn_cell] != 0):
pawn_cell = board[pawn_cell]
return nb_die_rolls
# This function must simulate 10000 single-player games, and give the average
# number of die rolls needed to win on those 10000 games (the average is the
# sum of all the number of die rolls divided by 10000).
# (you can use function single_player_nb_die_rolls already given)
def average_die_rolls(board):
#your code here
pass