1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
// Copyright (C) 2017 Steve Sprang
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

//! Card, Set, and SuperSet implementation.
//!
//! A `Card` has four features, each of which has three possible
//! values. As such, it can be represented by a four-element vector
//! where each element is a ternary digit (trit): 0, 1, or 2. The
//! implementation here packs this vector into the four bytes of a
//! `u32`.
//!

#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct Card(u32);

impl Card {
    pub fn new(mut index: usize) -> Card {
        // Convert the index to ternary and pack each of the resulting
        // four trits into the bytes of a u32. The least significant
        // trit ends up in the leftmost byte.
        let mut value = index % 3;

        for _ in 0..3 {
            value <<= 8;
            index /= 3;
            value |= index % 3;
        }

        Card(value as u32)
    }

    /// Maps the card value back to the index from which it was derived.
    pub fn index(&self) -> usize {
        let mut value = self.0;
        let mut result = value & 0xff;

        for _ in 0..3 {
            value >>= 8;
            result *= 3;
            result += value & 0xff;
        }

        result as usize
    }
}

////////////////////////////////////////////////////////////////////////////////
// Card: Feature Extraction
////////////////////////////////////////////////////////////////////////////////

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Feature { Count, Shape, Color, Shading }

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Shape { Oval, Squiggle, Diamond }

/// Unlike the other features, Color doesn't have a fixed interpretation.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Color { A, B, C }

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Shading { Solid, Striped, Outlined }

impl Card {
    /// Extracts the byte corresponding to the given `Feature`. Since
    /// the bytes represent ternary digits, the returned value will
    /// always be in the interval [0,2].
    fn feature(&self, feature: Feature) -> u8 {
        (self.0 >> (feature as u32 * 8) & 0xff) as u8
    }

    /// Returns a shape count in the interval [1,3]
    pub fn count(&self) -> u8 {
        self.feature(Feature::Count) + 1
    }

    pub fn shape(&self) -> Shape {
        match self.feature(Feature::Shape) {
            0 => Shape::Oval,
            1 => Shape::Squiggle,
            _ => Shape::Diamond,
        }
    }

    pub fn color(&self) -> Color {
        match self.feature(Feature::Color) {
            0 => Color::A,
            1 => Color::B,
            _ => Color::C,
        }
    }

    pub fn shading(&self) -> Shading {
        match self.feature(Feature::Shading) {
            0 => Shading::Solid,
            1 => Shading::Striped,
            _ => Shading::Outlined,
        }
    }
}

////////////////////////////////////////////////////////////////////////////////
// Card: Debug
////////////////////////////////////////////////////////////////////////////////

use std::fmt;

impl fmt::Debug for Card {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "Card({})", self.index())
    }
}

////////////////////////////////////////////////////////////////////////////////
// Set
////////////////////////////////////////////////////////////////////////////////

type Triple = (Card, Card, Card);

/// Validated Set
pub struct Set { cards: Triple }

impl Set {
    pub fn cards(&self) -> Triple {
        self.cards
    }
}

pub trait ToSet {
    fn to_set(self) -> Option<Set>;
}

impl ToSet for Triple {
    /// Three cards form a `Set` if their sum is (0,0,0,0) modulo 3.
    fn to_set(self) -> Option<Set> {
        let (a, b, c) = self;
        let mut sum = a.0 + b.0 + c.0;
        let mut result = true;

        while sum != 0 {
            // see if each byte is divisible by 3
            result &= (sum & 0xff) % 3 == 0;
            sum >>= 8;
        }

        if result {
            let set = Set { cards: self };
            Some(set)
        } else {
            None
        }
    }
}

////////////////////////////////////////////////////////////////////////////////
// SuperSet
////////////////////////////////////////////////////////////////////////////////

type Pair = (Card, Card);

/// Validated SuperSet
pub struct SuperSet { left: Pair, right: Pair }

impl SuperSet {
    pub fn left(&self) -> Pair {
        self.left
    }

    pub fn right(&self) -> Pair {
        self.right
    }
}

pub trait ToSuperSet {
    fn to_superset(self) -> Option<SuperSet>;
}

impl ToSuperSet for (Card, Card, Card, Card) {
    fn to_superset(self) -> Option<SuperSet> {
        let (a, b, c, d) = self;
        let pair_combos = &[((a, b), (c, d)),
                            ((a, c), (b, d)),
                            ((a, d), (b, c))];

        for &(left, right) in pair_combos {
            // Two pairs of cards form a SuperSet if and only if the
            // Set-completing card for each pair is the same
            if left.complete_set() == right.complete_set() {
                let superset = SuperSet { left: left, right: right };
                return Some(superset);
            }
        }

        None
    }
}

////////////////////////////////////////////////////////////////////////////////
// CompleteSet
////////////////////////////////////////////////////////////////////////////////

pub trait CompleteSet {
    fn complete_set(self) -> Card;
}

impl CompleteSet for Pair {
    /// Given a pair of cards, returns the third card needed to
    /// complete the `Set`.
    ///
    /// This method works by taking the trit-wise sum of the cards in
    /// the pair. Each unique sum maps to a matching trit which is the
    /// appropriate component of the missing card:
    ///
    ///   A | B | A+B | Match
    ///  ---+---+-----+-------
    ///   0 | 0 |   0 |     0
    ///   0 | 1 |   1 |     2
    ///   0 | 2 |   2 |     1
    ///   1 | 1 |   2 |     1
    ///   1 | 2 |   3 |     0
    ///   2 | 2 |   4 |     2
    ///
    fn complete_set(self) -> Card {
        const MATCHES: [u32; 5] = [0, 2, 1, 0, 2];

        let (a, b) = self;
        let mut sum = (a.0 + b.0) as usize;

        let mut value = MATCHES[sum & 0xff];
        for _ in 0..3 {
            value <<= 8;
            sum >>= 8;
            value |= MATCHES[sum & 0xff];
        }

        let reversed = value.swap_bytes();
        Card(reversed)
    }
}

impl<'a> CompleteSet for (&'a Card, &'a Card) {
    fn complete_set(self) -> Card {
        let (&a, &b) = self;
        (a, b).complete_set()
    }
}

////////////////////////////////////////////////////////////////////////////////
// Tests
////////////////////////////////////////////////////////////////////////////////

#[cfg(test)]
mod tests {
    use super::*;
    use deck::cards;
    use pair_iter::PairIter;

    #[test]
    fn check_card_conversions() {
        for i in 0..81 {
            let card = Card::new(i);
            assert_eq!(i, card.index());
        }
    }

    #[test]
    fn check_set_completion() {
        let cards = cards();
        let mut set_count = 0;

        for (&a, &b) in cards.pairs() {
            let c = (a, b).complete_set();
            let triple = (a, b, c);
            assert!(triple.to_set().is_some());
            set_count += 1;
        }
        // each set is encountered thrice
        assert_eq!(set_count, 1080 * 3)
    }
}