tundengine 0.0.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.
- checksums.yaml +7 -0
- data/Gemfile +4 -0
- data/LICENSE +21 -0
- data/README.md +29 -0
- data/Rakefile +8 -0
- data/lib/tundengine.rb +67 -0
- data/lib/tundengine/algebraic_data_type.rb +33 -0
- data/lib/tundengine/card_percolator.rb +9 -0
- data/lib/tundengine/cards/card.rb +63 -0
- data/lib/tundengine/cards/null.rb +18 -0
- data/lib/tundengine/dealer.rb +52 -0
- data/lib/tundengine/deck.rb +41 -0
- data/lib/tundengine/declarations/base.rb +24 -0
- data/lib/tundengine/declarations/las_cuarenta.rb +15 -0
- data/lib/tundengine/declarations/las_veinte.rb +29 -0
- data/lib/tundengine/declarations/null.rb +18 -0
- data/lib/tundengine/declarations/tute.rb +23 -0
- data/lib/tundengine/declarations/void.rb +15 -0
- data/lib/tundengine/hand.rb +35 -0
- data/lib/tundengine/move.rb +13 -0
- data/lib/tundengine/null_move.rb +13 -0
- data/lib/tundengine/player/in_match.rb +29 -0
- data/lib/tundengine/player/in_round.rb +71 -0
- data/lib/tundengine/player/in_turn.rb +31 -0
- data/lib/tundengine/ranks/base.rb +31 -0
- data/lib/tundengine/ranks/cinco.rb +10 -0
- data/lib/tundengine/ranks/cuatro.rb +10 -0
- data/lib/tundengine/ranks/diez.rb +10 -0
- data/lib/tundengine/ranks/doce.rb +10 -0
- data/lib/tundengine/ranks/dos.rb +10 -0
- data/lib/tundengine/ranks/null.rb +10 -0
- data/lib/tundengine/ranks/once.rb +10 -0
- data/lib/tundengine/ranks/seis.rb +10 -0
- data/lib/tundengine/ranks/siete.rb +10 -0
- data/lib/tundengine/ranks/tres.rb +10 -0
- data/lib/tundengine/ranks/uno.rb +10 -0
- data/lib/tundengine/ranks/white.rb +9 -0
- data/lib/tundengine/round_analyzer.rb +67 -0
- data/lib/tundengine/stages/base.rb +74 -0
- data/lib/tundengine/stages/match.rb +80 -0
- data/lib/tundengine/stages/null.rb +13 -0
- data/lib/tundengine/stages/round.rb +85 -0
- data/lib/tundengine/stages/tournament.rb +72 -0
- data/lib/tundengine/stages/trick.rb +90 -0
- data/lib/tundengine/stages/turn.rb +44 -0
- data/lib/tundengine/strategies/automatic.rb +27 -0
- data/lib/tundengine/strategies/base.rb +8 -0
- data/lib/tundengine/strategies/manual.rb +33 -0
- data/lib/tundengine/strategies/random.rb +15 -0
- data/lib/tundengine/stringifiable_by_class.rb +9 -0
- data/lib/tundengine/suits/base.rb +19 -0
- data/lib/tundengine/suits/basto.rb +7 -0
- data/lib/tundengine/suits/copa.rb +7 -0
- data/lib/tundengine/suits/espada.rb +7 -0
- data/lib/tundengine/suits/null.rb +14 -0
- data/lib/tundengine/suits/oro.rb +7 -0
- data/lib/tundengine/tute_values/base.rb +17 -0
- data/lib/tundengine/tute_values/capotes.rb +22 -0
- data/lib/tundengine/tute_values/nothing.rb +13 -0
- data/lib/tundengine/tute_values/victory.rb +17 -0
- data/lib/tundengine/version.rb +17 -0
- data/test/test_card_strength.rb +58 -0
- data/test/test_helper.rb +35 -0
- data/test/test_playable_cards.rb +66 -0
- data/test/test_random_tournaments.rb +31 -0
- data/tundengine.gemspec +28 -0
- metadata +154 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 92d1815dcf85bb0d4d18a2980b687704349e18f6
|
4
|
+
data.tar.gz: 70086624730d3053c5594a58814b184546c74168
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ac751d339605ac1c7368deb2ee58acfa3b924747d33deacc8ae6ae659a6d4e00c957266e17ba51374537cae5d62475535fd57309d158267a6a091f9affc664bd
|
7
|
+
data.tar.gz: 150acf306102d02a918ed35929d41d926065bbc2f9697130508e1edcce722f1724e3dbc14da0f103caa36cca0a04dc373efcf1ea6488cf76534fc576c0c23aaa
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2017 eugeniobruno
|
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 all
|
13
|
+
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 THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# Tundengine
|
2
|
+
|
3
|
+
A Ruby gem to build applications based on the rules of the Tute card game.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'tundengine'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install tundengine
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here.
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/lib/tundengine.rb
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
require 'forwardable'
|
2
|
+
|
3
|
+
require 'tundengine/stringifiable_by_class'
|
4
|
+
require 'tundengine/algebraic_data_type'
|
5
|
+
require 'tundengine/card_percolator'
|
6
|
+
require 'tundengine/null_move'
|
7
|
+
require 'tundengine/move'
|
8
|
+
|
9
|
+
require 'tundengine/suits/base'
|
10
|
+
require 'tundengine/suits/null'
|
11
|
+
require 'tundengine/suits/oro'
|
12
|
+
require 'tundengine/suits/copa'
|
13
|
+
require 'tundengine/suits/espada'
|
14
|
+
require 'tundengine/suits/basto'
|
15
|
+
|
16
|
+
require 'tundengine/ranks/base'
|
17
|
+
require 'tundengine/ranks/null'
|
18
|
+
require 'tundengine/ranks/white'
|
19
|
+
require 'tundengine/ranks/uno'
|
20
|
+
require 'tundengine/ranks/dos'
|
21
|
+
require 'tundengine/ranks/tres'
|
22
|
+
require 'tundengine/ranks/cuatro'
|
23
|
+
require 'tundengine/ranks/cinco'
|
24
|
+
require 'tundengine/ranks/seis'
|
25
|
+
require 'tundengine/ranks/siete'
|
26
|
+
require 'tundengine/ranks/diez'
|
27
|
+
require 'tundengine/ranks/once'
|
28
|
+
require 'tundengine/ranks/doce'
|
29
|
+
|
30
|
+
require 'tundengine/cards/card'
|
31
|
+
require 'tundengine/cards/null'
|
32
|
+
|
33
|
+
require 'tundengine/declarations/base'
|
34
|
+
require 'tundengine/declarations/null'
|
35
|
+
require 'tundengine/declarations/void'
|
36
|
+
require 'tundengine/declarations/las_veinte'
|
37
|
+
require 'tundengine/declarations/las_cuarenta'
|
38
|
+
require 'tundengine/declarations/tute'
|
39
|
+
|
40
|
+
require 'tundengine/tute_values/base'
|
41
|
+
require 'tundengine/tute_values/nothing'
|
42
|
+
require 'tundengine/tute_values/capotes'
|
43
|
+
require 'tundengine/tute_values/victory'
|
44
|
+
|
45
|
+
require 'tundengine/deck'
|
46
|
+
|
47
|
+
require 'tundengine/stages/base'
|
48
|
+
require 'tundengine/stages/null'
|
49
|
+
require 'tundengine/stages/turn'
|
50
|
+
require 'tundengine/stages/trick'
|
51
|
+
require 'tundengine/stages/round'
|
52
|
+
require 'tundengine/stages/match'
|
53
|
+
require 'tundengine/stages/tournament'
|
54
|
+
|
55
|
+
require 'tundengine/player/in_match'
|
56
|
+
require 'tundengine/player/in_round'
|
57
|
+
require 'tundengine/player/in_turn'
|
58
|
+
|
59
|
+
require 'tundengine/hand'
|
60
|
+
|
61
|
+
require 'tundengine/dealer'
|
62
|
+
require 'tundengine/round_analyzer'
|
63
|
+
|
64
|
+
require 'tundengine/strategies/base'
|
65
|
+
require 'tundengine/strategies/manual'
|
66
|
+
require 'tundengine/strategies/automatic'
|
67
|
+
require 'tundengine/strategies/random'
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module Tundengine
|
2
|
+
module AlgebraicDataType
|
3
|
+
|
4
|
+
def initialize
|
5
|
+
freeze
|
6
|
+
end
|
7
|
+
|
8
|
+
def to_s
|
9
|
+
(state.one? ? state.first : state).to_s
|
10
|
+
end
|
11
|
+
|
12
|
+
def ==(o)
|
13
|
+
o.class == self.class && o.state == state
|
14
|
+
end
|
15
|
+
|
16
|
+
alias_method :eql?, :==
|
17
|
+
|
18
|
+
def hash
|
19
|
+
state.hash
|
20
|
+
end
|
21
|
+
|
22
|
+
protected
|
23
|
+
|
24
|
+
def state
|
25
|
+
[self.class.name.split('::').last].concat identifier
|
26
|
+
end
|
27
|
+
|
28
|
+
def identifier
|
29
|
+
[]
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
module Tundengine
|
2
|
+
module Cards
|
3
|
+
class Card
|
4
|
+
|
5
|
+
include AlgebraicDataType
|
6
|
+
include Move
|
7
|
+
|
8
|
+
attr_reader :rank, :suit
|
9
|
+
|
10
|
+
def initialize(rank, suit)
|
11
|
+
@rank = rank
|
12
|
+
@suit = suit
|
13
|
+
super()
|
14
|
+
end
|
15
|
+
|
16
|
+
def to_s
|
17
|
+
"#{@rank.to_s} de #{@suit.to_s}"
|
18
|
+
end
|
19
|
+
|
20
|
+
def is_of_any_rank?(ranks)
|
21
|
+
ranks.include? @rank
|
22
|
+
end
|
23
|
+
|
24
|
+
def is_of_any_suit?(suits)
|
25
|
+
suits.include? @suit
|
26
|
+
end
|
27
|
+
|
28
|
+
def round_points
|
29
|
+
@rank.round_points
|
30
|
+
end
|
31
|
+
|
32
|
+
# assumes other_card is winning the current trick
|
33
|
+
def beats?(other_card, trump_suit)
|
34
|
+
suit_powers = { other_card.suit => 1, trump_suit => 2 }
|
35
|
+
suit_powers[Suits::Null.instance] = 0
|
36
|
+
|
37
|
+
own_suit_power, other_suit_power = [@suit, other_card.suit]
|
38
|
+
.map { |s| suit_powers.fetch(s, 0) }
|
39
|
+
|
40
|
+
if own_suit_power == other_suit_power
|
41
|
+
beats_same_suit?(other_card)
|
42
|
+
else
|
43
|
+
own_suit_power > other_suit_power
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def beats_same_suit?(other_card)
|
48
|
+
@rank > other_card.rank
|
49
|
+
end
|
50
|
+
|
51
|
+
def exists?
|
52
|
+
true
|
53
|
+
end
|
54
|
+
|
55
|
+
protected
|
56
|
+
|
57
|
+
def state
|
58
|
+
[@rank, @suit]
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
module Tundengine
|
2
|
+
class Dealer
|
3
|
+
|
4
|
+
include Singleton
|
5
|
+
|
6
|
+
def deal!(players, trump_suit, options = {})
|
7
|
+
default_options = {
|
8
|
+
premade_hands: Hash.new([])
|
9
|
+
}
|
10
|
+
options = default_options.merge(options)
|
11
|
+
|
12
|
+
premade_hands = options.fetch :premade_hands
|
13
|
+
|
14
|
+
validate_premade_hands(premade_hands)
|
15
|
+
|
16
|
+
premade_players_names = premade_hands.keys.map(&:name)
|
17
|
+
premade_players, other_players = players.partition do |p|
|
18
|
+
premade_players_names.include? p.name
|
19
|
+
end
|
20
|
+
|
21
|
+
premade_players.each do |p|
|
22
|
+
premade_hands[p].each { |c| p.take_card! c }
|
23
|
+
end
|
24
|
+
|
25
|
+
premade_cards = premade_hands.values.flatten(1)
|
26
|
+
|
27
|
+
all_cards = Deck.for(players.length, trump_suit).shuffle
|
28
|
+
do_deal!(other_players, all_cards - premade_cards)
|
29
|
+
end
|
30
|
+
|
31
|
+
protected
|
32
|
+
|
33
|
+
def do_deal!(players, cards)
|
34
|
+
number_of_players = players.length
|
35
|
+
cards.each_with_index do |c, i|
|
36
|
+
players[i % number_of_players].take_card! c
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def validate_premade_hands(hands)
|
41
|
+
if hands.values.map(&:length).uniq.length > 1
|
42
|
+
raise "all premade hands must have the same amount of cards"
|
43
|
+
end
|
44
|
+
|
45
|
+
premade_cards = hands.values.flatten(1)
|
46
|
+
unless premade_cards.length == premade_cards.uniq.length
|
47
|
+
raise "premade hands cannot share cards"
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
module Tundengine
|
2
|
+
class Deck
|
3
|
+
|
4
|
+
RANK_CLASSES = %w(Uno Dos Tres Cuatro Cinco Seis Siete Diez Once Doce)
|
5
|
+
.map { |r| Object.const_get("Tundengine::Ranks::#{r}") }
|
6
|
+
|
7
|
+
SUIT_CLASSES = %w(Oro Copa Espada Basto)
|
8
|
+
.map { |s| Object.const_get("Tundengine::Suits::#{s}") }
|
9
|
+
|
10
|
+
RANKS = RANK_CLASSES.map(&:instance).freeze
|
11
|
+
|
12
|
+
SUITS = SUIT_CLASSES.map(&:instance).freeze
|
13
|
+
|
14
|
+
CARDS = SUITS.product(RANKS).map { |s, r| r.de s }.freeze
|
15
|
+
|
16
|
+
DECLARATIONS = SUITS.map { |s| Declarations::LasVeinte.en(s) }
|
17
|
+
.concat( [Declarations::LasCuarenta, Declarations::Tute,
|
18
|
+
Declarations::Void]
|
19
|
+
.map(&:instance)).freeze
|
20
|
+
|
21
|
+
class << self
|
22
|
+
|
23
|
+
def for(number_of_players, trump_suit)
|
24
|
+
case number_of_players
|
25
|
+
when 3
|
26
|
+
CARDS - [Ranks::Dos.de(trump_suit)]
|
27
|
+
when 4..5
|
28
|
+
CARDS
|
29
|
+
else
|
30
|
+
raise "invalid number of players: #{number_of_players}"
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def cards_of(ranks = RANKS, suits = SUITS)
|
35
|
+
CARDS.select { |c| c.is_of_any_rank? ranks and c.is_of_any_suit? suits }
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module Tundengine
|
2
|
+
module Declarations
|
3
|
+
class Base
|
4
|
+
|
5
|
+
include StringifiableByClass
|
6
|
+
include Move
|
7
|
+
|
8
|
+
attr_reader :round_points
|
9
|
+
|
10
|
+
def initialize
|
11
|
+
@round_points = self.class::ROUND_POINTS
|
12
|
+
end
|
13
|
+
|
14
|
+
def has_tute_effect?(tute_value)
|
15
|
+
false
|
16
|
+
end
|
17
|
+
|
18
|
+
def finishes_round?(tute_value)
|
19
|
+
false
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module Tundengine
|
2
|
+
module Declarations
|
3
|
+
class LasVeinte < Base
|
4
|
+
|
5
|
+
include AlgebraicDataType
|
6
|
+
|
7
|
+
ROUND_POINTS = 20
|
8
|
+
|
9
|
+
def self.en(suit)
|
10
|
+
s = suit.is_a?(Suits::Base) ? suit : suit.instance
|
11
|
+
new(s)
|
12
|
+
end
|
13
|
+
|
14
|
+
def initialize(suit)
|
15
|
+
@suit = suit
|
16
|
+
super()
|
17
|
+
end
|
18
|
+
|
19
|
+
def is_declarable?(hand, trump_suit)
|
20
|
+
@suit != trump_suit and hand.has_knight_and_king_of?(@suit)
|
21
|
+
end
|
22
|
+
|
23
|
+
def identifier
|
24
|
+
[@suit]
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|