Class: NewLogo

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(n) ⇒ NewLogo

Returns a new instance of NewLogo.



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/logor.rb', line 5

def initialize(n)
  @size=n
  if @size%2!=0 && @size>1 
    @a = Array.new(@size) { Array.new(@size,".")}
    @a[@size/2][@size/2]="X"
    @pointer=[@size/2,@size/2]
    @flag=true
  else
    @flag=false
    puts "Enter an odd number greater than 1."
  end
end

Instance Attribute Details

#aObject

Returns the value of attribute a.



4
5
6
# File 'lib/logor.rb', line 4

def a
  @a
end

#flagObject

Returns the value of attribute flag.



4
5
6
# File 'lib/logor.rb', line 4

def flag
  @flag
end

#pointerObject

Returns the value of attribute pointer.



4
5
6
# File 'lib/logor.rb', line 4

def pointer
  @pointer
end

Instance Method Details

#[](x, y) ⇒ Object



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

def [](x,y)
  @a[x][y]
end

#[]=(x, y, value) ⇒ Object



22
23
24
# File 'lib/logor.rb', line 22

def []=(x,y,value)
  @a[x][y]=value
end

#down(value) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/logor.rb', line 46

def down(value)
  start=@pointer[0]
  @pointer[0]+=value
  goal=@pointer[0]
  print @pointer
  if goal < @size
    while start!=goal
      start+=1
      @a[start][@pointer[1]]="X"
    end
  else
    @pointer[0]=start
  end
end

#left(value) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/logor.rb', line 61

def left(value)
  start=@pointer[1]
  @pointer[1]-=value
  goal=@pointer[1]
  print @pointer
  if goal >= 0
    while start!=goal
      start-=1
      @a[@pointer[0]][start]="X"
    end
  else
    @pointer[1]=start
  end
end

#right(value) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/logor.rb', line 76

def right(value)
  start=@pointer[1]
  @pointer[1]+=value
  goal=@pointer[1]
  print @pointer
  if goal < @size
    while start!=goal
      start+=1
      @a[@pointer[0]][start]="X"
    end
  else
    @pointer[1]=start
  end
end

#up(value) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/logor.rb', line 31

def up(value)
  start=@pointer[0]
  @pointer[0]-=value
  goal=@pointer[0]
  print @pointer
  if goal >= 0
    while start!=goal
      start-=1
      @a[start][@pointer[1]]="X"
    end
  else
    @pointer[0]=start
  end
end

#viewObject



26
27
28
29
# File 'lib/logor.rb', line 26

def view
  system 'clear'
  @a.each {|x| puts x.map{|y| y}.join(" ")}
end