In: Computer Science
I need to write a program and can't seem to make it run properly.
Someone can help with the coding? It's with python
Thanks!
Here is the program:
The rules of Shut the Box are as follows (abbreviated
version):
+ You have two 5-sided die
+ You have 5 wooden blocks (labeled 1 through 5)
+ Each turn, you roll the dice and knock down the blocks
corresponding to the number on each die + You have to knock down
one block every turn. If you can’t, you lose
+ You may knock down two blocks in your turn (e.g. if you roll a
3 and a 5 you can knock them both down)
+ You must knock down every single block to win
+ Rounds continue until either 1) all blocks are knocked down (you
win!) or 2) you don't knock any blocks down in a given round (you
lose!)
Use those criteria to write a Shut the Box program that displays what turn it is, rolls the dice, knocks down blocks, and displays whether the user won or loss.
Your program should have the following functions:
1. main: Begins program execution: display "Let's play Shut the Box!". Initialize five block variables all to False (representing the blocks standing up at the beginning of the game). Display the current turn (e.g. Turn: 1, Turn: 2, etc.). Call the roll_die function twice to get values for the round, then display the values rolled (e.g. "You rolled a 5 and a 3"). Process the dice values by checking to see if the block associated with the value rolled is currently standing (False) or knocked down (True). If the block is False, flip it to True (you knocked it down). Make sure to keep track of the number of die being used in a given round, because if you don't knock any blocks down in a round you lose!
2. has_won: receives all five blocks as parameter variables (Booleans) and checks whether each block is True or False (False = block is standing, True = block has been knocked down). If a block is False, return False. If all blocks are True, return True (meaning they are all knocked down, thus you won).
3. roll_die: simulates rolling of a virtual die. Returns a single random integer between 1 and 5 (inclusive).
1
Hints:
+ The five variables representing the blocks should be Booleans.
Initialize them all to be False (standing up), then flip them to
True as they get knocked down.
+ Need to define a number of turns variable that gets incremented
(added to) each turn
+ Need to keep track of the number of die not used in that turn
+ If both die are not used (e.g. num_die_not_used == 2) in a given round, the game is over (no blocks were knocked down)
Your program should match the provided sample runs below:
Let's play Shut the Box! Turn: 1 You rolled a 5 and a 3 You knocked down block 3 You knocked down block 5 Turn: 2
You rolled a 2 and a 4
You knocked down block 2
You knocked down block 4
Turn: 3
You rolled a 5 and a 4
You lost in 3 turns, better luck next time.
Let's play Shut the Box! Turn: 1 You rolled a 5 and a 5 You knocked down block 5 Turn: 2
You rolled a 1 and a 3 You knocked down block 1 You knocked down block 3 Turn: 3 You rolled a 1 and a 4 You knocked down block 4 Turn: 4 You rolled a 2 and a 2 You knocked down block 2 Congrats, you won in 4 turns!
Please look at my code and in case of indentation issues check the screenshots.
------------main.py----------------
import random
def roll_die():
#rolling of a virtual die. Returns a single
random integer between 1 and 5 (inclusive).
return random.randint(1, 5)
def has_won(blocks):
if False in blocks:
#If a block is False, return
False.
return False
return True
#returns true if all blocks are True
def main():
print("Let's play Shut the Box!")
blocks = [False, False, False, False,
False]
#five variables representing the blocks should
be Booleans
turn = 0
#turn variable that gets
incremented (added to) each turn
num_die_not_used = 0
#to keep track of the number
of die not used in that turn
while num_die_not_used != 2:
#If both die are not used (e.g. num_die_not_used
== 2) in a given round, the game is over
turn = turn + 1
print("Turn:", turn)
dice_value_1 =
roll_die() #Call the roll_die
function twice to get values for the round
dice_value_2 = roll_die()
print("You rolled a", dice_value_1,
"and a", dice_value_2)
if blocks[dice_value_1-1] ==
False: #checking to see if the block
associated with the value rolled is currently False
blocks[dice_value_1-1] = True #If
the block is False, flip it to True (you knocked it down)
print("You
knocked down block", dice_value_1)
else:
num_die_not_used
= num_die_not_used + 1
if blocks[dice_value_2-1] ==
False:
blocks[dice_value_2-1] = True
print("You
knocked down block", dice_value_2)
else:
num_die_not_used
= num_die_not_used + 1
if(num_die_not_used ==
2):
#If both die are not used (e.g. num_die_not_used
== 2) in a given round, the game is over
print("You lost
in", turn, "turns, better luck next time.")
break
if has_won(blocks):
#If all blocks are True,
return True (meaning they are all knocked down, thus you won
print("Congrats,
you won in", turn, "turns!")
break
num_die_not_used = 0
main()
--------------Screenshots-------------------
----------------------Output------------------------------------
--------------------Updated code main.py------------------------------------------------
import random
def roll_die():
#rolling of a virtual die. Returns a single
random integer between 1 and 5 (inclusive).
return random.randint(1, 5)
def has_won(blocks):
for i in range(len(blocks)):
if blocks[i] == False:
#If a
block is False, return False.
return
False
return True
#returns true if all blocks are True
def main():
print("Let's play Shut the Box!")
blocks = [False, False, False, False,
False]
#five variables representing the blocks should
be Booleans
turn = 0
#turn variable that gets
incremented (added to) each turn
num_die_not_used = 0
#to keep track of the number
of die not used in that turn
while num_die_not_used != 2 and not
has_won(blocks): #If both die are not used in a round or game is
not won, then play another round
num_die_not_used = 0
turn = turn + 1
print("Turn:", turn)
dice_value_1 =
roll_die() #Call the roll_die
function twice to get values for the round
dice_value_2 = roll_die()
print("You rolled a", dice_value_1,
"and a", dice_value_2)
if blocks[dice_value_1-1] ==
False: #checking to see if the block
associated with the value rolled is currently False
blocks[dice_value_1-1] = True #If
the block is False, flip it to True (you knocked it down)
print("You
knocked down block", dice_value_1)
else:
num_die_not_used
= num_die_not_used + 1
if blocks[dice_value_2-1] ==
False:
blocks[dice_value_2-1] = True
print("You
knocked down block", dice_value_2)
else:
num_die_not_used
= num_die_not_used + 1
if(num_die_not_used ==
2):
#If both die are not used (e.g. num_die_not_used
== 2) in a given round, the game is over
print("You lost
in", turn, "turns, better luck next time.")
if has_won(blocks):
#If all blocks are True,
return True (meaning they are all knocked down, thus you won
print("Congrats,
you won in", turn, "turns!")
main()
---------------------------------------------------------------------------------------------------------------
--------------------Update 2 code main.py------------------------------------------------
import random
def roll_die():
#rolling of a virtual die. Returns a single
random integer between 1 and 5 (inclusive).
return random.randint(1, 5)
def has_won(block1, block2, block3, block4, block5):
if block1 and block2 and block3 and block4 and
block5:
return True
#returns true if all blocks
are True
else:
return False
def main():
print("Let's play Shut the Box!")
block1 = block2 = block3 = block4 = block5 =
False #five
variables representing the blocks should be Booleans
turn = 0
#turn variable that gets
incremented (added to) each turn
num_die_not_used = 0
#to keep track of the number
of die not used in that turn
while num_die_not_used != 2 and not has_won(block1,
block2, block3, block4, block5): #If both die are not used in a
round or game is not won, then play another round
num_die_not_used = 0
turn = turn + 1
print("Turn:", turn)
dice_value_1 =
roll_die() #Call the roll_die
function twice to get values for the round
dice_value_2 = roll_die()
print("You rolled a", dice_value_1,
"and a", dice_value_2)
#checking to see if the block
associated with the value rolled is currently False
#If the block is False, flip it to
True (you knocked it down)
if dice_value_1 == 1:
if block1 ==
False:
block1 = True
print("You knocked down block",
dice_value_1)
else:
num_die_not_used = num_die_not_used + 1
elif dice_value_1 == 2:
if block2 ==
False:
block2 = True
print("You knocked down block",
dice_value_1)
else:
num_die_not_used = num_die_not_used + 1
elif dice_value_1 == 3:
if block3 ==
False:
block3 = True
print("You knocked down block",
dice_value_1)
else:
num_die_not_used = num_die_not_used + 1
elif dice_value_1 == 4:
if block4 ==
False:
block4 = True
print("You knocked down block",
dice_value_1)
else:
num_die_not_used = num_die_not_used + 1
elif dice_value_1 == 5:
if block5 ==
False:
block5 = True
print("You knocked down block",
dice_value_1)
else:
num_die_not_used = num_die_not_used + 1
# For dice 2
if dice_value_2 == 1:
if block1 ==
False:
block1 = True
print("You knocked down block",
dice_value_2)
else:
num_die_not_used = num_die_not_used + 1
elif dice_value_2 == 2:
if block2 ==
False:
block2 = True
print("You knocked down block",
dice_value_2)
else:
num_die_not_used = num_die_not_used + 1
elif dice_value_2 == 3:
if block3 ==
False:
block3 = True
print("You knocked down block",
dice_value_2)
else:
num_die_not_used = num_die_not_used + 1
elif dice_value_2 == 4:
if block4 ==
False:
block4 = True
print("You knocked down block",
dice_value_2)
else:
num_die_not_used = num_die_not_used + 1
elif dice_value_2 == 5:
if block5 ==
False:
block5 = True
print("You knocked down block",
dice_value_2)
else:
num_die_not_used = num_die_not_used + 1
if(num_die_not_used ==
2):
#If both die are not used (e.g. num_die_not_used
== 2) in a given round, the game is over
print("You lost
in", turn, "turns, better luck next time.")
if has_won(block1, block2,
block3, block4, block5): #If all blocks are True,
return True (meaning they are all knocked down, thus you won
print("Congrats,
you won in", turn, "turns!")
main()
-----------------------------------------------------------------------------------------------------
Please give a thumbs up if you find this answer helpful.
If it doesn't help, please comment before giving a thumbs
down.
Please Do comment if you need any clarification.
I will surely help you.
Thankyou