25.5. Home Page Logic

Now that we have our HTML templates ready, it’s time to add some Python to get the game running.

25.5.1. The index() Function

The home page for our application is controlled by the index() function in main.py. It serves several purposes:

  1. To clear existing data in preparation for a new game,

  2. To process the Main Menu form submission,

  3. To hide a new batch of mines on the board,

  4. To redirect a player to the URL where they will play the game.

To complete your index() function, open your Minesweeper project and code along with the video:

25.5.2. Video Summary

Remember, this summary is NOT intended to replace the video walkthrough! The notes give you information about the ideas explained in the clip. To see the specific Python code and hear the logic behind it, you need to watch the movie.

  1. Two helper functions have already been written for you. The reset_board() and place_mines() functions can be found in the game_logic.py file. Line 2 in main.py imports these functions from that module.

  2. Use a conditional to break the index() function into two parts. One part deals with the form submission (a POST request), and the other resets the board after a GET request.

  3. After a POST request, use request.form[] to collect data from the Main Menu form. This will be the number of mines the user wants to hide.

    1. Add some server-side validation to deal with invalid form submissions (e.g. submitting and empty field, or letters, or symbols, etc.).

    2. For invalid form submissions, set the number of mines to some default value, like 10.

    3. For now, don’t worry about checking if the number of mines is within 5 - 25. You can return to this idea later.

  4. Save the number of mines to the session cookie. Make sure the value is the int data type.

    session['num_mines'] = number_submitted_from_form
    
  5. After validating the number of mines, call the place_mines() function. Include the number of mines as the argument, and save the returned list to session['mines'].

  6. Finally, use the redirect() function to send the user to the /play route.