Skip to content

Instantly share code, notes, and snippets.

@ETBlue
Created February 19, 2013 01:31
Show Gist options
  • Save ETBlue/4982335 to your computer and use it in GitHub Desktop.
Save ETBlue/4982335 to your computer and use it in GitHub Desktop.
def get_size
print"\n how many rows/columns do you wish to have? (please input a number between 4 and 100:) "
# ask user to input the correct format over and over again
while
table_size=gets.chomp.to_i
# start over if the format is incorrect
if !table_size || table_size<4 || table_size>100
print"\n please give me an integer between 4 and 100: "
next
end
# jump out of the loop if the format is correct
break
end
# return the value when done
table_size
end
def initialize_table(table_size)
# initialize a 2 dimentional array and give feedback to user
table=Array.new(table_size) {Array.new(table_size){0}}
puts"roger. your life table is established with the size of #{table_size}!"
# return the initialized 2d array
table
end
def get_cells(table_size)
print"\n what is the coordinate of the living cell? (please input a pair of numbers between 0 and #{table_size-1}, seperated by a space. for example, 2 3:) (or simply type d for default value) "
# prepare a variable for the loop later
to_beginning=true
cells=[]
# get the cell coordinate over and over again
while to_beginning==true
cell_coordinate=gets.chomp
# if user wanna use a default value
if cell_coordinate=="d"
puts"generating default cells..."
# make up a random set of cells
cell_numbers=rand(4..table_size**2)
cell_numbers.times {
cells<<"#{rand(0..table_size-1)} #{rand(0..table_size-1)}"
}
puts"roger. cell #{cells} is now alive!"
break
# ask user to input the correct format again
elsif !cell_coordinate || !cell_coordinate.match(/[0-#{table_size-1}] [0-#{table_size-1}]/)
print"\n please give me a pair of integers between 0 and #{table_size-1}, seperated by a space, for example, 2 3. or simply type d for default value: "
next
end
# ask user if he/she wanna input another cell
while to_beginning==true
# append the coordinate to the cells list
cells<<cell_coordinate
puts"roger. cell #{cells} is now alive!"
print"\n any other living cell? (y/n:) "
other_cells=gets.chomp
# allow to input coordinate directly as a shortcut
if other_cells.match(/[0-#{table_size-1}] [0-#{table_size-1}]/)
cell_coordinate=other_cells
next
elsif other_cells=="y"
print"\n what is the coordinate of the living cell? (a pair of numbers between 0 and #{table_size-1}, or simply type d for default value again:) "
break
else
to_beginning=false
break
end
end
end
# feedback and return value
puts"initial cells setup completed. current living cells are: "
puts cells
cells
end
def setup_cells(cells,table)
# split the input string into number arrays
cells.each{|coordinate|
coordinate=coordinate.split(" ").map{|s|s.to_i}
table[coordinate[0]][coordinate[1]]=1
}
# return the updated table
table
end
def draw_table(table,time=0)
# customized feedback messege
if time==-1
puts"it's now look like:"
elsif time==0
puts"your life table is set successfully as:"
elsif time==1
puts"the #{time}st evolve:"
elsif time==2
puts"the #{time}nd evolve:"
elsif time==3
puts"the #{time}rd evolve:"
else
puts"the #{time}th evolve:"
end
# draw the life table according to current data
table.each{|row|
row.each{|column|
print column==1 ? " O":" -"
}
print"\n"
}
end
def get_evolve_times
print"\n how many times do you want the table to run? (please input a number between 1 and 100:) "
# try to get a number from user
while
evolve_times=gets.chomp.to_i
if evolve_times<2
print"\n please give me a number greater than 1: "
next
else
puts"roger. the life table is going to evolve for #{evolve_times} times."
break
end
end
#return value in the end
evolve_times
end
def evolve(table_size,table,evolve_times)
evolve_times.times do |time|
# duplicate a temporary table for counting
old_table=Marshal.load(Marshal.dump(table))
# a nested iterator mapping to the nested array
(0..table_size-1).each{|row|
(0..table_size-1).each{|column|
# setup the conditional neighbor cell coordinates to avoid the array size from being modified
left=old_table[row][column-1]
right= column==table_size-1 ? old_table[row][0]:old_table[row][column+1]
up=old_table[row-1][column]
down= row==table_size-1 ? old_table[0][column]:old_table[row+1][column]
# override the table cell value according to neighbors amount
neighbors=left+right+up+down
if neighbors>2
table[row][column]=1
elsif neighbors<2
table[row][column]=0
end
}
}
# give feedback to user
draw_table(table,time+1)
# and then next
end
end
def ending(table_size,evolve_times)
print"\n congradulations! you have finished the life table journey with a #{table_size} x #{table_size} sized table and evolved for #{evolve_times} times. one more round? (y/n:) "
# ask if user wants another round and set value for while loop condition
one_more_round=gets.chomp
if one_more_round!="y"
puts"thanks for playing life table. see you next time! :D"
false
else
puts"roger. one more round begins!\n-----------------------------------"
true
end
end
# program starts!
one_more_round=true
# play the program and check if user wanna play again in the end
while one_more_round!=false
# setup table size
table_size=get_size
table=initialize_table(table_size)
draw_table(table,-1)
# setup alive cells
cells=get_cells(table_size)
# setup table with alive cells
table=setup_cells(cells,table)
draw_table(table)
# evolve the life table
evolve_times=get_evolve_times
evolve(table_size,table,evolve_times)
# ending
one_more_round=ending(table_size,evolve_times)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment