Question

In: Computer Science

I need to write a program and can't seem to make it run properly. Someone can...

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!

Solutions

Expert Solution

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


Related Solutions

I can't seem to make my inheritance to work properly between my parent class GameCharacter and...
I can't seem to make my inheritance to work properly between my parent class GameCharacter and the child classes hero and villain. Here's my code: import java.sql.SQLOutput; public class Main { public static void main(String[] args) { GameCharacter me = new GameCharacter("King Arthur", 5); GameCharacter tree = new GameCharacter("Tall Tree", 5); GameCharacter enemy = new GameCharacter("Monster Bug", 10); System.out.println(); System.out.println("\n~~~ Game Characters Created ~~~"); System.out.println(tree); System.out.println(me); System.out.println(enemy); System.out.println("\n~~~ The Bug Has Been Attacked ~~~"); me.attack(enemy); System.out.println(tree); System.out.println(me); System.out.println(enemy); System.out.println("\n~~~ The...
How can I write an essay about Endoscopies with an bibliography? I can't seem to find...
How can I write an essay about Endoscopies with an bibliography? I can't seem to find an example. I need a step by step guide. Who can help me write it please and Thank you
I need to draw a cylinder in java with user input, and I can't seem to...
I need to draw a cylinder in java with user input, and I can't seem to get my lines to line up with my ovals correctly from the users input... I know I will have to either add or subtract part of the radius or height but I'm just not getting it right, here is how I'm looking to do it.            g.drawOval(80, 110, radius, height);            g.drawLine(?, ?, ?, ?); g.drawLine(?, ?, ?, ?);   ...
I can't seem to figure out what the hybridization of PF6(-) is. could someone explain how...
I can't seem to figure out what the hybridization of PF6(-) is. could someone explain how to find it correctly for me?
can someone make me a shopping cart for me ? i need to make a shopping...
can someone make me a shopping cart for me ? i need to make a shopping cart ,but i have no idea about how to do this i have made 3 line of items , this is one of the pruduct line line 1 ------------------------------------- <!DOCTYPE html> <html lang="en"> <head> <style> .div1 { border: 2px outset red; background-color: lightblue; text-align: center; } </style> </head> <!-- body --> <body style="background-color:silver; "class="main-layout position_head"> <!-- loader --> </li> </ul> </section> </nav> </section> </header>...
I am struggling with this assignment. I can't get the program to run when I enter...
I am struggling with this assignment. I can't get the program to run when I enter a number with the $ symbol followed by a number below 10. any help would be greatly appreciated. Create a program named Auction that allows a user to enter an amount bid on an online auction item. Include three overloaded methods that accept an int, double, or string bid. Each method should display the bid and indicate whether it is over the minimum acceptable...
I need to make this into a Java Code: Write a program that prompts the user...
I need to make this into a Java Code: Write a program that prompts the user for a double value representing a radius. You will use the radius to calculate: circleCircumference = 2πr circleArea = πr2 sphereArea = 4πr2 sphereVolume = 43πr3 You must use π as defined in the java.lang.Math class. Your prompt to the user to enter the number of days must be: Enter radius: Your output must be of the format: Circle Circumference = circleCircumference Circle Area...
This isn't a homework but since I can't seem to find any answers for it, I...
This isn't a homework but since I can't seem to find any answers for it, I would like to receive some insights from you experts! So, I heard that UK has official left the European Union on Jan 31st (aka Brexit). Are there any trade deals that have been signed? or any negotiations that has been reached? please provide me with some updates I can't seem to find any info on which trade deals were signed and etc. thanks! p.s...
Hi there, I am having a bit of an issue that I just can't seem to...
Hi there, I am having a bit of an issue that I just can't seem to figure it out. My code is supposed to read from another file and then have an output of a number for each categorie ////////Required Output/////// Movies in Silent Era: 0\n Movies in Pre-Golden Era: 7\n Movies in Golden Era: 581\n Movies in Change Era: 3445\n Movies in Modern Era: 10165\n Movies in New Millennium Era: 15457\n My program is: import java.util.ArrayList; public class MovieReducerEraCount...
My Java program keeps "running." I know I need to close a "loop" but I can't...
My Java program keeps "running." I know I need to close a "loop" but I can't find it. I'm learning how to code. This is confusing for me. import java.util.Scanner; import java.util.ArrayList; public class SteppingStone4_Loops {    public static void main(String[] args) { Scanner scnr = new Scanner(System.in); String recipeName = ""; ArrayList<String> ingredientList = new ArrayList(); String newIngredient = ""; boolean addMoreIngredients = true; System.out.println("Please enter the recipe name: "); recipeName = scnr.nextLine();    do {    System.out.println("Would you...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT