Tracks
/
Python
Python
/
Exercises
/
Flower Field
Flower Field

Flower Field

Medium

Introduction

Flower Field is a compassionate reimagining of the popular game Minesweeper. The object of the game is to find all the flowers in the garden using numeric hints that indicate how many flowers are directly adjacent (horizontally, vertically, diagonally) to a square. "Flower Field" shipped in regional versions of Microsoft Windows in Italy, Germany, South Korea, Japan and Taiwan.

Instructions

Your task is to add flower counts to empty squares in a completed Flower Field garden. The garden itself is a rectangle board composed of squares that are either empty (' ') or a flower ('*').

For each empty square, count the number of flowers adjacent to it (horizontally, vertically, diagonally). If the empty square has no adjacent flowers, leave it empty. Otherwise replace it with the count of adjacent flowers.

For example, you may receive a 5 x 4 board like this (empty spaces are represented here with the '·' character for display on screen):

·*·*·
··*··
··*··
·····

Which your code should transform into this:

1*3*1
13*31
·2*2·
·111·

Exception messages

Sometimes it is necessary to raise an exception. When you do this, you should always include a meaningful error message to indicate what the source of the error is. This makes your code more readable and helps significantly with debugging. For situations where you know that the error source will be a certain type, you can choose to raise one of the built in error types, but should still include a meaningful message.

This particular exercise requires that you use the raise statement to "throw" a ValueError when the board() function receives malformed input. The tests will only pass if you both raise the exception and include a message with it.

To raise a ValueError with a message, write the message as an argument to the exception type:

# when the board receives malformed input
raise ValueError("The board is invalid with current input.")
Edit via GitHub The link opens in a new window or tab
Python Exercism

Ready to start Flower Field?

Sign up to Exercism to learn and master Python with 17 concepts146 exercises, and real human mentoring, all for free.

Deep Dive into Flower Field!

We explore nested for loops, clever use of min/max to simplify bounds checking, functional pipelines and using two-dimensional matrices.