craps 0.0.1 → 0.0.2
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.
- checksums.yaml +4 -4
- data/README.md +16 -7
- data/lib/craps/dice.rb +10 -8
- data/lib/craps/version.rb +1 -1
- data/spec/craps/dice_spec.rb +18 -0
- data/spec/craps/probability_spec.rb +62 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 153fe1c61339fa6cefe40a85c88e8b50244a8c2b
|
4
|
+
data.tar.gz: 76b5a8fe9c9b51c61649392b6a269c1bd1d3db27
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 160c71211fd61e173cc65a9adcfc43349426aff42216d97e450cda1b49eedefeb4f10526686ffbc064dd596b8f901178c3ce8fb2ed765c2bd3accce224a18220
|
7
|
+
data.tar.gz: bcf9396d65b58db5f5e96a8fb6bd68147b2175fff23c045361da0096157ad20992305f77ad52de73cce09a6d7c5cf8ae69decc348d11d5b3de4c802d07474023
|
data/README.md
CHANGED
@@ -18,7 +18,12 @@ Or install it yourself as:
|
|
18
18
|
|
19
19
|
## Basic Usage
|
20
20
|
|
21
|
-
|
21
|
+
First include Craps
|
22
|
+
```
|
23
|
+
include Craps
|
24
|
+
```
|
25
|
+
|
26
|
+
Then instantiate a new six sided die with:
|
22
27
|
```
|
23
28
|
dice = Dice.new(6)
|
24
29
|
```
|
@@ -29,24 +34,28 @@ Or install it yourself as:
|
|
29
34
|
```
|
30
35
|
|
31
36
|
Throw a single die with:
|
32
|
-
```
|
37
|
+
```
|
38
|
+
dice.roll
|
39
|
+
```
|
33
40
|
|
34
41
|
Throw multiple dice with:
|
35
|
-
```
|
42
|
+
```
|
43
|
+
dice.roll(2)
|
44
|
+
```
|
36
45
|
|
37
46
|
## Probability
|
38
47
|
|
39
48
|
To get the probability of throwing equal to or above a certain number on a die you can use the `higher_or_equal_to` method.
|
40
49
|
```
|
41
|
-
# On a 6 sided
|
42
|
-
dice.higher_or_equal_to(
|
50
|
+
# On a 6 sided die there is a chance 33.3% to get 5 or above:
|
51
|
+
dice.higher_or_equal_to(5)
|
43
52
|
# returns 0.33333333333333326
|
44
53
|
```
|
45
54
|
|
46
55
|
You can also do the oposite. Calculate the chanse to get equal to or bellow a certain number:
|
47
56
|
```
|
48
|
-
# On a 6 sided
|
49
|
-
dice.lower_or_equal_to(
|
57
|
+
# On a 6 sided die there is a 83.33333% chance to get 5 or bellow:
|
58
|
+
dice.lower_or_equal_to(5)
|
50
59
|
# returns 0.8333333333333334
|
51
60
|
```
|
52
61
|
|
data/lib/craps/dice.rb
CHANGED
@@ -10,9 +10,9 @@ module Craps
|
|
10
10
|
end
|
11
11
|
|
12
12
|
#Check how many dices that have been thrown in total
|
13
|
-
@@
|
14
|
-
def self.
|
15
|
-
@@
|
13
|
+
@@dice_thrown = 0
|
14
|
+
def self.dice_thrown
|
15
|
+
@@dice_thrown
|
16
16
|
end
|
17
17
|
|
18
18
|
# Throw one or many times
|
@@ -20,23 +20,22 @@ module Craps
|
|
20
20
|
result_array = []
|
21
21
|
throw_times.times do |n|
|
22
22
|
result_array[n] = rand(1..@sides)
|
23
|
-
@@
|
23
|
+
@@dice_thrown += 1
|
24
24
|
end
|
25
25
|
# Both remember the result and return it.
|
26
26
|
@last_result = result_array
|
27
27
|
end
|
28
28
|
|
29
|
-
|
30
29
|
# Some statistical helpers
|
31
30
|
# Check what the probability is of getting equalt to or more than a given result.
|
32
31
|
def base_prob
|
33
|
-
return (1/@sides)
|
32
|
+
return (1.0/@sides.to_f)
|
34
33
|
end
|
35
34
|
|
36
35
|
def higher_or_equal_to(numb)
|
37
36
|
if valid_integer(numb)
|
38
37
|
# Here we need to add 1/6 to get the correct result of "more than or equal"
|
39
|
-
1 - (numb.to_f/@sides.to_f) + (1.0
|
38
|
+
1 - (numb.to_f/@sides.to_f) + (1.0/@sides.to_f)
|
40
39
|
end
|
41
40
|
end
|
42
41
|
|
@@ -47,6 +46,9 @@ module Craps
|
|
47
46
|
end
|
48
47
|
end
|
49
48
|
|
49
|
+
|
50
|
+
private
|
51
|
+
|
50
52
|
def valid_integer(numb)
|
51
53
|
if numb.is_a? Integer
|
52
54
|
if @sides < numb
|
@@ -61,7 +63,7 @@ module Craps
|
|
61
63
|
end
|
62
64
|
end
|
63
65
|
|
64
|
-
end
|
66
|
+
end #end of Dice Class
|
65
67
|
|
66
68
|
# A standard D6 dice for easier access
|
67
69
|
class D6 < Dice
|
data/lib/craps/version.rb
CHANGED
data/spec/craps/dice_spec.rb
CHANGED
@@ -1,7 +1,23 @@
|
|
1
1
|
require "spec_helper"
|
2
2
|
|
3
3
|
module Craps
|
4
|
+
|
4
5
|
describe Dice do
|
6
|
+
|
7
|
+
|
8
|
+
describe "#dice_thrown" do
|
9
|
+
let(:thrown_times) { Craps::Dice.new(6) }
|
10
|
+
|
11
|
+
it "dice_thrown should be an integer" do
|
12
|
+
expect(Craps::Dice.dice_thrown).to be_an(Integer)
|
13
|
+
end
|
14
|
+
|
15
|
+
it "returns 0 times thrown for a new dice object" do
|
16
|
+
expect(Craps::Dice.dice_thrown).to eq(0)
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
|
5
21
|
describe "#roll" do
|
6
22
|
let(:roll) { Craps::D6.new.roll(6) }
|
7
23
|
|
@@ -14,6 +30,8 @@ module Craps
|
|
14
30
|
end
|
15
31
|
|
16
32
|
end
|
33
|
+
|
34
|
+
|
17
35
|
end
|
18
36
|
|
19
37
|
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
module Craps
|
4
|
+
|
5
|
+
describe Dice do
|
6
|
+
|
7
|
+
describe "#base_prob" do
|
8
|
+
let(:dice) { Craps::Dice.new(6) }
|
9
|
+
|
10
|
+
it "returns floating number" do
|
11
|
+
expect(dice.base_prob).to be_an(Float)
|
12
|
+
end
|
13
|
+
|
14
|
+
it "return correct result" do
|
15
|
+
expect(dice.base_prob).to eq(1.0/6.0)
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
|
20
|
+
describe "#higher_or_equal_to" do
|
21
|
+
let(:dice) { Craps::Dice.new(6) }
|
22
|
+
|
23
|
+
it "returns floating number" do
|
24
|
+
expect(dice.higher_or_equal_to(3)).to be_an(Float)
|
25
|
+
end
|
26
|
+
|
27
|
+
it "return correct result" do
|
28
|
+
expect(dice.higher_or_equal_to(5)).to eq(1.0 - (5.0/6.0)+(1.0/6.0))
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
|
33
|
+
describe "#lower_or_equal_to" do
|
34
|
+
let(:dice) { Craps::Dice.new(6) }
|
35
|
+
|
36
|
+
it "returns floating number" do
|
37
|
+
expect(dice.lower_or_equal_to(3)).to be_an(Float)
|
38
|
+
end
|
39
|
+
|
40
|
+
it "return correct result" do
|
41
|
+
expect(dice.lower_or_equal_to(5)).to eq(5.0/6.0)
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
|
46
|
+
#describe "#valid_integer" do
|
47
|
+
# let(:dice) { Craps::Dice.new(6) }
|
48
|
+
#
|
49
|
+
# it "returns floating number" do
|
50
|
+
# expect(dice.base_prob).to be_an(Float)
|
51
|
+
# end
|
52
|
+
#
|
53
|
+
# it "returns 16.66666%" do
|
54
|
+
# expect(dice.base_prob).to eq(1.0/6.0)
|
55
|
+
# end
|
56
|
+
#
|
57
|
+
#end
|
58
|
+
|
59
|
+
|
60
|
+
end
|
61
|
+
|
62
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: craps
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ole Henrik Skogstrøm
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-11-
|
11
|
+
date: 2014-11-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -71,6 +71,7 @@ files:
|
|
71
71
|
- lib/craps/dice.rb
|
72
72
|
- lib/craps/version.rb
|
73
73
|
- spec/craps/dice_spec.rb
|
74
|
+
- spec/craps/probability_spec.rb
|
74
75
|
- spec/spec_helper.rb
|
75
76
|
homepage: https://github.com/ohenrik/craps
|
76
77
|
licenses:
|
@@ -98,4 +99,5 @@ specification_version: 4
|
|
98
99
|
summary: Basic dice roller. Also gives basic probability calculations.
|
99
100
|
test_files:
|
100
101
|
- spec/craps/dice_spec.rb
|
102
|
+
- spec/craps/probability_spec.rb
|
101
103
|
- spec/spec_helper.rb
|