Play Pacman with Selenium 2
Posted: May 25th, 2010 | Author: Dave | Filed under: Challenges, Examples | Tags: challenge, example, google, selenium 2, webdriver | 27 Comments »When I saw Google’s recent interactive Pacman doodle to celebrate the game’s 30th anniversary, my first thought was ‘Wouldn’t it be cool to automate playing Pacman using Selenium’. I know – I’m a geek!
Anyway, below is my quick proof of concept using Selenium 2 alpha 4 – the latest version of Selenium 2 is available for download here.
package tests; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.Keys; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; public class WebDriverScratchPad { public static final long PAUSE = 2000; public static final Keys UP = Keys.ARROW_UP; public static final Keys DOWN = Keys.ARROW_DOWN; public static final Keys LEFT = Keys.ARROW_LEFT; public static final Keys RIGHT = Keys.ARROW_RIGHT; public static WebElement controls; public static void main(String [ ] args) throws InterruptedException { WebDriver driver = new FirefoxDriver(); driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); driver.get("http://www.google.com/pacman/"); controls = driver.findElement(By.id("pcm-c")); move(LEFT, UP, LEFT, DOWN, LEFT, UP, LEFT, DOWN, RIGHT); } private static void move(Keys... directions) { for (Keys direction : directions) { controls.sendKeys(direction); try { Thread.sleep(PAUSE); } catch (InterruptedException e) { e.printStackTrace(); } } } }
Try it out for yourselves, and I extend these challenges to you:
- Specify a distance for Pacman to travel rather than using an arbitrary pause
- Detect if Pacman dies in his quest to eat all the pills
- Work out the score at the end of the game
All of the above should be achievable… let me know how you get on. Also let me know what high scores you achieve playing Pacman with Selenium 2!

Excellent idea! I don’t even want to start trying it out because I’m sure I’d end up spending days till I get the right combination to win a whole game XD
It is tempting!
I don’t know much about Selenium2, but I have been able to automate the interactions and notification of ‘a ghost in near!’ using phpUnit and Selenium Core.
Next – giving Pacman the knowledge of ‘if i am in a corner’, I can only go in certain locations, plus some notion of where should I really be going.
In short, yup – this can be done. The hardest part is coming up with proper logic on ‘how do you play pacman’.
Fun times ahead!
I did it with Watir:
http://github.com/zeljkofilipin/pacman
There is a short video, too:
http://www.youtube.com/watch?v=r9Y6RtKiTQs
For now Pacman just randomly goes in all directions.
Excellent! Would it improve if you limited it so that Pacman didn’t go back on himself?
Dave, I am not sure what you mean by “so that Pacman didn’t go back on himself”. At the moment, he just randomly goes in all directions. If I get some time these days, I will try to avoid ghosts.
I just mean, when you randomly pick a direction, don’t pick the one that is directly opposite your current direction, that way you should cover less of the same ground and get more pills.
Dave, I get it now. There is definitely a lot of space for improvements.
I am thinking what is the simplest thing besides moving randomly (and without hardcoding where to go) that would get more pills. Not moving in directly opposite direction sounds easy enough. Maybe also not moving down if already at the bottom (and same for the top). Detecting walls, pills and ghosts would probably help too.
If you solve it I’ll buy you a beer next time you’re in town!
Wow! It is a interesting idea and it is fun. I am a user of watir, though I am not familiar with Selenium. aha~~ Good!
Now I _have to_ solve the first level.
I am in Croatia, so I doubt I will be near you soon, but you never know. I could not find where are you located. (I wanted to see which beers are brewed there.)
I’m in London. A couple of favourite ‘local’ beers of mine are from Fuller’s (http://en.wikipedia.org/wiki/Fuller's_Brewery) and Meantime (http://en.wikipedia.org/wiki/Meantime_Brewery). Now I fancy a pint myself!
I’ve actually worked on this some more myself. My current high score is 1880! I’ll post a follow up soon.
Just got up to 1430!
This is going to be great conference showoff material.
I took a look at your code, looks a lot similar to mine. Did you use my code for inspiration, or do we just think the same?
(I am not a Java guy, I could be wrong about the code being similar.)
Score 1490
http://www.youtube.com/watch?v=DV_whwOwVbk
Got to 3150, but I did not record that time.
2280
http://www.youtube.com/watch?v=KzuTKQgoY3U
What have I started?!
I’ve made a few improvements and my new high score is 2660. I told myself when I got over 2000 I’d post an update, but you’ve set the bar a little higher. When (not if
) I beat 3150 I’ll commit my changes and update with a new post. I have a few tweaks stil to make. I’ll also try to upload a video.
I will modify my script to start over after game over (now it just ends) and record it for a few hours. I hope I will record going over 3000.
What is needed to be able to have an observation of the ghosts position. It shouldn’t be that hard to devise a path finding algorithm that handles ghosts and dots that are not yet collected. I suppose you could probably find the ghosts with some sort of DOM introspection?
Finding out where the ghosts are is easy, writing the logic so that Pacman avoids them without making him so paranoid that he doesn’t take risks is the tough part!
Daniel, in my Watir implementation (http://github.com/zeljkofilipin/pacman) pacman at the moment knows where it is, where it can move and which moves have dots. The next step is probably to track the closest ghost and running away from it.
Tracking all that stuff slows it down. It sometimes takes him a second or two to inspect stuff and make a move, and that is not fast enough.
hey
I try to run this example in my pc, but selenium can’t talk with firefox, it returns an error “Failed to connect to binary FirefoxBinary”. do you know why?
the firefox is started, and I can see the webdriver, and I can see the error message in js console, “Warning: unrecognized command line flag – verbose”
will u please help me?
Notus:
I’m not sure what might be causing your issue – perhaps you could ask in the #selenium channel on IRC (freenode) or on StackOverflow using the ‘selenium’ and ‘webdriver’ tags, or in the official Google Groups for Selenium or WebDriver. Hope you find a solution to your issue.
http://groups.google.co.uk/group/selenium-users
http://groups.google.co.uk/group/webdriver
It would be interesting to write a heuristic for pacman to see if you could simulate a simplistic human AI.
Given the logical setup of the grid you could base it on threat detection based on range and number of ghosts to create a path of least obstruction or optimum distance.
Seeing how most people don’t really try to play pacman in the mode of predicting the ENTIRE game they judge it on short term goals and reflex’s this would work quite well.
Hi,
I have copied your code and trying to run from Eclipse. But I get the following error.
Exception in thread “main” org.openqa.selenium.NoSuchElementException: Unable to locate element: {“method”:”id”,”selector”:”pcm-c”}
Can you please help me?
Thanks,
Ramdas
Unfortunately it looks like Google have disabled the Pacman game.