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
extern crate uuid;

use uuid::Uuid;
use error::*;
use deck::*;

mod deck;
mod scoring;
mod error;

#[cfg(test)]
mod tests;

pub enum GameTransition {
    Bet(i32),
    Card(Card),
    Start,
}

#[derive(Debug)]
pub struct Player{
    pub id: Uuid,
    pub hand: Vec<Card>
}

impl Player {
    pub fn new(id: Uuid) -> Player {
        Player {
            id: id,
            hand: vec![]
        }
    }
}

#[derive(Debug)]
pub struct Game {
    id: Uuid,
    pub scoring: scoring::ScoringState,
    pub current_player: usize,
    pub rotation_status: usize,
    pub deck: Vec<deck::Card>,
    pub hands_played: Vec<[deck::Card; 4]>,
    pub bets_placed: Vec<[i32; 4]>,
    player_a: Player,
    player_b: Player,
    player_c: Player,
    player_d: Player,
    game_started: bool
}

impl Game {
    pub fn new(id: Uuid, player_ids: [Uuid; 4], max_points: i32) -> Game {
        Game {
            id: id,
            scoring: scoring::ScoringState::new(max_points),
            hands_played: vec![new_pot()],
            bets_placed: vec![[0;4]],
            deck: deck::new(),
            current_player: 0,
            rotation_status: 0,
            player_a: Player::new(player_ids[0]),
            player_b: Player::new(player_ids[1]),
            player_c: Player::new(player_ids[2]),
            player_d: Player::new(player_ids[3]),
            game_started: false
        }
    }

    pub fn get_hand(&self, player: usize) -> &Vec<Card> {
        match player {
            0 => & self.player_a.hand,
            1 => & self.player_b.hand,
            2 => & self.player_c.hand,
            3 => & self.player_d.hand,
            _ => & self.player_d.hand,
        }
    }

    fn deal_cards(&mut self) {
        deck::shuffle(&mut self.deck);
        let mut hands = deck::deal_four_players(&mut self.deck);

        self.player_a.hand = hands.pop().unwrap();
        self.player_b.hand = hands.pop().unwrap();
        self.player_c.hand = hands.pop().unwrap();
        self.player_d.hand = hands.pop().unwrap();
    }

    pub fn play(&mut self, entry: GameTransition) -> Result<Success, TransitionError> {
        match entry {
            GameTransition::Bet(bet) => {
                if !self.game_started {
                    return Err(TransitionError::NotStarted);
                }
                if self.scoring.is_over {
                    return Err(TransitionError::CompletedGame);
                }
                if !self.scoring.in_betting_stage {
                    return Err(TransitionError::BetInTrickStage);
                }
 
                self.bets_placed.last_mut().unwrap()[self.current_player] = bet;
                
                self.current_player = (self.current_player + 1) % 4;
                self.rotation_status = (self.rotation_status + 1) % 4;
                if self.rotation_status == 0 {
                    self.scoring.bet(*self.bets_placed.last().unwrap());
                    self.bets_placed.push([0;4]);
                    return Ok(Success::BetComplete);
                }

                return Ok(Success::Bet);
            },
            GameTransition::Card(card) => {
                if !self.game_started {
                    return Err(TransitionError::NotStarted);
                }
                if self.scoring.is_over {
                    return Err(TransitionError::CompletedGame);
                }
                if self.scoring.in_betting_stage {
                    return Err(TransitionError::CardInBettingStage);
                }


                let play_card_result = self.play_card(&card);

                if let Ok(Success::PlayCard) = play_card_result {
                    self.hands_played.last_mut().unwrap()[self.current_player] = card;
                    self.current_player = (self.current_player + 1) % 4;
                    self.rotation_status = (self.rotation_status + 1) % 4;
                    
                    if self.rotation_status == 0 {
                        let winner = self.scoring.trick(self.current_player, self.hands_played.last().unwrap());
                        if self.scoring.is_over { 
                            return Ok(Success::GameOver);
                        }
                        if self.scoring.in_betting_stage {
                            self.deal_cards();
                        } else {
                            self.current_player = winner;
                            self.hands_played.push(new_pot());
                        }
                        return Ok(Success::Trick);
                    }
                    return Ok(Success::PlayCard);
                };
                return play_card_result;
            },
            GameTransition::Start => {
                if self.game_started {
                    return Err(TransitionError::AlreadyStarted);
                }
                self.deal_cards();
                self.game_started = true;
                return Ok(Success::Start);
            }
        }
    }

    fn play_card(&mut self, card: &Card) -> Result<Success, TransitionError> {
        let player_hand = &mut match self.current_player {
            0 => &mut self.player_a,
            1 => &mut self.player_b,
            2 => &mut self.player_c,
            3 => &mut self.player_d,
            _ => &mut self.player_d,
        }.hand;

        if !player_hand.contains(card) {
            return Err(TransitionError::CardNotInHand);
        }

        let card_index = player_hand.iter().position(|x| x == card).unwrap();
        self.deck.push(player_hand.remove(card_index));

        return Ok(Success::PlayCard);
    }
}