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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
//! Module for the card game, after auctions are complete.
use std::fmt;

use super::pos;
use super::cards;
use super::trick;
use super::bid;
use super::points;

/// Describes the state of a coinche game, ready to play a card.
#[derive(Clone)]
pub struct GameState {
    players: [cards::Hand; 4],

    current: pos::PlayerPos,

    contract: bid::Contract,

    points: [i32; 2],
    tricks: Vec<trick::Trick>,
}

/// Result of a game.
#[derive(PartialEq,Debug)]
pub enum GameResult {
    /// The game is still playing
    Nothing,

    /// The game is over
    GameOver {
        /// Worth of won tricks
        points: [i32; 2],
        /// Winning team
        winners: pos::Team,
        /// Score for this game
        scores: [i32; 2],
    },
}

/// Result of a trick
#[derive(PartialEq,Debug)]
pub enum TrickResult {
    Nothing,
    TrickOver(pos::PlayerPos, GameResult),
}

/// Error that can occur during play
#[derive(PartialEq,Debug)]
pub enum PlayError {
    /// A player tried to act before his turn
    TurnError,
    /// A player tried to play a card he doesn't have
    CardMissing,
    /// A player tried to play the wrong suit, while he still have some
    IncorrectSuit,
    /// A player tried to play the wrong suit, while he still have trumps
    InvalidPiss,
    /// A player did not raise on the last played trump
    NonRaisedTrump,

    /// No last trick is available for display
    NoLastTrick,
}

impl fmt::Display for PlayError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            &PlayError::TurnError => write!(f, "invalid turn order"),
            &PlayError::CardMissing => write!(f, "you can only play cards you have"),
            &PlayError::IncorrectSuit => write!(f, "wrong suit played"),
            &PlayError::InvalidPiss => write!(f, "you must use trumps"),
            &PlayError::NonRaisedTrump => write!(f, "too weak trump played"),
            &PlayError::NoLastTrick => write!(f, "no trick has been played yet"),
        }
    }
}

impl GameState {
    /// Creates a new GameState, with the given cards, first player and contract.
    pub fn new(first: pos::PlayerPos, hands: [cards::Hand; 4], contract: bid::Contract) -> Self {
        GameState {
            players: hands,
            current: first,
            contract: contract,
            tricks: vec![trick::Trick::new(first)],
            points: [0; 2],
        }
    }

    /// Returns the contract used for this game
    pub fn contract(&self) -> &bid::Contract {
        &self.contract
    }

    /// Try to play a card
    pub fn play_card(&mut self,
                     player: pos::PlayerPos,
                     card: cards::Card)
                     -> Result<TrickResult, PlayError> {
        if self.current != player {
            return Err(PlayError::TurnError);
        }

        // Is that a valid move?
        try!(can_play(player,
                      card,
                      self.players[player as usize],
                      self.current_trick(),
                      self.contract.trump));

        // Play the card
        let trump = self.contract.trump;
        let trick_over = self.current_trick_mut().play_card(player, card, trump);

        // Is the trick over?
        let result = if trick_over {
            let winner = self.current_trick().winner;
            let score = self.current_trick().score(trump);
            self.points[winner.team() as usize] += score;
            if self.tricks.len() == 8 {
                // 10 de der
                self.points[winner.team() as usize] += 10;
            } else {
                self.tricks.push(trick::Trick::new(winner));
            }
            self.current = winner;
            TrickResult::TrickOver(winner, self.get_game_result())
        } else {
            self.current = self.current.next();
            TrickResult::Nothing
        };


        Ok(result)
    }

    /// Returns the player expected to play next.
    pub fn next_player(&self) -> pos::PlayerPos {
        self.current
    }

    fn get_game_result(&self) -> GameResult {
        if !self.is_over() {
            return GameResult::Nothing;
        }

        let taking_team = self.contract.author.team();
        let taking_points = self.points[taking_team as usize];

        let capot = self.is_capot(taking_team);

        let victory = self.contract.target.victory(taking_points, capot);

        let winners = if victory {
            taking_team
        } else {
            taking_team.opponent()
        };

        // TODO: Allow for variants in scoring. (See wikipedia article)
        let mut scores = [0; 2];
        if victory {
            scores[winners as usize] = self.contract.target.score();
        } else {
            scores[winners as usize] = 160;
        }

        GameResult::GameOver {
            points: self.points,
            winners: winners,
            scores: scores,
        }
    }

    fn is_capot(&self, team: pos::Team) -> bool {
        for trick in self.tricks.iter() {
            if trick.winner.team() != team {
                return false;
            }
        }

        true
    }

    /// Returns the cards of all players
    pub fn hands(&self) -> [cards::Hand; 4] {
        self.players
    }

    fn is_over(&self) -> bool {
        self.tricks.len() == 8
    }

    /// Return the last trick, if possible
    pub fn last_trick(&self) -> Result<&trick::Trick, PlayError> {
        if self.tricks.len() == 1 {
            Err(PlayError::NoLastTrick)
        } else {
            let i = self.tricks.len() - 2;
            Ok(&self.tricks[i])
        }
    }

    /// Returns the current trick.
    pub fn current_trick(&self) -> &trick::Trick {
        let i = self.tricks.len() - 1;
        &self.tricks[i]
    }

    fn current_trick_mut(&mut self) -> &mut trick::Trick {
        let i = self.tricks.len() - 1;
        &mut self.tricks[i]
    }
}

/// Returns `true` if the move appear legal.
pub fn can_play(p: pos::PlayerPos,
                card: cards::Card,
                hand: cards::Hand,
                trick: &trick::Trick,
                trump: cards::Suit)
                -> Result<(), PlayError> {
    // First, we need the card to be able to play
    if !hand.has(card) {
        return Err(PlayError::CardMissing);;
    }

    if p == trick.first {
        return Ok(());
    }

    let card_suit = card.suit();
    let starting_suit = trick.suit().unwrap();
    if card_suit != starting_suit {
        if hand.has_any(starting_suit) {
            return Err(PlayError::IncorrectSuit);
        }

        if card_suit != trump {
            let partner_winning = p.is_partner(trick.winner);
            if !partner_winning && hand.has_any(trump) {
                return Err(PlayError::InvalidPiss);
            }
        }
    }

    // One must raise when playing trump
    if card_suit == trump {
        let highest = highest_trump(trick, trump, p);
        if points::trump_strength(card.rank()) < highest {
            if has_higher(hand, card_suit, highest) {
                return Err(PlayError::NonRaisedTrump);;
            }
        }
    }

    Ok(())
}

fn has_higher(hand: cards::Hand, trump: cards::Suit, strength: i32) -> bool {
    for ri in 0..8 {
        let rank = cards::Rank::from_n(ri);
        if points::trump_strength(rank) > strength && hand.has(cards::Card::new(trump, rank)) {
            return true;
        }
    }

    false
}

fn highest_trump(trick: &trick::Trick, trump: cards::Suit, player: pos::PlayerPos) -> i32 {
    let mut highest = -1;

    for p in trick.first.until(player) {
        if trick.cards[p as usize].unwrap().suit() == trump {
            let str = points::trump_strength(trick.cards[p as usize].unwrap().rank());
            if str > highest {
                highest = str;
            }
        }
    }

    highest
}

#[cfg(test)]
mod tests {
    use super::*;
    use super::has_higher;
    use {points, cards, bid, pos};

    #[test]
    fn test_play_card() {
        let mut hands = [cards::Hand::new(); 4];
        hands[0].add(cards::Card::new(cards::Suit::Heart, cards::Rank::Rank8));
        hands[0].add(cards::Card::new(cards::Suit::Heart, cards::Rank::RankX));
        hands[0].add(cards::Card::new(cards::Suit::Heart, cards::Rank::RankA));
        hands[0].add(cards::Card::new(cards::Suit::Heart, cards::Rank::Rank9));
        hands[0].add(cards::Card::new(cards::Suit::Club, cards::Rank::Rank7));
        hands[0].add(cards::Card::new(cards::Suit::Club, cards::Rank::Rank8));
        hands[0].add(cards::Card::new(cards::Suit::Club, cards::Rank::Rank9));
        hands[0].add(cards::Card::new(cards::Suit::Club, cards::Rank::RankJ));

        hands[1].add(cards::Card::new(cards::Suit::Club, cards::Rank::RankQ));
        hands[1].add(cards::Card::new(cards::Suit::Club, cards::Rank::RankK));
        hands[1].add(cards::Card::new(cards::Suit::Club, cards::Rank::RankX));
        hands[1].add(cards::Card::new(cards::Suit::Club, cards::Rank::RankA));
        hands[1].add(cards::Card::new(cards::Suit::Spade, cards::Rank::Rank7));
        hands[1].add(cards::Card::new(cards::Suit::Spade, cards::Rank::Rank8));
        hands[1].add(cards::Card::new(cards::Suit::Spade, cards::Rank::Rank9));
        hands[1].add(cards::Card::new(cards::Suit::Spade, cards::Rank::RankJ));

        hands[2].add(cards::Card::new(cards::Suit::Diamond, cards::Rank::Rank7));
        hands[2].add(cards::Card::new(cards::Suit::Diamond, cards::Rank::Rank8));
        hands[2].add(cards::Card::new(cards::Suit::Diamond, cards::Rank::Rank9));
        hands[2].add(cards::Card::new(cards::Suit::Diamond, cards::Rank::RankJ));
        hands[2].add(cards::Card::new(cards::Suit::Spade, cards::Rank::RankQ));
        hands[2].add(cards::Card::new(cards::Suit::Spade, cards::Rank::RankK));
        hands[2].add(cards::Card::new(cards::Suit::Heart, cards::Rank::RankQ));
        hands[2].add(cards::Card::new(cards::Suit::Heart, cards::Rank::RankK));

        hands[3].add(cards::Card::new(cards::Suit::Diamond, cards::Rank::RankQ));
        hands[3].add(cards::Card::new(cards::Suit::Diamond, cards::Rank::RankK));
        hands[3].add(cards::Card::new(cards::Suit::Diamond, cards::Rank::RankX));
        hands[3].add(cards::Card::new(cards::Suit::Diamond, cards::Rank::RankA));
        hands[3].add(cards::Card::new(cards::Suit::Spade, cards::Rank::RankX));
        hands[3].add(cards::Card::new(cards::Suit::Spade, cards::Rank::RankA));
        hands[3].add(cards::Card::new(cards::Suit::Heart, cards::Rank::Rank7));
        hands[3].add(cards::Card::new(cards::Suit::Heart, cards::Rank::RankJ));

        let contract = bid::Contract {
            trump: cards::Suit::Heart,
            author: pos::PlayerPos::P0,
            target: bid::Target::Contract80,
            coinche_level: 0,
        };

        let mut game = GameState::new(pos::PlayerPos::P0, hands, contract);

        // Wrong turn
        assert_eq!(game.play_card(pos::PlayerPos::P1,
                                  cards::Card::new(cards::Suit::Club, cards::Rank::RankX))
                       .err(),
                   Some(PlayError::TurnError));
        assert_eq!(game.play_card(pos::PlayerPos::P0,
                                  cards::Card::new(cards::Suit::Club, cards::Rank::Rank7))
                       .ok(),
                   Some(TrickResult::Nothing));
        // Card missing
        assert_eq!(game.play_card(pos::PlayerPos::P1,
                                  cards::Card::new(cards::Suit::Heart, cards::Rank::Rank7))
                       .err(),
                   Some(PlayError::CardMissing));
        // Wrong color
        assert_eq!(game.play_card(pos::PlayerPos::P1,
                                  cards::Card::new(cards::Suit::Spade, cards::Rank::Rank7))
                       .err(),
                   Some(PlayError::IncorrectSuit));
        assert_eq!(game.play_card(pos::PlayerPos::P1,
                                  cards::Card::new(cards::Suit::Club, cards::Rank::RankQ))
                       .ok(),
                   Some(TrickResult::Nothing));
        // Invalid piss
        assert_eq!(game.play_card(pos::PlayerPos::P2,
                                  cards::Card::new(cards::Suit::Diamond, cards::Rank::Rank7))
                       .err(),
                   Some(PlayError::InvalidPiss));
        assert_eq!(game.play_card(pos::PlayerPos::P2,
                                  cards::Card::new(cards::Suit::Heart, cards::Rank::RankQ))
                       .ok(),
                   Some(TrickResult::Nothing));
        // UnderTrump
        assert_eq!(game.play_card(pos::PlayerPos::P3,
                                  cards::Card::new(cards::Suit::Heart, cards::Rank::Rank7))
                       .err(),
                   Some(PlayError::NonRaisedTrump));
        assert_eq!(game.play_card(pos::PlayerPos::P3,
                                  cards::Card::new(cards::Suit::Heart, cards::Rank::RankJ))
                       .ok(),
                   Some(TrickResult::TrickOver(pos::PlayerPos::P3, game.get_game_result())));
    }

    #[test]
    fn test_has_higher_1() {
        // Simple case: X is always higher than Q.
        let mut hand = cards::Hand::new();

        hand.add(cards::Card::new(cards::Suit::Heart, cards::Rank::Rank8));
        hand.add(cards::Card::new(cards::Suit::Spade, cards::Rank::RankX));
        assert!(has_higher(hand,
                           cards::Suit::Spade,
                           points::trump_strength(cards::Rank::RankQ)));
    }

    #[test]
    fn test_has_higher_2() {
        // Test that we don't mix colors
        let mut hand = cards::Hand::new();

        hand.add(cards::Card::new(cards::Suit::Heart, cards::Rank::Rank8));
        hand.add(cards::Card::new(cards::Suit::Spade, cards::Rank::RankX));
        assert!(!has_higher(hand,
                            cards::Suit::Heart,
                            points::trump_strength(cards::Rank::RankQ)));
    }

    #[test]
    fn test_has_higher_3() {
        // In the trump order, X is lower than 9
        let mut hand = cards::Hand::new();

        hand.add(cards::Card::new(cards::Suit::Heart, cards::Rank::RankJ));
        hand.add(cards::Card::new(cards::Suit::Spade, cards::Rank::RankX));
        assert!(!has_higher(hand,
                            cards::Suit::Spade,
                            points::trump_strength(cards::Rank::Rank9)));
    }

    #[test]
    fn test_has_higher_4() {
        // In the trump order, J is higher than A
        let mut hand = cards::Hand::new();

        hand.add(cards::Card::new(cards::Suit::Heart, cards::Rank::Rank8));
        hand.add(cards::Card::new(cards::Suit::Spade, cards::Rank::RankJ));
        assert!(has_higher(hand,
                           cards::Suit::Spade,
                           points::trump_strength(cards::Rank::RankA)));
    }

    #[test]
    fn test_has_higher_5() {
        // Test when we have no trump at all
        let mut hand = cards::Hand::new();

        hand.add(cards::Card::new(cards::Suit::Heart, cards::Rank::RankJ));
        hand.add(cards::Card::new(cards::Suit::Diamond, cards::Rank::RankJ));
        hand.add(cards::Card::new(cards::Suit::Spade, cards::Rank::RankJ));
        assert!(!has_higher(hand,
                            cards::Suit::Club,
                            points::trump_strength(cards::Rank::Rank7)));
    }
}


#[cfg(feature="use_bench")]
mod benchs {
    use test::Bencher;
    use deal_seeded_hands;

    use {pos, cards, bid};
    use super::*;

    #[bench]
    fn bench_can_play(b: &mut Bencher) {

        fn try_deeper(game: &GameState, depth: usize) {
            let player = game.next_player();
            for c in game.hands()[player as usize].list() {
                let mut new_game = game.clone();
                match new_game.play_card(player, c) {
                    Ok(_) => {
                        if depth > 0 {
                            try_deeper(&new_game, depth - 1);
                        }
                    }
                    _ => (),
                };
            }
        }

        let seed = &[3, 32, 654, 1, 844];
        let hands = deal_seeded_hands(seed);
        let game = GameState::new(pos::PlayerPos::P0,
                                  hands,
                                  bid::Contract {
                                      author: pos::PlayerPos::P0,
                                      trump: cards::Suit::Heart,
                                      target: bid::Target::Contract80,
                                      coinche_level: 0,
                                  });
        b.iter(|| try_deeper(&game, 4));
    }
}