Skip to content

Instantly share code, notes, and snippets.

@SimonHauguel
Created January 10, 2022 23:05
Show Gist options
  • Save SimonHauguel/de90c5c16aa1b5a6c6788f5f4f8cbb10 to your computer and use it in GitHub Desktop.
Save SimonHauguel/de90c5c16aa1b5a6c6788f5f4f8cbb10 to your computer and use it in GitHub Desktop.
Snake Game as a single expression
_ = (lambda curses, random: lambda SOUTH, NORTH, WEST, EST, FRUIT, SNAKE_BODY, BORDER_TOP_BOTTOM, BORDER_LEFT_RIGHT:(lambda Snake: (lambda SnakeView: curses.wrapper(lambda screen: (lambda view: (not curses.curs_set(0)) or [None for _ in iter(view.actualize_and_catch_events, -1)])(SnakeView(Snake(), screen))))(type("SnakeView", (), {"__init__": lambda self, game, screen: setattr(self, "game_state", game) or setattr(self, "screen", screen) or screen.nodelay(True) or screen.resize(15, 40),"actualize_and_catch_events":lambda self:(lambda actual_event:(self.screen.erase() or ((-1 if self.game_state.next_step(actual_event - 258) == -1 else None) if (258 <= actual_event <= 261) else (-1 if self.game_state.next_step(None) == -1 else None))))(self.screen.getch()) or ([(self.screen.addch(0, i, BORDER_TOP_BOTTOM), self.screen.addch(9, i, BORDER_TOP_BOTTOM)) for i in range(0, 40)] and [(self.screen.addch(i, 0, BORDER_LEFT_RIGHT), self.screen.addch(i, 39, BORDER_LEFT_RIGHT)) for i in range(1, 10)] and [self.screen.addch(i, j, FRUIT) for i, j in self.game_state.all_fruits] and [self.screen.addch(i, j, SNAKE_BODY) for i, j in self.game_state.body] and None) or self.screen.refresh() or self.screen.timeout(100)})))(type("Snake", (), {"__init__": lambda self: None,"next_direction": EST,"body": [(5,10), (5,11), (5,12), (5,13)],"all_fruits" : {(random.randint(1,9), random.randint(1,38))for _ in range(10)},"next_step" : ( lambda self, direction: (setattr(self, "next_direction", direction) if (direction is not None) else None) or (lambda headx, heady: (self.body.append((headx-1, heady)) if self.next_direction == NORTH and headx != 1 else (self.body.append((headx+1, heady)) if self.next_direction == SOUTH and headx != 9 else (self.body.append((headx, heady+1)) if self.next_direction == EST and heady != 39 else (self.body.append((headx, heady-1)) if self.next_direction == WEST and heady != 1 else 1)))))(self.body[-1][0], self.body[-1][1]) or (self.body.pop(0) if not self.body[-1] in self.all_fruits else self.all_fruits.remove(self.body[-1]) or self.all_fruits.add((random.randint(1,9), random.randint(1,39)))))})))(__import__("curses"), __import__("random"))(0,1,2,3, ord('O'), ord('#'), ord('_'), ord('|'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment