Last active
January 10, 2019 14:59
-
-
Save kazoo04/11133090 to your computer and use it in GitHub Desktop.
Dr. Mario: placement viruses
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: utf-8 | |
WIDTH = 8 | |
HEIGHT = 16 | |
$field = Array.new(WIDTH).map{Array.new(HEIGHT, :none)} | |
def cell(x, y, offset_x = 0, offset_y = 0) | |
_x = x + offset_x | |
_y = y + offset_y | |
return :none if _x < 0 or _x >= WIDTH | |
return :none if _y < 0 or _y >= HEIGHT | |
$field[_x][_y] | |
end | |
def placeable?(x, y, virus) | |
return false if $field[x][y] != :none | |
x_len = y_len = 0 | |
for i in -2..2 | |
x_len += 1 | |
y_len += 1 | |
x_len = 0 if cell(x, y, i, 0) != virus and i != 0 | |
y_len = 0 if cell(x, y, 0, i) != virus and i != 0 | |
return false if x_len >= 3 or y_len >= 3 | |
end | |
true | |
end | |
def put_virus(margin) | |
r = Random.new() | |
x = r.rand(WIDTH) | |
y = r.rand(margin..HEIGHT) | |
virus = [:red, :blue, :yellow].sample | |
if placeable?(x, y, virus) | |
$field[x][y] = virus | |
return true | |
end | |
false | |
end | |
def draw() | |
characters = { | |
:none => " ", | |
:red => "◯", | |
:blue => "●", | |
:yellow => "◎", | |
} | |
for y in 0...HEIGHT | |
for x in 0...WIDTH | |
print characters[cell(x, y)] | |
end | |
puts | |
end | |
end | |
level = 16 | |
num_viruses = 4 * (level + 1) | |
margin = 6 | |
margin = 5 if level >= 15 | |
margin = 4 if level >= 17 | |
i = 0 | |
while i < num_viruses do | |
i += 1 if put_virus(margin) | |
end | |
draw |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment