Skip to content

Instantly share code, notes, and snippets.

@albert-tomanek
Last active February 15, 2021 18:37
Show Gist options
  • Save albert-tomanek/18ee8c6590cab148d8c4174aefb91a3d to your computer and use it in GitHub Desktop.
Save albert-tomanek/18ee8c6590cab148d8c4174aefb91a3d to your computer and use it in GitHub Desktop.
Conway's Game of Life and Death for Casio fx-CG50
# screen size: 21x7; Albert Tomanek (8B) 2021-02-15 http://github.com/albert-tomanek
import random
def screen(ls):ls=ls.split('|');[print(' '*(10-len(l)//2)+l) for l in ls];[print() for l in range(6-len(ls))];return input();
ltoi= lambda l:[int(x)for x in l]
screen("|Conway's Game|of Life and Death|By Albert Tomanek||Press ENT")
screen("|For instructions|visit: ||long.af/mhnjsz ")
screen("Enter coordinates| to toggle a cell,|or blank to run a|cycle. '*' starts|randomly. Use 'x'|to quit. ")
board = [[' ']*20 for q in range(6)]
toggle={'#':' ',' ':'#'}
live={'#':1,' ':0}
def jn(s): return ''.join(s)
def tl(col, row): return live[board[row][col]] if 0 <= col < len(board[0]) and 0 <= row < len(board) else 0
def lneighbs(col, row): return tl(row-1,col)+tl(row-1,col-1)+tl(row,col-1)+tl(row+1,col-1)+tl(row+1,col)+tl(row+1,col+1)+tl(row,col+1)+tl(row-1,col+1)
while True:
print(' 01234567890123456789')
i=0; [print(str(i)+jn(board[i])) for i in range(len(board)-1)]
c=input('5'+jn(board[-1][:-8])+' ')
try:
if c=='x': break
if c=='+': board=[[toggle[col] for col in row]for row in board];
elif c=='*': board=[[random.choice("# ") for col in row]for row in board];
elif c!='':
x,y=ltoi(c.split('.') if '.' in c else c.split(','))
board[y][x]=toggle[board[y][x]]
else:
board=[[('#' if 2 <= lneighbs(row, col) <= 3 else ' ') if live[board[row][col]] else ('#' if lneighbs(row, col) == 3 else ' ') for col in range(20)]for row in range(6)];
except Exception as err: pass # Don't crash on invalid input
# Albert Tomanek (8B) 2021-02-15 http://github.com/albert-tomanek
import random
def screen(ls):ls=ls.split('|');[print(' '*(10-len(l)//2)+l) for l in ls];[print() for l in range(6-len(ls))];return input();
ltoi= lambda l:[int(x)for x in l]
screen("|Conway's Game|of Life and Death|By Albert Tomanek||Press ENT")
screen("|For instructions|visit: ||long.af/mhnjsz ")
screen("Enter coordinates| to toggle a cell,|or blank to run a|cycle. '*' starts|randomly. Use 'x'|to quit. ")
board = [[' ']*20 for q in range(12)]
toggle={'#':' ',' ':'#'}
live={'#':1,' ':0}
visu={(' ','#'): '.', ('#','#'): ':', ('#',' '): "'", (' ',' '): ' '}
def jn(s): return ''.join(s)
def tl(col, row): return live[board[row][col]] if 0 <= col < len(board[0]) and 0 <= row < len(board) else 0
def lneighbs(col, row): return tl(row-1,col)+tl(row-1,col-1)+tl(row,col-1)+tl(row+1,col-1)+tl(row+1,col)+tl(row+1,col+1)+tl(row,col+1)+tl(row-1,col+1)
while True:
print(' 01234567890123456789')
i=0; [print(str(row)[-1]+jn([visu[(board[row][col], board[row+1][col])] for col in range(len(board[row]) if row != 12-2 else len(board[row]) - 8) ]), end=''if row==12-2 else '\n') for row in range(0, len(board), 2)]
c=input()
try:
if c=='x':break;
elif c=='+': board=[[toggle[col] for col in row]for row in board];
elif c=='*': board=[[random.choice("# ") for col in row]for row in board];
elif c!='':
x,y=ltoi(c.split('.') if '.' in c else c.split(','))
board[y][x]=toggle[board[y][x]]
else:
board=[[('#' if 2 <= lneighbs(row, col) <= 3 else ' ') if live[board[row][col]] else ('#' if lneighbs(row, col) == 3 else ' ') for col in range(20)]for row in range(12)];
except Exception as err: pass # Don't crash on invalid input
# Albert Tomanek (8B) 2021-02-15 https://gist.github.com/albert-tomanek/18ee8c6590cab148d8c4174aefb91a3d
import random
def screen(ls):ls=ls.split('|');[print(' '*(10-len(l)//2)+l) for l in ls];[print() for l in range(6-len(ls))];return input();
ltoi= lambda l:[int(x)for x in l]; low_res=None
screen("|Conway's Game|of Life and Death|By Albert Tomanek||Press ENT")
screen("|For instructions|visit: ||long.af/mhnjsz ")
screen("Enter coordinates| to toggle a cell,|or blank to run a|cycle. '*' starts|randomly. Use 'x'|to quit. ")
while low_res != '1' and low_res != '2': low_res = screen("|Select resolution ||(1) Low-res |(2) Hi-res |")
low_res = (low_res == '1'); nrows = 6 if low_res else 12; last = 5 if low_res else 10; # Last printed row
board = [[' ']*20 for q in range(nrows)]
toggle={'#':' ',' ':'#'}; live={'#':1,' ':0};
visu={(' ','#'): '.', ('#','#'): ':', ('#',' '): "'", (' ',' '): ' '}
def jn(s): return ''.join(s)
def tl(col, row): return live[board[row][col]] if 0 <= col < len(board[0]) and 0 <= row < len(board) else 0
def lneighbs(col, row): return tl(row-1,col)+tl(row-1,col-1)+tl(row,col-1)+tl(row+1,col-1)+tl(row+1,col)+tl(row+1,col+1)+tl(row,col+1)+tl(row-1,col+1)
while True:
print(' 01234567890123456789')
[print(str(row)[-1]+jn([(board[row][col] if low_res else visu[(board[row][col],board[row+1][col])])for col in range(len(board[row])if row!=last else len(board[row]) - 8)]), end=''if row==last else'\n') for row in range(0,len(board),1 if low_res else 2)]
c=input()
try:
if c=='x':break;
elif c=='+': board=[[toggle[col] for col in row]for row in board];
elif c=='*': board=[[random.choice("# ") for col in row]for row in board];
elif c!='':
x,y=ltoi(c.split('.') if '.' in c else c.split(','))
board[y][x]=toggle[board[y][x]]
else:
board=[[('#' if 2 <= lneighbs(row, col) <= 3 else ' ') if live[board[row][col]] else ('#' if lneighbs(row, col) == 3 else ' ') for col in range(20)]for row in range(nrows)];
except Exception as err: pass # Don't crash on invalid input
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment