Skip to content

Instantly share code, notes, and snippets.

@mauricioaniche
Created November 25, 2020 13:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mauricioaniche/08ed233b1dd4fc6029976282a41d729b to your computer and use it in GitHub Desktop.
Save mauricioaniche/08ed233b1dd4fc6029976282a41d729b to your computer and use it in GitHub Desktop.
# @ -> our hero
# G -> ghosts
# P -> pills
# . -> empty spaces
# | and - -> walls
map = [
"|--------|",
"|G..|..G.|",
"|...PP...|",
"|G...@.|.|",
"|........|",
"|--------|"
]
def find_pacman(map):
pacman_x = -1
pacman_y = -1
for x in range(len(map)):
for y in range(len(map[x])):
if map[x][y] == '@':
pacman_x = x
pacman_y = y
return pacman_x, pacman_y
def move_pacman(map, next_pacman_x, next_pacman_y):
pacman_x, pacman_y = find_pacman(map)
# the place where the pacman was is now empty
everything_to_the_left = map[pacman_x][0:pacman_y]
everything_to_the_right = map[pacman_x][pacman_y+1:]
map[pacman_x] = everything_to_the_left + "." + everything_to_the_right
# the new place has the pacman
everything_to_the_left = map[next_pacman_x][0:next_pacman_y]
everything_to_the_right = map[next_pacman_x][next_pacman_y+1:]
map[next_pacman_x] = everything_to_the_left + "@" + everything_to_the_right
import unittest
from pacman import find_pacman, move_pacman
class PacmanTest(unittest.TestCase):
def test_find_pacman(self):
map = [
"|--------|",
"|G..|..G.|",
"|...PP...|",
"|G...@.|.|",
"|........|",
"|--------|"
]
x, y = find_pacman(map)
self.assertEqual(x, 3)
self.assertEqual(y, 5)
def test_find_pacman_when_pacman_doesnt_exist(self):
map = [
"|--------|",
"|G..|..G.|",
"|...PP...|",
"|G.....|.|",
"|........|",
"|--------|"
]
x, y = find_pacman(map)
self.assertEqual(x, -1)
self.assertEqual(y, -1)
def test_move_pacman(self):
map = [
"|--------|",
"|G..|..G.|",
"|...PP...|",
"|G...@.|.|",
"|........|",
"|--------|"
]
move_pacman(map, 4, 1)
new_x, new_y = find_pacman(map)
self.assertEqual(new_x, 4)
self.assertEqual(new_y, 1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment