rmathguard 0.1.0

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.
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Jakub Dušek
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.
data/README.markdown ADDED
File without changes
@@ -0,0 +1,97 @@
1
+ module RMathGuard
2
+ class Digit < Array
3
+ PATTERN = ('A'..'Z').to_a + (1..9).to_a
4
+
5
+ def initialize(rows, columns)
6
+ super(rows) { Array.new(columns, 0) }
7
+ end
8
+
9
+ def self.build(rows = 5, columns = 3, &blk)
10
+ digit = Digit.new(rows, columns)
11
+ digit.instance_eval(&blk)
12
+ digit
13
+ end
14
+
15
+ def build(&blk)
16
+ digit = Digit.new(self.size, self.first.size)
17
+ digit.copy(self)
18
+ digit.instance_eval(&blk)
19
+ digit
20
+ end
21
+
22
+ def copy(digit)
23
+ digit.each_index do |row|
24
+ digit[row].each_index do |col|
25
+ self[row][col] = digit[row][col]
26
+ end
27
+ end
28
+ end
29
+
30
+ def masked
31
+ digit = Digit.new(self.size, self.first.size)
32
+ digit.copy(self)
33
+ digit.each_index do |row|
34
+ digit[row].each_index do |col|
35
+ digit[row][col] = PATTERN[rand(PATTERN.size)] unless digit[row][col] == 0
36
+ end
37
+ end
38
+ digit
39
+ end
40
+
41
+ def full_row(position)
42
+ position = case position
43
+ when :top, :first
44
+ 0
45
+ when :middle
46
+ (self.size / 2.0).round - 1
47
+ when :bottom, :last
48
+ self.size - 1
49
+ else
50
+ position.to_i
51
+ end
52
+ self[position].fill(1)
53
+ end
54
+
55
+ def full_column(position)
56
+ position = case position
57
+ when :last
58
+ self[0].size - 1
59
+ when :first
60
+ 0
61
+ end
62
+ self.each_index do |row|
63
+ self[row][position] = 1
64
+ end
65
+ end
66
+
67
+ def half_column(position)
68
+ position = case position
69
+ when :top_left
70
+ [0..middle, 0]
71
+ when :top_right
72
+ [0..middle, self.first.size - 1]
73
+ when :bottom_left
74
+ [middle...self.size, 0]
75
+ when :bottom_right
76
+ [middle...self.size, self.first.size - 1]
77
+ end
78
+ position.first.each do |row|
79
+ self[row][position.last] = 1
80
+ end
81
+ end
82
+
83
+ def column(position, range)
84
+ range.each do |row|
85
+ self[row][position] = 1
86
+ end
87
+ end
88
+
89
+ def middle
90
+ self.size / 2
91
+ end
92
+
93
+ def middle_column
94
+ self.first.size / 2
95
+ end
96
+ end
97
+ end
@@ -0,0 +1,52 @@
1
+ module RMathGuard
2
+ class Expression
3
+ OPERANDS = %w[ZERO ONE TWO THREE FOUR FIVE SIX SEVEN EIGHT NINE]
4
+ OPERATORS = {"+" => "PLUS",
5
+ "-" => "MINUS"}
6
+
7
+ attr_reader :result
8
+
9
+ def initialize(options = {})
10
+ @max_value = options[:max_value] || 10
11
+ @row_count = options[:row_count] || 5
12
+ @col_count = options[:col_count] || 3
13
+ @sep_size = options[:sep_size] || 2
14
+
15
+ RMathGuard::Numbers.resize(@row_count, @col_count)
16
+ RMathGuard::Operators.resize(@row_count, @col_count)
17
+ generate
18
+ end
19
+
20
+ def show
21
+ n = RMathGuard::Numbers
22
+ op1 = n.const_get(OPERANDS[@operand1]).masked
23
+ op2 = n.const_get(OPERANDS[@operand2]).masked
24
+ o = RMathGuard::Operators
25
+ sign = o.const_get(OPERATORS[@operator]).masked
26
+ eq = o::EQUALS.masked
27
+
28
+
29
+ result = []
30
+ (0...@row_count).each do |row|
31
+ result[row] = []
32
+ result[row] << op1[row]
33
+ @sep_size.times { result[row] << 0 }
34
+ result[row] << sign[row]
35
+ @sep_size.times { result[row] << 0 }
36
+ result[row] << op2[row]
37
+ @sep_size.times { result[row] << 0 }
38
+ result[row] << eq[row]
39
+ result[row].flatten!
40
+ end
41
+ result.map { |row| row.join("") }.join("\n").tr("0", " ")
42
+ end
43
+
44
+ private
45
+ def generate
46
+ @operator = %w(- +)[rand(2)]
47
+ @operand1 = rand(@max_value)
48
+ @operand2 = rand(@max_value)
49
+ @result = eval("#{@operand1}#{@operator}#{@operand2}")
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,145 @@
1
+ module RMathGuard
2
+ module Numbers
3
+ extend self
4
+
5
+ @@rows = 5
6
+ @@columns = 3
7
+
8
+ def resize(rows = 3, columns = 5)
9
+ @@rows = rows
10
+ @@columns = columns
11
+ constants.each do |const|
12
+ remove_const(const)
13
+ const_set(const, send("make_#{const.downcase}"))
14
+ end
15
+ end
16
+
17
+ def make_zero
18
+ Digit.build(@@rows, @@columns) do
19
+ full_row :top
20
+ full_row :bottom
21
+ full_column :first
22
+ full_column :last
23
+ end
24
+ end
25
+
26
+ def make_one
27
+ Digit.build(@@rows, @@columns) do
28
+ full_column :last
29
+ end
30
+ end
31
+
32
+ def make_two
33
+ Digit.build(@@rows, @@columns) do
34
+ full_row :top
35
+ full_row :middle
36
+ full_row :bottom
37
+ half_column :top_right
38
+ half_column :bottom_left
39
+ end
40
+ end
41
+
42
+ def make_three
43
+ Digit.build(@@rows, @@columns) do
44
+ full_row :top
45
+ full_row :middle
46
+ full_row :last
47
+ full_column :last
48
+ end
49
+ end
50
+
51
+ def make_four
52
+ Digit.build(@@rows, @@columns) do
53
+ half_column :top_left
54
+ full_row :middle
55
+ full_column :last
56
+ end
57
+ end
58
+
59
+ def make_five
60
+ Digit.build(@@rows, @@columns) do
61
+ full_row :top
62
+ full_row :middle
63
+ full_row :bottom
64
+ half_column :top_left
65
+ half_column :bottom_right
66
+ end
67
+ end
68
+
69
+ def make_six
70
+ make_five.build do
71
+ full_column :first
72
+ end
73
+ end
74
+
75
+ def make_seven
76
+ Digit.build(@@rows, @@columns) do
77
+ full_row :top
78
+ full_column :last
79
+ end
80
+ end
81
+
82
+ def make_eight
83
+ make_three.build do
84
+ full_column :first
85
+ end
86
+ end
87
+
88
+ def make_nine
89
+ make_five.build do
90
+ full_column :last
91
+ end
92
+ end
93
+
94
+ ZERO = make_zero
95
+ ONE = make_one
96
+ TWO = make_two
97
+ THREE = make_three
98
+ FOUR = make_four
99
+ FIVE = make_five
100
+ SIX = make_six
101
+ SEVEN = make_seven
102
+ EIGHT = make_eight
103
+ NINE = make_nine
104
+ end
105
+
106
+ module Operators
107
+ extend self
108
+
109
+ @@rows = 5
110
+ @@columns = 3
111
+
112
+ def resize(rows = 3, columns = 5)
113
+ @@rows = rows
114
+ @@columns = columns
115
+ constants.each do |const|
116
+ remove_const(const)
117
+ const_set(const, send("make_#{const.downcase}"))
118
+ end
119
+ end
120
+
121
+ def make_minus
122
+ Digit.build(@@rows, @@columns) do
123
+ full_row :middle
124
+ end
125
+ end
126
+
127
+ def make_plus
128
+ Digit.build(@@rows, @@columns) do
129
+ full_row :middle
130
+ column(middle_column, 1..(self.size - 2))
131
+ end
132
+ end
133
+
134
+ def make_equals
135
+ Digit.build(@@rows, @@columns) do
136
+ full_row(middle - 1)
137
+ full_row(middle + 1)
138
+ end
139
+ end
140
+
141
+ PLUS = make_plus
142
+ MINUS = make_minus
143
+ EQUALS = make_equals
144
+ end
145
+ end
data/lib/rmathguard.rb ADDED
@@ -0,0 +1,7 @@
1
+ require 'rmathguard/digit'
2
+ require 'rmathguard/number'
3
+ require 'rmathguard/expression'
4
+
5
+ module RMathGuard
6
+ VERSION = "0.1.0"
7
+ end
metadata ADDED
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rmathguard
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - "Jakub Du\xC5\xA1ek"
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-02-24 00:00:00 +01:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description:
22
+ email:
23
+ - jakub.dusek@email.cz
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files: []
29
+
30
+ files:
31
+ - lib/rmathguard/expression.rb
32
+ - lib/rmathguard/number.rb
33
+ - lib/rmathguard/digit.rb
34
+ - lib/rmathguard.rb
35
+ - MIT-LICENSE
36
+ - README.markdown
37
+ has_rdoc: true
38
+ homepage: http://github.com/salax/rmathguard
39
+ licenses: []
40
+
41
+ post_install_message:
42
+ rdoc_options: []
43
+
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ segments:
51
+ - 0
52
+ version: "0"
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ segments:
58
+ - 1
59
+ - 3
60
+ - 1
61
+ version: 1.3.1
62
+ requirements: []
63
+
64
+ rubyforge_project:
65
+ rubygems_version: 1.3.6
66
+ signing_key:
67
+ specification_version: 3
68
+ summary: ""
69
+ test_files: []
70
+