Last active
June 28, 2016 08:35
-
-
Save xpmatteo/c235eefba574a74a55da2fffa471fbb3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$:.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 |
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
Run with
ruby pacman.rb