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
#![cfg_attr(feature = "use_bench", feature(test))]
//! Models a game of [coinche](https://en.wikipedia.org/wiki/Coinche) (a french card game).
//!
//! See [coinched](https://github.com/Gyscos/coinched) for an example of usage.
//!
//! Here is a simple example:
//!
//! ```rust
//! extern crate libcoinche;
//! use libcoinche::{bid,cards,pos};
//!
//! fn main() {
//!     // The first player
//!     let first = pos::PlayerPos::P0;
//!
//!     // Start the first phase with an auction
//!     let mut auction = bid::Auction::new(first);
//!
//!     // Check their cards
//!     let hands = auction.hands();
//!
//!     // Players bid or pass
//!     auction.bid(pos::PlayerPos::P0, cards::Suit::Heart, bid::Target::Contract80).unwrap();
//!     auction.pass(pos::PlayerPos::P1).unwrap();
//!     auction.pass(pos::PlayerPos::P2).unwrap();
//!     // The result is `Over` when the auction is ready to complete
//!     match auction.pass(pos::PlayerPos::P3) {
//!         Ok(bid::AuctionState::Over) => (),
//!         _ => panic!("Should not happen"),
//!     };
//!
//!     // Complete the auction to enter the second phase
//!     let mut game = auction.complete().unwrap();
//!
//!     // Play some cards
//!     game.play_card(pos::PlayerPos::P0, hands[0].get_card());
//!     // ...
//! }
//! ```
extern crate rand;
extern crate rustc_serialize;
#[cfg(feature = "use_bench")]
extern crate test;

pub mod bid;
pub mod cards;
pub mod game;
pub mod points;
pub mod pos;
pub mod trick;

// Expose the module or their content directly? Still unsure.

// pub use bid::*;
// pub use cards::*;
// pub use game::*;
// pub use points::*;
// pub use pos::*;
// pub use trick::*;

/// Quick method to get cards for 4 players.
///
/// Deals cards to 4 players randomly.
pub fn deal_hands() -> [cards::Hand; 4] {
    let mut hands = [cards::Hand::new(); 4];

    let mut d = cards::Deck::new();
    d.shuffle();

    d.deal_each(&mut hands, 3);
    d.deal_each(&mut hands, 2);
    d.deal_each(&mut hands, 3);

    hands
}

/// Deal cards for 4 players deterministically.
pub fn deal_seeded_hands(seed: &[u32]) -> [cards::Hand; 4] {
    let mut hands = [cards::Hand::new(); 4];

    let mut d = cards::Deck::new();
    d.shuffle_seeded(seed);

    d.deal_each(&mut hands, 3);
    d.deal_each(&mut hands, 2);
    d.deal_each(&mut hands, 3);

    hands
}

#[test]
fn test_deals() {
    let hands = deal_hands();

    let mut count = [0; 32];
    for hand in hands.iter() {
        assert!(hand.size() == 8);
        for card in hand.list().iter() {
            count[card.id() as usize] += 1;
        }
    }

    for c in count.iter() {
        assert!(*c == 1);
    }
}