r/tinycode • u/[deleted] • Nov 05 '13
Game Of Life in 418 bytes of C++!
My first post in this sub. It's not as great as the guy who did it in 300bytes of C, but it does the trick! Hope you like it!
#include<iostream>
#define A for(int
#define B [d][e]
#define r A d=1;d<=t;d++)A e=1;e<=t;e++)
#define s A f=-1;f<=1;f++)A g=-1;g<=1;g++)
using namespace std;const int t=9;char b[t+2][t+2],c[t+2][t+2];int main(){int u=0;r{b B=rand()%2<1?'o':'.';}for(;;){system("sleep .1 && clear");r{cout<<b B;if(e==t)cout<<endl;}r c B=b B;r{u=0;s if(c[d+f][e+g]=='o'&&(f||g))u++;if(u<2||u>3)b B='.';else if(u==3&&c B=='.')b B='o';}}}Feel free to give me your suggestions on how to make it tinier!
EDIT: By /u/cybermind 's suggestion. I made some small changes (see his comment):
#include<iostream>
#define A for(int
#define B [d][e]
#define r A d=1;d<9;d++)A e=1;e<9;e++)
#define s A f=-1;f<2;f++)A g=-1;g<2;g++)
using namespace std;char b[10][10],c[10][10], u;int main(){r{b B=48-rand()%2*2;}for(;;){system("sleep .1 && clear");r{cout<<b B;if(e>7)cout<<endl;}r c B=b B;r{u=0;s if(c[d+f][e+g]>47&&(f|g))u++;if(u/2-1)b B=46;else if(u==3&&c B&2)b B=48;}}}I feel like it can be reduced even more. I may think ok changing the algorithm. Just to feed your 'tiny'-curiosity, the original piece I wrote (before any attempt to make it tiny) was 1965 bytes!!!
Is there some hacky thing I can do to avoid repeating #define so many times?
EDIT2: Thanks to /u/Rangi42, I made some improvements. I am down to 339 bytes!!!
#include<iostream>
#define A for(int
#define B [d][e]
#define r A d=1;d<9;d++)A e=1;e<9;e++)
#define s A f=-1;f<2;f++)A g=-1;g<2;g++)
char b[10][10],c[10][10], u;int main(){r{b B=48-rand()%2*2;}for(;;){system("sleep .1 && clear");r{putchar(b B);e>7?puts(""):0;}r c B=b B;r{u=0;s u+=f|g&&c[d+f][e+g]>47;b B=u/2-1?46:u==3&&c B&2?48:b B;}}}15
u/lifthrasiir Nov 06 '13 edited Nov 06 '13
I strongly suggest you to switch to C, as it enables much more golfing possibility. (Edit: Not that C++ is not a worthwhile golfing target, your code is just a C99 code disguised as C++ so the switch is trivial.) My attempt within 20 minutes: (255 bytes)
#define r for(v=11;v<89;v+=v%10>7?3:1)
char*p,b[100],c[100],u,v;main(){r b[v]=48-rand()%2*2;for(;;){system("sleep .1&&clear");r putchar(b[v]),v%10>7&&puts("");r c[v]=b[v];r{u=0;for(p="678@BJKL";*p;u+=c[v+*p++-65]>47);b[v]=u/2-1?46:u==3&&c[v]&2?48:b[v];}}}#include <stdio.h>etc. are optional on the traditional C. As your program only uses some stdio functions and system, it is fine.main()is assumed to beint main()in the traditional C.Two-dimensional arrays are overkill. You can just flatten them and use a modulo operation to jump to the correct offset. In this case, if the column number (
v%10) is more than 7, you have to jump two more border cells. This simplication eliminates most macros.Likewise for the neighborhood matrix, you can encode all the information to the string. Since we have already simplified two-dimensional arrays, what you have to encode is down to an array [-11, -10, -9, -1, 1, 9, 10, 11], which can be comfortably encoded to a string offseted by 65 (
'A').&&in the system call does not require whitespaces around it.Braces can be eliminated in some cases by using a comma:
r{A;B;}=r A,B;. One byte down.p?q:0;in the statement is equivalent top&&q. (qshould not be void, but it's already assumed from the use of ?: operators.) Similarily,p?0:q;isp||q;.
I'm very sure that there are plenty of rooms for improvement (well down to 200 bytes), but anyway here it is.
1
u/xem06 Dec 12 '13
Related, in JavaScript: http://xem.github.io/miniGameOfLife (works best on Chrome)
0
Nov 05 '13
[deleted]
2
Nov 05 '13 edited Nov 05 '13
I'm down to 374, thanks! It compiles perfectly even without <cstdlib>. I also removed the initialization 'u=0'. Your tips were really good. The 4th, 5th and 6th bullet points really surprised me.
Thanks, TIL!
EDIT: Also, I changed from 9x9 to 8x8, so I can use '<9' and save 3 bytes!
0
Nov 05 '13
[deleted]
1
Nov 05 '13
Again, thank you! I'm down to 352, since my compiler doesn't allow me to change to <cstdio>. It says the problem comes from the rand() and system() functions.
1
u/JoeySalsa Nov 06 '13
...wait so how do I even run this program?
3
Nov 06 '13
Copy-Paste it to your text editor and run it like you usually run C++ programs:
Example:
g++ -o myProg myProg.cpp && ./myProgTip: Never run code you find in the web without making sure you understand all of it - specially this kind of freaky code. Code CAN harm your computer. Specially if there are commands like system(""), as you see in my code.
1
u/JoeySalsa Nov 06 '13
See, when I try to run it on DevC++ it just gives me a bunch of errors I dont feel like looking through . :/
1
Nov 06 '13
hm... i dunno. Maybe try compiling it in the terminal. I never ever used something like DevC++, so I can't help you there.
1
u/JoeySalsa Nov 06 '13
Yeah, I'm new to programming, so I wouldn't know much about using terminal, I'm always using an IDE. It's interesting to see these tiny programs made though!
2
u/Rangi42 Nov 05 '13
You can replace
ifwith the ternary operator. For instance:is 43 characters. The ternary equivalent:
is 32 characters. And I suspect it could be made shorter by finding some equivalent logical formula.
Also:
is 29 characters. The equivalent, using implicit conversion of
true/falseto1/0:is 25 characters.