Class: Nuts

Inherits:
Object
  • Object
show all
Includes:
Directions_7
Defined in:
lib/games_paradise/rubynuts/rubynuts.rb

Constant Summary

Constants included from Directions_7

Directions_7::DIRECTIONS

Instance Method Summary collapse

Constructor Details

#initializeNuts

Returns a new instance of Nuts.



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/games_paradise/rubynuts/rubynuts.rb', line 58

def initialize
  @count = 0
  @nuts = [ Nut.new(4, 2, 5, 3, 1, 6),
            Nut.new(2, 1, 6, 5, 4, 3),
            Nut.new(1, 4, 6, 2, 3, 5),
            Nut.new(1, 6, 5, 3, 2, 4),
            Nut.new(1, 4, 3, 6, 5, 2),
            Nut.new(1, 6, 2, 4, 5, 3),  
            Nut.new(1, 2, 3, 4, 5, 6) ]
  spin_random
  @nuts.shuffle
end

Instance Method Details

#actionObject

action



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/games_paradise/rubynuts/rubynuts.rb', line 144

def action
  prompt = ( @count == 0 ? "Type \"help\" for command list: " : "Make move #{@count + 1}: ")
  decide = command(prompt) 
  case decide
    when /solve/
       until all_solved? do
         spin_random; @nuts.shuffle            
       end
       display
       exit
    when /swap\s+(\d)\s+(\d)/
      swap($1.to_i, $2.to_i)
      @count += 1
    when /(spin|rotate)\s+(\d)\s+(\w+)/
      spin_ccw($1.to_i) if $3 == "ccw"
      spin_cw($1.to_i)  if $3 != "ccw"
      @count += 1
    when /spin\s+(\d)\s*$/
      spin_cw($1.to_i)
      @count += 1
    when /help/, /h/
      print "\nSpin and swap nuts until numbers on nuts match numbers on adjacent nuts.\n\n"
      print "Commands:  (help - shows this list of commands)  \n"
      print "  display - displays puzzle (or just press \"enter\")\n"         
      print "  spin # ccw (or cw, by default) - rotates a nut\n"           
      print "  swap # # - swaps two nuts\n"                
      print "  exit - give up and exit puzzle session\n"                
      print " Nuts are referenced with a single digit number: \n"
      print "          _____  \n" 
      print "         /     \\ \n"
      print "   _____/   0   \\_____ \n"
      print "  /     \\       /     \\ \n"
      print " /   5   \\_____/   1   \\ \n"
      print " \\       /     \\       / \n"
      print "  \\_____/   6   \\_____/ \n"
      print "  /     \\       /     \\ \n"
      print " /   4   \\_____/   2   \\ \n"
      print " \\       /     \\       / \n"
      print "  \\_____/   3   \\_____/ \n"
      print "        \\       / \n"
      print "         \\_____/ \n"
      return 'help'
    when /(exit|quit|stop|end)/
      exit
  end
end

#all_solved?Boolean

Returns:

  • (Boolean)


133
134
135
# File 'lib/games_paradise/rubynuts/rubynuts.rb', line 133

def all_solved?
  edges_solved? and center_solved?
end

#center_solved?Boolean

center_solved?

Returns:

  • (Boolean)


125
126
127
# File 'lib/games_paradise/rubynuts/rubynuts.rb', line 125

def center_solved?
  (@nuts[N].side(S) == @nuts[C].side(N)) and  (@nuts[NE].side(SW)  == @nuts[C].side(NE)) and (@nuts[SE].side(NW) == @nuts[C].side(SE)) and (@nuts[S].side(N) == @nuts[C].side(S)) and (@nuts[SW].side(NE) == @nuts[C].side(SW)) and (@nuts[NW].side(SE) == @nuts[C].side(NW))
end

#command(prompt) ⇒ Object

command



138
139
140
141
# File 'lib/games_paradise/rubynuts/rubynuts.rb', line 138

def command(prompt)
  print "\n#{prompt}"
  gets
end

#displayObject

#

display

Display the hexagons.

#


80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/games_paradise/rubynuts/rubynuts.rb', line 80

def display
  moves  = all_solved? ? "Moves to solve: #{@count}" : "Moves so far: #{@count}"
  center = center_solved? ? "Center nut numbers match outer nuts" : ""
  edges  = edges_solved? ? "Outer nut numbers match outer nuts" : ""
  print "          _____  \n" # Ride the comet!
  print "         /  #{@nuts[N].side(N)}  \\  #{moves} \n"
  print "   _____/ #{@nuts[N].side(NW)}   #{@nuts[N].side(NE)} \\_____ \n"
  print "  /  #{@nuts[NW].side(N)}  \\ #{@nuts[N].side(SW)}   #{@nuts[N].side(SE)} /  #{@nuts[NE].side(N)}  \\ \n"
  print " / #{@nuts[NW].side(NW)}   #{@nuts[NW].side(NE)} \\__#{@nuts[N].side(S)}__/ #{@nuts[NE].side(NW)}   #{@nuts[NE].side(NE)} \\ \n"
  print " \\ #{@nuts[NW].side(SW)}   #{@nuts[NW].side(SE)} /  #{@nuts[C].side(N)}  \\ #{@nuts[NE].side(SW)}   #{@nuts[NE].side(SE)} / \n"
  print "  \\__#{@nuts[NW].side(S)}__/ #{@nuts[C].side(NW)}   #{@nuts[C].side(NE)} \\__#{@nuts[NE].side(S)}__/ \n"
  print "  /  #{@nuts[SW].side(N)}  \\ #{@nuts[C].side(SW)}   #{@nuts[C].side(SE)} /  #{@nuts[SE].side(N)}  \\ \n"
  print " / #{@nuts[SW].side(NW)}   #{@nuts[4].side(NE)} \\__#{@nuts[C].side(S)}__/ #{@nuts[SE].side(NW)}   #{@nuts[SE].side(NE)} \\ \n"
  print " \\ #{@nuts[SW].side(SW)}   #{@nuts[4].side(SE)} /  #{@nuts[S].side(N)}  \\ #{@nuts[SE].side(SW)}   #{@nuts[SE].side(SE)} / \n"
  print "  \\__#{@nuts[SW].side(S)}__/ #{@nuts[S].side(NW)}   #{@nuts[S].side(NE)} \\__#{@nuts[SE].side(S)}__/ \n"
  print "        \\ #{@nuts[S].side(SW)}   #{@nuts[S].side(SE)} / #{center}\n"
  print "         \\__#{@nuts[S].side(S)}__/ #{edges}\n"
end

#display_each_nutObject



71
72
73
# File 'lib/games_paradise/rubynuts/rubynuts.rb', line 71

def display_each_nut
  @nuts.each {|n| n.display }
end

#edges_solved?Boolean

Returns:

  • (Boolean)


129
130
131
# File 'lib/games_paradise/rubynuts/rubynuts.rb', line 129

def edges_solved?
  (@nuts[N].side(SE) == @nuts[NE].side(NW)) and  (@nuts[NE].side(S) == @nuts[SE].side(N))  and (@nuts[SE].side(SW) == @nuts[S].side(NE)) and (@nuts[S].side(NW) == @nuts[SW].side(SE))  and (@nuts[NW].side(S) == @nuts[SW].side(N)) and (@nuts[NW].side(NE) == @nuts[N].side(SW))
end

#in_range?(nut) ⇒ Boolean

#

in_range?

#

Returns:

  • (Boolean)


102
103
104
# File 'lib/games_paradise/rubynuts/rubynuts.rb', line 102

def in_range?(nut)
  nut.between?(0, 6)
end

#spin_ccw(nut) ⇒ Object



110
111
112
# File 'lib/games_paradise/rubynuts/rubynuts.rb', line 110

def spin_ccw(nut)
  @nuts[nut].spin_ccw if in_range?(nut)
end

#spin_cw(nut) ⇒ Object



106
107
108
# File 'lib/games_paradise/rubynuts/rubynuts.rb', line 106

def spin_cw(nut)
  @nuts[nut].spin_cw if in_range?(nut)
end

#spin_randomObject

spin_random



115
116
117
# File 'lib/games_paradise/rubynuts/rubynuts.rb', line 115

def spin_random
  @nuts.each { |n| rand(6).times { n.spin_cw } }
end

#swap(nut_a, nut_b) ⇒ Object

swap



120
121
122
# File 'lib/games_paradise/rubynuts/rubynuts.rb', line 120

def swap(nut_a, nut_b)
  @nuts[nut_b], @nuts[nut_a] = @nuts[nut_a], @nuts[nut_b] if in_range?(nut_a) and in_range?(nut_b)
end