24games 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,8 @@
1
+ *.gem
2
+ *.sw?
3
+ .DS_Store
4
+ coverage
5
+ doc
6
+ pkg
7
+ Gemfile*
8
+ .bundle
@@ -0,0 +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
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,21 @@
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.
@@ -0,0 +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"
@@ -0,0 +1,26 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'calc24'
4
+
5
+ begin
6
+ # validate user input
7
+ digits = ARGV.to_i
8
+
9
+ raise "need 4 digits, such as: 5 9 5 9" unless digits.size == 4
10
+
11
+ t0 = Time.now
12
+ player = Calc24::TwentyFourGamePlayer.new(digits)
13
+
14
+ if player.solutions.empty?
15
+ puts "no solutions"
16
+ else
17
+ puts "found #{player.solutions.size} solutions:"
18
+ #STDIN.gets
19
+ puts player.solutions.values.join("\n").to_p
20
+ puts "#{Time.now - t0}s"
21
+ end
22
+ rescue Exception => e
23
+ print "#{e.class}: " unless e.class == RuntimeError
24
+ puts "#{e.message}"
25
+ exit 1
26
+ end
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ $LOAD_PATH.unshift File.expand_path("../lib", __FILE__)
4
+ require 'calc24/version'
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{24games}
8
+ s.version = Calc24::VERSION
9
+ s.platform = Gem::Platform::RUBY
10
+ s.authors = ["takafan"]
11
+ s.email = %q{takafan@hululuu.com}
12
+ s.homepage = %q{http://github.com/takafan/24games"}
13
+ s.summary = %q{24 game/Solve}
14
+ s.description = %q{
15
+ 解24点。computes an expression to solve the 24 game if possible.
16
+ }
17
+
18
+ s.required_rubygems_version = %q{>= 1.3.6}
19
+ s.rubyforge_project = %q{24games}
20
+
21
+ s.files = `git ls-files`.split("\n")
22
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
23
+ s.rdoc_options = ["--charset=UTF-8"]
24
+ s.require_path = %q{lib}
25
+ end
@@ -0,0 +1,2 @@
1
+ require 'calc24/calc24_core'
2
+ require 'calc24/ext'
@@ -0,0 +1,167 @@
1
+ require 'rational'
2
+
3
+ module Calc24
4
+
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
+ 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',
97
+
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)'
135
+ ]
136
+
137
+ @@objective = Rational(24,1)
138
+
139
+ def initialize(digits)
140
+ @digits = digits
141
+ #@solutions = []
142
+ @solutions = {}
143
+ solve
144
+ end
145
+
146
+ attr_reader :digits, :solutions
147
+
148
+ def solve
149
+ 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
+ MAN4D4S.each do |expr|
154
+ # evaluate using rational arithmetic
155
+ #test = expr.gsub('%d', 'Rational(%d,1)') % [a, op1, b, op2, c, op3, d]
156
+ test = expr.gsub('%d', 'Rational(%d,1)') % [a, b, c, d]
157
+ value = eval(test) rescue -1 # catch division by zero
158
+ if value == @@objective
159
+ #@solutions << expr % [a, op1, b, op2, c, op3, d]
160
+ @solutions[expr] = expr % [a, b, c, d]
161
+ end
162
+ end#;end;end;end
163
+ end
164
+ end
165
+ end
166
+
167
+ end
@@ -0,0 +1,24 @@
1
+ class Array
2
+ def to_i
3
+ self.map do |arg|
4
+ begin
5
+ case arg.upcase
6
+ when 'J' then 11
7
+ when 'Q' then 12
8
+ when 'K' then 13
9
+ when 'A' then 1
10
+ else Integer(arg)
11
+ end
12
+ rescue ArgumentError
13
+ raise "unknown digit: '#{arg}'"
14
+ end
15
+ end
16
+ end
17
+ end
18
+
19
+ class String
20
+ def to_p
21
+ {'11' => 'J', '12' => 'Q', '13' => 'K'}.each {|digit, poker| self.gsub!(digit, poker)}
22
+ self
23
+ end
24
+ end
@@ -0,0 +1,3 @@
1
+ module Calc24
2
+ VERSION = "0.2.1"
3
+ end
@@ -0,0 +1,74 @@
1
+ require "calc24"
2
+
3
+ module Calc24
4
+
5
+ describe TwentyFourGamePlayer do
6
+ digicoll = [
7
+ %w{ 1 3 4 6 },
8
+ %w{ 1 4 5 6 },
9
+ %w{ 1 5 5 5 },
10
+ %w{ 1 5 J J },
11
+ %w{ 1 6 6 8 },
12
+ %w{ 1 6 J K },
13
+ %w{ 1 2 7 7 },
14
+ %w{ 1 7 K K },
15
+ %w{ 1 8 Q Q },
16
+ %w{ 2 2 J J },
17
+ %w{ 2 2 K K },
18
+ %w{ 2 3 5 Q },
19
+ %w{ 2 3 7 Q },
20
+ %w{ 2 3 J J },
21
+ %w{ 2 3 K K },
22
+ %w{ 2 4 7 Q },
23
+ %w{ 2 4 10 10 },
24
+ %w{ 2 5 5 10 },
25
+ %w{ 2 7 7 10 },
26
+ %w{ 3 3 7 7 },
27
+ %w{ 3 3 8 8 },
28
+ %w{ 3 5 7 K },
29
+ %w{ 3 6 6 J },
30
+ %w{ 3 7 9 K },
31
+ %w{ 3 8 8 10 },
32
+ %w{ 4 4 7 7 },
33
+ %w{ 4 4 10 10 },
34
+ %w{ 4 5 8 K },
35
+ %w{ 4 7 J K },
36
+ %w{ 4 8 8 J },
37
+ %w{ 4 8 8 K },
38
+ %w{ 4 10 10 J },
39
+ %w{ 5 5 7 J },
40
+ %w{ 5 7 7 J },
41
+ %w{ 5 5 8 J },
42
+ %w{ 5 8 9 K },
43
+ %w{ 5 9 10 J },
44
+ %w{ 5 10 10 J },
45
+ %w{ 5 10 10 K },
46
+ %w{ 5 J Q Q },
47
+ %w{ 6 6 6 J },
48
+ %w{ 6 6 7 J },
49
+ %w{ 6 6 9 K },
50
+ %w{ 6 10 10 K },
51
+ %w{ 6 J J Q },
52
+ %w{ 6 Q Q K },
53
+ %w{ 8 8 8 J },
54
+ %w{ 8 8 8 Q },
55
+ %w{ 8 8 9 Q },
56
+ %w{ 8 J Q Q },
57
+ %w{ 9 10 J K },
58
+ %w{ 9 J Q Q },
59
+ %w{ 10 Q Q Q }
60
+ ]
61
+
62
+ it "found solutions in #{digicoll.count} difficult subjects." do
63
+ digicoll.each do |digits|
64
+ TwentyFourGamePlayer.new(digits.to_i).solutions.should_not be_empty
65
+ end
66
+ end
67
+
68
+ it "found no solutions with 7 6 4 3." do
69
+ TwentyFourGamePlayer.new([7,6,4,3]).solutions.should be_empty
70
+ end
71
+
72
+ end
73
+
74
+ end
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
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
10
+ platform: ruby
11
+ authors:
12
+ - takafan
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-01-17 00:00:00 +08:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description: "\n \xE8\xA7\xA324\xE7\x82\xB9\xE3\x80\x82computes an expression to solve the 24 game if possible.\n "
22
+ email: takafan@hululuu.com
23
+ executables:
24
+ - 24games
25
+ extensions: []
26
+
27
+ extra_rdoc_files: []
28
+
29
+ files:
30
+ - .gitignore
31
+ - MIT-LICENSE
32
+ - README.rdoc
33
+ - Rakefile
34
+ - bin/24games
35
+ - calc24.gemspec
36
+ - lib/calc24.rb
37
+ - lib/calc24/calc24_core.rb
38
+ - lib/calc24/ext.rb
39
+ - lib/calc24/version.rb
40
+ - spec/calc24_spec.rb
41
+ has_rdoc: true
42
+ homepage: http://github.com/takafan/24games"
43
+ licenses: []
44
+
45
+ post_install_message:
46
+ rdoc_options:
47
+ - --charset=UTF-8
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ 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
59
+ none: false
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ segments:
64
+ - 1
65
+ - 3
66
+ - 6
67
+ version: 1.3.6
68
+ requirements: []
69
+
70
+ rubyforge_project: 24games
71
+ rubygems_version: 1.3.7
72
+ signing_key:
73
+ specification_version: 3
74
+ summary: 24 game/Solve
75
+ test_files: []
76
+