# Torbert, 11.6.2007 $n=8 # N-Queens $ec=27.chr # ANSI Colors $max=$n*($n-1)/2 # Worst Case def show board puts for r in 0...$n s="" for c in 0...$n if (r+c)%2==1 s+=$ec+"[31;46" else s+=$ec+"[31;43" end s+="m" if board[c]==r s+=$ec+"[1m"+"X" else s+=" " end end puts $ec+"[0m"+s+$ec+"[0m" end puts $ec+"[0m" end def main board=[] # Sparse 2-D board stored in 1-D. for k in 0...$n # Index is column. board << rand($n) # Value is row. end show board end main