In: Computer Science
You must program the game called MaDi which is described below.
There is a board of NxN (the size N is defined by the user and must
be a number greater than or equal to 2), where in each box there is
an instruction. The first (the [0] [0]) and the last box (the [N-1]
[N-1]) have no instruction. Possible instructions are:
1. Don't move
2. Advance 4 places
3. Jump to the next row
4. Go back 2 places
5. It exploded! End of the game.
In order to facilitate the visualization of the matrix for the
player, the instruction number will be displayed on the board and
below it the list of instructions with their numbering.
Instructions:
1. Don't move
2. Advance 4 places
3. Jump to the next row
4. Go back 2 places
5. Bomb! Exploded.
The instruction that goes in each square of the board will be
assigned randomly each time the game starts.
The player starts with his chip in the first position (row: 0,
column: 0) and (bottom-right corner). On each turn the player rolls
a die that will tell him how many spaces to advance. You advance
across the board from left to right and from top to bottom as
follows:
On each turn the player rolls a dice that tells him how many
positions on the board to advance. After advancing, he must execute
what is indicated in the instruction of the box where he fell. For
each turn, only execute one instruction on the board. The player
has a maximum number of rolls of the dice, which will be asked to
the player when starting the game.
The game is lost if the dice are thrown without having reached the
goal or if the product of rolling the dice advances to a square
with a bomb. The game is won if you reach the goal (it does not
have to be by exact count) before the roll of the dice runs
out.
import numpy as np
import random
def roll_die():
return randint(1, 7)
n = int(input('Input the size of the board(greater than 2): ')
while n < 2:
print('The number should be above 2: \n Try again')
n = int(input('Input the size of the board(greater than 2): ')
max_rolls = int(input('Enter the maximum number of rolls: ')
total_steps = n * n
bomb_exploded = False
while max_rolls != 0 and not bomb_exploded:
die_number = roll_die()
if die_number == 1:
continue
elif die_number == 2:
total_steps -= 4
elif die_number == 3:
total_steps -= n
elif die_number == 4:
total_steps += 2
else:
bomb_exploded = True
max_rolls -= 1
if total_steps <= 0:
max_rolls = 0
if max_rolls >= 0 or not bomb_exploded:
print('The player has won!!!')
else:
print('Sorry mate! The bomb exploded or you've run out of turns! Try again next time!')