Skip to content

Instantly share code, notes, and snippets.

@AnimaWish
Created September 15, 2012 02:19
Show Gist options
  • Save AnimaWish/3726086 to your computer and use it in GitHub Desktop.
Save AnimaWish/3726086 to your computer and use it in GitHub Desktop.
# TO DO: fix difficulty
columns = Array.new
# rows inside columns ; (y, x)
def createmap(mcolumns) #sticks rows in columns
asdf = 0
while asdf < $boardy
rows = Array.new($boardx) {'.'}
mcolumns.push(rows)
asdf += 1
end
asdf = 0
while asdf < (($boardx + $boardy)/2 - 1) #--sets bombs
a = [rand($boardy), rand($boardx)]
if mcolumns[a[0]][a[1]] != '+'
mcolumns[a[0]][a[1]] = '+'
else
asdf -= 1
end
asdf += 1
end
mplayer = 'o'
playerspace = [rand($boardy), rand($boardx)] #--sets player
while mcolumns[playerspace[0]][playerspace[1]] == '+'
playerspace = [rand($boardy), rand($boardx)]
end
mcolumns[playerspace[0]][playerspace[1]] = 'o'
$playerloc = playerspace
end
def drawmap(mcolumns) #draws the map
mcolumns.each do |x|
puts "%s "*$boardx % x
end
end
def losecheck(mcolumns, x, y)
if mcolumns[$playerloc[0] + y][$playerloc[1] + x] != '.'
puts
puts 'Score: ' + mcolumns.flatten.count('.').to_s
($boardy/2).times do
puts '* '*$boardx
end
if $boardx%2 == 0
puts '* '*($boardx/2-2) + 'Oops. ' + '* '*($boardx/2-1)
else
puts '* '*($boardx/2-1) + 'Oops. ' + '* '*($boardx/2-1)
end
($boardy/2).times do
puts '* '*$boardx
end
exit
end
end
def move(cmd, mcolumns)
def procedure(x, y, mcolumns)
if ($playerloc[0] + y) == $boardy or ($playerloc[0] + y) == $boardy*(-1)
$playerloc[0] += $boardy*(-y)
elsif ($playerloc[1] + x) == $boardx or ($playerloc[1] + x) == $boardx*(-1)
$playerloc[1] += $boardx*(-x)
end
losecheck(mcolumns, x, y)
mcolumns[$playerloc[0]][$playerloc[1]] = '#'
$playerloc[0] += y
$playerloc[1] += x
mcolumns[$playerloc[0]][$playerloc[1]] = 'o'
end
case cmd
when 'w'
procedure(0, -1, mcolumns)
when 's'
procedure(0, 1, mcolumns)
when 'a'
procedure(-1, 0, mcolumns)
when 'd'
procedure(1, 0, mcolumns)
when 'cheat'
wincheck(['#']*mcolumns.size) #replaces the whole board with hashes
end
end
def wincheck(mcolumns)
unless mcolumns.flatten.include? '.'
puts
($boardy/2).times do
puts '# '*$boardx
end
if $boardx%2 == 0
puts '# '*($boardx/2-2) + 'Win!! ' + '# '*($boardx/2-1)
else
puts '# '*($boardx/2-1) + 'Win!! ' + '# '*($boardx/2-1)
end
($boardy/2).times do
puts '# '*$boardx
end
exit
end
end
def doitbro(mcolumns)
puts 'Score: ' + mcolumns.flatten.count('.').to_s
drawmap(mcolumns)
#print $playerloc
wincheck(mcolumns)
end
puts
puts '+---------------+'
puts '|IceBreaker v2.0|'
puts '+---------------+'
puts 'Directions: WASD (followed by enter) to move.'
puts ' Fill the screen with hashes (#), but avoid the bombs (+)!'
puts ' The lower the score, the better! Warning: Maps are randomly'
puts ' generated, and thus not guaranteed to be solvable.'
puts
puts 'Please enter desired board size: x,y (Enter "d" to default to 5x5)'
sizey = gets.chomp.split(',')
puts sizey
sizey[0] = sizey[0].to_f ; sizey[1] = sizey[1].to_f
if sizey[0]/1 != 0.0 and sizey[1]/1 != 0.0
$boardx = sizey[0].to_i
$boardy = sizey[1].to_i
else
puts "Defaulting to 5x5"
$boardx = 5
$boardy = 5
end
createmap(columns)
loop do
doitbro(columns)
cmd = gets.chomp
move(cmd, columns)
puts
end
puts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment