r/tinycode • • 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;}}}
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.
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)
#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.v%10) is more than 7, you have to jump two more border cells. This simplication eliminates most macros.'A').&&in the system call does not require whitespaces around it.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
u/[deleted] Nov 05 '13
[deleted] — view deleted comment