Class: Ascii_grid

Inherits:
Object
  • Object
show all
Defined in:
lib/ascii_grid.rb

Overview

This object contains the general size and shape of a grid. The object’s methods can then be used to print that grid to the terminal.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(x_size, y_size, x_offset, y_offset) ⇒ Ascii_grid

Returns a new instance of Ascii_grid.



20
21
22
23
24
25
26
27
28
# File 'lib/ascii_grid.rb', line 20

def initialize(x_size, y_size, x_offset, y_offset)
    @x_size = x_size.to_i
    @y_size = x_size.to_i
    @x_offset = x_offset.to_i
    @y_offset = y_offset.to_i
    @point = "\e[31m@\e[0m"
    @blank_space = "+"
    @clear = false
end

Instance Attribute Details

#blank_spaceObject

This is what will be printed as blank space in the grid, should only be one character



16
17
18
# File 'lib/ascii_grid.rb', line 16

def blank_space
  @blank_space
end

#clearObject

Bolean value, if true the clear command will be run when the make_grid method is called



18
19
20
# File 'lib/ascii_grid.rb', line 18

def clear
  @clear
end

#pointObject

This is what will be printed as a point, should only be one character



14
15
16
# File 'lib/ascii_grid.rb', line 14

def point
  @point
end

#x_offsetObject

Helps decide origin, make it exactly 1/2 of x_size to keep (0, 0) in the center



10
11
12
# File 'lib/ascii_grid.rb', line 10

def x_offset
  @x_offset
end

#x_sizeObject

Grid Width



6
7
8
# File 'lib/ascii_grid.rb', line 6

def x_size
  @x_size
end

#y_offsetObject

Helps decide origin, make it exactly 1/2 of y_size to keep (0, 0) in the center



12
13
14
# File 'lib/ascii_grid.rb', line 12

def y_offset
  @y_offset
end

#y_sizeObject

Grid Height



8
9
10
# File 'lib/ascii_grid.rb', line 8

def y_size
  @y_size
end

Instance Method Details

#convert_valuesObject

This method should convert all of the object’s attributes into the correct data type. You shouldn’t have to use this unless you are getting weird errors.



95
96
97
98
99
100
101
102
# File 'lib/ascii_grid.rb', line 95

def convert_values
    @x_size = @x_size.to_i
    @y_size = @y_size.to_i
    @x_offset = @x_offset.to_i
    @y_offset = @y_offset.to_i
    @point = @point.to_s
    @blank_space = @blank_space.to_s
end

#make_grid(*points) ⇒ Object

This method prints the grid to the terminal. It takes an array of x and y values that will be printed as points.

Example:

ascii_grid.make_grid(1, 2, 3, -4)

This will print the points (1, 2) and (3, -4)

It also returns a string with the grid that was printed.



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
# File 'lib/ascii_grid.rb', line 38

def make_grid(*points)
    self.convert_values()
    printed = ""
    if (@clear)
        system ('clear')
    end
    x_points = Array.new
    y_points = Array.new
    # Divide points
    (0..points.length - 1).each do |position|
        if (position % 2 == 0)
            x_points.push(points[position].to_i)
        else
            y_points.push(points[position].to_i)
        end
    end
    # Start loop
    (0..@y_size.to_i).each do |current_y|
        (0..@x_size.to_i).each do |current_x|
            # Setup
            print_fill = true
            relative_x = current_x - @x_offset
            relative_y = @y_offset - current_y
            point_amount = x_points.length - 1
            # Check point
            (0..point_amount.to_i).each do |position_point|
                if (x_points[position_point] == relative_x)
                    if (y_points[position_point] == relative_y)
                        printed << @point.to_s
                        print_fill = false
                    end
                end
            end
            # Fill
            if (print_fill)
                if (@x_offset == current_x)
                    printed << "|"
                elsif (@y_offset == current_y)
                    printed << "-"
                else
                    printed << @blank_space.to_s
                end
            end
            if (y_offset == current_y && current_x != @x_size)
                printed << "-"
            else
                printed << " "
            end
        end
        printed << "\n"
    end
    print printed
    return printed
end