r/dailyprogrammer • u/Elite6809 1 1 • Jun 01 '14
[6/2/2014] Challenge #165 [Easy] ASCII Game of Life
(Easy): ASCII Game of Life
Hello people. Sorry for submitting this early, but I have exams this week and the next so I'll have to submit these challenges a little bit early - I'm sure that's not an issue though! Welcome to June, and it's time for a run of similarly themed challenges - all of them will be based on ASCII data. Not too dissimilar to this challenge from a while ago.
This first challenge is based on a game (the mathematical variety - not quite as fun!) called Conway's Game of Life. This is called a cellular automaton. This means it is based on a 'playing field' of sorts, made up of lots of little cells or spaces. For Conway's game of life, the grid is square - but other shapes like hexagonal ones could potentially exist too. Each cell can have a value - in this case, on or off - and for each 'iteration' or loop of the game, the value of each cell will change depending on the other cells around it. This might sound confusing at first, but looks easier when you break it down a bit.
- A cell's "neighbours" are the 8 cells around it.
- If a cell is 'off' but exactly 3 of its neighbours are on, that cell will also turn on - like reproduction.
- If a cell is 'on' but less than two of its neighbours are on, it will die out - like underpopulation.
- If a cell is 'on' but more than three of its neighbours are on, it will die out - like overcrowding.
Fairly simple, right? This might sound boring, but it can generate fairly complex patterns - this one, for example, is called the Gosper Glider Gun and is designed in such a way that it generates little patterns that fly away from it. There are other examples of such patterns, like ones which grow indefinitely.
Your challenge is, given an initial 'state' of 'on' and 'off' cells, and a number, simulate that many steps of the Game of Life.
Formal Inputs and Outputs
Input Description
You will be given a number N, and then two more numbers X and Y. After that you will be given a textual ASCII grid of 'on' and 'off' states that is X cells wide and Y cells tall. On the grid, a period or full-stop . will represent 'off', and a hash sign # will represent 'on'.
The grid that you are using must 'wrap around'. That means, if something goes off the bottom of the playing field, then it will wrap around to the top, like this: http://upload.wikimedia.org/wikipedia/en/d/d1/Long_gun.gif See how those cells act like the top and bottom, and the left and right of the field are joined up? In other words, the neighbours of a cell can look like this - where the lines coming out are the neighbours:
#-...- ...... ../|\.
|\.../ ...... ......
...... |/...\ ......
...... #-...- ......
...... |\.../ ..\|/.
|/...\ ...... ..-#-.Output Description
Using that starting state, simulate N iterations of Conway's Game of Life. Print the final state in the same format as above - . is off and # is on.
Sample Inputs & Output
Sample Input
7 10 10
..........
..........
..#.......
...#......
.###......
..........
..........
..........
..........
..........Sample Output
..........
..........
..........
..........
...#......
....##....
...##.....
..........
..........
..........Challenge
Challenge Input
32 17 17
.................
.................
....###...###....
.................
..#....#.#....#..
..#....#.#....#..
..#....#.#....#..
....###...###....
.................
....###...###....
..#....#.#....#..
..#....#.#....#..
..#....#.#....#..
.................
....###...###....
.................
.................(just for laughs, see how the output changes when you change N. Cool, eh?)
Notes
To test your program, use one of the many online simulation programs. There are plenty written in JavaScript you can get at with a Google search (or Bing, if you'd prefer. I wouldn't.)
8
u/skeeto -9 8 Jun 02 '14 edited Jun 02 '14
C++11. The title is called ASCII Game of Life, but what about Unicode Game of Life? My program uses Unicode block elements instead of plain ASCII characters. Each character on the screen represents two Game of Life cells so that the rectangular terminal display appears square.
You'll need the ncursesw (widechar) library and a unicode-supporting terminal in order to run this.
3
u/badgers_uk Jun 02 '14
Python 3. It also prints out every iteration. Looks pretty cool! Had a lot of fun making this, thanks for taking the time to set this challenge and good luck with your exams.
3
u/jeaton Jun 01 '14
I actually just recently wrote a Game of Life simulator in JavaScript if anyone wants to use it to test theirs. http://1995eaton.github.io/game-of-life/
10
u/ehcubed Jun 01 '14
Python 3.3.2. Cool challenge! I ran into a bit of trouble for the case where none of the if conditions in the given 3 bullets would apply (I figured they exhausted all cases). Turns out that the character should just stay the same. Thanks to /u/jeaton's simulator; it really helped with debugging!
Code:
Challenge Output:
7
u/chunes 1 2 Jun 01 '14
Are the changes to cells simultaneous or sequential?
5
u/badgers_uk Jun 02 '14
I was messing around with this some more and made Gosper's glider gun. It stops after 180 frames when the gliders are about to start destroying the gun.
4
u/slackermanz Jun 01 '14
Check out /r/cellular_automata if you find these programs interesting :)
4
u/alteraego Jun 01 '14 edited Jun 01 '14
In Matlab, constucting one wide edges around the start array to achieve the looping so that the creating the small neighbor array would work, and then removing them before spitting out the end array. Takes start map as one line, no delimiters.
edit: Realized I could have initially set numeric values to the numerical ascii, change the appropriate rules to be multiples thereof and totally remove the last element by element change and just grabbed the core matrix and changed it to char. Dumb mistake.
6
u/flightcrank 0 0 Jun 04 '14 edited Jun 05 '14
This should not be put under EASY. im not saying its hard but the author has put the sample output after 7 iterations. This makes it hard to debug because you dont know at what iteration 1 - 7 where your output may be wrong. how is that considered EASY ? Just take a look at problem #1 [EASY] and compare it to the more recent ones.
There seems to be almost no difference between [EASY] and [INTERMEDIATE]. Challenge #162 [EASY] and [INTERMEDIATE] are basically of the same difficulty level. mods really need to pull there heads in and establish some guide lines on difficulty levels.
anyway my solution in C.
code: https://github.com/flightcrank/daily-programmer/blob/master/challange_165_e.c
output:
3
u/dohaqatar7 1 1 Jun 01 '14
I made this almost a year ago, so the quality of the code is low, but I put some rather nice graphics to it. The code is kind of long so i'll just link to a gist
Game Of Life (Graphics)
3
u/tet5uo Jun 05 '14 edited Jun 05 '14
C# :
Was having much fun with this one I didn't want to stop coding.
This version will render each generation of the simulation. I wanted to keep adding features but I guess that's beyond what's needed for this.
I'll have to keep playing with it later for fun.
https://gist.github.com/anonymous/7cb7a3d8f282d05d1ea5
Challenge output: http://i.imgur.com/bmFNZni.png
Random Output : http://i.imgur.com/atchLwC.gif
4
u/Godspiral 3 3 Jun 02 '14 edited Jun 03 '14
J wins this :P
life =: (_3 _3 (+/ e. 3 + 0 , 4&{)@,;._3 ])@(0 , 0 ,~ 0 ,. 0 ,.~ ])
challenge
2
u/Nodocify Jun 03 '14 edited Jun 03 '14
Here are my results with Python 3.4 Code:
Output:
This was fun, but I decided to take it a step further and actually create the gif for the entire life cycle. This is on ubuntu 14.04 with ImageMagick installed. Code:
And thanks to /u/badgers_uk for his glider gun input i was able to use the above code to create http://i.imgur.com/vVcgeOK.gif (with the life span changed to 250 so you can see the gliders impact ;p)
2
u/oasisguy Jun 03 '14 edited Jun 03 '14
My try in Lua. I'm just getting to know the language, so I'd imagine this is not very elegant and/or efficient; your critique is welcome. [EDIT - minor changes.]
1
u/h3ckf1r3 Jun 01 '14 edited Jun 01 '14
I ran into the silliest bug that took me a while to understand. I was updating the map as I went which gave different results, I just had to add a buffer and all was better. Wrote this in C.
EDIT: I actually forgot to mention this, but I had recently written a game of life program using curses. I feel rather silly because writing this made me discover some bugs in that supposedly finished product. Feel free to check it out :). https://github.com/h3ckboy/GameOfLife
1
u/chrisnails Jun 02 '14
powershell; second time ever
since powershell is a bit tricky in regards to multi-dimensional-arrays i thought i'll be clever and use an array of strings...
well, turns out, i was not very clever, but stuck to it as punishment :)
Output
1
u/ooesili Jun 02 '14
Here is my ncurses-based C solution. It plays the game at 10 moves per second.
1
u/0x746d616e Jun 02 '14
Go :)
1
u/YouAreNotASlave Jun 02 '14
In python 3.4...
CODE
INPUT
OUTPUT
1
u/ddsnowboard Jun 03 '14
Here's my hack at Ruby. It could be greatly improved, and if anyone could give me some advise as to how to make it less repetitious, that would be greatly appreciated. It's quite lengthy, as you can see.
1
u/RednBIack Jun 03 '14
Written in C. I just started learning C. After a million seg faults and gawking at strange, unexpected behaviour I finally managed to get it to work. I'm already starting to hate pointers. Any feedback is appreciated.
1
u/TheSageMage Jun 03 '14 edited Jun 03 '14
Ruby! I hear people talk all day about Ruby, meanwhile I'm just over here doing Java. Thought I'd give it a spin. I was able to override the array indices function so I could do the wrap around of the array without a bunch of mod code.
Reads from the first argument provided.
1
u/TheSageMage Jun 03 '14 edited Jun 06 '14
When I first read this problem, if you got to a large enough scale this sounds ideal for a BSP system, something like Pregel.
Any one else see this or am I crazy?
1
u/fvandepitte 0 0 Jun 03 '14 edited Jun 03 '14
In C# Input works with textfile
Propably not the best way, if there are sugestions, let me know.
Code:
1
u/spfy Jun 03 '14 edited Jun 03 '14
I'm trying this in Python 3.4.1. I won't post my whole code because it doesn't work. I'm wondering if anyone can see what's wrong with my thinking here. The reference list is a copy of the original grid. For some reason, when I try the sample inputs, my creatures all end up dead every time. Surely the problem lies in how I decide whether they should die or not?
1
u/xynta Jun 03 '14 edited Jun 03 '14
My first solution. Feel free to comment. Java
Output:
1
u/[deleted] Jun 03 '14
A solution in Java. Comments and criticisms welcome.
Code:
Challenge Output:
1
u/Frigguggi 0 1 Jun 03 '14 edited Jun 03 '14
Java. Initial configuration should be in a file named life.txt. As an added bonus, if you enter n as 0, it will keep going indefinitely. The stepThrough flag causes the simulation to pause after each step and wait for the user to hit enter, allowing you to see the progression. The filename and stepThrough values can be changed from command line using -file and -step options.
Output:
Challenge output:
1
u/spfy Jun 03 '14
Here's my completed python3.4 version! I hard-coded the grid into the program because I am lazy. I didn't include that part, of course!
1
u/wcastello Jun 04 '14 edited Jun 04 '14
Python 3.4, using UTF-8 block elements to output '.' and '#'
1
u/CaptainCa Jun 04 '14 edited Jun 04 '14
Here's my implementation in C. Not my favourite code as I tried to do it will calloc() in a function but got caught up trying to return a multidimensional array (char **). Feedback would be lovely.
1
u/toodim Jun 04 '14
A little late to the party, but here goes. Python 3.4 solution, prints all steps toward the final solution. Seems a little on the tough side for an easy problem.
Challenge output:
1
u/[deleted] Jun 05 '14
Running behind already this week, but here it is in C (gcc --std=c89). The output is correct but I can't get it to post as a spoiler. I did enjoy C's easy conversion between string and character types to simplify logic.
1
u/Instinct212 Jun 05 '14
C++ First time trying a challenge, always wanted to make a Game of Life program, was very fun! (Not very modern C++ here using raw arrays/pointers since it doesn't take much memory management) Prints each iteration, but can be changed to only printing the last by separating out updateAndPrint()
main:
gameBoard.h:
gameBoard.cpp:
1
u/WerkWerk Jun 05 '14 edited Jun 05 '14
Here is my implementation in Python 3.3.
Challenge Output:
1
u/altanic Jun 05 '14
C#, prints the grid after each step & sleeps for 400 ms before continuing... now I want to keep feeding it different patterns & rules to see more pretty shapes... Fun challenge! :)
1
u/haquaman_reddit Jun 06 '14
Could optimize this more, did a similar project in uni in distributed programming
$ node --version v0.10.26
$ npm list /home/me ├─┬ sleep@1.1.6 │ └── mkdirp@0.3.5 └─┬ split@0.3.0 └── through@2.3.4
$ cat gameoflife.js | sed 's// /'
var sleep = require('sleep');
$ echo "32 17 17
................. ................. ....###...###.... ................. ..#....#.#....#.. ..#....#.#....#.. ..#....#.#....#.. ....###...###.... ................. ....###...###.... ..#....#.#....#.. ..#....#.#....#.. ..#....#.#....#.. ................. ....###...###.... ................. ................." | node gameoflife.js | sed 's// /'
1
u/easher1 Jun 09 '14
Credit to /u/ehcubed newGrid Algorithm
1
u/codemac Jun 10 '14
Here it is in Go. i'm suprised about a few things with go:
So here is the code!: package main
1
u/TSLRed Jun 10 '14 edited Jun 10 '14
Java
I had a lot of fun with this challenge. There were a lot of small issues along the way, but nothing major. My solution's a little buggy still; you can make it freeze by entering nothing on a line when inputting the initial configuration, but other than that, it works well enough.
If anybody has suggestions to improve the code, please tell me. I could use the help. Anything about the freezing issue in particular would be much appreciated. For some reason, entering nothing in a line causes the program to get stuck on the scanner's "next" method call. I'm not entirely sure why it does, but I don't completely understand how scanners work, so that's making it tough to figure out.
Code:
Challenge output:
1
u/felix1429 Jun 12 '14 edited Jun 17 '14
PHP
Edited, now works with challenge input
Edited again, now works with non-square grids and prints each generation
Edit 3, because I know you're all waiting with bated breath for these. Optimized a bit.
1
u/FedeMP Jul 05 '14
Coffeescript in NodeJS. Use
new Game './board'0
u/[deleted] Jun 01 '14
[deleted] — view deleted comment