ascii_grid 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/ascii_grid.rb +104 -0
  3. metadata +45 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: fb0f6f17868c2df090dec0927f1a4d87c11e3dcd
4
+ data.tar.gz: a15e60a06cd6ff2b05931e457d3ea9839eb76ce4
5
+ SHA512:
6
+ metadata.gz: 55871bc089b2641da7f50c3073e9f33e40aa11e6e62db2e715838970236e6a5d87c3f43c2a3f795e297677a893f5e90f94e5c79b2a37cbb152c306ba1fb5f896
7
+ data.tar.gz: 40c89be87c9794f0fc34b7dbb3bfab6a8c43c1e9c5d69b843eafe3d04a5e83735947432e3797eca19bc324e1c10d4084f94bcec1f7cc2652c1ae871c14a3654a
data/lib/ascii_grid.rb ADDED
@@ -0,0 +1,104 @@
1
+ # This object contains the general size and shape of a grid. The object's methods
2
+ # can then be used to print that grid to the terminal.
3
+ class Ascii_grid
4
+
5
+ # Grid Width
6
+ attr_accessor :x_size
7
+ # Grid Height
8
+ attr_accessor :y_size
9
+ # Helps decide origin, make it exactly 1/2 of x_size to keep (0, 0) in the center
10
+ attr_accessor :x_offset
11
+ # Helps decide origin, make it exactly 1/2 of y_size to keep (0, 0) in the center
12
+ attr_accessor :y_offset
13
+ # This is what will be printed as a point, should only be one character
14
+ attr_accessor :point
15
+ # This is what will be printed as blank space in the grid, should only be one character
16
+ attr_accessor :blank_space
17
+ # Bolean value, if true the clear command will be run when the make_grid method is called
18
+ attr_accessor :clear
19
+
20
+ def initialize(x_size, y_size, x_offset, y_offset)
21
+ @x_size = x_size.to_i
22
+ @y_size = x_size.to_i
23
+ @x_offset = x_offset.to_i
24
+ @y_offset = y_offset.to_i
25
+ @point = "@"
26
+ @blank_space = "+"
27
+ @clear = false
28
+ end
29
+
30
+ # This method prints the grid to the terminal. It takes an array of x and y values that will be printed as points.
31
+ #
32
+ # Example:
33
+ # ascii_grid.make_grid(1, 2, 3, -4)
34
+ #
35
+ # This will print the points (1, 2) and (3, -4)
36
+ #
37
+ # It also returns a string with the grid that was printed.
38
+ def make_grid(*points)
39
+ self.convert_values()
40
+ printed = ""
41
+ if (@clear)
42
+ system ('clear')
43
+ end
44
+ x_points = Array.new
45
+ y_points = Array.new
46
+ # Divide points
47
+ (0..points.length - 1).each do |position|
48
+ if (position % 2 == 0)
49
+ x_points.push(points[position].to_i)
50
+ else
51
+ y_points.push(points[position].to_i)
52
+ end
53
+ end
54
+ # Start loop
55
+ (0..@y_size.to_i).each do |current_y|
56
+ (0..@x_size.to_i).each do |current_x|
57
+ # Setup
58
+ print_fill = true
59
+ relative_x = current_x - @x_offset
60
+ relative_y = @y_offset - current_y
61
+ point_amount = x_points.length - 1
62
+ # Check point
63
+ (0..point_amount.to_i).each do |position_point|
64
+ if (x_points[position_point] == relative_x)
65
+ if (y_points[position_point] == relative_y)
66
+ printed << @point.to_s
67
+ print_fill = false
68
+ end
69
+ end
70
+ end
71
+ # Fill
72
+ if (print_fill)
73
+ if (@x_offset == current_x)
74
+ printed << "|"
75
+ elsif (@y_offset == current_y)
76
+ printed << "-"
77
+ else
78
+ printed << @blank_space.to_s
79
+ end
80
+ end
81
+ if (y_offset == current_y)
82
+ printed << "-"
83
+ else
84
+ printed << " "
85
+ end
86
+ end
87
+ printed << "\n"
88
+ end
89
+ print printed
90
+ return printed
91
+ end
92
+
93
+ # This method should convert all of the object's attributes into the correct data type.
94
+ # You shouldn't have to use this unless you are getting weird errors.
95
+ def convert_values
96
+ @x_size = @x_size.to_i
97
+ @y_size = @y_size.to_i
98
+ @x_offset = @x_offset.to_i
99
+ @y_offset = @y_offset.to_i
100
+ @point = @point.to_s
101
+ @blank_space = @blank_space.to_s
102
+ end
103
+
104
+ end
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ascii_grid
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Harim
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-04-26 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: This gem should allow you to easily create grids in the terminal and
14
+ display points in that grid
15
+ email: harimscode@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/ascii_grid.rb
21
+ homepage:
22
+ licenses:
23
+ - MIT
24
+ metadata: {}
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubyforge_project:
41
+ rubygems_version: 2.5.2.1
42
+ signing_key:
43
+ specification_version: 4
44
+ summary: Create a grid in the terminal
45
+ test_files: []