cardtrick 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d92d0b461ca5b94df9259704cd7aeab673b288a326d0c5b5187d2c8c51834c7a
4
- data.tar.gz: bf80085543309454b9a8143613de191777139d3670fba2a0fba7865aa4da226a
3
+ metadata.gz: 55ae77b86a97b9b249124eb41c2a66d55634a7158090a460b3bf86a57d5f7aac
4
+ data.tar.gz: 3b2c64c029dc52419d90f25f6a76781436b60c7a2beee027f107344d5f0c8dbe
5
5
  SHA512:
6
- metadata.gz: dfb1023931bf0c57caf54844158581597e0c2f62e9f43a02d82bdb99dfdcc3c0de17212fa79782888d13130690591e8438f9004e6db4009d6d90684c8119d866
7
- data.tar.gz: a4ae983a199aeb5cb974c54eef891a7ba5c84c0a4e15a0a714d1eb74a298ecf14cdebee114287823cf30e90c1d06121976f521f98aafba1f5200388552b2c769
6
+ metadata.gz: 94d2b0a0a88b8169cc4526c80fb733f89a8e23764c3a7d044c582fce449925435db39dfaff60ee6f2f9254c68c5d10f7ff03547b08343a6d845ebc56228f846a
7
+ data.tar.gz: fc47c80ae4c27e8fde78b1077ca4af6c0ba388e724e5cd6fcd18643dcabacb7b61f4b314fee74d963792605011fcd9f4404e868105b55b6e293bb3fc5a0b53ae
data/README.md CHANGED
@@ -1,39 +1,124 @@
1
- # Cardtrick
2
-
3
- TODO: Delete this and the text below, and describe your gem
4
-
5
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/cardtrick`. To experiment with that code, run `bin/console` for an interactive prompt.
6
-
7
- ## Installation
8
-
9
- TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
10
-
11
- Install the gem and add to the application's Gemfile by executing:
12
-
13
- $ bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
14
-
15
- If bundler is not being used to manage dependencies, install the gem by executing:
16
-
17
- $ gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
18
-
19
- ## Usage
20
-
21
- TODO: Write usage instructions here
22
-
23
- ## Development
24
-
25
- After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
26
-
27
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
28
-
29
- ## Contributing
30
-
31
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/cardtrick. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/cardtrick/blob/master/CODE_OF_CONDUCT.md).
32
-
33
- ## License
34
-
35
- The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
36
-
37
- ## Code of Conduct
38
-
39
- Everyone interacting in the Cardtrick project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/cardtrick/blob/master/CODE_OF_CONDUCT.md).
1
+ ## INSTALLING
2
+ ### bundler
3
+ ```
4
+ bundle gem cardtrick
5
+ ```
6
+ ### gems
7
+ ```
8
+ gem install cardtrick
9
+ ```
10
+ ## CREATING
11
+
12
+ ### Card Decks
13
+
14
+ #### A standard deck of cards...
15
+ ```
16
+ Cardtrick.deck("card", Cardtrick.decks[:standard])
17
+ ```
18
+
19
+ #### Create historic decks...
20
+ ```
21
+ Cardtrick.deck("tarot", Cardtrick.decks[:tarot])
22
+ ```
23
+
24
+ #### Or roll your own decks...
25
+ ```
26
+ Cardtrick.deck("cereal", {
27
+ suits: [:hearts, :stars, :diamonds, :clovers, :baloons],
28
+ faces: { leprechan: 10, rabbit: 1 },
29
+ numbers: { low: 3, high: 7 },
30
+ hand: 5,
31
+ specials: [
32
+ { suit: 'magic',
33
+ card: 'sugar',
34
+ value: 1000
35
+ }
36
+ ],
37
+ shuffle: true,
38
+ deck: 'tricks'
39
+ })
40
+ ```
41
+
42
+ ### Dice
43
+
44
+ #### Simple Dice roll
45
+ ```
46
+ Cardtrick.dice 'monopoly', num: 2, sides: 6, key: 'Monopoly'
47
+ ```
48
+
49
+ #### Multipart Dice roll
50
+ ```
51
+ Cardtrick.dice 'hp', num: 3, sides: 12, key: 'Hit Points'
52
+ Cardtrick.dice 'ac', num: 5, sides: 20, key: 'Armor'
53
+ ```
54
+
55
+ #### Then create the roll...
56
+ ```
57
+ Cardtrick.roll 'vs', 'ac', 'hp'
58
+ ```
59
+
60
+ ### Coins
61
+
62
+ #### A simple coin for tossing
63
+ ```
64
+ Cardtrick.coin 'quarter', heads: 'George Washington', tails: "The Bald Eagle", key: "A medium silver coin."
65
+ ```
66
+
67
+ #### Multipart coin toss
68
+ ```
69
+ Cardtrick.coin 'dime', heads: "A head", tails: "A building", key: "A small Silver Coin"
70
+ Cardtrick.coin 'penny', heads: "A bird", tails: "A tree", key: "A small copper coin"
71
+ ```
72
+
73
+ #### Then create the toss..
74
+ ```
75
+ Cardtrick.coins 'change', 'dime', 'penny'
76
+ ```
77
+
78
+ #### Special "Magical" multisided coins...
79
+ ```
80
+ Cardtrick.coin 'quartz', up: "a shiny face", down: "a scratched face", left: "a bright face", right: "a dim face", key: 'Rose Quartz'
81
+ Cardtrick.coin 'tigereye', up: "a shiny face", down: "a scratched face", top: "a bright face", bottom: "a dim face", key: 'Tiger Eye'
82
+ Cardtrick.coin 'ruby', up: "a shiny face", top: "a bright face", key: 'Ruby'
83
+ ```
84
+
85
+ #### Just the same as before...
86
+ ```
87
+ Cardtrick.coins 'gems', 'quartz', 'tigereye', 'ruby'
88
+ ```
89
+
90
+ ## USING
91
+ ### Card Decks
92
+ #### map each card in a hand (any deck)
93
+ ```
94
+ Cardtrick['card'].map { |e| e }
95
+ Cardtrick['tarot'].map { |e| e }
96
+ Cardtrick['cereal'].map { |e| e }
97
+ ```
98
+
99
+ ### Dice
100
+ #### map each die in a roll
101
+ ```
102
+ Cardtrick['monopoly'].map { |e| e }
103
+ ```
104
+
105
+ #### map each die in a collection of rolls
106
+ ```
107
+ Cardtrick['vs'].map { |e| e }
108
+ ```
109
+
110
+ ### Coins
111
+ #### map a single coin flip
112
+ ```
113
+ Cardtrick['quarter'].map { |e| e }
114
+ ```
115
+
116
+ #### map a collection of coin flips
117
+ ```
118
+ Cardtrick['change'].map { |e| e }
119
+ ```
120
+
121
+ #### once again...
122
+ ```
123
+ Cardtrick['gems'].map { |e| e }
124
+ ```
data/cardtrick.rb ADDED
@@ -0,0 +1,79 @@
1
+ # CREATING
2
+
3
+ ## Card Decks
4
+
5
+ ### A standard deck of cards...
6
+ Cardtrick.deck("card", Cardtrick.decks[:standard])
7
+
8
+
9
+ ### Create historic decks...
10
+ Cardtrick.deck("tarot", Cardtrick.decks[:tarot])
11
+
12
+ ### Or roll your own decks...
13
+ Cardtrick.deck("cerial", {
14
+ suits: [:hearts, :stars, :diamonds, :clovers, :baloons],
15
+ faces: { leprechan: 10, rabbit: 1 },
16
+ numbers: { low: 3, high: 7 },
17
+ hand: 5,
18
+ specials: [
19
+ { suit: 'magic',
20
+ card: 'sugar',
21
+ value: 1000
22
+ }
23
+ ],
24
+ shuffle: true,
25
+ deck: 'tricks'
26
+ })
27
+
28
+ ## Dice
29
+
30
+ ### Simple Dice roll
31
+ Cardtrick.dice 'monopoly', num: 2, sides: 6, key: 'Monopoly'
32
+
33
+ ### Multipart Dice roll
34
+ Cardtrick.dice 'hp', num: 3, sides: 12, key: 'Hit Points'
35
+ Cardtrick.dice 'ac', num: 5, sides: 20, key: 'Armor'
36
+
37
+ ### Then create the roll...
38
+ Cardtrick.roll 'vs', 'ac', 'hp'
39
+
40
+ ## Coins
41
+
42
+ ### A simple coin for tossing
43
+ Cardtrick.coin 'quarter', heads: 'George Washington', tails: "The Bald Eagle", key: "A medium silver coin."
44
+
45
+ ### Multipart coin toss
46
+ Cardtrick.coin 'dime', heads: "A head", tails: "A building", key: "A small Silver Coin"
47
+ Cardtrick.coin 'penny', heads: "A bird", tails: "A tree", key: "A small copper coin"
48
+
49
+ ### Then create the toss..
50
+ Cardtrick.coins 'change', 'dime', 'penny'
51
+
52
+ ### Special "Magical" multisided coins...
53
+ Cardtrick.coin 'quartz', up: "a shiny face", down: "a scratched face", left: "a bright face", right: "a dim face", key: 'Rose Quartz'
54
+ Cardtrick.coin 'tigereye', up: "a shiny face", down: "a scratched face", top: "a bright face", bottom: "a dim face", key: 'Tiger Eye'
55
+ Cardtrick.coin 'ruby', up: "a shiny face", top: "a bright face", key: 'Ruby'
56
+
57
+ ### Just the same as before...
58
+ Cardtrick.coins 'gems', 'quartz', 'tigereye', 'ruby'
59
+
60
+ # USING
61
+ ## Card Decks
62
+ ### map each card in a hand (any deck)
63
+ Cardtrick['card'].map { |e| e }
64
+ Cardtrick['tarot'].map { |e| e }
65
+ Cardtrick['cerial'].map { |e| e }
66
+
67
+ ## Dice
68
+ ### map each die in a roll
69
+ Cardtrick['monopoly'].map { |e| e }
70
+ ### map each die in a collection of rolls
71
+ Cardtrick['vs'].map { |e| e }
72
+
73
+ ## Coins
74
+ ### map a single coin flip
75
+ Cardtrick['quarter'].map { |e| e }
76
+ ### map a collection of coin flips
77
+ Cardtrick['change'].map { |e| e }
78
+ ### once again...
79
+ Cardtrick['gems'].map { |e| e }
@@ -0,0 +1,20 @@
1
+ class Card
2
+ def initialize h={}
3
+ @card = h
4
+ end
5
+ def value
6
+ @card[:value]
7
+ end
8
+ def card
9
+ @card[:card]
10
+ end
11
+ def suit
12
+ @card[:suit]
13
+ end
14
+ def to_i
15
+ @card[:value].to_i
16
+ end
17
+ def to_s
18
+ %[#{@card[:card]} of #{@card[:suit]}]
19
+ end
20
+ end
@@ -0,0 +1,37 @@
1
+
2
+ class Coin
3
+ def initialize h={}
4
+ @key = h.delete(:key)
5
+ @h = h
6
+ @last = @h.keys.sample
7
+ end
8
+ def key
9
+ @key
10
+ end
11
+ def map &b
12
+ @last = @h.keys.sample
13
+ b.call(self)
14
+ end
15
+ def last
16
+ @last
17
+ end
18
+ def value
19
+ @h[@last]
20
+ end
21
+ def to_s
22
+ %[Flipped #{key} and got #{value}.]
23
+ end
24
+ end
25
+
26
+ class Coins
27
+ def initialize *a
28
+ @coins = [a].flatten
29
+ end
30
+ def map &b
31
+ h = {}
32
+ @coins.each { |e|
33
+ h[e] = Cardtrick[e].map { |ee| b.call(ee) }
34
+ }
35
+ return h
36
+ end
37
+ end
@@ -0,0 +1,65 @@
1
+ class Deck
2
+ def initialize h={}
3
+ @deck = h
4
+ @d = []
5
+ @b = []
6
+ create!
7
+ end
8
+
9
+ def create!
10
+ (@deck[:decks] || 1).times { build! }
11
+ if @deck[:shuffle] == true
12
+ shuffle!
13
+ end
14
+ end
15
+
16
+ def build!
17
+ @deck[:suits].each do |suit|
18
+ # puts %[build: #{suit}]
19
+ @deck[:faces].each_pair { |face, value|
20
+ # puts %[build: #{suit} #{face} #{value}]
21
+ @d << Card.new( { suit: suit, card: face, value: value } )
22
+ }
23
+ ((@deck[:numbers][:low] || 1)..(@deck[:numbers][:high] || 10)).each { |number|
24
+ # puts %[build: #{suit} #{number}]
25
+ @d << Card.new( { suit: suit, card: number, value: number } )
26
+ }
27
+ end
28
+ if @deck.has_key? :specials
29
+ [@deck[:specials]].flatten.each { |e|
30
+ # puts %[build: #{e[:suit]} #{e[:face]} #{e[:value]}]
31
+ @d << Card.new( { suit: e[:suit], card: e[:card], value: e[:value] } )
32
+ }
33
+ end
34
+ return nil
35
+ end
36
+
37
+ def shuffle!
38
+ @d.shuffle!
39
+ end
40
+
41
+ def map &b
42
+ if @d.length < (@deck[:hand] || 1)
43
+ puts %[Only #{@d.length} cards left...]
44
+ create!
45
+ puts %[Now we have #{@d.length}.]
46
+ end
47
+ a = []
48
+ (@deck[:hand] || 1).times { |t| x = @d.shift; @b << x; a << b.call(x) }
49
+ return a
50
+ end
51
+
52
+ def left
53
+ @d
54
+ end
55
+
56
+ def burn
57
+ @b
58
+ end
59
+
60
+ def deck
61
+ @deck[:deck]
62
+ end
63
+ end
64
+
65
+
@@ -0,0 +1,67 @@
1
+ class Dice
2
+ attr_reader :total, :frequency
3
+ def initialize h={}
4
+ @h = h
5
+ @res, @totals, @total, @frequency = [], [], 0, Hash.new { |h,k| h[k] = 0 }
6
+ end
7
+
8
+ def next!
9
+ ra, rt = [], 0
10
+ (@h[:num] || 1).times do
11
+ a, x = [], rand(1..(@h[:sides] || 2));
12
+ @frequency[x] += 1;
13
+ @total += x
14
+ ra << x
15
+ rt += x
16
+ end
17
+ @totals << rt
18
+ @res << ra
19
+ return nil
20
+ end
21
+
22
+ def map &b
23
+ next!
24
+ b.call(self)
25
+ end
26
+
27
+ def length
28
+ @res.length
29
+ end
30
+
31
+ def value
32
+ @totals[-1]
33
+ end
34
+
35
+ def values
36
+ @res[-1]
37
+ end
38
+
39
+ def sides
40
+ @h[:sides]
41
+ end
42
+
43
+ def num
44
+ @h[:num]
45
+ end
46
+
47
+ def key
48
+ @h[:key] || to_dice
49
+ end
50
+
51
+ def to_a
52
+ @res
53
+ end
54
+
55
+ def to_i
56
+ @total
57
+ end
58
+
59
+ def to_s
60
+ %[I rolled #{to_dice} and got #{@total}.]
61
+ end
62
+
63
+ def to_dice
64
+ %[#{@h[:num]}d#{@h[:sides]}]
65
+ end
66
+
67
+ end
@@ -0,0 +1,23 @@
1
+ class Roll
2
+ attr_reader :dice
3
+ def initialize *d
4
+ @dice = [d].flatten
5
+ end
6
+
7
+ def each &b
8
+ @dice.each { |e| b.call(e) }
9
+ return nil
10
+ end
11
+
12
+ def map &b
13
+ h = {}
14
+ each { |e|
15
+ ee = Cardtrick[e];
16
+ k = ee.key
17
+ v = ee.map { |eee| b.call(eee) }
18
+ h[k] = v
19
+ }
20
+ return h
21
+ end
22
+
23
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Cardtrick
4
- VERSION = "0.1.0"
4
+ VERSION = "0.1.1"
5
5
  end
data/lib/cardtrick.rb CHANGED
@@ -4,5 +4,180 @@ require_relative "cardtrick/version"
4
4
 
5
5
  module Cardtrick
6
6
  class Error < StandardError; end
7
- # Your code goes here...
7
+ @@GAME = lambda() { |e| puts %[GAME: #{e}] }
8
+ end
9
+
10
+ require_relative "cardtrick/dice"
11
+
12
+ require_relative "cardtrick/roll"
13
+
14
+ require_relative "cardtrick/card"
15
+
16
+ require_relative "cardtrick/deck"
17
+
18
+ require_relative "cardtrick/coin"
19
+
20
+ module Cardtrick
21
+
22
+ @@S = {
23
+ standard: {
24
+ suits: [:hearts, :diamonds, :spades, :clubs],
25
+ faces: { jack: 11, queen: 12, king: 13, ace: 1 },
26
+ numbers: { low: 2, high: 10 },
27
+ hand: 1,
28
+ shuffle: true,
29
+ specials: [
30
+ { suit: 'red',
31
+ card: 'Joker',
32
+ value: 0
33
+ },
34
+ { suit: 'black',
35
+ card: 'Joker',
36
+ value: 0
37
+ }
38
+ ],
39
+ deck: 'Standard poker cards'
40
+ },
41
+
42
+ tarot: {
43
+ suits: [:cups, :pentacles, :wands, :swords],
44
+ faces: { page: 10, knight: 10, queen: 10, king: 10, ace: 10 },
45
+ numbers: { low: 2, high: 10 },
46
+ hand: 3,
47
+ shuffle: true,
48
+ specials: [
49
+ { suit: 'Major Arcarna',
50
+ card: 'The Fool',
51
+ value: 0
52
+ },
53
+ { suit: 'Major Arcarna',
54
+ card: 'The Juggler',
55
+ value: 1
56
+ },
57
+ { suit: 'Major Arcarna',
58
+ card: 'The Popess',
59
+ value: 2
60
+ },
61
+ { suit: 'Major Arcarna',
62
+ card: 'The Empress',
63
+ value: 3
64
+ },
65
+ { suit: 'Major Arcarna',
66
+ card: 'The Emperor',
67
+ value: 4
68
+ },
69
+ { suit: 'Major Arcarna',
70
+ card: 'The Pope',
71
+ value: 5
72
+ },
73
+ { suit: 'Major Arcarna',
74
+ card: 'The Lovers',
75
+ value: 6
76
+ },
77
+ { suit: 'Major Arcarna',
78
+ card: 'The Chariot',
79
+ value: 7
80
+ },
81
+ { suit: 'Major Arcarna',
82
+ card: 'Justice',
83
+ value: 8
84
+ },
85
+ { suit: 'Major Arcarna',
86
+ card: 'The Hermit',
87
+ value: 9
88
+ },
89
+ { suit: 'Major Arcarna',
90
+ card: 'The Wheel of Fortune',
91
+ value: 10
92
+ },
93
+ { suit: 'Major Arcarna',
94
+ card: 'Strength',
95
+ value: 11
96
+ },
97
+ { suit: 'Major Arcarna',
98
+ card: 'The Hanged Man',
99
+ value: 12
100
+ },
101
+ { suit: 'Major Arcarna',
102
+ card: 'Death',
103
+ value: 13
104
+ },
105
+ { suit: 'Major Arcarna',
106
+ card: 'Temperance',
107
+ value: 14
108
+ },
109
+ { suit: 'Major Arcarna',
110
+ card: 'The Devil',
111
+ value: 15
112
+ },
113
+ { suit: 'Major Arcarna',
114
+ card: 'The House of God',
115
+ value: 16
116
+ },
117
+ { suit: 'Major Arcarna',
118
+ card: 'The Star',
119
+ value: 17
120
+ },
121
+ { suit: 'Major Arcarna',
122
+ card: 'The Moon',
123
+ value: 18
124
+ },
125
+ { suit: 'Major Arcarna',
126
+ card: 'The Sun',
127
+ value: 19
128
+ },
129
+ { suit: 'Major Arcarna',
130
+ card: 'Judgement',
131
+ value: 20
132
+ },
133
+ { suit: 'Major Arcarna',
134
+ card: 'The World',
135
+ value: 21
136
+ },
137
+ ],
138
+ deck: 'Tarot de Marsailles'
139
+ }
140
+ }
141
+
142
+ @@C = {}
143
+
144
+ def self.decks
145
+ @@S
146
+ end
147
+
148
+ def self.coin n, h={}
149
+ @@C[n] = Coin.new(h)
150
+ end
151
+
152
+ def self.deck n, h={}
153
+ @@C[n] = Deck.new(h)
154
+ end
155
+
156
+ def self.dice n, h={}
157
+ @@C[n] = Dice.new(h)
158
+ end
159
+
160
+ def self.roll n, *a
161
+ @@C[n] = Roll.new(a)
162
+ end
163
+
164
+ def self.coins n, *a
165
+ @@C[n] = Coins.new(a)
166
+ end
167
+
168
+ def self.[] k
169
+ @@C[k]
170
+ end
171
+
172
+ def self.keys
173
+ @@C.keys
174
+ end
175
+
176
+ def self.load f
177
+ eval(File.read(f))
178
+ end
179
+ end
180
+
181
+ if File.exist? 'cardtrick.rb'
182
+ Cardtrick.load 'cardtrick.rb'
8
183
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cardtrick
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Erik Olson
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-03-14 00:00:00.000000000 Z
11
+ date: 2024-03-18 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Generate and handle deck(s) of cards.
14
14
  email:
@@ -23,7 +23,13 @@ files:
23
23
  - README.md
24
24
  - Rakefile
25
25
  - cardtrick.gemspec
26
+ - cardtrick.rb
26
27
  - lib/cardtrick.rb
28
+ - lib/cardtrick/card.rb
29
+ - lib/cardtrick/coin.rb
30
+ - lib/cardtrick/deck.rb
31
+ - lib/cardtrick/dice.rb
32
+ - lib/cardtrick/roll.rb
27
33
  - lib/cardtrick/version.rb
28
34
  - sig/cardtrick.rbs
29
35
  homepage: https://github.com/xorgnak/cardtrick