Posts

Showing posts with the label graphs

MatchWalker, a puzzle game of shape and colour

Image
Pt En In today's post I will be sharing a game I made with just under $400$ lines in Processing, a wrapper for Java that makes drawing to the screen really easy. The goal of the game is really simple: go from the cell you are standing on (marked with the black outline of the ellipse, in the screenshot) to the cell that is framed in white. To do that, you can move a "cursor" (the black frame) with the $AWSD$ keys to choose the next cell you want to go to. To move, press the space bar. There are a couple of rules to moving, though: You can only move to the selected cell if it is in the same row or same column as the cell you are in; You can only move to the selected cell if it has the same colour or the same shape as the cell you are in. Rule number $1$ says you can only go in the directions these orange arrows cover: Rule number $2$ says that, from the cells specified by the above rule, you can only go to the white circle, diamond or vertical ellipse (precisely becau...

Random maze generation

Image
Pt En In this post I just want to share a simple algorithm that I used to create random mazes. The idea came from an e-mail I got, about a past competition, where one of the contestants did this exact thing: a program to generate random mazes. I saw the animation of the program working here and I deduced how to do it. All the code can be found on GitHub , as well as an executable of the program, the animations from the beginning and end of this post, an image of a bigger maze, and this other animation: where you can see a different style of maze; a less straight one. The maze starts in the top left red corner and ends wherever the other red square is, which need not be on the bottom right corner. The algorithm is simple: travel randomly inside the black area without ever hitting a white path; whenever no random move can be made, start going back until you find a place where the path can branch out again. While we are creating white paths, keep updating the final position to be ...

Solving mazes with programming

Image
Everyone knows what mazes/labyrinths are! And plenty of us find it amusing to try and solve one! Take the image above as an example... Can you find a path from the top left opening to the bottom right opening? There exists at least one! I wrote a program in Python (making use of pygame and Pillow) to solve any maze given. I did this project several years ago, after learning about Dijkstra's algorithm . I read about the algorithm and decided to implement it, to get a feel for it. I realized that I could apply it to a maze in a picture, if I took each pixel of the image to be a vertex in the graph where I'd apply the method. I wanted to make my program fairly general, in terms of the mazes that could be given, and so I dwelled with a problem: how to tell if two adjacent pixels should be connected in the graph or not. I knew I only wanted to connect pixels that referred to the path, and not to the walls, so my idea was simple: I supposed that a picture of a maze would have mainly ...

Water buckets and infinite tap water

Image
[ Link to code, you can try it in the end of the post] Suppose you have an infinite source of water and two buckets of capacities $5$L and $3$L. Through juggling around the water in the buckets, can you find a way to have one of the buckets hold exactly $1$L of water? A possible way of doing it would be: Fill the bucket of $3$L and then pour its water into the $5$L one; Fill the bucket of $3$L. At this point you have $3$L in each bucket; Pour the bucket of $3$L into the $5$L one. Since the bigger one already had $3$L in it, only $2$ will fit, meaning there will be a remaining litre in the $3$L bucket. I find this problem a very interesting one, and it is not hard to generalize it: given $N$ buckets of capacities $c_1, c_2, \cdots, c_N$, as well as a target value $T$ and an infinite source of water, is there a sequence of moves that puts exactly $T$ litres in one of the buckets? There are some cases for which one can immediately say that there is no such sequence. On one hand, if $T ...

Problem #03 - a quarrel in the Shire

Image
    the scenery in which this proble m takes place Once again I bring you a problem alongside my proposed solution. If you find any mistakes or come up with a different solution, please let me know! Problem statement (and a lovely short story): The Shire is a lovely place where $N $ hobbits live in perfect harmony. Or at least they lived, until a hobbit decided to become an outside decorator and convinced some of its friends to paint their front doors with a very "fashionable" purple (all doors were yellow before that preposterous change). Overnight, the perfect balance and harmony in which the hobbits lived shattered, and hobbits whose doors were different colours couldn't stand one another. Worried, the great and wise Gandalf hurried to the Shire to try and settle this matter. This was what he decided to do: in alphabetical order, he would visit each hobbit. When visiting a hobbit $h $, he would change the colour of its door if and only if there were more hobbits mad...