24games 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -1,8 +1,8 @@
1
- *.gem
2
- *.sw?
3
- .DS_Store
4
- coverage
5
- doc
6
- pkg
7
- Gemfile*
8
- .bundle
1
+ *.gem
2
+ *.sw?
3
+ .DS_Store
4
+ coverage
5
+ doc
6
+ pkg
7
+ Gemfile*
8
+ .bundle
@@ -1,20 +1,20 @@
1
- Copyright (c) 2010 Takafan
2
-
3
- Permission is hereby granted, free of charge, to any person obtaining
4
- a copy of this software and associated documentation files (the
5
- "Software"), to deal in the Software without restriction, including
6
- without limitation the rights to use, copy, modify, merge, publish,
7
- distribute, sublicense, and/or sell copies of the Software, and to
8
- permit persons to whom the Software is furnished to do so, subject to
9
- the following conditions:
10
-
11
- The above copyright notice and this permission notice shall be
12
- included in all copies or substantial portions of the Software.
13
-
14
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
- LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
- OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
1
+ Copyright (c) 2010 Takafan
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
20
  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -1,21 +1,17 @@
1
- = 24 game/Solve
2
-
3
- 解24点。computes an expression to solve the 24 game if possible.
4
-
5
- == Using
6
-
7
- gem install 24games
8
-
9
- 24games 5 9 5 9
10
-
11
- you get it:
12
-
13
- 5 * 5 - 9 / 9
14
-
15
- == Authors
16
-
17
- * {Takafan}[http://hululuu.com]
18
-
19
- == License
20
-
21
- MIT License. See the included MIT-LICENSE file.
1
+ = 24 game/Solve
2
+
3
+ 解24点。computes an expression to solve the 24 game if possible.
4
+
5
+ == Using
6
+
7
+ gem install 24games
8
+
9
+ 24games 5 9 5 9
10
+
11
+ you get it:
12
+
13
+ 5 * 5 - 9 / 9
14
+
15
+ == Authors
16
+
17
+ * {Takafan}[http://hululuu.com]
data/Rakefile CHANGED
@@ -1,12 +1,12 @@
1
- require 'rake/clean'
2
- require 'rake/testtask'
3
- require 'rspec/core/rake_task'
4
-
5
- namespace :spec do
6
- desc "Run all examples"
7
- RSpec::Core::RakeTask.new(:examples) do |task|
8
- task.pattern = 'spec/*_spec.rb'
9
- end
10
- end
11
-
12
- task :default => "spec:examples"
1
+ require 'rake/clean'
2
+ require 'rake/testtask'
3
+ require 'rspec/core/rake_task'
4
+
5
+ namespace :spec do
6
+ desc "Run all examples"
7
+ RSpec::Core::RakeTask.new(:examples) do |task|
8
+ task.pattern = 'spec/*_spec.rb'
9
+ end
10
+ end
11
+
12
+ task :default => "spec:examples"
@@ -1,8 +1,13 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
+ lib_path = File.expand_path('../../lib', __FILE__)
4
+ $:.unshift(lib_path)
3
5
  require 'calc24'
6
+ require 'calc24/version'
4
7
 
5
8
  begin
9
+ raise Calc24::VERSION if ['--version', '-v'].include? ARGV[0]
10
+
6
11
  # validate user input
7
12
  digits = ARGV.to_i
8
13
 
@@ -12,7 +12,7 @@ Gem::Specification.new do |s|
12
12
  s.homepage = %q{http://github.com/takafan/24games"}
13
13
  s.summary = %q{24 game/Solve}
14
14
  s.description = %q{
15
- 解24点。computes an expression to solve the 24 game if possible.
15
+ computes an expression to solve the 24 game if possible.
16
16
  }
17
17
 
18
18
  s.required_rubygems_version = %q{>= 1.3.6}
@@ -1,144 +1,132 @@
1
1
  require 'rational'
2
2
 
3
3
  module Calc24
4
-
4
+
5
5
  class TwentyFourGamePlayer
6
- =begin
7
- EXPRESSIONS = [
8
- '((%d %s %d) %s %d) %s %d',
9
- '(%d %s (%d %s %d)) %s %d',
10
- '(%d %s %d) %s (%d %s %d)',
11
- '%d %s ((%d %s %d) %s %d)',
12
- '%d %s (%d %s (%d %s %d))',
13
- ]
14
-
15
- OPERATORS = [:+, :-, :*, :/]
16
- =end
17
6
  MAN4D4S = [
18
- # '((%d %s %d) %s %d) %s %d' 48
19
- # +++
20
- '%d + %d + %d + %d',
21
- # ++-
22
- '%d + %d + %d - %d',
23
- # ++*
24
- '(%d + %d + %d) * %d',
25
- '(%d + %d) * %d + %d',
26
- '%d * %d + %d + %d',
27
- # ++/
28
- '(%d + %d + %d) / %d',
29
- '(%d + %d) / %d + %d',
30
- '(%d / %d) + %d + %d',
31
- # +--
32
- '%d + %d - %d - %d',
33
- # +-*
34
- '(%d + %d - %d) * %d',
35
- '(%d + %d) * %d - %d',
36
- '(%d - %d) * %d + %d',
37
- '%d * %d + %d - %d',
38
- # +-/
39
- '(%d + %d - %d) / %d',
40
- '(%d + %d) / %d - %d',
41
- '(%d - %d) / %d + %d',
42
- '%d / %d + %d - %d',
43
- # +**
44
- '(%d + %d) * %d * %d',
45
- '(%d * %d + %d) * %d',
46
- '%d * %d * %d + %d',
47
- # +*/
48
- '(%d + %d) * %d / %d',
49
- '(%d * %d + %d) / %d',
50
- '%d * %d / %d + %d',
51
- '(%d / %d + %d) * %d',
52
- # +//
53
- '(%d + %d) / %d / %d',
54
- '(%d / %d + %d) / %d',
55
- '%d / %d / %d + %d',
56
- # ---
57
- '%d - %d - %d - %d',
58
- # --*
59
- '(%d - %d - %d) * %d',
60
- '(%d - %d) * %d - %d',
61
- '%d * %d - %d - %d',
62
- # --/
63
- '(%d - %d - %d) / %d',
64
- '(%d - %d) / %d - %d',
65
- '%d / %d - %d - %d',
66
- # -**
67
- '(%d - %d) * %d * %d',
68
- '(%d * %d - %d) * %d',
69
- '%d * %d * %d - %d',
70
- # -*/
71
- '(%d - %d) * %d / %d',
72
- '(%d * %d - %d) / %d',
73
- '%d * %d / %d - %d',
74
- '(%d / %d - %d) * %d',
75
- # -//
76
- '(%d - %d) / %d / %d',
77
- '(%d / %d - %d) / %d',
78
- '%d / %d / %d - %d',
79
- # ***
80
- '%d * %d * %d * %d',
81
- # **/
82
- '%d * %d * %d / %d',
83
- # *//
84
- '%d * %d / %d / %d',
85
- # ///
86
- '%d / %d / %d / %d',
87
-
88
- # '(%d %s (%d %s %d)) %s %d' +8
89
- '(%d - %d * %d) * %d',
90
- '(%d - %d / %d) * %d',
91
- '%d / (%d + %d) * %d',
92
- '%d / (%d - %d) * %d',
93
- '(%d - %d * %d) / %d',
94
- '(%d - %d / %d) / %d',
95
- '%d / (%d + %d) / %d',
96
- '%d / (%d - %d) / %d',
7
+ # '((%d %s %d) %s %d) %s %d' 48
8
+ # +++
9
+ '%d + %d + %d + %d',
10
+ # ++-
11
+ '%d + %d + %d - %d',
12
+ # ++*
13
+ '(%d + %d + %d) * %d',
14
+ '(%d + %d) * %d + %d',
15
+ '%d * %d + %d + %d',
16
+ # ++/
17
+ '(%d + %d + %d) / %d',
18
+ '(%d + %d) / %d + %d',
19
+ '(%d / %d) + %d + %d',
20
+ # +--
21
+ '%d + %d - %d - %d',
22
+ # +-*
23
+ '(%d + %d - %d) * %d',
24
+ '(%d + %d) * %d - %d',
25
+ '(%d - %d) * %d + %d',
26
+ '%d * %d + %d - %d',
27
+ # +-/
28
+ '(%d + %d - %d) / %d',
29
+ '(%d + %d) / %d - %d',
30
+ '(%d - %d) / %d + %d',
31
+ '%d / %d + %d - %d',
32
+ # +**
33
+ '(%d + %d) * %d * %d',
34
+ '(%d * %d + %d) * %d',
35
+ '%d * %d * %d + %d',
36
+ # +*/
37
+ '(%d + %d) * %d / %d',
38
+ '(%d * %d + %d) / %d',
39
+ '%d * %d / %d + %d',
40
+ '(%d / %d + %d) * %d',
41
+ # +//
42
+ '(%d + %d) / %d / %d',
43
+ '(%d / %d + %d) / %d',
44
+ '%d / %d / %d + %d',
45
+ # ---
46
+ '%d - %d - %d - %d',
47
+ # --*
48
+ '(%d - %d - %d) * %d',
49
+ '(%d - %d) * %d - %d',
50
+ '%d * %d - %d - %d',
51
+ # --/
52
+ '(%d - %d - %d) / %d',
53
+ '(%d - %d) / %d - %d',
54
+ '%d / %d - %d - %d',
55
+ # -**
56
+ '(%d - %d) * %d * %d',
57
+ '(%d * %d - %d) * %d',
58
+ '%d * %d * %d - %d',
59
+ # -*/
60
+ '(%d - %d) * %d / %d',
61
+ '(%d * %d - %d) / %d',
62
+ '%d * %d / %d - %d',
63
+ '(%d / %d - %d) * %d',
64
+ # -//
65
+ '(%d - %d) / %d / %d',
66
+ '(%d / %d - %d) / %d',
67
+ '%d / %d / %d - %d',
68
+ # ***
69
+ '%d * %d * %d * %d',
70
+ # **/
71
+ '%d * %d * %d / %d',
72
+ # *//
73
+ '%d * %d / %d / %d',
74
+ # ///
75
+ '%d / %d / %d / %d',
76
+
77
+ # '(%d %s (%d %s %d)) %s %d' +8
78
+ '(%d - %d * %d) * %d',
79
+ '(%d - %d / %d) * %d',
80
+ '%d / (%d + %d) * %d',
81
+ '%d / (%d - %d) * %d',
82
+ '(%d - %d * %d) / %d',
83
+ '(%d - %d / %d) / %d',
84
+ '%d / (%d + %d) / %d',
85
+ '%d / (%d - %d) / %d',
97
86
 
98
- # '(%d %s %d) %s (%d %s %d)' +14
99
- '%d * %d + %d * %d',
100
- '%d * %d + %d / %d',
101
- '%d / %d + %d / %d',
102
- '%d * %d - %d * %d',
103
- '%d * %d - %d / %d',
104
- '%d / %d - %d * %d',
105
- '%d / %d - %d / %d',
106
- '(%d + %d) * (%d + %d)',
107
- '(%d + %d) * (%d - %d)',
108
- '(%d - %d) * (%d - %d)',
109
- '(%d + %d) / (%d + %d)',
110
- '(%d + %d) / (%d - %d)',
111
- '(%d - %d) / (%d + %d)',
112
- '(%d - %d) / (%d - %d)',
113
-
114
- # '%d %s ((%d %s %d) %s %d)' +14
115
- '%d - ((%d + %d) * %d)',
116
- '%d - ((%d + %d) / %d)',
117
- '%d - ((%d - %d) * %d)',
118
- '%d - ((%d - %d) / %d)',
119
- '%d - (%d * %d * %d)',
120
- '%d - (%d * %d / %d)',
121
- '%d - (%d / %d / %d)',
122
- '%d / (%d + %d + %d)',
123
- '%d / (%d + %d - %d)',
124
- '%d / (%d - %d - %d)',
125
- '%d / (%d * %d + %d)',
126
- '%d / (%d * %d - %d)',
127
- '%d / (%d / %d + %d)',
128
- '%d / (%d / %d - %d)',
129
-
130
- # '%d %s (%d %s (%d %s %d))' +4
131
- '%d - (%d / (%d + %d))',
132
- '%d - (%d / (%d - %d))',
133
- '%d / (%d - %d * %d)',
134
- '%d / (%d - %d / %d)'
87
+ # '(%d %s %d) %s (%d %s %d)' +14
88
+ '%d * %d + %d * %d',
89
+ '%d * %d + %d / %d',
90
+ '%d / %d + %d / %d',
91
+ '%d * %d - %d * %d',
92
+ '%d * %d - %d / %d',
93
+ '%d / %d - %d * %d',
94
+ '%d / %d - %d / %d',
95
+ '(%d + %d) * (%d + %d)',
96
+ '(%d + %d) * (%d - %d)',
97
+ '(%d - %d) * (%d - %d)',
98
+ '(%d + %d) / (%d + %d)',
99
+ '(%d + %d) / (%d - %d)',
100
+ '(%d - %d) / (%d + %d)',
101
+ '(%d - %d) / (%d - %d)',
102
+
103
+ # '%d %s ((%d %s %d) %s %d)' +14
104
+ '%d - ((%d + %d) * %d)',
105
+ '%d - ((%d + %d) / %d)',
106
+ '%d - ((%d - %d) * %d)',
107
+ '%d - ((%d - %d) / %d)',
108
+ '%d - (%d * %d * %d)',
109
+ '%d - (%d * %d / %d)',
110
+ '%d - (%d / %d / %d)',
111
+ '%d / (%d + %d + %d)',
112
+ '%d / (%d + %d - %d)',
113
+ '%d / (%d - %d - %d)',
114
+ '%d / (%d * %d + %d)',
115
+ '%d / (%d * %d - %d)',
116
+ '%d / (%d / %d + %d)',
117
+ '%d / (%d / %d - %d)',
118
+
119
+ # '%d %s (%d %s (%d %s %d))' +4
120
+ '%d - (%d / (%d + %d))',
121
+ '%d - (%d / (%d - %d))',
122
+ '%d / (%d - %d * %d)',
123
+ '%d / (%d - %d / %d)'
135
124
  ]
136
125
 
137
126
  @@objective = Rational(24,1)
138
127
 
139
128
  def initialize(digits)
140
129
  @digits = digits
141
- #@solutions = []
142
130
  @solutions = {}
143
131
  solve
144
132
  end
@@ -147,21 +135,17 @@ module Calc24
147
135
 
148
136
  def solve
149
137
  digits.permutation.to_a.uniq.each do |a,b,c,d|
150
- #OPERATORS.each do |op1|
151
- #OPERATORS.each do |op2|
152
- #OPERATORS.each do |op3|
153
138
  MAN4D4S.each do |expr|
154
139
  # evaluate using rational arithmetic
155
- #test = expr.gsub('%d', 'Rational(%d,1)') % [a, op1, b, op2, c, op3, d]
156
140
  test = expr.gsub('%d', 'Rational(%d,1)') % [a, b, c, d]
157
141
  value = eval(test) rescue -1 # catch division by zero
158
142
  if value == @@objective
159
- #@solutions << expr % [a, op1, b, op2, c, op3, d]
160
143
  @solutions[expr] = expr % [a, b, c, d]
161
144
  end
162
- end#;end;end;end
145
+ end
163
146
  end
164
147
  end
148
+
165
149
  end
166
150
 
167
151
  end
@@ -1,3 +1,3 @@
1
1
  module Calc24
2
- VERSION = "0.2.1"
2
+ VERSION = "0.2.2"
3
3
  end
@@ -1,7 +1,9 @@
1
- require "calc24"
1
+ lib_path = File.expand_path('../../lib', __FILE__)
2
+ $:.unshift(lib_path)
3
+ require 'calc24'
2
4
 
3
5
  module Calc24
4
-
6
+
5
7
  describe TwentyFourGamePlayer do
6
8
  digicoll = [
7
9
  %w{ 1 3 4 6 },
@@ -58,17 +60,17 @@ module Calc24
58
60
  %w{ 9 J Q Q },
59
61
  %w{ 10 Q Q Q }
60
62
  ]
61
-
63
+
62
64
  it "found solutions in #{digicoll.count} difficult subjects." do
63
65
  digicoll.each do |digits|
64
66
  TwentyFourGamePlayer.new(digits.to_i).solutions.should_not be_empty
65
67
  end
66
68
  end
67
-
69
+
68
70
  it "found no solutions with 7 6 4 3." do
69
71
  TwentyFourGamePlayer.new([7,6,4,3]).solutions.should be_empty
70
72
  end
71
73
 
72
74
  end
73
-
75
+
74
76
  end
metadata CHANGED
@@ -1,32 +1,23 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: 24games
3
- version: !ruby/object:Gem::Version
4
- prerelease: false
5
- segments:
6
- - 0
7
- - 2
8
- - 1
9
- version: 0.2.1
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.2
5
+ prerelease:
10
6
  platform: ruby
11
- authors:
7
+ authors:
12
8
  - takafan
13
9
  autorequire:
14
10
  bindir: bin
15
11
  cert_chain: []
16
-
17
- date: 2011-01-17 00:00:00 +08:00
18
- default_executable:
12
+ date: 2011-07-21 00:00:00.000000000Z
19
13
  dependencies: []
20
-
21
- description: "\n \xE8\xA7\xA324\xE7\x82\xB9\xE3\x80\x82computes an expression to solve the 24 game if possible.\n "
14
+ description: ! "\n computes an expression to solve the 24 game if possible.\n "
22
15
  email: takafan@hululuu.com
23
- executables:
16
+ executables:
24
17
  - 24games
25
18
  extensions: []
26
-
27
19
  extra_rdoc_files: []
28
-
29
- files:
20
+ files:
30
21
  - .gitignore
31
22
  - MIT-LICENSE
32
23
  - README.rdoc
@@ -38,39 +29,29 @@ files:
38
29
  - lib/calc24/ext.rb
39
30
  - lib/calc24/version.rb
40
31
  - spec/calc24_spec.rb
41
- has_rdoc: true
42
32
  homepage: http://github.com/takafan/24games"
43
33
  licenses: []
44
-
45
34
  post_install_message:
46
- rdoc_options:
35
+ rdoc_options:
47
36
  - --charset=UTF-8
48
- require_paths:
37
+ require_paths:
49
38
  - lib
50
- required_ruby_version: !ruby/object:Gem::Requirement
39
+ required_ruby_version: !ruby/object:Gem::Requirement
51
40
  none: false
52
- requirements:
53
- - - ">="
54
- - !ruby/object:Gem::Version
55
- segments:
56
- - 0
57
- version: "0"
58
- required_rubygems_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ! '>='
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
46
  none: false
60
- requirements:
61
- - - ">="
62
- - !ruby/object:Gem::Version
63
- segments:
64
- - 1
65
- - 3
66
- - 6
47
+ requirements:
48
+ - - ! '>='
49
+ - !ruby/object:Gem::Version
67
50
  version: 1.3.6
68
51
  requirements: []
69
-
70
52
  rubyforge_project: 24games
71
- rubygems_version: 1.3.7
53
+ rubygems_version: 1.8.5
72
54
  signing_key:
73
55
  specification_version: 3
74
56
  summary: 24 game/Solve
75
57
  test_files: []
76
-