 Passion has his passion is math and he's going to speak about math and video games and I found a very special curiosity about him that he's even able to give some artificial intelligence to Matchboxes, I don't know how he does it. He's really amazing He even does his PhD in math, but now and today he will speak about the short path possible in Pac-Man, for example and also about other video games and I Hope you will enjoy. I will I also like very much video games and math and Yeah, they come Thank you very much Right. Good evening everyone. So yeah, I'm Matthew. I'm talking about maths and video games to gate I'm not talking about the machine learning Matchboxes machine But if you want to we can chat about afterwards for hours and hours because it is really cool But that is not my talk today. Today. We're talking about video games And the main character of our talk today is going to be Pac-Man And really the title is a little bit misleading It should say maths and video game because I'm basically going to ramble about Pac-Man for about 40 minutes So Pac-Man And Pac-Man, hope you've all played the game Pac-Man Just in case you haven't played Pac-Man and the aim of the game Pac-Man you play as this character Pac-Man Who has to go out of maze eating these packed dots while avoiding the four ghosts which kill Pac-Man if The ghosts touch Pac-Man And Pac-Man starts in maze like this So the aim of the game is you start here you go around maze eating the packed dots and Once you've eaten all the packed dots you get to go to the next level Okay, so as a mathematician I kind of I play this for a little bit But then I kind of start thinking as a mathematician and start wondering What's the best way of doing this? What's the shortest path I can take? Or possibly can I do it without having to double back on myself? If I could do it without double them back on myself, it would be very very nice and I could do it very quickly So this is related to a very famous mathematical problem called the seven bridges of Kernigsberg So this is a map of Kernigsberg Which is a town in what is now Russia and on the map? You can see you can just about see that there are islands like this with seven bridges shown in orange here And the story goes that the townspeople of Kernigsberg used to spend their Sunday afternoons Starting at our house and trying to walk around the island crossing each bridge exactly once So very similar to the way Pac-Man wants to go down each road exactly once The townspeople tried to go across each bridge exactly once. So for example, they might go around here Keep going around there. And now they found that they are stuck and they have missed out this bridge here So that is not a solution to the problem So they tried this for a very long time and they found it really really difficult to manage a path where they went over all the bridges exactly once and eventually this man Leonard Euler Came along and he proved that actually the reason they were finding it so hard was because it is actually impossible And he explained why it's impossible and invented a whole new branch of maths called graph theory from this problem and the way he Proved that it was impossible and this is then going to be useful to apply to Pac-Man and was By realizing that first of all The map and the way of things laid out is kind of an is unimportant What you can do is you can represent each island with kind of a point and then replace the bridges with lines between these points Something like this and then we can ignore the map completely and that smaller diagram here has all the important information It shows us this four islands and this is how they're connected with bridges So now instead of imagining us walking around the islands we can imagine that we are walking around the lines in this graph Okay, so now imagine we are thinking about just one of the islands now and it's got some combination of bridges going out of it Here we've got an even number of bridges and the nice thing with an even number of bridges is that if you come into the island You can go off the island again And if you come in again, you can go off again And if there's an even number every time you come in there's a pair that you can go off in Whereas if there's an odd number of bridges coming on to the island Then we can pair up the other ones and then we're left with one bridge left And now if we come at the island we get stuck on the island so That is if we have got dots and there is an even number of bridges going off the dots We will be fine because every time you come on we can get off again But if we have an odd number of bridges going on to an island, we're going to get stuck on the island So that means that if we have islands with an odd number of bridges, we are not immediately screwed Because we can always end there or we can start there But if there are more than two bridges with an odd number sorry more than two islands with an odd number of bridges Then we are not going to be able to do it Okay, so back to the kernigsberg graph. You can see here that We have got five bridges three bridges three bridges three bridges and we've got four islands with an odd number of bridges Which means that We are going to get stuck on all the islands at some point. So we cannot get around this graph without Going over an edge more than once which means that It is impossible to work on kernsberg going over the bridges exactly once and this is what Euler proved Okay, so an interesting aside at this point. Um, so during the war, um, kernigsberg was bombed It's now called klinengrad as well and it now looks something like this and now has less bridges Like that so now we can solve a different problem, which is the six bridges of klinengrad Which has a different representation Which looks like this Um, so you can see the bridge that was here has been destroyed which is going to help us and there's also a new bridge that's built been built down here um And now oh, sorry Now you can see that there are two islands with an even number of bridges And two islands with an odd number of bridges, which means that now if you're going to go on holiday to klinengrad You can actually do the walk going around each island Going over each bridge exactly once as long as you start on one of the odd islands and end on the other odd island For example that route there will get you around all the bridges Without going on any bridge more than once Okay So that is the related mathematical problem now. Let's go back to pac-man So we want to apply the same thing to pac-man we want to look around our pac-man route and we want to see if there are any Islands and the islands are going to build kind of the junctions like here and here and here where multiple Roots meet And we want to see if any of them are odd And if you kind of look around you can see there are an awful lot of places where there are an odd number of Roads coming in and now if we change it to a graph all the ones colors in red are odd um So the bad news for us is that for pac-man It is impossible to complete pac-man without doubling back in yourself Which is perhaps not too surprising if you've played pac-man You will know that you cannot kind of really go around it and not have to dub backing stuff at some point So it's not particularly surprising. It's impossible Right So we've found out that it's impossible to complete pac-man without doubling back on ourselves, but There's more to this question that we can still ask Because we want to know now what is the shortest route we can take to complete pac-man There's going to be some doubling back But how should we do this doubling back to make the route as short as possible? And for this we need to talk about what is called the chinese postman problem um, so this is a problem Again that is older than pac-man And the idea here was that you have a graph something like this And now you have a postman um who wants to travel around the graph And he needs to go down every street to post letters and The question is which road should he go down more than once In order to make the shortest walk possible while delivering all the letters And the way you can solve this problem um If looking at your graph if all of these were even numbers so every if every island had an even number of bridges coming off At every vertex has an even number edges coming off it you would be fine because you could go without repeating But when you have odd vertices when you have an odd number coming out of any of these nodes Then those are the ones we're going to have a problem at so what we can do is if we take all the odd vertices in pairs and match them up and like this so Where I have doubled up an edge So here and here i'm kind of saying doubling up an edge is like saying i'm going to walk down that street twice So if we change the graph to that we can now walk around that without going back on ourselves Except we are secretly going back on ourselves down These edges that we have doubled But we don't know that's the best way of doing it and actually for four odd vertices There are these three different ways of pairing up the odd edges This one even gets more complicated because there wasn't actually a road going from this island to this island So you'd have to go via one of the other islands, which is a sign that this problem isn't going to be the best solution So to solve the problem what you would do is look at all these possible ways of pairing up the edges So pairing up the vertices and then look at which one you've added the least amount of distance to and that is the one That will give you the shortest path Okay, so we can take this idea and we can throw this back at pac-man and work out what the shortest path is There is a slight problem But we're going to get over it that as the number of odd vertices increases The number of pairs that there are different pairings. You have to check increases quite quickly So if we look at pac-man If you count those there are 20 red circles, I believe Which means that to Solve pac-man we're going to have 654 million different ways of pairing up these edges to find the shortest one Which is quite a lot But if I left my laptop going for a couple of days and it managed to solve it So for this problem there are actually faster methods you can use to solve this But this wasn't a big enough problem that I bothered implementing them So you can do it slightly faster, although it still increases quite quickly as you add edges And I found that for pac-man To complete it in the shortest possible amount of time These are the edges you should repeat So you can see now that every Place where we had an odd number of lines coming into it We have now chosen something to pair it with so these are the edges that we should repeat So if the slides work we should now have a video of me doing exactly this Hopefully with sound So that was the shortest route I could take To finish a level of pac-man Except some of you out there might have noticed there's actually a few problems with this method so far And we can't just do slightly better So there are a few points most noticeably up in that corner up there Where I went a little bit too far and turned around a little bit too late And that was because I was playing with my stupid human reaction times and I was quite slow So if I did a machine assisted tool run, I could totally do it slightly faster But there are a few other problems that are a little bit subtler that we need to deal with in order to actually do the fastest level of pac-man possible So now we're going to look at some small parts of the map and see what is the best thing to do at some small parts of the map So the idea is that we're going to We want to double up all of these Little roots, but we now need to decide which order to do them in because there are some slight improvements we can make So the first one if we have a just a standard old corner So if we take screenshots kind of frame by frame and you press Down in this frame then it will take this many frames to get around that corner Whereas if you press down one frame later, it takes one extra frame to go down Which means that every time you do a corner you need to press down in the exact right frame um Luckily for us If you just hold the down button, this is the frame it will move down in So if you play pac-man by holding the arrow key before you get to the corner You are doing it in the best way possible. So the corners are actually quite easy to get perfect Okay, let's look at one of the Situation that can happen if we want to go straight on across a straight line so you might think that This is really the only way of doing this. You just let pac-man go straight on But actually there is a slight improvement you can make here Because when you turn a corner pac-man goes sideways quite quickly In fact, if you press down in that frame there and then press left again in that frame there pac-man totally skips out one frame of movement So that is if you go straight on and if you do a kind of like nudging downwards and you skip one whole frame And the game runs at 60 frames a second. So you've just saved one sixtieth of a whole second To slightly speed up your time Okay, and there are some other situations we could look at like we can look at if we've got a crossroad we can do this one or this one And the two corners is going to turn out fast if you look at the ways you do them Oh as well, I should have mentioned interestingly with this one If you're going upwards or downwards this trick doesn't work and actually slows you down So you want to only use the trick if you're going horizontally Okay now so if you've got crossroads, yeah, it turns out that this one is faster Even if you use the slight speed up trick there So whenever you've got crossroads you want to try and corner twice rather than going straight on twice Because you can see that going around the corners twice takes 50 frames whereas going straight across takes 57 frames We're talking large fractions of a second here, which is very very important Similarly if you've got this kind of thing here, so you've got a t-junction and another t-junction This one lets you skip out actually almost like a second's worth of movement here By not going over that bit twice. So we want to try and do that if we can and avoid this one And we can look at other situations So here it turns out that one is slow and these two are almost exactly the same I think they are actually exactly the same And by analyzing all the different positions in slightly more detail We can now find that this route here is the route we want to take to do pac-man in the fast way possible So we're repeating the same edges as I said before But we have now ordered them in a way so that you can see all the crossroads have two corners in them here and here We're turning around in the right places there to make it as fast as possible So this is the route we want to pac-man to take now So hopefully there is another video Yes, we have a slight problem here that Now that we have fixed a lot more so there's a lot less When we looked at before and I just said which edges I wanted to repeat There are a lot of choices for where I could go whereas here There are only about four or five different routes that take all the best options possible I tried them all and I could not find a way of doing it without bumping into a ghost So I gave up So I am actually not sure what the fastest way of completing a level of pac-man is so my talk description was a lie I don't know how to make pac-man the best way possible This is how I want to do it But I need to find some compromise between this and the other one that will be quite fast For that I need to work out how the ghost move but that is going to be something I'm going to come back another time and tell you about We're going to go on now and think about another thing that happens in pac-man So I hope everyone as well has played pac-man and is aware that if you go off here You magically appear over here and come back on the other side like so And this might seem a little bit weird because we're used to like living on a flat surface But actually we can think about this is not actually weird behavior at all if pac-man was living on a cylinder Because if we take the pac-man surface and wrap it round Then pac-man is just kind of going round around the cylinder and it makes perfect sense that this kind of behavior would happen Another video game this happens in is the amazing bubble bubble Where if you take bob off the bottom you fall back on the top Although with this one weirdly you can't then jump off the top again and come on the bottom But again, we can imagine that this game is taking place on a cylinder at this time the other way around Okay, and this again ties into a fairly famous mathematical thing that you can do Which is to take a bit of paper and kind of imagine folding up and see what surfaces we get Excuse me So I'd like to imagine that you have got a nice big flat sheet of paper And this paper is slightly stretchy so we can kind of bend it if we need to later And what we're going to do is we're going to label edges of this paper and imagine wrapping it up so the arrows line up So I'd like to imagine this is a piece of paper and you want to bend it round So that these two arrows line up and face the same direction and imagine what shape that's going to make And hopefully you won't be too surprised that if you bend this round you're going to get a cylinder So this is very much this is what pac-man does is pac-man goes off one side And he comes back on the other side having not turned upside down And we wrap around and get a cylinder although actually in fact I guess pac-man couldn't really tell if he was upside down So maybe it could be the other way up, but that is a cylinder If it is the other way up So I'd like you again to imagine that You're taking this paper. You're wrapping around but this time there's a bit of a twist in it So here you need to use the fact I said that the paper is a bit stretchy Well with actual paper you're just going to tear the paper and it's going to be really messy But with stretchy paper you're going to bend it round and you're going to get a shape like this Which is called a mobius strip If you've not played with mobius strips before I recommend going and playing with mobius strips One really fun thing to do is if you try and cut it in half Imagine like you're trying to make two mobius strips so cut one in half You'll be very surprised to see what happens Another great thing you can do is if you get two of them and glue them together then cut both of them in half Really really strange things happen. So that's something to go and play with there But of course we've so far only labeled two of the sides we could label multiple sides So here I'd like to imagine again before I show you what's going to happen if we bend this paper and to fold it up So we're bending it round It's again, it needs to be stretchy and we're not going to kind of fold it flat We're going to leave some space inside it And this one you can kind of see that it's going to make If we squish it about enough it's going to make something like a sphere With a lot of kind of bending and stretching and flattening some sort of foodie shaped like a sphere Okay, here is another one. So another way of leaving the sides here again I'd like to imagine what's happening before I show you so you're bending The red areas kind of make it like a cylinder And then you're connecting the top and the bottom Around again, and you are going to get a donut or a tourist is hopefully what everyone is imagining We're going to come back to this shape very shortly I'm just going to show you for more interesting mathematical shapes first In fact, perhaps you can think of a game that is going to become a tourist Very shortly that has this kind of relationship going on And you can make more interesting mathematical shapes So this one is a lot of mathematicians favorite shape And I'd like to imagine what happened. So again, if you take the red arrows, you can see it bending around the cylinder And now we're kind of doing like we did before except You've got to kind of like pop the other bit upside down as you make it into a donut again And you're going to get in this case what is called the Klein bottle So you can see here you kind of you got the cylinder you tried to bend it around And then you kind of had a big problem here where it went through itself So this is actually a four-dimensional shape Here in our normal three-dimensional world that can't happen because the shape can't go through itself But in four dimensions, you can kind of imagine that there's enough space in the extra dimension to kind of like bend it around and go back inside itself So by simply telling you how to fold up 2d paper, I've already made you make a four-dimensional shape And we're not limited to just folding up squares as well. You can fold up more complicated things This is one I cooked up Which looks difficult But actually if you notice the those top four are actually just the same as the torso the donut I showed you And these are the same as another torso donut So maybe not so surprisingly it folds up to make a double donut And in fact, you can make an awful lot of different mathematical surfaces like this and it's a big Area of interest in math that like you can use this method to describe the properties of surfaces quite well And it's actually quite useful Okay, back to video games So hopefully when I said if you're thinking of a game that would work like this some of you may have thought of asteroids So we all know that if you play asteroids So this is where you play as a little spaceship and you move around kind of shooting the objects And if you go off this side you magically come back on over there And if you go off the bottom you magically come back on the top So we can imagine that it works like this if you go off the sides we're kind of going to want to bend those rounds so they fit together and At the top of the bottom we can bend those rounds so they fit together as well So just like we saw when imagining our abstract paper the game of asteroids is going to bend round And bend into a tour or something like this So if you've played asteroids and think it's some kind of lame 2d game You were wrong. It's actually an amazing three-dimensional game But There is a problem still with asteroids here And anyone that's played it Will know that if you go upwards in any of these three directions, these are the same length Similarly, if you go across in any of these directions, these are also the same length But if we map it onto a tourist like we've done if you bend it into a tourist because we've done some stretching This loop around the middle is an awful lot smaller than that loop around the outside So if this was what asteroids looked like You would it would not behave in the same way as possible The spaceship would move faster towards one end of the three and the other end of the screen Which is not how asteroids behaves Um, so we need to think a little bit more carefully about what shape Asteroids is going to look like and to do this I would like you to imagine If I go back a slide I'm taking one of these directions So we're going to kind of bend it around one way and imagine just one of these lines So as we bend it around once into a cylinder We've not stretched anything to go to a cylinder Which means that everything is still going to work so that we're happy with a cylinder And each of these lines is going to fold around into a circle So I'd like you to imagine one of these lines which becomes a circle And we're now going to move that circle not like this in a different way And the next line takes a little bit of time to load So bear with my computer. Here we go. So we're taking that circle and now we're going to move the circle like What is going to happen now? Move it around like this So what we're doing is we're taking the circle and we are spinning it in a loop without rotating the circle And now if you look at different points on the circle Each point on the circle while going round is taking the same amount of distance because we're not rotating the circle at all Each point moves by the same amount as we go around this circle Which means that if we make if we look at the shape that this circle traces out as we move it around This will represent asteroids because at every point on the loop Our rings our directions in one way are going to say the same length And our directions the other way are also all the same length going this way So let's trace out the shape and see what we get Go something like this We get this shape and hopefully as you go around somebody we're going a little bit cross-eyed about that point And the same point at the bottom because it is actually going a little bit weird there if I just put the whole thing up And what happens is it's all looking fine until at this point at the top and this point at the bottom Where the circle kind of passes back through where it's already gone somewhere before And that is because this shape is impossible in three dimensions So just like the climb bottle I showed you a short while ago You cannot make this in three dimensions Because As you go down the bottom the shape then has to kind of cut through itself again Which is why our normal tourists doesn't have this nice property of having the same distance everywhere. So again This is a four-dimensional shape because in four dimensions you have that extra bit of space You can kind of like just kind of take it into the fourth dimension round itself and then put it back there so We have just worked out that Asteroids is not a rubbish 2d game or a rubbish 3d game Asteroids is actually an incredible four-dimensional game and probably the first four-dimensional game Which is surprising because it seems like it's kind of two-dimensional and boring But asteroids is definitely played on the surface of this four-dimensional tourists Um, okay. There are some other games that are played on four-dimensional tourists One of my favorite games ever the Final Fantasy series. This is a The map from Final Fantasy 8 Anyone who's played it when you get to the world map will know that Excuse me When you get to the final disk and you've got kind of the spaceship You can fly off the top and come back on the bottom and you can fly off the side and come back on that side Just the same as asteroids and which means that Again, you could label it like this just like we did and you will get the tourists But again, because we know what we know about Asteroids because all the lines this way take the same amount of time to go across and these lines take the same way to go across Final Fantasy 8 and all the other Final Fantasy games on the PlayStation 1 were all also played on the surfaces of a four-dimensional tourists Um, which makes it super embarrassing when you look at this screenshot and they've drawn a sphere Which is entirely incorrect and you should all write to the game developers and complain Um, okay, so that is where I'm going to leave you today. Thank you very much for listening. I think I have some time for questions Thank you very much Matthew groggs groggs So there's still some time left for qna Please queue here Or on the other microphone in the back Like you can stay here Okay, so uh with your graph representation of the pacman map You did not add edges for the Uh paths that didn't have dots Yes, let me go back But it looks like at least in one instance it would be Quicker to take one of those paths then to backtrack Through two other paths So have you thought about Doing that to maybe find a quick away. So you're saying you're probably looking at this one here It looks like it's shorter to pop down there. Um, which is I haven't included in the graph. I showed you here where I have I've left those two detached. Um, so yeah, I did look into that The problem you have is if you connect those two up, um If we go, uh, it's a long way forwards There we go. Whoops Look at this one if you connect those two up You end up being left with having to connect kind of like this one One of the ones over here with one the ones over here So what looks there like a good idea means you have an odd number of Of nodes over there an odd number nodes over here and you have to have a long one going all the way across the top Which ends up ruining that being a good idea Okay, but like there might be another one of those paths might be might be quicker I have you like have you actually proven that this is the case for all these edges So I went through every pair and looked at all the different routes between Taking every pair looked all the shortest routes but all the different routes between every route and found the shortest one So I think I have done that but I may have made a mistake So any more questions? We have still a lot of time left You were very fast. Yeah, you're running through your slides ready to go to bed. It's late Okay, so thank you again And we have a late question. Oh, so it's another question. Yeah. Hi. I have a question about asteroids Why is it specifically a 4d torus when it looks like a sphere could work just as well? So if you could explain this to us mere mortals, that would be great. Um, yes, sure This one's picture down here Okay, cool. Put the picture up there. Um, okay. So the idea is that with asteroids because When you go off the side you come back on the other side, um, that means that When folding this into a shape you kind of have to take this side here and kind of fold it round So it matched up with that side and in that way you're going to get a cylinder like a tube But now because the top and the bottom which have now been wrapped into circles When you go off the top you come back on the bottom You've got to bend it around so those two circles touch each other Which means that you've got to take your cylinder and kind of bend the ends around to go together If you made it a sphere Then like if you imagine this was a map of the world Um, when you go off the top of the arctic on the world, you don't come back on at the Antarctic Um, and that'd be the problem if they wanted asteroids to be on a sphere You would have to go off the top and come back on a different part of the top um Because you can't you can't draw any map of the actual world where Like on the actual world you do go off the side and come back on that side But you cannot ever go off the top and come back on the bottom Which is what leads it to having to bend around into a torus Okay, we we we can talk afterwards if you like and I can draw some pictures So But that basically means if you keep this the distance the same, but you would automatically speed up the ship Uh when it goes through these where you change the because the um Uh asteroids only had one speed like go and stop Yeah, basically if you would adjust the go speed to the place on the map You could make it a three-dimensional game again But by just sort of like taking space taking space and falling it a bit but keeping it spatially the same if you know what I mean Yeah, so you could you could definitely represent it flat again Just like the like the planet earth if you kind of run around the equator and run around the north pole Yeah, you get very different speeds on there will be kind of fun. I would like to see what it will do to the game dynamics Yeah, if anyone here wants to program asteroids for the badge, please make one that's actually on sphere I would love to play asteroids on a climb bottle as well where you go off one side and come back And then if you go off the top there you come back on here So you're actually playing on a climb bottle that would be really cool Okay, thank you. Lovely talk. Thank you so much. Thank you So any more questions? I'll say another one So what ideas have you got for how you can account for the ghosts in not computing the octopac man's solution? Um, so the first thing I need to do is I need to read up on what the ghosts actually do Because they do follow predefined patterns And if you work out what the ghosts are going to do you can then You could plan the route while at the same time planning where the ghosts are going to go And kind of make your decisions of which thing to do based on where you know, the ghosts will go next And try and work out a short route that avoids the ghosts Um, I think it's going to be a lot of work, but hopefully in four years time I can come back and show you the video You don't necessarily have to avoid the ghosts if you can get to the big dots Before meeting them Yes, and actually that was one of the problems I had trying to do it. Oh, that's weird I'm just scrolling up and it can't deal with it I'll stop now Yeah, so the problem I had with the dots was when you get to this dot So I did I I have some I didn't take a video of where I managed to get slightly further And the problem is from there You have to go all the way around the top all the way down here all the way around that top all the way down here And all the way up there and there's no other way of doing between that big dot and that big dot That doesn't take in all of that route there and that is way too long But when the ghosts always get you along the top If you do the perfect route, so I reckon to do it we need to make some adjustment at the top So that those dots are slightly closer together in time because there is just too much distance at the top Please go a bit more close to the micro It doesn't work Okay, no this better. No, so even though this particular attempt didn't work presumably this gives you a lower bound of how fast you could do it so What what is actually the The margin between this lower bound and the upper bound that you did find That is a very good question. I've totally not counted the number of frames it takes I come find me tomorrow and I will work that out for you. Um, there's Probably not very much in it. Like most of the improvements I've made are a few frames here and there So it's probably like less than a second in it. Um, so we're not far off doing it perfectly So any more questions we have still time left. Yes There uh, uh, would it be possible to calculate gravity force between two asteroids analytically The the of calculating what sorry can this go a bit closer to the micro Louder is it possible to get the gravity force between two asteroids analytically Probably I haven't done it, but I Wouldn't be surprised if you could Talk to me at the end and we can see if we can work it out tomorrow or something Oh one more question and then I would close the line for the day You mentioned with bubble bubble that one could go down the openings in the bottom and you'd then fall onto the screen from the top You also said that you can't then jump up From the top and appear at the bottom But have you tried blowing a bubble and bouncing on top of the bubble to get the extra heights? And I think you might be able to come up through the bottom at that point Can you um in that case I take like everything I've said about bubble bubble and it's way deep And I thought it was um, so that simply means that I guess that this This distance here is probably just larger than I gave it account for there And I just need to expand that distance and then it will be accurate. Um, thanks. I'll change the slide Okay, thank you again Matthew Scrozz Thank you Was a beautiful talk. Thank you