Advertisement
Guest User

Untitled

a guest
Aug 27th, 2014
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.61 KB | None | 0 0
  1. <html><body onload="k=40;g=10;b=[];setInterval(function(){c=[];for(y=k*k;y--;){n=0;for(f=9;f--;)n+=b[(~~(y/k)+k+f%3-1)%k*k+(y+k+~~(f/3)-1)%k];c[y]=n==3||n-b[y]==3;r.fillStyle=b[y]?'red':'tan';r.fillRect(y%k*g,~~(y/k)*g,g-1,g-1)}if(v.nextSibling.checked)b=c},1);v=document.body.firstChild;v.width=v.height=g*k;v.addEventListener('click',function(e){b[~~((e.pageY-v.offsetTop)/g)*k+~~((e.pageX-v.offsetLeft)/g)]^=1},0);r=v.getContext('2d');for(y=k*k;y--;)b[y]=0"><canvas></canvas><input type="checkbox"/>Run</body></html>
  2.  
  3. import time
  4. P=input()
  5. N=range(20)
  6. while 1:
  7. for i in N:print''.join(' *'[i*20+j in P]for j in N)
  8. time.sleep(.1);Q=[(p+d)%400 for d in(-21,-20,-19,-1,1,19,20,21)for p in P];P=set(p for p in Q if 2-(p in P)<Q.count(p)<4)
  9.  
  10. echo "[8,29,47,48,49]" | ./life.py
  11.  
  12. Manipulate[x=Switch[run,1,x,2,CellularAutomaton[{224,{2,{{2,2,2},{2,1,2},{2,2,2}}},
  13. {1,1}},x],3,Table[0,{k,40},{j,40}]];EventHandler[Dynamic[tds=Reverse[Transpose[x]];
  14. ArrayPlot[tds,Mesh->True]],{"MouseClicked":>(pos=Ceiling[MousePosition["Graphics"]];
  15. x=ReplacePart[x,pos->1-x[[Sequence@@pos]]];)}],{{run,3,""},{1->"||",2->">",3->"X"}}]
  16.  
  17. ListAnimate@NestList[ImageFilter[If[3<=Total@Flatten@#<=3+#[[2]][[2]],1,0]&,#,1]&,Image[Round/@RandomReal[1,{200,200}]],99]
  18.  
  19. #include <windows.h>
  20. #include<process.h>
  21. #define K ][(x+80)%20+(y+80)%20*20]
  22. #define H R.Event.MouseEvent.dwMousePosition
  23. #define J R.Event.MouseEvent.dwButtonState
  24. HANDLE Q,W;char*E[3],O;Y(x,y){return E[0 K;}U(x,y,l,v){E[l K=v;}I(){E[2]=E[1];E[1]=*E;*E=E[2];memset(E[1],0,400);}A(i,j,k,l,P){while(1){Sleep(16);for(i=0;i<20;++i)for(j=0;j<20;++j){COORD a={i,j};SetConsoleCursorPosition(Q,a);putchar(E[0][i+j*20]==1?'0':' ');}if(O){for(i=0;i<20;++i)for(j=0;j<20;++j){for(k=i-1,P=0;k<i+2;++k)for(l=j-1;l<j+2;++l){P+=Y(k,l);}U(i,j,1,P==3?1:Y(i,j)==1&&P==4?1:0);}I();}}}main(T,x,y,F,D){for(x=0;x<21;++x)puts("#####################");E[0]=malloc(800);E[1]=E[0]+400;I();I();W=GetStdHandle(-10);Q=GetStdHandle(-11);SetConsoleMode(W,24);INPUT_RECORD R;F=D=O=0;COORD size={80,25};SetConsoleScreenBufferSize(Q,size);_beginthread(A,99,0);while(1){ReadConsoleInput(W,&R,1,&T);switch(R.EventType){case 1:O=R.Event.KeyEvent.dwControlKeyState&128;break;case 2:switch(R.Event.MouseEvent.dwEventFlags){case 1:x=H.X;y=H.Y;case 0:F=J&1;D=J&2;}if(F)U(x,y,0,1);if(D)U(x,y,0,0);}}}
  25.  
  26. import swing._
  27. import event._
  28. object L extends SimpleSwingApplication{import java.awt.event._
  29. import javax.swing._
  30. var(w,h,c,d,r)=(20,20,20,0,false)
  31. var x=Array.fill(w,h)(0)
  32. def n(y:Int,z:Int)=for(b<-z-1 to z+1;a<-y-1 to y+1 if(!(a==y&&b==z)))d+=x((a+w)%w)((b+h)%h)
  33. def top=new MainFrame with ActionListener{preferredSize=new Dimension(500,500)
  34. menuBar=new MenuBar{contents+=new Menu("C"){contents+={new MenuItem("Go/Stop"){listenTo(this)
  35. reactions+={case ButtonClicked(c)=>r= !r}}}}}
  36. contents=new Component{listenTo(mouse.clicks)
  37. reactions+={case e:MouseClicked=>var p=e.point
  38. x(p.x/c)(p.y/c)^=1
  39. repaint}
  40. override def paint(g:Graphics2D){for(j<-0 to h-1;i<-0 to w-1){var r=new Rectangle(i*c,j*c,c,c)
  41. x(i)(j)match{case 0=>g draw r
  42. case 1=>g fill r}}}}
  43. def actionPerformed(e:ActionEvent){if(r){var t=x.map(_.clone)
  44. for(j<-0 to h-1;i<-0 to w-1){d=0
  45. n(i,j)
  46. x(i)(j)match{case 0=>if(d==3)t(i)(j)=1
  47. case 1=>if(d<2||d>3)t(i)(j)=0}}
  48. x=t.map(_.clone)
  49. repaint}}
  50. val t=new Timer(200,this)
  51. t.start}}
  52.  
  53. import swing._
  54. import event._
  55.  
  56. object Life extends SimpleSwingApplication
  57. {
  58. import java.awt.event._
  59. import javax.swing._
  60. var(w,h,c,d,run)=(20,20,20,0,false)
  61. var x=Array.fill(w,h)(0)
  62. def n(y:Int,z:Int)=for(b<-z-1 to z+1;a<-y-1 to y+1 if(!(a==y&&b==z)))d+=x((a+w)%w)((b+h)%h)
  63. def top=new MainFrame with ActionListener
  64. {
  65. title="Life"
  66. preferredSize=new Dimension(500,500)
  67. menuBar=new MenuBar
  68. {
  69. contents+=new Menu("Control")
  70. {
  71. contents+={new MenuItem("Start/Stop")
  72. {
  73. listenTo(this)
  74. reactions+=
  75. {
  76. case ButtonClicked(c)=>run= !run
  77. }
  78. }}
  79. }
  80. }
  81. contents=new Component
  82. {
  83. listenTo(mouse.clicks)
  84. reactions+=
  85. {
  86. case e:MouseClicked=>
  87. var p=e.point
  88. if(p.x<w*c)
  89. {
  90. x(p.x/c)(p.y/c)^=1
  91. repaint
  92. }
  93. }
  94. override def paint(g:Graphics2D)
  95. {
  96. for(j<-0 to h-1;i<-0 to w-1)
  97. {
  98. var r=new Rectangle(i*c,j*c,c,c)
  99. x(i)(j) match
  100. {
  101. case 0=>g draw r
  102. case 1=>g fill r
  103. }
  104. }
  105. }
  106. }
  107. def actionPerformed(e:ActionEvent)
  108. {
  109. if(run)
  110. {
  111. var t=x.map(_.clone)
  112. for(j<-0 to h-1;i<-0 to w-1)
  113. {
  114. d=0
  115. n(i,j)
  116. x(i)(j) match
  117. {
  118. case 0=>if(d==3)t(i)(j)=1
  119. case 1=>if(d<2||d>3)t(i)(j)=0
  120. }
  121. }
  122. x=t.map(_.clone)
  123. repaint
  124. }
  125. }
  126. val timer=new Timer(200,this)
  127. timer.start
  128. }
  129. }
  130.  
  131. using System.Windows.Forms;class G:Form{static void Main(){new G(25).ShowDialog();}
  132. public G(int z){var g=new Panel[z,z];var n=new int [z,z];int x,y,t;for(int i=0;i<z;
  133. i++)for(int j=0;j<z;j++){var p=new Panel{Width=9,Height=9,Left=i*9,Top=j*9,BackColor
  134. =System.Drawing.Color.Tan};p.Click+=(s,a)=>p.Visible=!p.Visible;Controls.Add(g[i,j]=
  135. p);}KeyUp+=(s,_)=>{for(int i=0;i<99;i++){for(x=0;x<z;x++)for(y=0;y<z;y++){t=0;for(int
  136. c=-1;c<2;c++)for(int d=-1;d<2;d++)if(c!=0||d!=0){int a=x+c,b=y+d;a=a<0?24:a>24?0:a;b=
  137. b<0?24:b>24?0:b;t+=g[a,b].Visible?0:1;}if(t==3||t>1&&!g[x,y].Visible)n[x,y]=1;if(t<2
  138. ||t>3)n[x,y]=0;}for(x=0;x<z;x++)for(y=0;y<z;y++)g[x,y].Visible=n[x,y]<1;Update();}};}}
  139.  
  140. l=:[:+/(3 4=/[:+/(,/,"0/~i:1)|.])*.1,:]
  141.  
  142. r =: (i.3 3) e. 1 2 3 5 8
  143. r
  144. 0 1 1 NB. A glider!
  145. 1 0 1
  146. 0 0 1
  147.  
  148. R =: _1 _2 |. 5 7 {. r
  149. R
  150. 0 0 0 0 0 0 0 NB. Test board
  151. 0 0 0 1 1 0 0
  152. 0 0 1 0 1 0 0
  153. 0 0 0 0 1 0 0
  154. 0 0 0 0 0 0 0
  155.  
  156. l R
  157. 0 0 0 0 0 0 0 NB. Single step
  158. 0 0 0 1 1 0 0
  159. 0 0 0 0 1 1 0
  160. 0 0 0 1 0 0 0
  161. 0 0 0 0 0 0 0
  162.  
  163. import java.awt.Color._
  164. import swing._
  165. import event._
  166. import actors.Actor._
  167. new SimpleSwingApplication{var(y,r,b)=(200,false,Array.fill(20,20)(false))
  168. lazy val u=new Panel{actor{loop{if(r){b=Array.tabulate(20,20){(i,j)=>def^(i:Int)= -19*(i min 0)+(i max 0)%20
  169. var(c,n,r)=(0,b(i)(j),-1 to 1)
  170. for(x<-r;y<-r;if x!=0||y!=0){if(b(^(i+x))(^(j+y)))c+=1}
  171. if(n&&(c<2||c>3))false else if(!n&&c==3)true else n}};repaint;Thread.sleep(y)}}
  172. focusable=true
  173. preferredSize=new Dimension(y,y)
  174. listenTo(mouse.clicks,keys)
  175. reactions+={case e:MouseClicked=>val(i,j)=(e.point.x/10,e.point.y/10);b(i)(j)= !b(i)(j)case _:KeyTyped=>r= !r}
  176. override def paintComponent(g:Graphics2D){g.clearRect(0,0,y,y);g.setColor(red)
  177. for(x<-0 to 19;y<-0 to 19 if b(x)(y))g.fillRect(x*10,y*10,9,9)}}
  178. def top=new Frame{contents=u}}.main(null)
  179.  
  180. 10 DEFINT A-Z:DEF SEG=&HB800:KEY OFF:COLOR 7,0:CLS:DEF FNP(X,Y)=PEEK((((Y+25)MOD 25)*80+((X+80)MOD 80))*2)
  181. 20 X=0:Y=0
  182. 30 LOCATE Y+1,X+1,1
  183. 40 S$=INKEY$:IF S$=""GOTO 40
  184. 50 IF S$=CHR$(13)GOTO 150
  185. 60 IF S$=" "GOTO 130
  186. 70 IF S$=CHR$(0)+CHR$(&H48)THEN Y=(Y-1+25)MOD 25:GOTO 30
  187. 80 IF S$=CHR$(0)+CHR$(&H50)THEN Y=(Y+1)MOD 25:GOTO 30
  188. 90 IF S$=CHR$(0)+CHR$(&H4B)THEN X=(X-1+80)MOD 80:GOTO 30
  189. 100 IF S$=CHR$(0)+CHR$(&H4D)THEN X=(X+1)MOD 80:GOTO 30
  190. 110 IF S$="c"THEN CLS:GOTO 20
  191. 120 GOTO 40
  192. 130 Z=PEEK((Y*80+X)*2):IF Z=42 THEN Z=32ELSE Z=42
  193. 140 POKE(Y*80+X)*2,Z:GOTO 40
  194. 150 LOCATE 1,1,0:ON KEY(1)GOSUB 320:KEY(1) ON
  195. 160 V!=TIMER+.5:FOR Y=0 TO 24:FOR X=0 TO 79:N=0
  196. 170 Z=FNP(X-1,Y-1):IF Z=42 OR Z=46 THEN N=N+1
  197. 180 Z=FNP(X,Y-1):IF Z=42 OR Z=46 THEN N=N+1
  198. 190 Z=FNP(X+1,Y-1):IF Z=42 OR Z=46 THEN N=N+1
  199. 200 Z=FNP(X-1,Y):IF Z=42 OR Z=46 THEN N=N+1
  200. 210 Z=FNP(X+1,Y):IF Z=42 OR Z=46 THEN N=N+1
  201. 220 Z=FNP(X-1,Y+1):IF Z=42 OR Z=46 THEN N=N+1
  202. 230 Z=FNP(X,Y+1):IF Z=42 OR Z=46 THEN N=N+1
  203. 240 Z=FNP(X+1,Y+1):IF Z=42 OR Z=46 THEN N=N+1
  204. 250 Z=PEEK((Y*80+X)*2):IF Z=32 THEN IF N=3 THEN Z=43
  205. 260 IF Z=42 THEN IF N<2 OR N>3 THEN Z=46
  206. 270 POKE(Y*80+X)*2,Z:NEXT:NEXT:FOR Y=0 TO 24:FOR X=0 TO 79:Z=PEEK((Y*80+X)*2):IF Z=46 THEN Z=32
  207. 280 IF Z=43 THEN Z=42
  208. 290 POKE(Y*80+X)*2,Z:NEXT:NEXT
  209. 300 IF TIMER<V!GOTO 300
  210. 310 IF INKEY$=""GOTO 160
  211. 320 SYSTEM
  212.  
  213. 1000 REM Conway's Game of Life
  214. 1001 REM -
  215. 1002 REM Copyright (c) 2012 Thorsten "mirabilos" Glaser
  216. 1003 REM All rights reserved. Published under The MirOS Licence.
  217. 1004 REM -
  218. 1005 DEFINT A-Z:DEF SEG=&hB800
  219. 1006 KEY OFF:COLOR 7,0:CLS
  220. 1007 DEF FNP(X,Y)=PEEK((((Y+25) MOD 25)*80+((X+80) MOD 80))*2)
  221. 1010 PRINT "Initial setting mode, press SPACE to toggle, RETURN to continue"
  222. 1020 PRINT "Press C to clear the board, R to reset. OK? Press a key then."
  223. 1030 WHILE INKEY$="":WEND
  224. 1050 CLS
  225. 1065 DATA 3,3,4,3,5,3,6,3,7,3,8,3,3,4,4,4,5,4,6,4,7,4,8,4
  226. 1066 DATA 10,3,10,4,10,5,10,6,10,7,10,8,11,3,11,4,11,5,11,6,11,7,11,8
  227. 1067 DATA 11,10,10,10,9,10,8,10,7,10,6,10,11,11,10,11,9,11,8,11,7,11,6,11
  228. 1068 DATA 4,11,4,10,4,9,4,8,4,7,4,6,3,11,3,10,3,9,3,8,3,7,3,6
  229. 1069 DATA 21,0,22,1,22,2,21,2,20,2,-1,-1
  230. 1070 RESTORE 1065
  231. 1080 READ X,Y
  232. 1090 IF X=-1 GOTO 1120
  233. 1100 POKE (Y*80+X)*2,42
  234. 1110 GOTO 1080
  235. 1120 X=0:Y=0
  236. 1125 LOCATE Y+1,X+1,1
  237. 1130 S$=INKEY$
  238. 1140 IF S$="" GOTO 1130
  239. 1150 IF S$=CHR$(13) GOTO 1804
  240. 1160 IF S$=" " GOTO 1240
  241. 1170 IF S$=CHR$(0)+CHR$(&h48) THEN Y=(Y-1+25) MOD 25:GOTO 1125
  242. 1180 IF S$=CHR$(0)+CHR$(&h50) THEN Y=(Y+1) MOD 25:GOTO 1125
  243. 1190 IF S$=CHR$(0)+CHR$(&h4B) THEN X=(X-1+80) MOD 80:GOTO 1125
  244. 1200 IF S$=CHR$(0)+CHR$(&h4D) THEN X=(X+1) MOD 80:GOTO 1125
  245. 1210 IF S$="c" THEN CLS:GOTO 1120
  246. 1220 IF S$="r" GOTO 1050
  247. 1225 IF S$=CHR$(27) THEN END
  248. 1230 GOTO 1130
  249. 1240 Z=PEEK((Y*80+X)*2)
  250. 1250 IF Z=42 THEN Z=32 ELSE Z=42
  251. 1260 POKE (Y*80+X)*2,Z
  252. 1270 GOTO 1130
  253. 1804 LOCATE 1,1,0
  254. 1900 ON KEY(1) GOSUB 2300
  255. 1910 KEY(1) ON
  256. 2000 V!=TIMER+.5
  257. 2010 FOR Y=0 TO 24
  258. 2020 FOR X=0 TO 79
  259. 2030 N=0
  260. 2040 Z=FNP(X-1,Y-1):IF Z=42 OR Z=46 THEN N=N+1
  261. 2050 Z=FNP(X ,Y-1):IF Z=42 OR Z=46 THEN N=N+1
  262. 2060 Z=FNP(X+1,Y-1):IF Z=42 OR Z=46 THEN N=N+1
  263. 2070 Z=FNP(X-1,Y ):IF Z=42 OR Z=46 THEN N=N+1
  264. 2080 Z=FNP(X+1,Y ):IF Z=42 OR Z=46 THEN N=N+1
  265. 2090 Z=FNP(X-1,Y+1):IF Z=42 OR Z=46 THEN N=N+1
  266. 2100 Z=FNP(X ,Y+1):IF Z=42 OR Z=46 THEN N=N+1
  267. 2110 Z=FNP(X+1,Y+1):IF Z=42 OR Z=46 THEN N=N+1
  268. 2120 Z=PEEK((Y*80+X)*2)
  269. 2130 IF Z=32 THEN IF N=3 THEN Z=43
  270. 2140 IF Z=42 THEN IF N<2 OR N>3 THEN Z=46
  271. 2150 POKE (Y*80+X)*2,Z
  272. 2160 NEXT X
  273. 2170 NEXT Y
  274. 2200 FOR Y=0 TO 24
  275. 2210 FOR X=0 TO 79
  276. 2220 Z=PEEK((Y*80+X)*2)
  277. 2230 IF Z=46 THEN Z=32
  278. 2240 IF Z=43 THEN Z=42
  279. 2250 POKE (Y*80+X)*2,Z
  280. 2260 NEXT X
  281. 2270 NEXT Y
  282. 2280 IF TIMER<V! GOTO 2280
  283. 2290 IF INKEY$="" GOTO 2000
  284. 2300 SYSTEM
  285.  
  286. ListAnimate[ArrayPlot/@CellularAutomaton[{224,{2,{{2,2,2},{2,1,2},
  287. {2,2,2}}},{1,1}},{RandomInteger[1,{9,9}],0},90]]
  288.  
  289. [/f ARGUMENTS 0 get(r)file/n 20>>begin[/m
  290. n 1 sub/b[n{[n{f token pop}repeat]}repeat]/c 400
  291. n div/F{dup 0 lt{n add}if dup n ge{n sub}if}>>begin{0
  292. 1 m{dup 0 1 m{2 copy b exch get exch get 1 xor setgray
  293. c mul exch c mul c c rectfill dup}for pop pop}for
  294. showpage/b[0 1 m{/x exch def[0 1 m{/y exch def 0
  295. y 1 sub 1 y 1 add{F dup x 1 sub 1 x
  296. 1 add{F b exch get exch get 3 2 roll add exch
  297. dup}for pop pop}for b x get y get sub b x get y get
  298. 0 eq{3 eq{1}{0}ifelse}{dup 2 eq exch 3 eq
  299. or{1}{0}ifelse}ifelse}for]}for]def}loop
  300.  
  301. [
  302. /f ARGUMENTS 0 get(r)file
  303. /n 20
  304. /sz 400
  305. %/r{rand 2147483647 div}
  306. >>begin
  307. [
  308. /m n 1 sub
  309. /b[
  310. %n{[n{r .15 le{1}{0}ifelse}repeat]}repeat
  311. n{[n{f token pop}repeat]}repeat
  312. ]
  313. /c sz n div
  314. /F{dup 0 lt{n add}if dup n ge{n sub}if}
  315. >>begin
  316. {
  317. 0 1 m{dup % y y
  318. 0 1 m{ % y y x
  319. 2 copy b exch get exch get 1 xor setgray
  320. c mul exch c mul c c rectfill
  321. dup
  322. }for pop pop}for
  323. pstack
  324. showpage
  325. /b[0 1 m{/x exch def
  326. [0 1 m{/y exch def
  327. 0
  328. y 1 sub 1 y 1 add{F dup %s y y
  329. x 1 sub 1 x 1 add{F %s y y x
  330. b exch get exch get %s y bxy
  331. 3 2 roll add exch %s+bxy y
  332. dup %s y y
  333. }for pop pop}for
  334. b x get y get sub
  335. b x get y get
  336. 0 eq{3 eq{1}{0}ifelse}{dup 2 eq exch 3 eq or{1}{0}ifelse}ifelse
  337. }for]
  338. }for]def
  339. }loop
  340.  
  341. 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  342. 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  343. 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  344. 0 0 0 0 0 1 1 1 0 0 0 1 1 1 0 0 0 0 0 0
  345. 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  346. 0 0 0 1 0 0 0 0 1 0 1 0 0 0 0 1 0 0 0 0
  347. 0 0 0 1 0 0 0 0 1 0 1 0 0 0 0 1 0 0 0 0
  348. 0 0 0 1 0 0 0 0 1 0 1 0 0 0 0 1 0 0 0 0
  349. 0 0 0 0 0 1 1 1 0 0 0 1 1 1 0 0 0 0 0 0
  350. 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  351. 0 0 0 0 0 1 1 1 0 0 0 1 1 1 0 0 0 0 0 0
  352. 0 0 0 1 0 0 0 0 1 0 1 0 0 0 0 1 0 0 0 0
  353. 0 0 0 1 0 0 0 0 1 0 1 0 0 0 0 1 0 0 0 0
  354. 0 0 0 1 0 0 0 0 1 0 1 0 0 0 0 1 0 0 0 0
  355. 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  356. 0 0 0 0 0 1 1 1 0 0 0 1 1 1 0 0 0 0 0 0
  357. 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  358. 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  359. 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  360. 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  361.  
  362. (]+.&(3&=)+)+/((4&{.,(_4&{.))(>,{,~<i:1))&|.
  363.  
  364. f =: 5 5 $ 0 1 0 0 0 0 0 1 0 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0
  365. f
  366. 0 1 0 0 0
  367. 0 0 1 0 0
  368. 1 1 1 0 0
  369. 0 0 0 0 0
  370. 0 0 0 0 0
  371. f (]+.&(3&=)+)+/((4&{.,(_4&{.))(>,{,~<i:1))&|. f
  372. 0 0 0 0 0
  373. 1 0 1 0 0
  374. 0 1 1 0 0
  375. 0 1 0 0 0
  376. 0 0 0 0 0
  377.  
  378. int h=22,t=24,i,j;int[][]w=new int[t][t],b=new int[t][t];int[]q={1,0,-1};void draw(){if(t<9){clear();for(i=2;i<h;i++){for(j=2;j<h;j++)w[i][j]=b[i][j];w[i][1]=w[i][21];w[i][h]=w[i][2];w[1][i]=w[21][i];w[h][i]=w[2][i];}for(i=1;i<23;i++)for(j=1;j<23;j++){t=-w[i][j];for(int s:q)for(int d:q)t+=w[i+s][j+d];b[i][j]=w[i][j]>0&(t<2|t>3)?0:t==3?1:b[i][j];}a();}}void keyPressed(){t=0;}void mousePressed(){int i=mouseX/5+2,j=mouseY/5+2;w[i][j]=b[i][j]=1;a();}void a(){for(i=0;i<h-2;i++)for(j=0;j<h-2;j++)if(w[i+2][j+2]==1)rect(i*5,j*5,5,5);}
  379.  
  380. int h=22,t=24,i,j;
  381. int[][]w=new int[t][t],b=new int[t][t];
  382. int[]q={1,0,-1};
  383. void draw(){
  384. if(t<9){
  385. clear();
  386. for(i=2;i<h;i++){
  387. for(j=2;j<h;j++)
  388. w[i][j]=b[i][j];
  389. w[i][1]=w[i][21];
  390. w[i][h]=w[i][2];
  391. w[1][i]=w[21][i];
  392. w[h][i]=w[2][i];
  393. }
  394. for(i=1;i<23;i++)
  395. for(j=1;j<23;j++){
  396. t=-w[i][j];
  397. for(int s:q)
  398. for(int d:q)
  399. t+=w[i+s][j+d];
  400. b[i][j]=w[i][j]>0&(t<2|t>3)?0:t==3?1:b[i][j];
  401. }
  402. a();
  403. }
  404. }
  405. void keyPressed(){
  406. t=0;
  407. }
  408. void mousePressed(){
  409. int i=mouseX/5+2,j=mouseY/5+2;
  410. w[i][j]=b[i][j]=1;
  411. a();
  412. }
  413. void a(){
  414. for(i=0;i<h-2;i++)
  415. for(j=0;j<h-2;j++)
  416. if(w[i+2][j+2]==1)
  417. rect(i*5,j*5,5,5);
  418. }
  419.  
  420. mapfile a
  421. for e in {0..863};{
  422. for i in {0..8};{
  423. [ "${a[(e/36+i/3-1)%24]:(e+i%3-1)%36:1}" == O ]&&((n++))
  424. }
  425. d=
  426. c=${a[e/36]:e%36:1}
  427. [ "$c" == O ]&&((--n==2))&&d=O
  428. ((n-3))||d=O
  429. b[e/36]+=$d
  430. n=
  431. }
  432. printf -vo %s\n "${b[@]}"
  433. echo "$o"
  434. exec $0<<<"$o"
  435.  
  436. $ ./conway.sh << EOF
  437.  
  438. O
  439. O
  440. OOO
  441.  
  442. EOF
  443.  
  444.  
  445. O O
  446. OO
  447. O
  448.  
  449.  
  450.  
  451.  
  452.  
  453.  
  454.  
  455.  
  456.  
  457.  
  458.  
  459.  
  460.  
  461.  
  462.  
  463.  
  464.  
  465. O
  466. O O
  467. OO
  468.  
  469. man tr | tr [:alnum:] O | ./conway.sh
  470.  
  471. from Tkinter import*
  472. import copy
  473. z=range
  474. F=50
  475. T=Tk()
  476. S=9
  477. f=[F*[0]for i in'7'*F]
  478. c=Canvas(T,bg='white',width=S*F,height=S*F)
  479. c.pack()
  480. def p(x,y,a):f[y][x]=f[y][x]or c.create_rectangle(x*S,y*S,x*S+S,y*S+S,fill='gray')if a else c.delete(f[y][x])
  481. r=1
  482. def R(e):global r;r=1-r
  483. exec("c.bind('<Button-%i>',lambda e:p(e.x/S,e.y/S,%i));c.bind('<B%i-Motion>',lambda e:p(e.x/S,e.y/S,%i));"*2%(1,1,1,1,3,0,3,0))
  484. c.bind('<Button-2>',R)
  485. def L():
  486. T.after(99,L)
  487. if r:return
  488. g=copy.deepcopy(f)
  489. for y in z(F):
  490. for x in z(F):
  491. n=8
  492. for j in z(-1,2):
  493. for i in z(-1,2):
  494. if i or j:n-=not g[(y+j)%F][(x+i)%F]
  495. if 1<n<4:
  496. if n==3and not g[y][x]:p(x,y,1)
  497. else:p(x,y,0)
  498. L()
  499. T.mainloop()
  500.  
  501. from Tkinter import*
  502. import copy
  503. z=range
  504. F=50
  505. T=Tk()
  506. S=9
  507. f=[F*[0]for i in'7'*F]
  508. c=Canvas(T,bg='white',width=S*F,height=S*F)
  509. c.pack()
  510. def p(x,y,a):f[y][x]=f[y][x]or c.create_rectangle(x*S,y*S,x*S+S,y*S+S,fill='gray')if a else c.delete(f[y][x])
  511. r=1
  512. def R(e):global r;r=1-r
  513. exec("c.bind('<Button-%i>',lambda e:p(e.x/S,e.y/S,%i));c.bind('<B%i-Motion>',lambda e:p(e.x/S,e.y/S,%i));"*2%(1,1,1,1,3,0,3,0))
  514. c.bind('<Button-2>',R)
  515. def L():
  516. T.after(99,L)
  517. if r:return
  518. g=copy.deepcopy(f)
  519. for y in z(F):
  520. for x in z(F):
  521. n=8
  522. for j in z(-1,2):
  523. for i in z(-1,2):
  524. if i or j:n-=not g[(y+j)%F][(x+i)%F]
  525. if 1<n<4:
  526. if n==3and not g[y][x]:p(x,y,1)
  527. else:p(x,y,0)
  528. L()
  529. T.mainloop()
  530.  
  531. /* Fill an array with 0's and 1's, and call g(array, width, height) to iterate */
  532. g=function(f,c,g,d,e,b,h){g=[];e=[c+1,c,c-1,1];for(b=c*c;b--;g[b]=3==d||f[b]&&2==d,d=0)for(h in e)d+=f[b+e[h]]+f[b-e[h]];return g}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement