This repository has been archived by the owner on Mar 8, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mapgentest.rb
91 lines (78 loc) · 2.22 KB
/
mapgentest.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# Trying to write a map generation algorithm, this file will be integrated into main when it's completed
require "ncurses"
def generate_organic_map(width, height)
map_array = []
for y in 0...height
map_array[y] = "#{rand(2)}" * width
for x in 0...width
map_array[y][x] = rand(2).to_s
end
end
# Replace all 1's with #'s and everying else with spaces
map_array.each_index do |n|
for x in 0...map_array[n].length
if map_array[n][x] == "1"
map_array[n][x] = "#"
else
map_array[n][x] = " "
end
end
# Ensure the left and right of the map are solid walls
map_array[n][0] = "#"
map_array[n][-1] = "#"
end
map_array[0] = "#" * map_array[0].length
map_array[-1] = "#" * map_array[-1].length
return map_array
end
def do_simulation_step(old_array)
map_array = old_array
end
def count_alive_neighbors(map_array, x, y)
count = 0
for i in -1...2
for j in -1...2
neighbour_x = x + i
neighbour_y = y + j
if (i == 0 && j == 0)
# Do nothing
elsif (neighbour_x < 0 || neighbour_y < 0 || neighbour_x >= map.length || neighbour_y >= map[0].length)
count += 1
elsif (map_array[neighour_x][neighbour_y])
count += 1
end
end
end
return count
end
def init_scr
begin
Ncurses.initscr # Initialize ncurses
Ncurses.curs_set(0) # Hide cursor
Ncurses.cbreak # Provide unbuffered input
Ncurses.noecho # Turn off input echoing
Ncurses.nonl # Turn off newline translation
Ncurses.stdscr.intrflush(false) # Turn off flush-on-interrupt
Ncurses.stdscr.keypad(true) # Turn on keypad mode
# Start the game_loop if user presses Y
game_map = generate_organic_map(rand(30) + 5, rand(10) + 5)
for rows in 0..game_map.length
Ncurses.stdscr.mvaddstr(rows, 0, game_map[rows].to_s)
end
if Ncurses.stdscr.getch == 32
Ncurses.stdscr.clear
Ncurses.echo
Ncurses.nocbreak
Ncurses.nl
Ncurses.endwin
init_scr
end
ensure
# Clean up everything when we're done using ncurses
Ncurses.echo
Ncurses.nocbreak
Ncurses.nl
Ncurses.endwin
end
end
init_scr