Play Pacman with Selenium 2

Posted: May 25th, 2010 | Author: | Filed under: Challenges, Examples | Tags: , , , , | 29 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!

Google's Pacman Doodle

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);
		controls = driver.findElement("pcm-c"));
	private static void move(Keys... directions) {
		for (Keys direction : directions) {
			try {
			} catch (InterruptedException e) {

Try it out for yourselves, and I extend these challenges to you:

  1. Specify a distance for Pacman to travel rather than using an arbitrary pause
  2. Detect if Pacman dies in his quest to eat all the pills
  3. 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!

29 Comments on “Play Pacman with Selenium 2”

  1. 1 Santiago said at 7:25 pm on May 25th, 2010:

    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

  2. 2 Dave said at 9:55 am on May 26th, 2010:

    It is tempting! :)

  3. 3 Trimper said at 10:02 pm on May 26th, 2010:

    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!

  4. 4 Željko Filipin said at 12:34 pm on May 27th, 2010:

    I did it with Watir:

    There is a short video, too:

    For now Pacman just randomly goes in all directions.

  5. 5 Dave said at 12:40 pm on May 27th, 2010:

    Excellent! Would it improve if you limited it so that Pacman didn’t go back on himself?

  6. 6 Željko Filipin said at 3:39 pm on May 27th, 2010:

    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. :)

  7. 7 Dave said at 3:56 pm on May 27th, 2010:

    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.

  8. 8 Željko Filipin said at 4:27 pm on May 27th, 2010:

    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.

  9. 9 Dave said at 4:31 pm on May 27th, 2010:

    If you solve it I’ll buy you a beer next time you’re in town! :)

  10. 10 James said at 4:17 am on May 28th, 2010:

    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!

  11. 11 Željko Filipin said at 9:05 am on May 28th, 2010:

    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.)

  12. 12 Dave said at 9:16 am on May 28th, 2010:

    I’m in London. A couple of favourite ‘local’ beers of mine are from Fuller’s ('s_Brewery) and Meantime ( Now I fancy a pint myself! :)

  13. 13 Dave said at 3:12 pm on May 28th, 2010:

    I’ve actually worked on this some more myself. My current high score is 1880! I’ll post a follow up soon.

  14. 14 Željko Filipin said at 10:34 pm on May 28th, 2010:

    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.)

  15. 15 Željko Filipin said at 11:48 pm on May 28th, 2010:

    Score 1490

  16. 16 Željko Filipin said at 11:50 pm on May 28th, 2010:

    Got to 3150, but I did not record that time. :(

  17. 17 Željko Filipin said at 12:27 am on May 29th, 2010:

    2280 :)

  18. 18 Dave said at 7:59 am on May 29th, 2010:

    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.

  19. 19 Željko Filipin said at 9:11 am on May 29th, 2010:

    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. :)

  20. 20 Daniel Honig said at 3:07 am on June 2nd, 2010:

    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?

  21. 21 Dave said at 9:28 am on June 2nd, 2010:

    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!

  22. 22 Željko Filipin said at 11:15 pm on June 2nd, 2010:

    Daniel, in my Watir implementation ( 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.

  23. 23 Notus said at 8:26 am on June 25th, 2010:

    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?

  24. 24 Dave said at 3:13 pm on June 30th, 2010:


    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.

  25. 25 Martin Smith said at 1:23 pm on October 21st, 2010:

    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.

  26. 26 Ramdas Singh said at 5:44 pm on March 7th, 2011:


    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?



  27. 27 Dave said at 3:14 pm on March 10th, 2011:

    Unfortunately it looks like Google have disabled the Pacman game. :(

  28. 28 Tarun said at 6:28 am on July 11th, 2012:

    It’s sad to see that your post has been copied word by word on –

    You may like to file a complaint at –

  29. 29 Dave said at 1:21 pm on July 20th, 2012:

    Thanks Tarun, I have reported this unauthorized republishing of my article.

Leave a Reply