Question
Consider the last program for the previous LAB report to get the mines for a minefield game from the user. The minefield is sized 5x5,
Consider the last program for the previous LAB report to get the mines for a minefield game from the user. The minefield is sized 5x5, and we have a maximum of 5 mines (one per line). The mines positions in the minefield are saved in a string variable, and we used two loops for the population of the minefield. The following code is one possible implementation:
Modify the code to make a function (place_mines) to ask the mines and save them on a returned string.
Then, make a function to print such a string as a minefield (print_minefield).
Provide the code and the run-output screenshot (entire screen, no cuts).
Add the program below (only code here): |
|
Add a new function (place_random_mines), modifying the function place_mines to place mines automatically and randomly by the computer. It must place 5 mines in all the minefield and one mine per line. You can work on a single loop that parses (traverses) the minefield by lines. You decide where to place the mine by importing random and generating a random integer number from 1 to 5 (both included).
Make a function (step_on) to get a location (of coordinates i, j) in which the user will virtually step. Then, such a function must say if the user is on a mine or not. This function must have as input the minefield and as output a Boolean value. This function will ask a location (i, j), and it returns False if there is no mine and True if there is a mine. This last function is the central part of the game and must be repeated 5 times with a for loop in the main. If the user steps in 5 positions without touching a mine or touching a maximum of 2 mines, he wins (the user has two spare lives). If he takes more than two mines, he loses.
Finally, to run the game, we require a call to the function to randomly create a mine (place_random_mines) and the for loop running the function to test if there are mines on the stepped position (step_on). This last loop must count the True values returned from the function step_on. If they are more than 2 it must display "You lose!" and break the for loop. After the for loop, if the number of mines stepped is below 3, the program must say: "You won!".
Provide the program and the run-output screenshot (entire screen, no cuts).
Add the program below (only code here): |
|
There are some missing parts to complete and improve the game (we will add them in the following classes, but think about):
You can trick the game by stepping every time in the same position if this is one without a mine. To avoid this, we should keep track of the previous steps. Therefore the program should repeat asking the coordinates until the user provides a different position. This part is missing.
You can give values that are not numerical or not an integer or beyond the minefield limit. A check on received values is missing.
The following settings are missing: the possibility to easily change the number of spare lives, the number of required steps before ending the game, the size of the minefield.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started