|
| https://realpython.com/ |
| Start Here | https://realpython.com/start-here/ |
|
Learn Python
| https://realpython.com/lessons/python-maze-border-patterns/ |
| Your Learning Plan →Your personalized Python learning plan | https://realpython.com/account/learning-plan/ |
| Python Tutorials →In-depth articles and video courses | https://realpython.com/search?kind=article&kind=course&order=newest&_from=nav_library |
| Learning Paths →Guided study plans for accelerated learning | https://realpython.com/learning-paths/ |
| Quizzes & Exercises →Check your learning progress | https://realpython.com/search?kind=exercise&kind=quiz&_from=nav_library |
| Browse Topics →Focus on a specific area or skill level | https://realpython.com/tutorials/all/ |
| Reference →Concise definitions for common Python terms | https://realpython.com/ref/ |
| Mentor AI →NewYour personal Python mentor | https://realpython.com/mentor/ |
| Office Hours →Live Q&A calls with Python experts | https://realpython.com/office-hours/ |
| Community Chat →Learn with other Pythonistas | https://realpython.com/community/ |
| Live Courses →Live, instructor-led Python courses | https://realpython.com/live/ |
| Books →Round out your knowledge and learn offline | https://realpython.com/products/books/ |
| Podcast →Hear what’s new in the world of Python | https://realpython.com/podcasts/rpp/ |
| Unlock All Content and Become a Python Expert →Get unlimited access to all Real Python has to offer | https://realpython.com/account/join/?from=navbar_learn |
| Unlock All Content →Become a Python Expert | https://realpython.com/account/join/?from=navbar_learn |
|
More
| https://realpython.com/lessons/python-maze-border-patterns/ |
| Learner Stories | https://realpython.com/learner-stories/ |
| Python Newsletter | https://realpython.com/newsletter/ |
| Python Job Board | https://www.pythonjobshq.com |
| Meet the Team | https://realpython.com/team/ |
| Become a Contributor | https://realpython.com/jobs/ |
| Search | https://realpython.com/search |
| https://realpython.com/search |
| Join | https://realpython.com/account/join/ |
| Sign‑In | https://realpython.com/account/login/?intent=navbar&next=%2Flessons%2Fpython-maze-border-patterns%2F |
| Unlock This Lesson | https://realpython.com/account/join/?utm_source=rp_lesson&utm_content=python-maze-solver |
| Unlock This Lesson | https://realpython.com/account/join/?utm_source=rp_lesson&utm_content=python-maze-solver |
|
| https://realpython.com/courses/python-maze-solver/#team |
| Mazes in Python: Build, Visualize, Store, and Solve | https://realpython.com/courses/python-maze-solver/ |
| Darren Jones | https://realpython.com/courses/python-maze-solver/#team |
| Recommended Tutorial | https://realpython.com/python-maze-solver/ |
| Course Slides (.pdf) | https://realpython.com/courses/python-maze-solver/downloads/mazes-in-python-slides/ |
| Sample Code (.zip) | https://realpython.com/courses/python-maze-solver/downloads/mazes-in-python-code/ |
| Ask a Question | https://realpython.com/lessons/python-maze-border-patterns/#discussion |
| https://realpython.com/feedback/survey/course/python-maze-solver/liked/?from=lesson-title |
| https://realpython.com/feedback/survey/course/python-maze-solver/disliked/?from=lesson-title |
| Contents | https://realpython.com/lessons/python-maze-border-patterns/#description |
| Transcript | https://realpython.com/lessons/python-maze-border-patterns/#transcript |
| Discussion | https://realpython.com/lessons/python-maze-border-patterns/#discussion |
| 00:00 | https://realpython.com/lessons/python-maze-border-patterns/#t=0.89 |
| Creating Border Patterns. | https://realpython.com/lessons/python-maze-border-patterns/#t=0.89 |
| Each square can have between zero and four sides painted along the main compass | https://realpython.com/lessons/python-maze-border-patterns/#t=4.7 |
| directions: north, south, east, and west. | https://realpython.com/lessons/python-maze-border-patterns/#t=9.04 |
| When you do the math, | https://realpython.com/lessons/python-maze-border-patterns/#t=14.85 |
| you’ll find out that there are sixteen combinations of unique side patterns in total, | https://realpython.com/lessons/python-maze-border-patterns/#t=15.78 |
| ranging from an empty square to one with all four sides painted. | https://realpython.com/lessons/python-maze-border-patterns/#t=19.85 |
| 00:24 | https://realpython.com/lessons/python-maze-border-patterns/#t=24.19 |
| There’s one empty square, four squares with only one side, | https://realpython.com/lessons/python-maze-border-patterns/#t=24.19 |
| six squares with two sides, four squares with three sides, | https://realpython.com/lessons/python-maze-border-patterns/#t=28.58 |
| and one square with all four sides. | https://realpython.com/lessons/python-maze-border-patterns/#t=32.3 |
| 00:35 | https://realpython.com/lessons/python-maze-border-patterns/#t=35.42 |
| Now you can build almost any maze pattern with these tiles by connecting them | https://realpython.com/lessons/python-maze-border-patterns/#t=35.42 |
| like a jigsaw puzzle. | https://realpython.com/lessons/python-maze-border-patterns/#t=38.8 |
| To compactly represent a border pattern around a square with just a single | https://realpython.com/lessons/python-maze-border-patterns/#t=42.56 |
| number, you should consider using a bit field. | https://realpython.com/lessons/python-maze-border-patterns/#t=45.98 |
| 00:49 | https://realpython.com/lessons/python-maze-border-patterns/#t=49.64 |
| This will map each of the four sides to a specific binary digit. | https://realpython.com/lessons/python-maze-border-patterns/#t=49.64 |
| Extracting information from such a number usually requires the use of bitwise | https://realpython.com/lessons/python-maze-border-patterns/#t=54.26 |
| operators, which aren’t very common in Python. | https://realpython.com/lessons/python-maze-border-patterns/#t=58.33 |
| 01:00 | https://realpython.com/lessons/python-maze-border-patterns/#t=60.856 |
| However, | https://realpython.com/lessons/python-maze-border-patterns/#t=60.856 |
| in this course, you’ll use a handy shortcut from the standard library, which hides | https://realpython.com/lessons/python-maze-border-patterns/#t=61.56 |
| the details. | https://realpython.com/lessons/python-maze-border-patterns/#t=65.9 |
| Because there are four sides, | https://realpython.com/lessons/python-maze-border-patterns/#t=67.218 |
| you must allocate four bits that you can individually turn on or off depending | https://realpython.com/lessons/python-maze-border-patterns/#t=70.52 |
| on whether or not there’s a border on that side. | https://realpython.com/lessons/python-maze-border-patterns/#t=74.18 |
| 01:17 | https://realpython.com/lessons/python-maze-border-patterns/#t=77.05 |
| When you convert the corresponding bit string into a decimal number, | https://realpython.com/lessons/python-maze-border-patterns/#t=77.05 |
| you’ll get the unambiguous representation of one of the border patterns, as seen | https://realpython.com/lessons/python-maze-border-patterns/#t=80.28 |
| on-screen. | https://realpython.com/lessons/python-maze-border-patterns/#t=84.8 |
| 01:25 | https://realpython.com/lessons/python-maze-border-patterns/#t=85.786 |
| As you can see, | https://realpython.com/lessons/python-maze-border-patterns/#t=85.786 |
| each border pattern is represented with a decimal number ranging from zero to | https://realpython.com/lessons/python-maze-border-patterns/#t=87.41 |
| fifteen. | https://realpython.com/lessons/python-maze-border-patterns/#t=91.5 |
| You can check which sides are included in the border by looking at the binary | https://realpython.com/lessons/python-maze-border-patterns/#t=93.0 |
| representation of the number and reading the corresponding bits. | https://realpython.com/lessons/python-maze-border-patterns/#t=96.5 |
| 01:40 | https://realpython.com/lessons/python-maze-border-patterns/#t=100.48 |
| If a bit is set to one, | https://realpython.com/lessons/python-maze-border-patterns/#t=100.48 |
| then you know that you should paint that side of the border. | https://realpython.com/lessons/python-maze-border-patterns/#t=101.77 |
| The bit count, or the number of ones in the bit string, reflects the number of | https://realpython.com/lessons/python-maze-border-patterns/#t=105.74 |
| sides in the border. | https://realpython.com/lessons/python-maze-border-patterns/#t=109.44 |
| 01:50 | https://realpython.com/lessons/python-maze-border-patterns/#t=110.712 |
| Additionally, | https://realpython.com/lessons/python-maze-border-patterns/#t=110.712 |
| it carries some useful information about the type of square. | https://realpython.com/lessons/python-maze-border-patterns/#t=111.58 |
| For example, | https://realpython.com/lessons/python-maze-border-patterns/#t=114.927 |
| you can recognize a dead end by detecting precisely three sides of the border, | https://realpython.com/lessons/python-maze-border-patterns/#t=115.94 |
| leaving only one end to connect with other squares. | https://realpython.com/lessons/python-maze-border-patterns/#t=119.75 |
| 02:03 | https://realpython.com/lessons/python-maze-border-patterns/#t=123.87 |
| Fewer than two sides may indicate a potential intersection of paths in the maze | https://realpython.com/lessons/python-maze-border-patterns/#t=123.87 |
| if the square isn’t a wall or part of the exterior. | https://realpython.com/lessons/python-maze-border-patterns/#t=127.82 |
| A T-shaped intersection will have one side, while a four-way intersection will | https://realpython.com/lessons/python-maze-border-patterns/#t=131.74 |
| have none. | https://realpython.com/lessons/python-maze-border-patterns/#t=135.94 |
| 02:18 | https://realpython.com/lessons/python-maze-border-patterns/#t=138.09 |
| Detecting a corner is a bit more tricky because it requires you to compare the | https://realpython.com/lessons/python-maze-border-patterns/#t=138.09 |
| border against one of the four specific patterns, | https://realpython.com/lessons/python-maze-border-patterns/#t=141.74 |
| even though they all have exactly two sides. | https://realpython.com/lessons/python-maze-border-patterns/#t=144.53 |
| 02:28 | https://realpython.com/lessons/python-maze-border-patterns/#t=148.33 |
| This helps eliminate the other two border patterns that also have two sides but | https://realpython.com/lessons/python-maze-border-patterns/#t=148.33 |
| aren’t corners. | https://realpython.com/lessons/python-maze-border-patterns/#t=152.22 |
| Their sides run in parallel instead of meeting at a corner. | https://realpython.com/lessons/python-maze-border-patterns/#t=153.096 |
| 02:40 | https://realpython.com/lessons/python-maze-border-patterns/#t=160.01 |
| So now that you know what makes a square border, how do you define it | https://realpython.com/lessons/python-maze-border-patterns/#t=160.01 |
| in Python? | https://realpython.com/lessons/python-maze-border-patterns/#t=163.07 |
| You’ll use the Enum data type again, but with a slight twist. | https://realpython.com/lessons/python-maze-border-patterns/#t=163.688 |
| 02:48 | https://realpython.com/lessons/python-maze-border-patterns/#t=168.65 |
| This time around, you’ll extend the enum.IntFlag base class, | https://realpython.com/lessons/python-maze-border-patterns/#t=168.65 |
| which is an even more specific enumeration type that implements the bit field | https://realpython.com/lessons/python-maze-border-patterns/#t=172.27 |
| logic. | https://realpython.com/lessons/python-maze-border-patterns/#t=176.31 |
| 02:56 | https://realpython.com/lessons/python-maze-border-patterns/#t=176.776 |
| You’ll give more common names to your enum members | https://realpython.com/lessons/python-maze-border-patterns/#t=176.776 |
| instead of using the compass directions. | https://realpython.com/lessons/python-maze-border-patterns/#t=180.23 |
| Add the code seen on-screen to the border module. | https://realpython.com/lessons/python-maze-border-patterns/#t=185.27 |
| 03:16 | https://realpython.com/lessons/python-maze-border-patterns/#t=196.5 |
| You override the value of Border.EMPTY with 0 to indicate the absence of | https://realpython.com/lessons/python-maze-border-patterns/#t=196.5 |
| border sides. | https://realpython.com/lessons/python-maze-border-patterns/#t=200.8 |
| This class resembles the Role enumeration that you defined earlier, | https://realpython.com/lessons/python-maze-border-patterns/#t=202.75 |
| but IntFlag allows you to do much more with its members. | https://realpython.com/lessons/python-maze-border-patterns/#t=205.94 |
| 03:29 | https://realpython.com/lessons/python-maze-border-patterns/#t=209.69 |
| Rather than being a mutually exclusive choice of one member, | https://realpython.com/lessons/python-maze-border-patterns/#t=209.69 |
| the Border enumeration lets you combine any number of its members to create a | https://realpython.com/lessons/python-maze-border-patterns/#t=213.13 |
| composite value. | https://realpython.com/lessons/python-maze-border-patterns/#t=217.07 |
| 03:39 | https://realpython.com/lessons/python-maze-border-patterns/#t=219.269 |
| For example, | https://realpython.com/lessons/python-maze-border-patterns/#t=219.269 |
| to define a closed border, | https://realpython.com/lessons/python-maze-border-patterns/#t=221.21 |
| you’d combine all four sides using the bitwise OR operator (|) like this. | https://realpython.com/lessons/python-maze-border-patterns/#t=222.68 |
| 04:04 | https://realpython.com/lessons/python-maze-border-patterns/#t=244.2 |
| The .name and .value attributes of the resulting border are | https://realpython.com/lessons/python-maze-border-patterns/#t=244.2 |
| calculated dynamically based on the combination of its sides. | https://realpython.com/lessons/python-maze-border-patterns/#t=248.97 |
| Note that the order of the individual sides doesn’t matter | https://realpython.com/lessons/python-maze-border-patterns/#t=253.69 |
| when you define a composite bit field. | https://realpython.com/lessons/python-maze-border-patterns/#t=256.1 |
| 04:18 | https://realpython.com/lessons/python-maze-border-patterns/#t=258.945 |
| Underneath, | https://realpython.com/lessons/python-maze-border-patterns/#t=258.945 |
| it’s a numeric value resulting from turning on the specific bits. | https://realpython.com/lessons/python-maze-border-patterns/#t=259.89 |
| Python will always show a consistent text representation of that value, | https://realpython.com/lessons/python-maze-border-patterns/#t=264.05 |
| determined by the order of your members in the class definition. | https://realpython.com/lessons/python-maze-border-patterns/#t=268.63 |
| 04:32 | https://realpython.com/lessons/python-maze-border-patterns/#t=272.95 |
| To compare your border to another border pattern, | https://realpython.com/lessons/python-maze-border-patterns/#t=272.95 |
| you can use the identity test operator (is). | https://realpython.com/lessons/python-maze-border-patterns/#t=275.63 |
| 04:45 | https://realpython.com/lessons/python-maze-border-patterns/#t=285.55 |
| Alternatively, | https://realpython.com/lessons/python-maze-border-patterns/#t=285.55 |
| you could use the equality test operator (==) to directly compare your | https://realpython.com/lessons/python-maze-border-patterns/#t=286.62 |
| border against a known numeric value. | https://realpython.com/lessons/python-maze-border-patterns/#t=291.44 |
| 04:58 | https://realpython.com/lessons/python-maze-border-patterns/#t=298.78 |
| Additionally, | https://realpython.com/lessons/python-maze-border-patterns/#t=298.78 |
| you can check if the border contains a given side by using the membership test | https://realpython.com/lessons/python-maze-border-patterns/#t=299.2 |
| operator (in). | https://realpython.com/lessons/python-maze-border-patterns/#t=303.58 |
| Comparing an instance of enum.IntFlag to a number is possible because the flag | https://realpython.com/lessons/python-maze-border-patterns/#t=307.93 |
| is a subclass of the built-in integer type, just like enum.IntEnum, | https://realpython.com/lessons/python-maze-border-patterns/#t=312.28 |
| which you used before. | https://realpython.com/lessons/python-maze-border-patterns/#t=316.26 |
| 05:22 | https://realpython.com/lessons/python-maze-border-patterns/#t=322.13 |
| Next, add a few convenience properties to the enumeration so that you can detect | https://realpython.com/lessons/python-maze-border-patterns/#t=322.13 |
| corners, dead ends, and intersections. | https://realpython.com/lessons/python-maze-border-patterns/#t=326.49 |
| 05:33 | https://realpython.com/lessons/python-maze-border-patterns/#t=333.94 |
| All three properties return a Boolean value. | https://realpython.com/lessons/python-maze-border-patterns/#t=333.94 |
| In the .corner property, | https://realpython.com/lessons/python-maze-border-patterns/#t=336.12 |
| you use the membership test operator (in) to check if an instance of the Border | https://realpython.com/lessons/python-maze-border-patterns/#t=338.22 |
| enumeration—indicated by self—is one of the predefined corners. | https://realpython.com/lessons/python-maze-border-patterns/#t=342.31 |
| 05:54 | https://realpython.com/lessons/python-maze-border-patterns/#t=354.61 |
| The other two properties rely on the integer’s .bit_count() method, | https://realpython.com/lessons/python-maze-border-patterns/#t=354.61 |
| which returns the number of ones in a binary representation of your border. | https://realpython.com/lessons/python-maze-border-patterns/#t=357.69 |
| 06:22 | https://realpython.com/lessons/python-maze-border-patterns/#t=382.22 |
| With the Role and Border building blocks in place, | https://realpython.com/lessons/python-maze-border-patterns/#t=382.22 |
| you can now tie them together on a higher abstraction level. | https://realpython.com/lessons/python-maze-border-patterns/#t=384.65 |
| And in the next | https://realpython.com/lessons/python-maze-border-patterns/#t=387.391 |
| section, you’ll implement the Square class. | https://realpython.com/lessons/python-maze-border-patterns/#t=388.11 |
| Become a Member | https://realpython.com/account/join/ |
| https://realpython.com/lessons/represent-maze-with-objects/ |
| Overview | https://realpython.com/courses/python-maze-solver/ |
| https://realpython.com/lessons/model-python-maze-square/ |
|
Mazes in Python: Build and Visualize 02:47
| https://realpython.com/videos/python-maze-solver-part-1-overview/ |
|
Check Out What You'll Make 02:39
| https://realpython.com/videos/python-maze-solver-project/ |
|
Lay the Groundwork 04:02
| https://realpython.com/videos/python-maze-solver-groundwork/ |
|
Scaffold the Project Structure 04:50
| https://realpython.com/lessons/python-maze-solver-structure/ |
|
Represent the Maze With Objects 06:37
| https://realpython.com/lessons/represent-maze-with-objects/ |
|
Create Border Patterns 06:32
| https://realpython.com/lessons/python-maze-border-patterns/ |
|
Model the Square 02:25
| https://realpython.com/lessons/model-python-maze-square/ |
|
Build the Maze 09:07
| https://realpython.com/lessons/build-python-maze/ |
|
Visualize the Maze With SVG 04:45
| https://realpython.com/lessons/python-maze-svg/ |
|
Implement Geometric Primitives 04:48
| https://realpython.com/lessons/python-maze-geometric-primitives/ |
|
Add More Primitives 03:47
| https://realpython.com/lessons/python-maze-more-primitives/ |
|
Decompose the Border Into Primitives 04:41
| https://realpython.com/lessons/decompose-python-maze-border/ |
|
Add More Border Patterns 03:58
| https://realpython.com/lessons/python-maze-more-border-patterns/ |
|
Build the SVG Renderer 03:49
| https://realpython.com/lessons/python-maze-svg-renderer/ |
|
Add Features to the Renderer 06:50
| https://realpython.com/lessons/python-maze-renderer-features/ |
|
Draw the Solution 03:13
| https://realpython.com/lessons/draw-python-maze-solution/ |
|
Preview the Rendered Maze and Its Solution 04:56
| https://realpython.com/lessons/preview-python-maze/ |
|
Mazes in Python: Build and Visualize (Recap) 01:14
| https://realpython.com/lessons/python-maze-solver-part-1-summary/ |
|
Mazes in Python: Store and Solve 01:48
| https://realpython.com/videos/python-maze-solver-part-2-overview/ |
|
Load the Maze From a Binary File 04:55
| https://realpython.com/videos/python-maze-binary-file/ |
|
Choose Your Data Alignment 05:09
| https://realpython.com/videos/python-maze-data-alignment/ |
|
Define the File Header and Body 08:12
| https://realpython.com/lessons/python-maze-file-header-and-body/ |
|
Serialize the Maze 05:22
| https://realpython.com/lessons/serialize-python-maze/ |
|
Deserialize the Maze 04:32
| https://realpython.com/lessons/deserialize-python-maze/ |
|
Make the Maze Self-Contained 04:30
| https://realpython.com/lessons/python-self-contained-maze/ |
|
Update the Maze Class 03:16
| https://realpython.com/lessons/python-maze-class/ |
|
Solve the Maze Using a Graph-Based Approach 03:01
| https://realpython.com/lessons/solve-python-maze-graph-based/ |
|
Transform the Maze Into a Graph 07:06
| https://realpython.com/lessons/transform-python-maze-graph/ |
|
Find the Shortest Path 05:23
| https://realpython.com/lessons/python-maze-shortest-path/ |
|
Add Weights to Graph Edges 02:36
| https://realpython.com/lessons/python-map-weights-graph-edges/ |
|
Introduce Enemies and Rewards 06:04
| https://realpython.com/lessons/python-maze-enemies-rewards/ |
|
Make a Runnable Script 03:59
| https://realpython.com/lessons/python-maze-runnable-script/ |
|
Mazes in Python: Store and Solve (Recap) 01:23
| https://realpython.com/lessons/python-maze-solver-part-2-summary/ |
| Privacy Policy | https://realpython.com/privacy-policy/ |
Viewport: width=device-width, initial-scale=1, shrink-to-fit=no, viewport-fit=cover