Skip to content

Instantly share code, notes, and snippets.

@xpmatteo
Last active June 28, 2016 08:35
Show Gist options
  • Save xpmatteo/c235eefba574a74a55da2fffa471fbb3 to your computer and use it in GitHub Desktop.
Save xpmatteo/c235eefba574a74a55da2fffa471fbb3 to your computer and use it in GitHub Desktop.
class Pacman
attr_accessor :screen
def initialize(starting_screen)
@screen = starting_screen
end
def evolve!
if (dash_index = @screen.index '-')
@screen[dash_index] = '<'
elsif (dot_index = screen.index '.')
@screen[dot_index] = '-'
@screen[dot_index-1] = ' '
else
@screen = nil
end
end
end
pacman = Pacman.new('<.....................')
while pacman.screen
print pacman.screen + "\r"
pacman.evolve!
sleep 0.3
end
$:.unshift File.dirname(__FILE__) + '/../lib'
require 'minitest/autorun'
require 'pacman'
class PacmanTest < Minitest::Test
def evolve screen
pacman = Pacman.new(screen)
pacman.evolve!
pacman.screen
end
def test_end
assert_equal nil, evolve('XXX>')
end
def test_open_mouth
assert_equal '<', evolve('-')
assert_equal ' <', evolve(' -')
end
def test_eat_one_dot
assert_equal ' -', evolve('<.')
# 1234 1234
assert_equal ' -.', evolve(' <..')
end
end
@xpmatteo
Copy link
Author

Run with
ruby pacman.rb

@xpmatteo
Copy link
Author

You'll have to imagine the "waka-waka" sound :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment