Created
November 11, 2022 20:22
-
-
Save Sigmanificient/bda86d44296d2d44af5bc7f017a26021 to your computer and use it in GitHub Desktop.
Lights out in 1 line
This file contains hidden or 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
print("Light out in 1 line, use A1 to E5 to play (A-E vertical, 1-5 horizontal)"*0,type('',((__:=setattr)and(BS:=5)and(r:=__import__('random'))and()),{'__init__':lambda s:(__(s,'b',[[0 for _ in range(BS)]for _ in range(BS)])or __(s,'ir',0)or s.sb())and None,'__iter__':lambda s: s,'__next__':lambda s: s.ir,'__repr__':lambda s:'\n'.join(' '.join('.x'[c]for c in line)for line in s.b),'main':lambda s:(__(s,'ir',0),[(print(s),(s.hp(),s.cw()))for _ in iter(s.__next__, 1)], "You won!")[2],'c':property(lambda s:sum(map(sum,s.b))),'cp':lambda _,cs:(len(cs) == 2 and cs[0]in'ABCDE'and cs[1] in '12345'),'cw':lambda s:(__(s,'ir',s.iw)),'iw':property(lambda s: (s.c==0)),'hp':lambda s:(s.cp(p:=input("Where to play? "))and(s.sl(*s.tc(p)))),'sb':lambda s:[s.sl(r.randrange(0,BS),r.randrange(0,BS))for _ in range(100)],'sl':(lambda s,x,y:[s.sw(x+dx,y+dy)for dx,dy in((0,0),(0,-1),(0,1),(-1,0),(1,0))if((0<=(x+dx)<BS)and(0<=(y+dy)<BS))]),'sw':lambda s,x,y:(__(s,'b',[[c if((cx!=x)or(cy!=y))else(not(s.b[y][x]))for cx,c in enumerate(l)]for cy,l in enumerate(s.b)])),'tc':lambda _,p:(ord(p[0])-ord('A'),int(p[1])-1)})().main()) |
This file contains hidden or 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
import random | |
BOARD_SIZE = 5 | |
class Game: | |
def __init__(self): | |
self.board = [ | |
[False for _ in range(BOARD_SIZE)] | |
for _ in range(BOARD_SIZE) | |
] | |
self.setup_board() | |
self.is_running = False | |
def main(self): | |
self.is_running = True | |
while self.is_running: | |
print(self) | |
play = input("Where to play? ") | |
if check_play(play): | |
x, y = translate_to_coord(play) | |
self.switch_light(x, y) | |
if self.is_won: | |
print("You won!") | |
self.is_running = False | |
@property | |
def count_on(self): | |
return sum(map(sum, self.board)) | |
@property | |
def is_won(self): | |
return self.count_on == 0 | |
def switch_light(self, x, y): | |
print(x, y) | |
self.board[y][x] = not self.board[y][x] | |
for dx, dy in ((0, -1), (0, 1), (-1, 0), (1, 0)): | |
c_x = x + dx | |
if c_x < 0 or c_x >= BOARD_SIZE: | |
continue | |
c_y = y + dy | |
if c_y < 0 or c_y >= BOARD_SIZE: | |
continue | |
self.board[c_y][c_x] = not self.board[c_y][c_x] | |
def setup_board(self): | |
acceptable_range = 0 | |
av_count = (BOARD_SIZE ** 2) // 2 | |
min_move = 10 | |
while abs(av_count - self.count_on) > acceptable_range or min_move > 0: | |
min_move -= 1 | |
self.switch_light( | |
random.randrange(0, BOARD_SIZE), | |
random.randrange(0, BOARD_SIZE), | |
) | |
def __repr__(self): | |
return '\n'.join( | |
' '.join('.x'[c] for c in line) | |
for line in self.board | |
) | |
def translate_to_coord(play): | |
return ord(play[0]) - ord('A'), int(play[1]) - 1 | |
def check_play(cell_string): | |
return ( | |
len(cell_string) == 2 | |
and cell_string[0] in 'ABCDE' | |
and cell_string[1] in '12345' | |
) | |
def main(): | |
game = Game() | |
game.main() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment