simplicard 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/CODE_OF_CONDUCT.md +13 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +39 -0
- data/Rakefile +9 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/lib/simplicard.rb +294 -0
- data/lib/simplicard/version.rb +3 -0
- data/simplicard.gemspec +25 -0
- metadata +97 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 715c1aa1076daaedf4a1878cbf83e0f46052827f
|
4
|
+
data.tar.gz: 85ce92ff0000529196e05f1b25a431aea1a3c810
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 527464428d9f0cab77e115fb5c47e2794572d794ffae105160cf76dc4b59a2fcd5fa9305f9c48ce95323c6208ae9865492984d31ef7159a310e2766bb4e8dea3
|
7
|
+
data.tar.gz: 85b40f90ba09e7c51f0719e05306653e990449c95dc0a630d4e680ea5b85b33e5944a00ae73032304c48b1f7df16249ffd495770abb5deeab4afdc3814c96e26
|
data/.gitignore
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
|
4
|
+
|
5
|
+
We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
|
6
|
+
|
7
|
+
Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
|
8
|
+
|
9
|
+
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
|
10
|
+
|
11
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
|
12
|
+
|
13
|
+
This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 Damien le
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
# Simplicard
|
2
|
+
|
3
|
+
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/simplicard`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'simplicard'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install simplicard
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
+
|
31
|
+
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` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
1. Fork it ( https://github.com/[my-github-username]/simplicard/fork )
|
36
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
37
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
38
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
39
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "simplicard"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/lib/simplicard.rb
ADDED
@@ -0,0 +1,294 @@
|
|
1
|
+
require "simplicard/version"
|
2
|
+
|
3
|
+
module Simplicard
|
4
|
+
class Suit
|
5
|
+
SPADES = 0
|
6
|
+
CLUBS = 1
|
7
|
+
DIAMONDS = 2
|
8
|
+
HEARTS = 3
|
9
|
+
|
10
|
+
VALID_SUIT = [SPADES,CLUBS,DIAMONDS,HEARTS]
|
11
|
+
|
12
|
+
|
13
|
+
def initialize(v)
|
14
|
+
@val = v
|
15
|
+
validate_suit
|
16
|
+
end
|
17
|
+
|
18
|
+
def value
|
19
|
+
@val
|
20
|
+
end
|
21
|
+
|
22
|
+
def suit_to_s
|
23
|
+
case @val
|
24
|
+
when SPADES
|
25
|
+
"Spades"
|
26
|
+
when CLUBS
|
27
|
+
"Clubs"
|
28
|
+
when DIAMONDS
|
29
|
+
"Diamonds"
|
30
|
+
when HEARTS
|
31
|
+
"Hearts"
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
private
|
36
|
+
def validate_suit
|
37
|
+
if VALID_SUIT.include?(@val)
|
38
|
+
return true
|
39
|
+
else
|
40
|
+
raise ArgumentError.new("Suit value is not valid")
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
|
47
|
+
class Card
|
48
|
+
|
49
|
+
ACE = 1
|
50
|
+
JACK =11
|
51
|
+
QUEEN =12
|
52
|
+
KING =13
|
53
|
+
|
54
|
+
# VALID_SUITS = [SPADES,CLUBS,DIAMONDS,HEARTS]
|
55
|
+
VALID_VALUES = [2,3,4,5,6,7,8,9,10,JACK,QUEEN,KING,ACE]
|
56
|
+
|
57
|
+
# this is the facevalue and suit of the card
|
58
|
+
attr_reader :value,:suit
|
59
|
+
|
60
|
+
def initialize(value, s)
|
61
|
+
@value = value
|
62
|
+
# @suit = suit
|
63
|
+
@suit = Suit.new(s)
|
64
|
+
validation_check
|
65
|
+
end
|
66
|
+
|
67
|
+
# this returns the value of the card. Depends on the game, the get_value can return different value than
|
68
|
+
# the facevalue
|
69
|
+
def get_value
|
70
|
+
@value
|
71
|
+
end
|
72
|
+
def get_suit
|
73
|
+
@suit
|
74
|
+
end
|
75
|
+
|
76
|
+
|
77
|
+
def value_to_s
|
78
|
+
case @value
|
79
|
+
when ACE
|
80
|
+
"Ace"
|
81
|
+
when JACK
|
82
|
+
"Jack"
|
83
|
+
when QUEEN
|
84
|
+
"Queen"
|
85
|
+
when KING
|
86
|
+
"King"
|
87
|
+
else
|
88
|
+
@value.to_s
|
89
|
+
end
|
90
|
+
|
91
|
+
end
|
92
|
+
|
93
|
+
def show
|
94
|
+
return "#{value_to_s} of #{@suit.suit_to_s}"
|
95
|
+
end
|
96
|
+
|
97
|
+
def is_face_card?
|
98
|
+
if [JACK,QUEEN,KING].include?(@value)
|
99
|
+
return true
|
100
|
+
else
|
101
|
+
return false
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
def is_an_ace?
|
106
|
+
return true if @value == ACE
|
107
|
+
end
|
108
|
+
|
109
|
+
private
|
110
|
+
|
111
|
+
def validation_check
|
112
|
+
if !VALID_VALUES.include?(@value)
|
113
|
+
raise ArgumentError.new("Card value is not valid")
|
114
|
+
end
|
115
|
+
|
116
|
+
end
|
117
|
+
|
118
|
+
end
|
119
|
+
|
120
|
+
class CardDeck
|
121
|
+
attr_reader :dealt_cards, :picked_cards, :added_cards
|
122
|
+
|
123
|
+
def initialize
|
124
|
+
@deck ||= []
|
125
|
+
(1..13).each do |value|
|
126
|
+
4.times do |suit|
|
127
|
+
card = Card.new(value,suit)
|
128
|
+
@deck << card
|
129
|
+
end
|
130
|
+
end
|
131
|
+
@dealt_cards = []
|
132
|
+
@picked_cards = []
|
133
|
+
@added_cards = []
|
134
|
+
end
|
135
|
+
|
136
|
+
def my_deck
|
137
|
+
@deck
|
138
|
+
end
|
139
|
+
|
140
|
+
def pick_a_card
|
141
|
+
if cards_left > 0
|
142
|
+
random = rand(cards_left)
|
143
|
+
card = get_deck.delete_at(random)
|
144
|
+
@picked_cards << card
|
145
|
+
card
|
146
|
+
end
|
147
|
+
end
|
148
|
+
|
149
|
+
def put_back_cards
|
150
|
+
(@picked_cards + @dealt_cards).each do |x|
|
151
|
+
if !find_card(x)
|
152
|
+
@deck << x
|
153
|
+
end
|
154
|
+
end
|
155
|
+
@dealt_cards = []
|
156
|
+
@picked_cards = []
|
157
|
+
@added_cards = []
|
158
|
+
@deck
|
159
|
+
end
|
160
|
+
|
161
|
+
def empty?
|
162
|
+
if @deck.length == 0
|
163
|
+
return true
|
164
|
+
else
|
165
|
+
return false
|
166
|
+
end
|
167
|
+
end
|
168
|
+
|
169
|
+
def is_full?
|
170
|
+
return true if @deck.length >=52
|
171
|
+
end
|
172
|
+
|
173
|
+
# check to see if the deck has all 52 cards without duplicate
|
174
|
+
def is_complete?
|
175
|
+
if empty?
|
176
|
+
puts "Deck is empty"
|
177
|
+
return false
|
178
|
+
elsif !is_full?
|
179
|
+
puts "Deck is missing some cards"
|
180
|
+
return false
|
181
|
+
else
|
182
|
+
myhash = {}
|
183
|
+
@deck.each do |x|
|
184
|
+
myhash[x.suit.value] ||= []
|
185
|
+
myhash[x.suit.value] << x.value
|
186
|
+
end
|
187
|
+
|
188
|
+
Suit::VALID_SUIT.each do |suit|
|
189
|
+
return false if myhash[suit].length != 13
|
190
|
+
end
|
191
|
+
return true
|
192
|
+
|
193
|
+
end
|
194
|
+
|
195
|
+
end
|
196
|
+
|
197
|
+
# return the position that the card is inserted into
|
198
|
+
# return false if deck is full or card exists in the deck
|
199
|
+
|
200
|
+
def add_a_card(card)
|
201
|
+
if is_full?
|
202
|
+
puts "Deck is full.You can't add a card"
|
203
|
+
return false
|
204
|
+
elsif empty?
|
205
|
+
@deck << card
|
206
|
+
@added_cards << card
|
207
|
+
cards_left
|
208
|
+
elsif !find_card(card)
|
209
|
+
random = rand(cards_left)
|
210
|
+
insert_card(random, card)
|
211
|
+
@added_cards << card
|
212
|
+
random
|
213
|
+
else
|
214
|
+
puts "Can't add a duplicate card to the Deck"
|
215
|
+
return false
|
216
|
+
end
|
217
|
+
end
|
218
|
+
|
219
|
+
|
220
|
+
|
221
|
+
def shuffle_it
|
222
|
+
cards_left.times do |i|
|
223
|
+
random = rand(cards_left)
|
224
|
+
temp = @deck[i]
|
225
|
+
@deck[i] = @deck[random]
|
226
|
+
@deck[random] = temp
|
227
|
+
end
|
228
|
+
get_deck
|
229
|
+
end
|
230
|
+
|
231
|
+
def deal
|
232
|
+
card = get_deck.pop
|
233
|
+
@dealt_cards << card
|
234
|
+
card
|
235
|
+
end
|
236
|
+
|
237
|
+
def cards_left
|
238
|
+
get_deck.length
|
239
|
+
end
|
240
|
+
|
241
|
+
def show
|
242
|
+
result = []
|
243
|
+
get_deck.each do |x|
|
244
|
+
result << x.show
|
245
|
+
end
|
246
|
+
result
|
247
|
+
end
|
248
|
+
|
249
|
+
def find_card(card)
|
250
|
+
if card_valid?(card)
|
251
|
+
get_deck.each_with_index do |x,index|
|
252
|
+
return index if (x.value == card.value) && (x.suit.value == card.suit.value)
|
253
|
+
end
|
254
|
+
return false
|
255
|
+
end
|
256
|
+
end
|
257
|
+
|
258
|
+
|
259
|
+
private
|
260
|
+
|
261
|
+
def card_valid?(card)
|
262
|
+
if card.is_a? Card
|
263
|
+
return true
|
264
|
+
else
|
265
|
+
raise "The argument is not a Card"
|
266
|
+
end
|
267
|
+
end
|
268
|
+
|
269
|
+
def get_deck
|
270
|
+
if @deck.empty? || @deck.nil?
|
271
|
+
raise "Deck is empty"
|
272
|
+
else
|
273
|
+
@deck
|
274
|
+
end
|
275
|
+
end
|
276
|
+
|
277
|
+
def insert_card(pos,card)
|
278
|
+
@deck.insert(pos, card)
|
279
|
+
end
|
280
|
+
|
281
|
+
def remove_card_from_dealt_cards(card)
|
282
|
+
if card_valid?(card)
|
283
|
+
@dealt_cards.each_with_index do |x,index|
|
284
|
+
if (x.value == card.value) && (x.suit.value == card.suit.value)
|
285
|
+
@dealt_cards.delete_at(index)
|
286
|
+
return index
|
287
|
+
end
|
288
|
+
end
|
289
|
+
end
|
290
|
+
end
|
291
|
+
|
292
|
+
|
293
|
+
end
|
294
|
+
end
|
data/simplicard.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'simplicard/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "simplicard"
|
8
|
+
spec.version = Simplicard::VERSION
|
9
|
+
spec.authors = ["Damien le"]
|
10
|
+
spec.email = ["mycauhoi@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Basic Card & CardDeck Class}
|
13
|
+
spec.description = %q{You can use this library to write a card game.}
|
14
|
+
spec.homepage = "https://github.com/cauhoi/simplicard"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.bindir = "exe"
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.8"
|
23
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
24
|
+
spec.add_development_dependency "minitest"
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: simplicard
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Damien le
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-04-29 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.8'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.8'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: You can use this library to write a card game.
|
56
|
+
email:
|
57
|
+
- mycauhoi@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- .gitignore
|
63
|
+
- CODE_OF_CONDUCT.md
|
64
|
+
- Gemfile
|
65
|
+
- LICENSE.txt
|
66
|
+
- README.md
|
67
|
+
- Rakefile
|
68
|
+
- bin/console
|
69
|
+
- bin/setup
|
70
|
+
- lib/simplicard.rb
|
71
|
+
- lib/simplicard/version.rb
|
72
|
+
- simplicard.gemspec
|
73
|
+
homepage: https://github.com/cauhoi/simplicard
|
74
|
+
licenses:
|
75
|
+
- MIT
|
76
|
+
metadata: {}
|
77
|
+
post_install_message:
|
78
|
+
rdoc_options: []
|
79
|
+
require_paths:
|
80
|
+
- lib
|
81
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
82
|
+
requirements:
|
83
|
+
- - '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - '>='
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0'
|
91
|
+
requirements: []
|
92
|
+
rubyforge_project:
|
93
|
+
rubygems_version: 2.4.6
|
94
|
+
signing_key:
|
95
|
+
specification_version: 4
|
96
|
+
summary: Basic Card & CardDeck Class
|
97
|
+
test_files: []
|