src/player.rs
author Peter Gervai <grin@grin.hu>
Sun, 05 Feb 2023 01:42:36 +0100
changeset 8 91154fe07100
parent 5 0dd7f2c9fd81
permissions -rw-r--r--
Add some test comment, remove original draft comments.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
     1
/// This is the implementation of a Player.
4
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
     2
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
     3
use std::collections::VecDeque;
5
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
     4
// We use this to move variables out of a Vec with ownership.
4
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
     5
use std::mem;
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
     6
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
     7
use crate::deck::*;
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
     8
use crate::card::*;
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
     9
use crate::row::*;
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    10
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    11
#[derive(Debug)]
5
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
    12
/// A `Player` is a person in the game.
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
    13
/// # Structure members
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
    14
/// * `name` - the name of the Player
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
    15
/// * `hand` - the cards in the hand of Player, stored as a [`Deck`], so, for example, it could be shuffled or sorted.
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
    16
/// * `pile` - the pile of busted [`Row`] [`Card`]s, counted at the end of the round.
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
    17
/// * `game_point` - points in the current game round
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
    18
/// * `total_point` - points in all the games total
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
    19
/// * `rows_busted` - just counting how many rows the player have collected
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
    20
/// * `wins` - how many games have the Player won
4
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    21
pub(crate) struct Player {
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    22
    name: String,
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    23
    hand: Deck,
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    24
    pile: VecDeque<Card>,
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    25
    game_point: i32,
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    26
    total_point: i32,
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    27
    rows_busted: i32,
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    28
    wins: i32,
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    29
}
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    30
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    31
impl Player {
5
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
    32
    /// Creates a new [`Player`] with a given `name`.
4
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    33
    pub(crate) fn new(name: String)->Self {
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    34
        debug!("Player {} created", name);
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    35
        Player {
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    36
            name,
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    37
            hand: Deck::new_empty(),
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    38
            pile: VecDeque::new(),
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    39
            game_point: 0,
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    40
            total_point: 0,
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    41
            rows_busted: 0,
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    42
            wins: 0,
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    43
        }
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    44
    }
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    45
5
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
    46
    /// get one card from the dealer
4
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    47
    pub(crate) fn get_card( &mut self, card: Card ) {
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    48
        trace!("Player {} got a card {:?}, cards before {}", self.name, &card, self.hand.len());
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    49
        self.hand.push(card);
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    50
    }
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    51
5
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
    52
    /// throw a card from hand to the table
4
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    53
    pub(crate) fn throw_card( &mut self )->Card {
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    54
        if let Some(c) = self.hand.pop() {
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    55
            trace!("Player {} throws a card {:?}", self.name, &c);
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    56
            c
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    57
        } else {
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    58
            panic!("throw_card: Player {} has no card in hand!", self.name);
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    59
        }
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    60
    }
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    61
5
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
    62
    /// get a busted row of cards
4
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    63
    pub(crate) fn give_pile( &mut self, cards: VecDeque<Card> ) {
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    64
        for c in cards.into_iter() {
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    65
            self.game_point += c.points as i32;
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    66
            self.pile.push_back(c);
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    67
        }
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    68
        self.rows_busted += 1;
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    69
        trace!("Player {} got busted, count {}", self.name, &self.rows_busted);
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    70
    }
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    71
5
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
    72
    /// Get the name of the Player
4
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    73
    pub fn get_name(&self) -> &String {
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    74
        &self.name
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    75
    }
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    76
5
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
    77
    /// ask the player their score
4
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    78
    pub(crate) fn get_points( &self ) -> i32 {
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    79
        self.game_point
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    80
    }
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    81
5
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
    82
    /// Get the number of games won
4
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    83
    pub fn get_wins(&self) -> i32 {
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    84
        self.wins
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    85
    }
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    86
5
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
    87
    /// Player won this game, increment counter
4
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    88
    pub(crate) fn inc_wins( &mut self ) {
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    89
        self.wins += 1;
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    90
    }
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    91
5
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
    92
    /// Get total points of all games
4
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    93
    pub fn get_total_points(&self) -> i32 {
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    94
        self.total_point
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    95
    }
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    96
5
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
    97
    /// Get the number of total busted rows
4
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    98
    pub fn get_rows_busted(&self) -> i32 {
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    99
        self.rows_busted
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   100
    }
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   101
5
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
   102
    /// give back cards from the player's pile ino the Deck
4
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   103
    pub(crate) fn get_pile( &mut self ) -> VecDeque<Card> {
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   104
        trace!("Player {} gives back their pile", self.name);
5
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
   105
        // Here we take the ownership of the `pile` VecDeque<Card> structure, 
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
   106
        // and replace it with "the default" (an empty VecDeque); we can
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
   107
        // return it now without taking a mutable borrow of `self`.
4
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   108
        mem::take( &mut self.pile )
5
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
   109
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
   110
        // same effect (`drain()` also transfers ownership):
4
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   111
        // self.pile.drain(..).collect()
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   112
5
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
   113
        // very cumbersome manual fiddling (also in wrong/reverted order...)
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
   114
/*      let mut throw: Vec<Card> = Vec::new();
4
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   115
        for _i in 0 .. self.pile.len() {
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   116
            throw.push( self.pile.pop().unwrap() );
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   117
        }
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   118
        throw
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   119
 */    
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   120
    }
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   121
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   122
    // I can do this just because I *throw away* c!
5
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
   123
    // Doesn't work if I want to use/return it.
4
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   124
/*     fn _gimme_pile(self)->Self {
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   125
        for c in &self.pile {
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   126
            println!("Throw {} ", c);
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   127
        }
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   128
        self
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   129
    }
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   130
 */
5
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
   131
    /// End of round, "close" the Player and verify that we did all right.
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
   132
    /// These ought to be `assert()` calls, but... same thing.
4
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   133
    pub(crate) fn close_round( &mut self ) {
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   134
        if self.hand.len() > 0 {
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   135
            panic!("Closing round when {} has {} cards in hand", self.name, self.hand.len());
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   136
        }
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   137
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   138
        if self.pile.len() > 0 {
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   139
            panic!("Closing round when {} stil have pile with {} cards", self.name, self.pile.len());
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   140
        }
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   141
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   142
        trace!("Player {} closing round; points={} total so far {}", self.name, self.game_point, self.total_point);
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   143
        self.total_point += self.game_point;
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   144
        self.game_point = 0;
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   145
    }
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   146
5
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
   147
    /// Card too small: pick a row to collect from the rows.
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
   148
    /// It selects the first row with the lowest `points`.
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
   149
    /// 
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
   150
    /// # Arguments
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
   151
    /// * `rows` - A `Vec`tor of [`Row`]s to pick from 
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
   152
    /// * `playercard` - The actual card the player just thrown, which could be used in decision (but it isn't now)
4
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   153
    pub(crate) fn pick_row_for_small_card( &self, rows: &Vec<Row>, playercard: &Card ) -> usize {
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   154
        trace!("Player {} picking a row for small card, card {:?}, rows {:?}", self.name, playercard, rows);
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   155
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   156
        // contains the summary point for each row
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   157
        let mut row_points = Vec::with_capacity(5);
5
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
   158
        // the smallest row score (we start by something larger than anything possible)
4
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   159
        let mut smallest = 999;
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   160
        // how many rows have the same smallest score
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   161
        let mut same_point = 0;
5
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
   162
        // the first smallest row_id (start by larger than max)
4
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   163
        let mut smallest_rowid = 255;
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   164
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   165
        for rowid in 0 .. rows.len() {
5
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
   166
            // debug!("pick_row_for_small_card: rowlen {}, rowid {}", rows.len(), rowid);
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
   167
            // As Row to calculate the summary points for itself and store it in a Vec
4
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   168
            row_points.push( rows[rowid].sum() );
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   169
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   170
            if row_points[rowid] < smallest {
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   171
                // we have a new smallest row
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   172
                smallest = row_points[rowid];
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   173
                same_point = 0;
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   174
                smallest_rowid = rowid;
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   175
            
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   176
            } else if row_points[rowid] == smallest {
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   177
                // we have another row with same point as smallest
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   178
                same_point += 1;
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   179
            }
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   180
        }
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   181
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   182
        if same_point < 1 {
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   183
            // we have one smallest row
5
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
   184
            smallest_rowid.try_into().unwrap()      // it's tiny, will fit into u8, so unwrap() is safe
4
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   185
        
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   186
        } else {
5
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
   187
            // bored, we pick the first now anyway; this could be smarter, but possibly 
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
   188
            // it would require peeking at other rows and the thrown cards of others.
4
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   189
            smallest_rowid.try_into().unwrap()
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   190
        }
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   191
        
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   192
    }
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   193
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   194
}
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   195
5
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
   196
/// `PlayerCard` is a [`Card`] associated with a Player.
4
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   197
#[derive(Debug)]
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   198
pub(crate) struct PlayerCard {
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   199
    pub player_id: i32,
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   200
    pub card: Card,
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   201
}
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   202
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   203
impl PlayerCard {
5
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
   204
    /// It isn't used because I was tired fighting with the borrow checker ;-)
4
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   205
    fn _get_player(&self) -> i32 {
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   206
        self.player_id
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   207
    }
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   208
}
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   209
5
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
   210
/// We use this to sort the [`Card`]s, using its own knowledge about equality.
4
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   211
impl PartialEq for PlayerCard {
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   212
    fn eq(&self, other: &Self) -> bool {
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   213
        self.card == other.card
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   214
    }
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   215
}
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   216
5
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
   217
/// We use this to sort the [`Card`]s, using its own knowledge about ordering.
4
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   218
impl PartialOrd for PlayerCard {
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   219
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   220
        match self.card.partial_cmp(&other.card) {
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   221
            Some(core::cmp::Ordering::Equal) => {None}
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   222
            ord => return ord,
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   223
        }
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   224
    }
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   225
}
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   226
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   227