puzzled 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,6 @@
1
+ .rvmrc
2
+ *.swp
3
+ *.gem
4
+ .bundle
5
+ Gemfile.lock
6
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in puzzled.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,18 @@
1
+ Puzzled?
2
+ ========
3
+ Generates scrambles for, and solves, the Rubik's Cube.
4
+
5
+ Current Status
6
+ --------------
7
+ `Puzzled::RubiksCube.scramble` generates Rubik's Cube scrambles
8
+
9
+ Usage
10
+ -----
11
+
12
+ $ irb
13
+
14
+ > require 'puzzled'
15
+ > cube = Puzzled::RubiksCube.new
16
+ > cube.scramble # generates typical 25-move scramble
17
+ > cube.scramble 5 # generates 5-move scramble
18
+ #=> ["F2","R","F2","U2","L2"]
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,47 @@
1
+ module Puzzled
2
+ class RubiksCube
3
+ GROUPS = [["F","B"],["L","R"],["D","U"]]
4
+ DIRS = ["","'","2"]
5
+
6
+ # Retun Rubik's Cube scramble of size = size
7
+ def scramble(size=25)
8
+ scramble = []
9
+ prev_group = nil
10
+ prev_prev_group = nil
11
+ size.times do
12
+ # Find a move group for the next move
13
+ # We cannot have 3 moves in a row that are in the same group
14
+ group = rand(GROUPS.size)
15
+ if prev_group == nil
16
+ prev_group = group
17
+ elsif prev_prev_group == nil
18
+ prev_prev_group = prev_group
19
+ prev_group = group
20
+ else
21
+ # While we have 3 moves in the same group, pick a new group
22
+ while group == prev_group && group == prev_prev_group
23
+ group = rand(GROUPS.size)
24
+ end
25
+ prev_prev_group = prev_group
26
+ prev_group = group
27
+ end
28
+
29
+ # Find the next move from move group
30
+ # We cannot have two turns in a row on the same face
31
+ move = GROUPS[group][rand(2)]
32
+ if scramble.size > 0
33
+ while move == scramble.last[0] do
34
+ move = GROUPS[group][rand(2)]
35
+ end
36
+ end
37
+
38
+ # Find a direction for this move
39
+ dir = DIRS[rand(DIRS.size)]
40
+
41
+ # Add move to scramble
42
+ scramble << move + dir
43
+ end
44
+ return scramble
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,3 @@
1
+ module Puzzled
2
+ VERSION = "0.0.1"
3
+ end
data/lib/puzzled.rb ADDED
@@ -0,0 +1,6 @@
1
+ require "puzzled/version"
2
+ require "puzzled/rubiks_cube"
3
+
4
+ module Puzzled
5
+ # Your code goes here...
6
+ end
data/puzzled.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "puzzled/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "puzzled"
7
+ s.version = Puzzled::VERSION
8
+ s.authors = ["Chris Hunt"]
9
+ s.email = ["huntca@gmail.com"]
10
+ s.homepage = ""
11
+ s.summary = %q{Rubik's Cube scramble generator.}
12
+ s.description = %q{Generates scrambles for, and solves, the Rubik's Cube.}
13
+
14
+ s.rubyforge_project = "puzzled"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ # specify any dependencies here; for example:
22
+ s.add_development_dependency "rspec"
23
+ # s.add_runtime_dependency "rest-client"
24
+ end
@@ -0,0 +1,62 @@
1
+ require 'puzzled'
2
+
3
+ describe Puzzled::RubiksCube do
4
+ describe "#scramble" do
5
+ before(:each) do
6
+ @rubiks_cube = Puzzled::RubiksCube.new
7
+ end
8
+
9
+ it "returns correct number of moves" do
10
+ scramble_size = 30
11
+ @rubiks_cube.scramble(scramble_size).size.should == scramble_size
12
+ end
13
+
14
+ it "returns correct number of moves when no size is given" do
15
+ @rubiks_cube.scramble.size.should > 0
16
+ end
17
+
18
+ it "contains only valid moves" do
19
+ moves = ["F","B","L","R","D","U"]
20
+ dirs = ["'","2"]
21
+ @rubiks_cube.scramble.each do |move|
22
+ move.size.should > 0
23
+ move.size.should <= 2
24
+ moves.include?(move[0]).should == true
25
+ dirs.include?(move[1]).should == true if move.size > 1
26
+ end
27
+ end
28
+
29
+ it "does not rotate the same face 2 times in a row" do
30
+ prev = nil
31
+ @rubiks_cube.scramble(500).each do |move|
32
+ if prev != nil
33
+ move[0].should_not == prev
34
+ end
35
+ prev = move[0]
36
+ end
37
+ end
38
+
39
+ it "does not rotate parallel faces 3 times in a row" do
40
+ group = { "F" => 1, "B" => 1,
41
+ "L" => 2, "R" => 2,
42
+ "D" => 3, "U" => 3 }
43
+ prev = nil
44
+ prev_prev = nil
45
+ @rubiks_cube.scramble(500).each do |move|
46
+ if prev == nil
47
+ prev = move
48
+ elsif prev_prev == nil
49
+ prev_prev = prev
50
+ prev = move
51
+ else
52
+ if group[prev[0]] == group[move[0]]
53
+ group[prev_prev[0]].should_not == group[move[0]]
54
+ end
55
+ prev_prev = prev
56
+ prev = move
57
+ end
58
+ end
59
+ end
60
+
61
+ end
62
+ end
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: puzzled
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.1
6
+ platform: ruby
7
+ authors:
8
+ - Chris Hunt
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-08-15 00:00:00 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rspec
17
+ prerelease: false
18
+ requirement: &id001 !ruby/object:Gem::Requirement
19
+ none: false
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ type: :development
25
+ version_requirements: *id001
26
+ description: Generates scrambles for, and solves, the Rubik's Cube.
27
+ email:
28
+ - huntca@gmail.com
29
+ executables: []
30
+
31
+ extensions: []
32
+
33
+ extra_rdoc_files: []
34
+
35
+ files:
36
+ - .gitignore
37
+ - Gemfile
38
+ - README.md
39
+ - Rakefile
40
+ - lib/puzzled.rb
41
+ - lib/puzzled/rubiks_cube.rb
42
+ - lib/puzzled/version.rb
43
+ - puzzled.gemspec
44
+ - spec/rubiks_cube_spec.rb
45
+ homepage: ""
46
+ licenses: []
47
+
48
+ post_install_message:
49
+ rdoc_options: []
50
+
51
+ require_paths:
52
+ - lib
53
+ required_ruby_version: !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: "0"
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ none: false
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: "0"
65
+ requirements: []
66
+
67
+ rubyforge_project: puzzled
68
+ rubygems_version: 1.8.8
69
+ signing_key:
70
+ specification_version: 3
71
+ summary: Rubik's Cube scramble generator.
72
+ test_files:
73
+ - spec/rubiks_cube_spec.rb