swisspairing 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.
- checksums.yaml +7 -0
- data/.rspec +3 -0
- data/.rubocop.yml +8 -0
- data/CHANGELOG.md +25 -0
- data/CODE_OF_CONDUCT.md +132 -0
- data/LICENSE.txt +21 -0
- data/README.md +155 -0
- data/Rakefile +12 -0
- data/lib/swisspairing/version.rb +5 -0
- data/lib/swisspairing.rb +203 -0
- data/prd.md +0 -0
- data/sig/swisspairing.rbs +51 -0
- metadata +55 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 6ba88b0ac4cfe33e44110048f6977183d94ba8babfde073352827acb43d97028
|
4
|
+
data.tar.gz: 64f33d60708d7935b05f3c5c903496e6a3ffb6e84767a4c245691ca4d2efa48f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b6ea11a8f4dc241f77c0835af2b3505ea8db8c28327cd285374996196243351cc3f55f8be215d3e33ae44065e3c2d234a2b7aa5e122e7c5674dbf9b2da7c56e2
|
7
|
+
data.tar.gz: 2c25b7666e8c57ce2d6d0a66b61a62ff1414c5363e37a8059a06d7458b81e6e6c83cc31d4ae4f7410c604ae62e7736ff352d909b3c1a2d75643c516f74236e28
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
data/CHANGELOG.md
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# Changelog
|
2
|
+
|
3
|
+
All notable changes to this project will be documented in this file.
|
4
|
+
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
6
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
7
|
+
|
8
|
+
## [Unreleased]
|
9
|
+
|
10
|
+
## [1.0.0] - 2025-05-02
|
11
|
+
|
12
|
+
### Added
|
13
|
+
- Initial release with FIDE-compliant Swiss pairing system
|
14
|
+
- Player management with ratings and FIDE titles
|
15
|
+
- Tournament management with configurable rounds
|
16
|
+
- Color allocation following FIDE rules
|
17
|
+
- Bye handling for odd numbers of players
|
18
|
+
- Advanced acceleration system for large tournaments
|
19
|
+
- Tie-break systems:
|
20
|
+
- Buchholz score
|
21
|
+
- Sonneborn-Berger score
|
22
|
+
- Rating-based sorting
|
23
|
+
- Enhanced color allocation with strict FIDE compliance
|
24
|
+
- Comprehensive test suite
|
25
|
+
- Full documentation with examples
|
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,132 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our
|
6
|
+
community a harassment-free experience for everyone, regardless of age, body
|
7
|
+
size, visible or invisible disability, ethnicity, sex characteristics, gender
|
8
|
+
identity and expression, level of experience, education, socio-economic status,
|
9
|
+
nationality, personal appearance, race, caste, color, religion, or sexual
|
10
|
+
identity and orientation.
|
11
|
+
|
12
|
+
We pledge to act and interact in ways that contribute to an open, welcoming,
|
13
|
+
diverse, inclusive, and healthy community.
|
14
|
+
|
15
|
+
## Our Standards
|
16
|
+
|
17
|
+
Examples of behavior that contributes to a positive environment for our
|
18
|
+
community include:
|
19
|
+
|
20
|
+
* Demonstrating empathy and kindness toward other people
|
21
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
22
|
+
* Giving and gracefully accepting constructive feedback
|
23
|
+
* Accepting responsibility and apologizing to those affected by our mistakes,
|
24
|
+
and learning from the experience
|
25
|
+
* Focusing on what is best not just for us as individuals, but for the overall
|
26
|
+
community
|
27
|
+
|
28
|
+
Examples of unacceptable behavior include:
|
29
|
+
|
30
|
+
* The use of sexualized language or imagery, and sexual attention or advances of
|
31
|
+
any kind
|
32
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
33
|
+
* Public or private harassment
|
34
|
+
* Publishing others' private information, such as a physical or email address,
|
35
|
+
without their explicit permission
|
36
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
37
|
+
professional setting
|
38
|
+
|
39
|
+
## Enforcement Responsibilities
|
40
|
+
|
41
|
+
Community leaders are responsible for clarifying and enforcing our standards of
|
42
|
+
acceptable behavior and will take appropriate and fair corrective action in
|
43
|
+
response to any behavior that they deem inappropriate, threatening, offensive,
|
44
|
+
or harmful.
|
45
|
+
|
46
|
+
Community leaders have the right and responsibility to remove, edit, or reject
|
47
|
+
comments, commits, code, wiki edits, issues, and other contributions that are
|
48
|
+
not aligned to this Code of Conduct, and will communicate reasons for moderation
|
49
|
+
decisions when appropriate.
|
50
|
+
|
51
|
+
## Scope
|
52
|
+
|
53
|
+
This Code of Conduct applies within all community spaces, and also applies when
|
54
|
+
an individual is officially representing the community in public spaces.
|
55
|
+
Examples of representing our community include using an official email address,
|
56
|
+
posting via an official social media account, or acting as an appointed
|
57
|
+
representative at an online or offline event.
|
58
|
+
|
59
|
+
## Enforcement
|
60
|
+
|
61
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
62
|
+
reported to the community leaders responsible for enforcement at
|
63
|
+
[INSERT CONTACT METHOD].
|
64
|
+
All complaints will be reviewed and investigated promptly and fairly.
|
65
|
+
|
66
|
+
All community leaders are obligated to respect the privacy and security of the
|
67
|
+
reporter of any incident.
|
68
|
+
|
69
|
+
## Enforcement Guidelines
|
70
|
+
|
71
|
+
Community leaders will follow these Community Impact Guidelines in determining
|
72
|
+
the consequences for any action they deem in violation of this Code of Conduct:
|
73
|
+
|
74
|
+
### 1. Correction
|
75
|
+
|
76
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed
|
77
|
+
unprofessional or unwelcome in the community.
|
78
|
+
|
79
|
+
**Consequence**: A private, written warning from community leaders, providing
|
80
|
+
clarity around the nature of the violation and an explanation of why the
|
81
|
+
behavior was inappropriate. A public apology may be requested.
|
82
|
+
|
83
|
+
### 2. Warning
|
84
|
+
|
85
|
+
**Community Impact**: A violation through a single incident or series of
|
86
|
+
actions.
|
87
|
+
|
88
|
+
**Consequence**: A warning with consequences for continued behavior. No
|
89
|
+
interaction with the people involved, including unsolicited interaction with
|
90
|
+
those enforcing the Code of Conduct, for a specified period of time. This
|
91
|
+
includes avoiding interactions in community spaces as well as external channels
|
92
|
+
like social media. Violating these terms may lead to a temporary or permanent
|
93
|
+
ban.
|
94
|
+
|
95
|
+
### 3. Temporary Ban
|
96
|
+
|
97
|
+
**Community Impact**: A serious violation of community standards, including
|
98
|
+
sustained inappropriate behavior.
|
99
|
+
|
100
|
+
**Consequence**: A temporary ban from any sort of interaction or public
|
101
|
+
communication with the community for a specified period of time. No public or
|
102
|
+
private interaction with the people involved, including unsolicited interaction
|
103
|
+
with those enforcing the Code of Conduct, is allowed during this period.
|
104
|
+
Violating these terms may lead to a permanent ban.
|
105
|
+
|
106
|
+
### 4. Permanent Ban
|
107
|
+
|
108
|
+
**Community Impact**: Demonstrating a pattern of violation of community
|
109
|
+
standards, including sustained inappropriate behavior, harassment of an
|
110
|
+
individual, or aggression toward or disparagement of classes of individuals.
|
111
|
+
|
112
|
+
**Consequence**: A permanent ban from any sort of public interaction within the
|
113
|
+
community.
|
114
|
+
|
115
|
+
## Attribution
|
116
|
+
|
117
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
118
|
+
version 2.1, available at
|
119
|
+
[https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
|
120
|
+
|
121
|
+
Community Impact Guidelines were inspired by
|
122
|
+
[Mozilla's code of conduct enforcement ladder][Mozilla CoC].
|
123
|
+
|
124
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
125
|
+
[https://www.contributor-covenant.org/faq][FAQ]. Translations are available at
|
126
|
+
[https://www.contributor-covenant.org/translations][translations].
|
127
|
+
|
128
|
+
[homepage]: https://www.contributor-covenant.org
|
129
|
+
[v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
|
130
|
+
[Mozilla CoC]: https://github.com/mozilla/diversity
|
131
|
+
[FAQ]: https://www.contributor-covenant.org/faq
|
132
|
+
[translations]: https://www.contributor-covenant.org/translations
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2025 Lapo Elisacci
|
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,155 @@
|
|
1
|
+
# SwissPairing
|
2
|
+
|
3
|
+
A Ruby gem that implements FIDE-compliant Swiss pairing system for chess tournaments. This gem follows the official FIDE Swiss Rules effective from July 1, 2025.
|
4
|
+
|
5
|
+
## Features
|
6
|
+
|
7
|
+
- FIDE-compliant Swiss pairing system
|
8
|
+
- Handles player ratings and FIDE titles
|
9
|
+
- Supports tournament scoring and results
|
10
|
+
- Manages color allocation according to FIDE rules
|
11
|
+
- Handles byes for odd numbers of players
|
12
|
+
- Prevents rematches between players
|
13
|
+
|
14
|
+
## Installation
|
15
|
+
|
16
|
+
Add this line to your application's Gemfile:
|
17
|
+
|
18
|
+
```ruby
|
19
|
+
gem 'swisspairing'
|
20
|
+
```
|
21
|
+
|
22
|
+
And then execute:
|
23
|
+
|
24
|
+
```bash
|
25
|
+
$ bundle install
|
26
|
+
```
|
27
|
+
|
28
|
+
Or install it yourself as:
|
29
|
+
|
30
|
+
```bash
|
31
|
+
$ gem install swisspairing
|
32
|
+
```
|
33
|
+
|
34
|
+
## Usage
|
35
|
+
|
36
|
+
### Creating Players
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
require 'swisspairing'
|
40
|
+
|
41
|
+
# Create players with ratings and optional FIDE titles
|
42
|
+
players = [
|
43
|
+
Swisspairing::Player.new(name: "GM Smith", rating: 2500, title: "GM", id: 1),
|
44
|
+
Swisspairing::Player.new(name: "IM Jones", rating: 2400, title: "IM", id: 2),
|
45
|
+
Swisspairing::Player.new(name: "FM Brown", rating: 2300, title: "FM", id: 3),
|
46
|
+
Swisspairing::Player.new(name: "Alice", rating: 2200, id: 4)
|
47
|
+
]
|
48
|
+
```
|
49
|
+
|
50
|
+
### Setting Up a Tournament
|
51
|
+
|
52
|
+
```ruby
|
53
|
+
# Create a new tournament with players and number of rounds
|
54
|
+
tournament = Swisspairing::Tournament.new(players: players, total_rounds: 5)
|
55
|
+
```
|
56
|
+
|
57
|
+
### Generating Pairings
|
58
|
+
|
59
|
+
```ruby
|
60
|
+
# Generate pairings for the current round
|
61
|
+
pairings = tournament.generate_pairings
|
62
|
+
|
63
|
+
# Process each pairing
|
64
|
+
pairings.each do |pairing|
|
65
|
+
if pairing.is_bye
|
66
|
+
puts "#{pairing.white.name} receives a bye"
|
67
|
+
tournament.apply_result(pairing, "1")
|
68
|
+
else
|
69
|
+
puts "#{pairing.white.name} (White) vs #{pairing.black.name} (Black)"
|
70
|
+
# After the game, apply the result:
|
71
|
+
# "1-0" for white win
|
72
|
+
# "0-1" for black win
|
73
|
+
# "½-½" for draw
|
74
|
+
tournament.apply_result(pairing, "1-0") # example: white wins
|
75
|
+
end
|
76
|
+
end
|
77
|
+
```
|
78
|
+
|
79
|
+
## Advanced Features
|
80
|
+
|
81
|
+
### Acceleration
|
82
|
+
|
83
|
+
For tournaments with many players, you can enable acceleration to help stronger players meet each other earlier:
|
84
|
+
|
85
|
+
```ruby
|
86
|
+
tournament = Swisspairing::Tournament.new(
|
87
|
+
players: players,
|
88
|
+
total_rounds: 5,
|
89
|
+
accelerated: true # Enable acceleration
|
90
|
+
)
|
91
|
+
```
|
92
|
+
|
93
|
+
### Tie-Breaks
|
94
|
+
|
95
|
+
The system implements official FIDE tie-break systems:
|
96
|
+
|
97
|
+
1. Direct score
|
98
|
+
2. Buchholz score (sum of opponents' scores)
|
99
|
+
3. Sonneborn-Berger score
|
100
|
+
4. Rating
|
101
|
+
|
102
|
+
Get tournament standings with tie-breaks:
|
103
|
+
|
104
|
+
```ruby
|
105
|
+
standings = tournament.standings
|
106
|
+
standings.each_with_index do |player, index|
|
107
|
+
puts "#{index + 1}. #{player.name} (#{player.score} pts)"
|
108
|
+
end
|
109
|
+
```
|
110
|
+
|
111
|
+
### Color Allocation
|
112
|
+
|
113
|
+
The system follows strict FIDE color allocation rules:
|
114
|
+
|
115
|
+
- No player gets the same color three times in a row
|
116
|
+
- Color difference cannot exceed +2 or -2
|
117
|
+
- Players alternate colors when possible
|
118
|
+
- Color allocation considers previous tournament history
|
119
|
+
|
120
|
+
## FIDE Compliance
|
121
|
+
|
122
|
+
This gem strictly follows FIDE Swiss Rules from the FIDE Handbook C.04:
|
123
|
+
|
124
|
+
- C.04.1: Basic Rules for Swiss Systems
|
125
|
+
- C.04.2: General Handling Rules
|
126
|
+
- C.04.3: FIDE (Dutch) System
|
127
|
+
|
128
|
+
Features include:
|
129
|
+
- Proper initial ranking by rating and title
|
130
|
+
- Score-based pairing groups
|
131
|
+
- Correct color allocation rules
|
132
|
+
- Acceleration method for large tournaments
|
133
|
+
- Official tie-break systems
|
134
|
+
- Proper handling of byes and odd numbers of players
|
135
|
+
|
136
|
+
## Error Handling
|
137
|
+
|
138
|
+
The system includes various safeguards:
|
139
|
+
|
140
|
+
```ruby
|
141
|
+
begin
|
142
|
+
tournament.generate_pairings
|
143
|
+
rescue Swisspairing::Error => e
|
144
|
+
puts "Tournament error: #{e.message}"
|
145
|
+
end
|
146
|
+
```
|
147
|
+
|
148
|
+
Common errors:
|
149
|
+
- Tournament already complete
|
150
|
+
- Invalid result format
|
151
|
+
- Invalid player data
|
152
|
+
|
153
|
+
## License
|
154
|
+
|
155
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/lib/swisspairing.rb
ADDED
@@ -0,0 +1,203 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "swisspairing/version"
|
4
|
+
|
5
|
+
module Swisspairing
|
6
|
+
class Error < StandardError; end
|
7
|
+
|
8
|
+
FIDE_TITLES = %w[GM IM WGM FM WIM CM WFM WCM].freeze
|
9
|
+
|
10
|
+
class Player
|
11
|
+
attr_reader :name, :rating, :title, :id
|
12
|
+
attr_accessor :score, :opponents, :colors
|
13
|
+
|
14
|
+
def initialize(name:, rating:, title: nil, id: nil)
|
15
|
+
@name = name
|
16
|
+
@rating = rating.to_i
|
17
|
+
@title = title if FIDE_TITLES.include?(title)
|
18
|
+
@id = id
|
19
|
+
@score = 0
|
20
|
+
@opponents = []
|
21
|
+
@colors = [] # 'W' for white, 'B' for black, '=' for bye
|
22
|
+
end
|
23
|
+
|
24
|
+
def color_balance
|
25
|
+
colors.count('W') - colors.count('B')
|
26
|
+
end
|
27
|
+
|
28
|
+
def can_receive_bye?
|
29
|
+
!colors.include?('=')
|
30
|
+
end
|
31
|
+
|
32
|
+
def played_against?(player)
|
33
|
+
opponents.include?(player.id)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
class Tournament
|
38
|
+
attr_reader :players, :current_round, :total_rounds, :results
|
39
|
+
|
40
|
+
def initialize(players:, total_rounds:, accelerated: false)
|
41
|
+
@players = players
|
42
|
+
@total_rounds = total_rounds
|
43
|
+
@current_round = 0
|
44
|
+
@results = []
|
45
|
+
@accelerated = accelerated
|
46
|
+
validate_and_sort_players
|
47
|
+
end
|
48
|
+
|
49
|
+
def generate_pairings
|
50
|
+
raise Error, "Tournament is complete" if @current_round >= @total_rounds
|
51
|
+
|
52
|
+
@current_round += 1
|
53
|
+
score_groups = players.group_by(&:score).sort_by { |score, _| -score }.to_h
|
54
|
+
pairings = []
|
55
|
+
unpaired_players = []
|
56
|
+
|
57
|
+
score_groups.each do |score, players_in_group|
|
58
|
+
# Add any unpaired players from higher score groups
|
59
|
+
group_players = unpaired_players + players_in_group
|
60
|
+
unpaired_players = []
|
61
|
+
|
62
|
+
while group_players.size >= 2
|
63
|
+
white_candidate = group_players.first
|
64
|
+
possible_opponents = group_players[1..]
|
65
|
+
.reject { |p| white_candidate.played_against?(p) }
|
66
|
+
.select { |p| valid_colors?(white_candidate, p) }
|
67
|
+
|
68
|
+
if possible_opponents.empty?
|
69
|
+
unpaired_players << group_players.shift
|
70
|
+
next
|
71
|
+
end
|
72
|
+
|
73
|
+
black_candidate = select_best_opponent(white_candidate, possible_opponents)
|
74
|
+
group_players.delete(black_candidate)
|
75
|
+
group_players.shift # remove white_candidate
|
76
|
+
|
77
|
+
# Determine colors based on previous allocations
|
78
|
+
if should_swap_colors?(white_candidate, black_candidate)
|
79
|
+
pairings << Pairing.new(white: black_candidate, black: white_candidate)
|
80
|
+
else
|
81
|
+
pairings << Pairing.new(white: white_candidate, black: black_candidate)
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
# Add remaining player to unpaired list for next score group
|
86
|
+
unpaired_players.concat(group_players) if group_players.any?
|
87
|
+
end
|
88
|
+
|
89
|
+
# Handle remaining unpaired player with a bye if necessary
|
90
|
+
if unpaired_players.any?
|
91
|
+
bye_player = select_bye_player(unpaired_players)
|
92
|
+
pairings << Pairing.new(white: bye_player)
|
93
|
+
end
|
94
|
+
|
95
|
+
if @accelerated && @current_round <= 2
|
96
|
+
accelerate_pairings(score_groups)
|
97
|
+
end
|
98
|
+
|
99
|
+
pairings
|
100
|
+
end
|
101
|
+
|
102
|
+
def apply_result(pairing, result)
|
103
|
+
case result
|
104
|
+
when "1-0"
|
105
|
+
pairing.white.score += 1
|
106
|
+
when "0-1"
|
107
|
+
pairing.black.score += 1
|
108
|
+
when "1/2-1/2"
|
109
|
+
pairing.white.score += 0.5
|
110
|
+
pairing.black.score += 0.5
|
111
|
+
when "0-0"
|
112
|
+
# Both players get 0 points (double forfeit)
|
113
|
+
when "1" # bye
|
114
|
+
pairing.white.score += 1 if pairing.is_bye
|
115
|
+
else
|
116
|
+
raise Error, "Invalid result: #{result}"
|
117
|
+
end
|
118
|
+
|
119
|
+
unless pairing.is_bye
|
120
|
+
pairing.white.opponents << pairing.black.id
|
121
|
+
pairing.black.opponents << pairing.white.id
|
122
|
+
pairing.white.colors << "W"
|
123
|
+
pairing.black.colors << "B"
|
124
|
+
else
|
125
|
+
pairing.white.colors << "="
|
126
|
+
end
|
127
|
+
|
128
|
+
@results << [pairing, result]
|
129
|
+
end
|
130
|
+
|
131
|
+
private
|
132
|
+
|
133
|
+
def validate_and_sort_players
|
134
|
+
# Sort by rating, title, and name as per FIDE rules 2.2
|
135
|
+
@players.sort_by! do |player|
|
136
|
+
[
|
137
|
+
-player.rating,
|
138
|
+
-(FIDE_TITLES.index(player.title) || FIDE_TITLES.length),
|
139
|
+
player.name
|
140
|
+
]
|
141
|
+
end
|
142
|
+
end
|
143
|
+
|
144
|
+
def accelerate_pairings(score_groups)
|
145
|
+
return unless @accelerated && @current_round <= 2
|
146
|
+
|
147
|
+
# Split players into top and bottom half for acceleration
|
148
|
+
mid_point = (@players.length / 2.0).ceil
|
149
|
+
top_half = @players[0...mid_point]
|
150
|
+
bottom_half = @players[mid_point..]
|
151
|
+
|
152
|
+
# Add virtual point to top half players in first two rounds
|
153
|
+
top_half.each { |p| p.score += 1 } if @current_round <= 2
|
154
|
+
end
|
155
|
+
|
156
|
+
def valid_colors?(player1, player2)
|
157
|
+
return true if player1.colors.empty? || player2.colors.empty?
|
158
|
+
|
159
|
+
# Enhanced color allocation rules
|
160
|
+
[player1, player2].none? do |p|
|
161
|
+
# No more than two same colors in a row
|
162
|
+
p.colors.last(2) == ["W", "W"] || p.colors.last(2) == ["B", "B"] ||
|
163
|
+
# No color difference > 2
|
164
|
+
p.color_balance.abs > 2 ||
|
165
|
+
# No same color three times more than the other
|
166
|
+
p.colors.count("W") > (p.colors.count("B") + 2) ||
|
167
|
+
p.colors.count("B") > (p.colors.count("W") + 2)
|
168
|
+
end
|
169
|
+
end
|
170
|
+
|
171
|
+
def should_swap_colors?(white, black)
|
172
|
+
return true if white.color_balance > black.color_balance + 1
|
173
|
+
return true if white.colors.last == "W" && black.colors.last == "B"
|
174
|
+
false
|
175
|
+
end
|
176
|
+
|
177
|
+
def select_best_opponent(player, candidates)
|
178
|
+
# Select opponent with closest score and rating
|
179
|
+
candidates.min_by do |candidate|
|
180
|
+
[
|
181
|
+
(candidate.score - player.score).abs,
|
182
|
+
(candidate.rating - player.rating).abs
|
183
|
+
]
|
184
|
+
end
|
185
|
+
end
|
186
|
+
|
187
|
+
def select_bye_player(candidates)
|
188
|
+
# Select player with lowest score who hasn't had a bye
|
189
|
+
candidates.select(&:can_receive_bye?)
|
190
|
+
.min_by { |p| [p.score, p.rating] } || candidates.first
|
191
|
+
end
|
192
|
+
end
|
193
|
+
|
194
|
+
class Pairing
|
195
|
+
attr_reader :white, :black, :is_bye
|
196
|
+
|
197
|
+
def initialize(white:, black: nil)
|
198
|
+
@white = white
|
199
|
+
@black = black
|
200
|
+
@is_bye = black.nil?
|
201
|
+
end
|
202
|
+
end
|
203
|
+
end
|
data/prd.md
ADDED
File without changes
|
@@ -0,0 +1,51 @@
|
|
1
|
+
module Swisspairing
|
2
|
+
VERSION: String
|
3
|
+
# See the writing guide of rbs: https://github.com/ruby/rbs#guides
|
4
|
+
|
5
|
+
class Error < StandardError
|
6
|
+
end
|
7
|
+
|
8
|
+
FIDE_TITLES: Array[String]
|
9
|
+
|
10
|
+
class Player
|
11
|
+
attr_reader name: String
|
12
|
+
attr_reader rating: Integer
|
13
|
+
attr_reader title: String?
|
14
|
+
attr_reader id: Integer?
|
15
|
+
attr_accessor score: Float
|
16
|
+
attr_accessor opponents: Array[Integer]
|
17
|
+
attr_accessor colors: Array[String]
|
18
|
+
|
19
|
+
def initialize: (name: String, rating: Integer, ?title: String?, ?id: Integer?) -> void
|
20
|
+
def color_balance: -> Integer
|
21
|
+
def can_receive_bye?: -> bool
|
22
|
+
def played_against?: (Player) -> bool
|
23
|
+
end
|
24
|
+
|
25
|
+
class Tournament
|
26
|
+
attr_reader players: Array[Player]
|
27
|
+
attr_reader current_round: Integer
|
28
|
+
attr_reader total_rounds: Integer
|
29
|
+
attr_reader results: Array[[Pairing, String]]
|
30
|
+
|
31
|
+
def initialize: (players: Array[Player], total_rounds: Integer, ?accelerated: bool) -> void
|
32
|
+
def generate_pairings: -> Array[Pairing]
|
33
|
+
def apply_result: (Pairing, String) -> void
|
34
|
+
|
35
|
+
private
|
36
|
+
def validate_and_sort_players: -> void
|
37
|
+
def accelerate_pairings: (Hash[Float, Array[Player]]) -> void
|
38
|
+
def valid_colors?: (Player, Player) -> bool
|
39
|
+
def should_swap_colors?: (Player, Player) -> bool
|
40
|
+
def select_best_opponent: (Player, Array[Player]) -> Player
|
41
|
+
def select_bye_player: (Array[Player]) -> Player
|
42
|
+
end
|
43
|
+
|
44
|
+
class Pairing
|
45
|
+
attr_reader white: Player
|
46
|
+
attr_reader black: Player?
|
47
|
+
attr_reader is_bye: bool
|
48
|
+
|
49
|
+
def initialize: (white: Player, ?black: Player?) -> void
|
50
|
+
end
|
51
|
+
end
|
metadata
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: swisspairing
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Lapo Elisacci
|
8
|
+
bindir: exe
|
9
|
+
cert_chain: []
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
11
|
+
dependencies: []
|
12
|
+
description: This gem provides functionality to create a Swiss pairing system for
|
13
|
+
tournaments, allowing for fair and efficient matchups.
|
14
|
+
email:
|
15
|
+
- lapoelisacci@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- ".rspec"
|
21
|
+
- ".rubocop.yml"
|
22
|
+
- CHANGELOG.md
|
23
|
+
- CODE_OF_CONDUCT.md
|
24
|
+
- LICENSE.txt
|
25
|
+
- README.md
|
26
|
+
- Rakefile
|
27
|
+
- lib/swisspairing.rb
|
28
|
+
- lib/swisspairing/version.rb
|
29
|
+
- prd.md
|
30
|
+
- sig/swisspairing.rbs
|
31
|
+
homepage: https://github.com/LapoElisacci/swisspairing.git
|
32
|
+
licenses:
|
33
|
+
- MIT
|
34
|
+
metadata:
|
35
|
+
homepage_uri: https://github.com/LapoElisacci/swisspairing.git
|
36
|
+
source_code_uri: https://github.com/LapoElisacci/swisspairing.git
|
37
|
+
changelog_uri: https://github.com/LapoElisacci/swisspairing/blob/main/CHANGELOG.md
|
38
|
+
rdoc_options: []
|
39
|
+
require_paths:
|
40
|
+
- lib
|
41
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 3.1.0
|
46
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
47
|
+
requirements:
|
48
|
+
- - ">="
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: '0'
|
51
|
+
requirements: []
|
52
|
+
rubygems_version: 3.6.8
|
53
|
+
specification_version: 4
|
54
|
+
summary: The gem allows to create a Swiss pairing system for tournaments.
|
55
|
+
test_files: []
|