Mike N. wrote:
I took some golfed code from a discussion here little while back (
Search results for '' - Ruby-Forum
) and tried my hand at de-golfing and re-working it a bit to make it
readable.
What version did you deobfuscate?
I have here the version on J`ey’s site, and then a three-line version of
mine.
I used these clarification methods:
- add whitespace
- add comments
- rename variables
- translate conditional tri-graphs (foo?bar:baz) to if blocks
Cheers,
Dave
#############################################################################
j`ey (with thanks to Florian G. and Dave B.)
305 characters
#############################################################################
s,o,f,c,u=20,“#”," “;b=(z=0…s).map{z.map{(rand<0.3)?o:f}};$><<”\e[2J";until
c==b;$><<“\e[H”<<b.map{|x|x*" “}*”\n";c=b.map{|z|z.dup};s.times{|i|s.times{|
e|n=(q,z=-1…1,[];q.map{|x|q.map{|y|z<<[x,y]}};(z-=[[0,0]]).map{|x|c[i+x[0]][
e+x[1]]rescue u}-[f,u]).size;b[i][e]=(n==2?b[i][e]:n==3?o:f)}};sleep 0.2;end
size, occupied, forsaken, combat_area_last_turn, undefined =
20, “#”, " " # nil, nil
battlefield = (z = 0…size).map {
z.map {
if rand < 0.3
occupied
else
forsaken
end
}
}
$> << “\e[2J”
until combat_area_last_turn == battlefield
$> << “\e[H” <<
battlefield.map {|x| x * " " } * “\n”
combat_area_last_turn = battlefield.map {|z| z.dup }
iterate over the entire grid
size.times {|i|
size.times {|e|
neighbour_count =
(q, z = -1..1, []
q.map {|x| q.map {|y| z << [x, y] } }
# x iterates over the set of 8 relative neighbour coordinates
(z -= [[0, 0]]).map {|x|
# return the neighbour's value from last turn
combat_area_last_turn[i + x[0]][e + x[1]] rescue undefined
} - [forsaken, undefined] # don't count empties or
off-the-edges
).size
# update the cell based on last turn's neighbour count
battlefield[i][e] =
if neighbour_count == 2
battlefield[i][e]
else
if neighbour_count == 3
occupied
else
forsaken
end
end
}
}
sleep 0.2
end
And my version. You will need to hit Ctrl+Break to stop it.
o,f,m,c=“#”,"
“,[-1,0,1]*3;b=([7]20).map{(0…20).map{(rand<0.3)?o:f}};loop{
$><<b.map{|x|xf+”\n"};c=b.map{|x|x.dup};20.times{|i|20.times{|e|n=(m.sort.zip(
m)-[[0,0]]).select{|x,y|o==(c[i+x]||[])[e+y]}.size;n!=2?b[i][e]=n==3?o:f:9}}}
And the same thing with a little whitespace.
o = “#”
f = " "
c = nil # copy of b
b # main grid
m = [-1, 0, 1] * 3
b = ([7] * 20).map { (0…20).map { (rand < 0.3) ? o : f } }
loop {
$> << b.map {|x| x * f + “\n” }
c = b.map {|x| x.dup }
20.times {|i|
20.times {|e|
n =
(m.sort.zip(m) - [[0, 0]]).select {|x, y|
o == (c[i + x] || [])[e + y]
}.size
n != 2 ? b[i][e] = n == 3 ? o :
f :
9
}
}
#sleep 0.1
}