src/main.rs
author Peter Gervai <grin@grin.hu>
Tue, 31 Jan 2023 22:16:26 +0100
changeset 3 3d9dba5b16e0
parent 2 950660fddec0
child 4 a2f0cb2b5c13
permissions -rw-r--r--
v1.00 - single file
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
     1
// #[allow(dead_code, unused)]
0
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
     2
// vigyazz6! autplayer
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
     3
//
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
     4
1
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
     5
use core::{fmt, panic};
3
3d9dba5b16e0 v1.00 - single file
Peter Gervai <grin@grin.hu>
parents: 2
diff changeset
     6
use std::{collections::VecDeque, time::Instant, cmp::Reverse};
0
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
     7
use rand::Rng;
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
     8
use std::mem;
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
     9
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
    10
extern crate pretty_env_logger;
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
    11
#[macro_use] extern crate log;
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
    12
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
    13
const GAME_ROUNDS: i32 = 1_000_000;
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
    14
0
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    15
fn main() {
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
    16
    // RUST_LOG=debug cargo run
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
    17
    pretty_env_logger::init();
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
    18
    info!("Program starting.");
1
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
    19
    game();
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
    20
    info!("End of run.");
0
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    21
}
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    22
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
    23
/*** Card ****/
0
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    24
#[derive(Debug)]
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    25
struct Card {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    26
    value: i8,
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    27
    points: i8,
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    28
}
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    29
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    30
impl Card {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    31
    fn new(value: i8)->Self {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    32
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    33
        let mut points = 0;
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    34
        if value % 10 == 5 {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    35
            // ends with 5 = 2 point
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    36
            points = 2;
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    37
            // println!("*5 add 1, val={}, pt={}", value, points);
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    38
        }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    39
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    40
        if value % 10 == 0 {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    41
            // ends with 0 = 3 point
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    42
            points = 3;
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    43
            // println!("*0 add 2, val={}, pt={}", value, points);
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    44
        }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    45
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    46
        if value % 10 == value / 10 {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    47
            // same numbers = 5 points (55=7)
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    48
            points += 5;
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    49
            // println!("NN add 5, val={}, pt={}", value, points);
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    50
        }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    51
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    52
        if points == 0 {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    53
            points = 1;
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    54
        }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    55
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    56
        Card {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    57
            value,
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    58
            points,
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    59
        }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    60
    }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    61
}
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    62
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    63
impl fmt::Display for Card {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    64
    fn fmt( &self, f: &mut fmt::Formatter ) -> fmt::Result {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    65
        write!(f, "(Card {}, points {})", self.value, self.points)
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    66
    }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    67
}
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    68
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    69
impl PartialEq for Card {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    70
    fn eq(&self, other: &Self) -> bool {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    71
        self.value == other.value
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    72
    }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    73
}
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    74
1
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
    75
impl Eq for Card {} 
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
    76
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
    77
impl PartialOrd for Card {
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
    78
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
    79
        match self.value.partial_cmp(&other.value) {
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
    80
            Some(core::cmp::Ordering::Equal) => {None}
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
    81
            ord => return ord,
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
    82
        }
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
    83
    }
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
    84
}
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
    85
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
    86
impl Ord for Card {
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
    87
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
    88
        self.value.cmp(&other.value)
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
    89
    }
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
    90
}
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
    91
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
    92
/*** Deck ****/
1
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
    93
#[derive(Debug)]
0
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    94
struct Deck {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    95
    content: VecDeque<Card>,
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    96
}
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    97
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    98
impl Deck {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    99
    fn new_empty() -> Self {
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   100
        debug!("Empty deck generated");
0
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   101
        Deck {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   102
            content: VecDeque::new(),
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   103
        }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   104
    }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   105
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   106
    fn new() -> Self {
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   107
        debug!("Full deck generated");
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   108
        let content = (1..=104).into_iter().map( |n| Card::new(n) ).collect();
0
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   109
        Deck {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   110
            content,
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   111
        }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   112
    }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   113
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   114
    fn shuffle( &mut self ) {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   115
        let mut rng = rand::thread_rng();
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   116
        
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   117
        trace!("Deck before shuffle: len {}, {:?}", self.content.len(), self);
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   118
        debug!("Deck shuffled");
0
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   119
        // shufflers:
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   120
        // * naive: swap cards n times
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   121
        // * kgb: half the deck, take 1..4 cards sequentially from each
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   122
        // * grin: take 1..6 from front and put at bottom
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   123
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   124
        // naive shuffle: exchange random cards
1
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   125
        for _i in 1..=500 {
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   126
            let c1 = rng.gen_range(0 .. self.content.len());
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   127
            let c2 = rng.gen_range(0 .. self.content.len());
0
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   128
            if c1 != c2 {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   129
                self.content.swap(c1, c2);
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   130
            }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   131
        }
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   132
        trace!("Deck after shuffle: len {}, {:?}", self.content.len(), self);
0
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   133
    }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   134
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   135
    // get top card from deck
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   136
    fn pop( &mut self ) -> Option<Card> {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   137
        self.content.pop_front() 
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   138
    }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   139
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   140
    // put a card into the bottom of the deck
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   141
    fn push( &mut self, c: Card ) {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   142
        self.content.push_back(c);
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   143
    }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   144
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   145
    fn push_cards( &mut self, cards: VecDeque<Card> ) {
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   146
        trace!("Collecting back, deck len is {}, cards {}", self.content.len(), cards.len());
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   147
        cards.into_iter().for_each( |card| self.push(card) );
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   148
        trace!("Deck len is {}", self.content.len());
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   149
    }
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   150
0
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   151
    fn len( &self ) -> usize {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   152
        self.content.len()
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   153
    }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   154
3
3d9dba5b16e0 v1.00 - single file
Peter Gervai <grin@grin.hu>
parents: 2
diff changeset
   155
    fn _get_nth( &mut self, n: usize ) -> Card {
0
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   156
        if let Some(c) = self.content.remove(n) {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   157
            c
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   158
        } else {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   159
            panic!("get_nth: index {} out of bounds ({})!", n, self.content.len());
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   160
        }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   161
    }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   162
}
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   163
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   164
/*** Player ****/
1
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   165
#[derive(Debug)]
0
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   166
struct Player {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   167
    name: String,
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   168
    hand: Deck,
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   169
    pile: VecDeque<Card>,
0
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   170
    game_point: i32,
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   171
    total_point: i32,
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   172
    rows_busted: i32,
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   173
    wins: i32,
0
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   174
}
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   175
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   176
impl Player {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   177
    fn new(name: String)->Self {
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   178
        debug!("Player {} created", name);
0
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   179
        Player {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   180
            name,
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   181
            hand: Deck::new_empty(),
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   182
            pile: VecDeque::new(),
0
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   183
            game_point: 0,
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   184
            total_point: 0,
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   185
            rows_busted: 0,
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   186
            wins: 0,
0
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   187
        }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   188
    }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   189
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   190
    // get one card from th dealer
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   191
    fn get_card( &mut self, card: Card ) {
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   192
        trace!("Player {} got a card {:?}, cards before {}", self.name, &card, self.hand.len());
0
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   193
        self.hand.push(card);
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   194
    }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   195
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   196
    // throw a card from hand to the table
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   197
    fn throw_card( &mut self )->Card {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   198
        if let Some(c) = self.hand.pop() {
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   199
            trace!("Player {} throws a card {:?}", self.name, &c);
0
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   200
            c
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   201
        } else {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   202
            panic!("throw_card: Player {} has no card in hand!", self.name);
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   203
        }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   204
    }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   205
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   206
    // get a busted row of cards
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   207
    fn give_pile( &mut self, cards: VecDeque<Card> ) {
0
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   208
        for c in cards.into_iter() {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   209
            self.game_point += c.points as i32;
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   210
            self.pile.push_back(c);
0
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   211
        }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   212
        self.rows_busted += 1;
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   213
        trace!("Player {} got busted, count {}", self.name, &self.rows_busted);
0
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   214
    }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   215
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   216
    // ask the player their score
3
3d9dba5b16e0 v1.00 - single file
Peter Gervai <grin@grin.hu>
parents: 2
diff changeset
   217
    fn _tell_points( self ) -> i32 {
0
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   218
        self.game_point
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   219
    }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   220
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   221
    fn inc_wins( &mut self ) {
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   222
        self.wins += 1;
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   223
    }
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   224
0
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   225
    // give back cards from the pile
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   226
    fn get_pile( &mut self ) -> VecDeque<Card> {
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   227
        trace!("Player {} gives back their pile", self.name);
0
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   228
        mem::take( &mut self.pile )
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   229
        // same effect:
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   230
        // self.pile.drain(..).collect()
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   231
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   232
        // very cumbersome manual fiddling (also reverted...)
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   233
/*         let mut throw: Vec<Card> = Vec::new();
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   234
        for _i in 0 .. self.pile.len() {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   235
            throw.push( self.pile.pop().unwrap() );
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   236
        }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   237
        throw
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   238
 */    
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   239
    }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   240
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   241
    // I can do this just because I *throw away* c!
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   242
    // doesn't work if I want to use it.
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   243
/*     fn _gimme_pile(self)->Self {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   244
        for c in &self.pile {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   245
            println!("Throw {} ", c);
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   246
        }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   247
        self
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   248
    }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   249
 */
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   250
    fn close_round( &mut self ) {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   251
        if self.hand.len() > 0 {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   252
            panic!("Closing round when {} has {} cards in hand", self.name, self.hand.len());
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   253
        }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   254
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   255
        if self.pile.len() > 0 {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   256
            panic!("Closing round when {} stil have pile with {} cards", self.name, self.pile.len());
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   257
        }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   258
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   259
        trace!("Player {} closing round; points={} total so far {}", self.name, self.game_point, self.total_point);
0
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   260
        self.total_point += self.game_point;
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   261
        self.game_point = 0;
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   262
    }
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   263
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   264
    // card too small: pick a row to collect from the rows
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   265
    fn pick_row_for_small_card( &self, rows: &Vec<Row>, playercard: &Card ) -> usize {
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   266
        trace!("Player {} picking a row for small card, card {:?}, rows {:?}", self.name, playercard, rows);
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   267
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   268
        // contains the summary point for each row
3
3d9dba5b16e0 v1.00 - single file
Peter Gervai <grin@grin.hu>
parents: 2
diff changeset
   269
        let mut row_points = Vec::with_capacity(5);
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   270
        // the smallest row score
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   271
        let mut smallest = 999;
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   272
        // how many rows have the same smallest score
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   273
        let mut same_point = 0;
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   274
        // the first smallest row_id
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   275
        let mut smallest_rowid = 255;
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   276
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   277
        for rowid in 0 .. rows.len() {
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   278
            // DEBUG
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   279
            // println!("pick_row_for_small_card: rowlen {}, rowid {}", rows.len(), rowid);
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   280
            row_points.push( rows[rowid].sum() );
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   281
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   282
            if row_points[rowid] < smallest {
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   283
                // we have a new smallest row
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   284
                smallest = row_points[rowid];
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   285
                same_point = 0;
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   286
                smallest_rowid = rowid;
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   287
            
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   288
            } else if row_points[rowid] == smallest {
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   289
                // we have another row with same point as smallest
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   290
                same_point += 1;
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   291
            }
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   292
        }
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   293
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   294
        if same_point < 1 {
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   295
            // we have one smallest row
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   296
            smallest_rowid.try_into().unwrap()      // it's tiny, will fit into u8
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   297
        
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   298
        } else {
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   299
            // bored, we pick the first now anyway
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   300
            smallest_rowid.try_into().unwrap()
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   301
        }
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   302
        
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   303
    }
0
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   304
}
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   305
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   306
/*** Row ****/
0
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   307
// a row of cards on the table (max 5)
1
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   308
#[derive(Debug)]
0
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   309
struct Row {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   310
    cards: VecDeque<Card>,
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   311
}
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   312
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   313
impl Row {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   314
    const MAX_LEN: usize = 5;
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   315
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   316
    fn new() -> Self {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   317
        Row {
3
3d9dba5b16e0 v1.00 - single file
Peter Gervai <grin@grin.hu>
parents: 2
diff changeset
   318
            cards: VecDeque::with_capacity(5),
0
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   319
        }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   320
    }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   321
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   322
    fn push_or_collect( &mut self, card: Card ) -> Option<VecDeque<Card>> {
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   323
        trace!("Called push_or_collect on row {:?}", &self);
0
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   324
        if self.cards.len() < Self::MAX_LEN {
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   325
            trace!("Less than {} cards, putting at the end", Self::MAX_LEN);
0
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   326
            self.cards.push_back(card);
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   327
            None
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   328
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   329
        } else {
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   330
            trace!("Row is full, len {}, maxlen {}", self.cards.len(), Self::MAX_LEN);
0
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   331
            // row overflow
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   332
            let row_cards = mem::take( &mut self.cards );
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   333
            self.cards.push_back(card);
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   334
            if self.cards.len() != 1 {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   335
                panic!("New row must have one card, not {}", self.cards.len());
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   336
            }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   337
            Some(row_cards)
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   338
        }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   339
    }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   340
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   341
    fn take_row( &mut self ) -> VecDeque<Card> {
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   342
        // take cards and empty the row
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   343
        mem::take( &mut self.cards )
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   344
    }
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   345
0
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   346
    fn last_card_value(&self) -> i8 {
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   347
        // println!("last_card_value: cards {:?}, len {}", self.cards, self.cards.len());
0
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   348
        self.cards.get( self.cards.len()-1 ).unwrap().value
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   349
    }
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   350
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   351
    // sum of row card points
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   352
    fn sum(&self) -> i32 {
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   353
        let mut sum: i32 = 0;
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   354
        self.cards.iter().for_each(|card| {
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   355
            sum += card.points as i32;
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   356
        });
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   357
        sum
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   358
    }
0
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   359
}
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   360
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   361
/*** PlayerCard ****/
1
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   362
#[derive(Debug)]
0
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   363
struct PlayerCard {
1
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   364
    player_id: i32,
0
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   365
    card: Card,
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   366
}
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   367
1
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   368
impl PlayerCard {
3
3d9dba5b16e0 v1.00 - single file
Peter Gervai <grin@grin.hu>
parents: 2
diff changeset
   369
    fn _get_player(&self) -> i32 {
1
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   370
        self.player_id
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   371
    }
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   372
}
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   373
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   374
impl PartialEq for PlayerCard {
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   375
    fn eq(&self, other: &Self) -> bool {
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   376
        self.card == other.card
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   377
    }
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   378
}
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   379
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   380
impl PartialOrd for PlayerCard {
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   381
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   382
        match self.card.partial_cmp(&other.card) {
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   383
            Some(core::cmp::Ordering::Equal) => {None}
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   384
            ord => return ord,
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   385
        }
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   386
    }
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   387
}
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   388
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   389
/*** Table ****/
1
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   390
#[derive(Debug)]
0
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   391
struct Table {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   392
    rows: Vec<Row>,
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   393
    player_cards: VecDeque<PlayerCard>, // owned by a player
0
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   394
}
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   395
1
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   396
impl Table {
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   397
    fn new(row_cards: VecDeque<Card>) -> Self {
3
3d9dba5b16e0 v1.00 - single file
Peter Gervai <grin@grin.hu>
parents: 2
diff changeset
   398
        let mut rows = Vec::with_capacity(5);
1
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   399
        for card in row_cards {
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   400
            // create a new row then put a card into it
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   401
            let mut row = Row::new();
3
3d9dba5b16e0 v1.00 - single file
Peter Gervai <grin@grin.hu>
parents: 2
diff changeset
   402
            if let Some(_c) = row.push_or_collect(card) {
1
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   403
                panic!("Freshly created row overflowed");
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   404
            }
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   405
            rows.push( row );
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   406
        }
0
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   407
1
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   408
        Table {
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   409
            rows,
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   410
            player_cards: VecDeque::new(),
1
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   411
        }
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   412
    }
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   413
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   414
    fn lay_player_card( &mut self, card: Card, player_id: i32 ) {
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   415
        self.player_cards.push_back( PlayerCard { player_id, card } );
1
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   416
    }
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   417
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   418
    fn sort_cards( &mut self ) {
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   419
        self.player_cards.make_contiguous().sort_by( |a,b| b.card.cmp(&a.card) );
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   420
    }
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   421
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   422
    fn has_player_cards( &self ) -> bool {
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   423
        self.player_cards.len() > 0
1
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   424
    }
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   425
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   426
    fn get_smallest_player_card( &mut self ) -> PlayerCard {
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   427
        // FIXME: check!
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   428
        self.player_cards.pop_back().expect("out of player cards on table")
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   429
    }
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   430
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   431
    fn get_closest_row( &self, pcard: &PlayerCard ) -> Option<usize> {
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   432
        // get the row id with last card closest smaller to players'
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   433
        let row_heads = self.get_row_heads();
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   434
        let mut closest_val = None;
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   435
        let mut diff = 127;
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   436
        for i in 0..row_heads.len() {
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   437
            if row_heads[i] < pcard.card.value && pcard.card.value - row_heads[i] < diff {
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   438
                closest_val = Some(i);
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   439
                diff = pcard.card.value - row_heads[i];
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   440
                // println!("DEBUG: pcard {}, row {}, head {}, diff {}, closest {:?}", pcard.card.value, i, row_heads[i], diff, closest_val);
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   441
            }
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   442
        }
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   443
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   444
        closest_val
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   445
    }
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   446
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   447
    fn put_card_into_row( &mut self, pcard: PlayerCard, row_id: usize ) -> Option<VecDeque<Card>> {
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   448
        self.rows[row_id as usize].push_or_collect(pcard.card)
1
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   449
    }
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   450
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   451
    fn get_row_heads( &self ) -> Vec<i8> {
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   452
        let mut heads: Vec<i8> = Vec::new();
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   453
        for i in 0..self.rows.len() {
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   454
            heads.push( self.rows[i].last_card_value() );
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   455
        }
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   456
        heads
1
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   457
    }
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   458
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   459
    // take a whole row and hand it over
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   460
    fn take_row( &mut self, row_id: usize ) -> VecDeque<Card> {
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   461
        self.rows[row_id].take_row()
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   462
    }
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   463
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   464
    // collect remaining cards in the rows at the end of round
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   465
    fn collect_rows( &mut self ) -> VecDeque<Card> {
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   466
        let mut cards = VecDeque::new();
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   467
        for row in 0..self.rows.len() {
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   468
            self.rows[row]
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   469
                .take_row()
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   470
                .into_iter()
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   471
                .for_each(|card| cards.push_back(card));
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   472
        }
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   473
        cards
1
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   474
    }
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   475
}
0
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   476
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   477
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   478
/*** Game ****/
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   479
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   480
struct GameStat {
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   481
    game_count: i64,
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   482
    shuffle_count: i64,
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   483
    start_time: Instant, // use start_time.elapsed().as_nanos() or .as_secs()
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   484
}
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   485
0
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   486
fn game() {
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   487
    let mut stats = GameStat { game_count:0, shuffle_count: 0, start_time: Instant::now() };
1
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   488
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   489
    let mut deck = Deck::new();
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   490
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   491
    let player_names = vec![ "grin", "moni", "icbalint", "orsi", "topi", "kgb", "zsu", "csilla" ];
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   492
    let mut players: Vec<Player> = player_names.iter().map( |n| Player::new(n.to_string()) ).collect();
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   493
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   494
    let player_count = players.len(); // pc - 1
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   495
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   496
    for cnt_game in 1..=GAME_ROUNDS {
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   497
        debug!("Game round {} starts", cnt_game);
1
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   498
        deck.shuffle();
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   499
        stats.shuffle_count += 1;
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   500
        stats.game_count += 1;
1
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   501
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   502
        // dealing
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   503
        debug!("Dealing.");
3
3d9dba5b16e0 v1.00 - single file
Peter Gervai <grin@grin.hu>
parents: 2
diff changeset
   504
        for _i in 1..=10 {
1
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   505
            for player in 0 .. player_count {
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   506
                players[player].get_card( deck.pop().expect("Deck is empty while dealing to players") );
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   507
            }
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   508
        }
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   509
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   510
        // we need 5 crds from deck
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   511
        debug!("Building the rows.");
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   512
        let mut cards = VecDeque::new();
3
3d9dba5b16e0 v1.00 - single file
Peter Gervai <grin@grin.hu>
parents: 2
diff changeset
   513
        (1..=5).for_each(|_| {
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   514
            cards.push_back( deck.pop().expect("deck empty before starting the game") );
3
3d9dba5b16e0 v1.00 - single file
Peter Gervai <grin@grin.hu>
parents: 2
diff changeset
   515
        });
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   516
        // println!("We push 5 cards to rows: {:?}\n", cards);
1
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   517
        let mut table = Table::new(cards);
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   518
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   519
        // DEBUG
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   520
/*         println!("Table: {:?}\n", table);
1
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   521
        println!("PLayers: {:?}\n", players);
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   522
        println!("Deck: {:?}\n", deck);
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   523
 */        
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   524
        debug!("We have the table ready: {:?}", table);
1
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   525
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   526
        // playing
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   527
        debug!("Players start taking turns");
1
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   528
        for turn in 1..=10 {
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   529
            debug!("Turn {}!", turn);
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   530
            trace!("The table: {:?}", table);
0
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   531
1
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   532
            // everyone puts a card face down
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   533
            for player in 0 .. player_count {
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   534
                let player_id: i32 = player.try_into().unwrap();
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   535
                // get a card from the player
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   536
                let topcard = players[player].throw_card();
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   537
                // put it on the table ("turned face down")
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   538
                table.lay_player_card( topcard, player_id );
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   539
            }
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   540
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   541
            // process cards
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   542
            debug!("The Table process the throws.");
1
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   543
            table.sort_cards();
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   544
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   545
            while table.has_player_cards() {
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   546
                let smallest = table.get_smallest_player_card();
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   547
                trace!("Take smallest card {:?}", &smallest);
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   548
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   549
                let closest_row = table.get_closest_row(&smallest);
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   550
                trace!("Choose closest row: {:?}", closest_row);
0
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   551
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   552
                match closest_row {
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   553
                    Some(rowid) => {
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   554
                        debug!("Putting down card into row {}", rowid);
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   555
                        let player_id: usize = smallest.player_id.try_into().unwrap();
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   556
                        let overflow = table.put_card_into_row(smallest, rowid);
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   557
                        if let Some(cards) = overflow {
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   558
                            // row is full, got pile
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   559
                            debug!("Row is busted, {} collects", players[player_id].name);
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   560
                            // player gets pile, card gets into row head
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   561
                            players[ player_id ].give_pile( cards );
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   562
                        }
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   563
                    },
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   564
                    None => {
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   565
                        // card too small, need to pick row!
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   566
                        let player_id: usize = smallest.player_id.try_into().unwrap();
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   567
                        debug!("Too small from {}, picking row", players[player_id].name);
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   568
                        // pick any row to take
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   569
                        let rowid = players[ player_id ].pick_row_for_small_card(&table.rows, &smallest.card);
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   570
                        trace!("Picked row {}", rowid);
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   571
                        // take the row cards
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   572
                        let cards = table.take_row(rowid);
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   573
                        trace!("Took cards: {:?}", cards);
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   574
                        players[ player_id ].give_pile( cards );
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   575
                        // put new card in the row
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   576
                        let overflow = table.put_card_into_row(smallest, rowid);
3
3d9dba5b16e0 v1.00 - single file
Peter Gervai <grin@grin.hu>
parents: 2
diff changeset
   577
                        if let Some(_) = overflow {
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   578
                            panic!("Player took whole row and it's already full");
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   579
                        }
1
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   580
                    }
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   581
                }
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   582
            }
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   583
        }
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   584
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   585
        // end of round
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   586
        info!("Round finished, len is {} ??sec", stats.start_time.elapsed().as_micros());
1
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   587
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   588
        debug!("End of round, counting and collecting back piles");
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   589
        let mut winners: Vec<usize> = Vec::new();
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   590
        let mut winscore: i32 = i32::MAX-1;
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   591
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   592
        for i in 0..player_count {
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   593
            info!("Player {} has {} points", players[i].name, players[i].game_point);
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   594
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   595
            if players[i].game_point < winscore  {
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   596
                trace!("New winner {} with score {}", players[i].name, players[i].game_point);
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   597
                winners.clear();
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   598
                winners.push(i);
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   599
                winscore = players[i].game_point;
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   600
            
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   601
            } else if players[i].game_point == winscore {
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   602
                trace!("New co-winner {} with score {}", players[i].name, players[i].game_point);
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   603
                winners.push(i);
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   604
            }
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   605
            trace!("The list of winners is {:?}", winners);
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   606
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   607
            // get pile from player
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   608
            let cards = players[i].get_pile();
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   609
            // and give it back to the deck
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   610
            deck.push_cards(cards);
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   611
            // close player round and update stats
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   612
            players[i].close_round();
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   613
        }
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   614
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   615
        // collect cards from table
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   616
        deck.push_cards( table.collect_rows() );
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   617
        trace!("Shall have full deck now, len {}", deck.content.len());
1
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   618
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   619
        let mut finals = Vec::new();
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   620
        for i in winners.iter() {
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   621
            players[*i].inc_wins();
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   622
        }
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   623
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   624
        for i in winners {
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   625
            finals.push( &players[i].name );
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   626
        }
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   627
        info!("The winner(s): {:?}", &finals);
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   628
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   629
/*         players.iter().for_each(|player| {
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   630
            println!("Player {} has {} points", player.name, player.game_point);
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   631
            let cards = player.get_pile();
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   632
            deck.push_cards(cards);
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   633
            player.close_round();
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   634
        });
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   635
 */
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   636
    }
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   637
3
3d9dba5b16e0 v1.00 - single file
Peter Gervai <grin@grin.hu>
parents: 2
diff changeset
   638
    let elapsed_micro: f64 = stats.start_time.elapsed().as_micros() as f64;
3d9dba5b16e0 v1.00 - single file
Peter Gervai <grin@grin.hu>
parents: 2
diff changeset
   639
    let game_rounds: f64 = GAME_ROUNDS.into();
3d9dba5b16e0 v1.00 - single file
Peter Gervai <grin@grin.hu>
parents: 2
diff changeset
   640
3d9dba5b16e0 v1.00 - single file
Peter Gervai <grin@grin.hu>
parents: 2
diff changeset
   641
    let _res: f64 = stats.start_time.elapsed().as_micros() as f64 / <i32 as Into<f64>>::into(GAME_ROUNDS);
3d9dba5b16e0 v1.00 - single file
Peter Gervai <grin@grin.hu>
parents: 2
diff changeset
   642
3d9dba5b16e0 v1.00 - single file
Peter Gervai <grin@grin.hu>
parents: 2
diff changeset
   643
    println!("Totals (game time {} ??s, or {} s; {} ??s/game), {} games played ({} shuffles):", 
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   644
        stats.start_time.elapsed().as_micros(), 
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   645
        stats.start_time.elapsed().as_secs(),
3
3d9dba5b16e0 v1.00 - single file
Peter Gervai <grin@grin.hu>
parents: 2
diff changeset
   646
        elapsed_micro / game_rounds, 
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   647
        stats.game_count,
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   648
        stats.shuffle_count,
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   649
    );
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   650
3
3d9dba5b16e0 v1.00 - single file
Peter Gervai <grin@grin.hu>
parents: 2
diff changeset
   651
    // players.sort_by( |a, b| a.total_point.partial_cmp(&b.total_point).unwrap() );    // ASC points
3d9dba5b16e0 v1.00 - single file
Peter Gervai <grin@grin.hu>
parents: 2
diff changeset
   652
    // players.sort_by( |a, b| b.wins.partial_cmp(&a.wins).unwrap() );                  // DESC wins
3d9dba5b16e0 v1.00 - single file
Peter Gervai <grin@grin.hu>
parents: 2
diff changeset
   653
    players.sort_by_cached_key( |x| Reverse(x.wins) );                                  // DESC wins (caching is just for the show)
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   654
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   655
    for i in 0..players.len() {
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   656
        let p = &players[i];
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   657
        println!("Player {} has wins {}, score {} (busted {} times)", p.name, p.wins, p.total_point, p.rows_busted);
0
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   658
    }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   659
}
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   660
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   661
#[cfg(test)]
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   662
mod tests {
3
3d9dba5b16e0 v1.00 - single file
Peter Gervai <grin@grin.hu>
parents: 2
diff changeset
   663
    // use core::panic;
1
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   664
    use std::collections::VecDeque;
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   665
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   666
    use rand::Rng;
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   667
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   668
    use crate::{Card, Player, Row, Table, PlayerCard};
0
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   669
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   670
    #[test]
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   671
    fn card_values() {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   672
        let card_values = vec![1,2,5,10,33,55,77];
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   673
        let card_points = vec![1,1,2,3, 5, 7, 5];
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   674
        for i in 1 .. card_values.len() {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   675
            let c = Card::new( card_values[i] );
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   676
            let p = c.points;
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   677
            assert!(p == card_points[i], "card={} card points={} i={} expected point={}", card_values[i], p, i, card_points[i]);
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   678
        }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   679
    }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   680
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   681
    #[test]
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   682
    fn player_take_pile() {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   683
        // create a player
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   684
        let mut p = Player::new("bob".to_string());
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   685
        // create a pile
1
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   686
        let mut pile = VecDeque::new();
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   687
        let mut refpile = VecDeque::new();
0
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   688
        for i in 5..10 {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   689
            let c = Card::new(i);
1
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   690
            pile.push_back(c);
0
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   691
            let c = Card::new(i);
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   692
            refpile.push_back(c);
0
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   693
        }
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   694
        // give the pile to player
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   695
        p.give_pile(pile);
0
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   696
        assert!( p.rows_busted == 1 );
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   697
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   698
        // get back the pile from player
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   699
        // p = p.gimme_pile();
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   700
        let pile = p.get_pile();
0
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   701
        
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   702
        // the pile we got shall be same as the pile we gave
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   703
        // this check is O(n^2), doesn't matter for less than 100 items
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   704
        assert_eq!( pile, refpile );
0
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   705
        assert!( pile.iter().all( |item| refpile.contains(item)) );
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   706
1
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   707
        let mut pile = VecDeque::new();
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   708
        for i in 4..=9 {
0
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   709
            let c = Card::new(i);
1
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   710
            pile.push_back(c);
0
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   711
        }
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   712
        p.give_pile(pile);
0
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   713
        assert!( p.rows_busted == 2 );
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   714
    }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   715
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   716
    #[test]
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   717
    fn row_push() {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   718
        let mut row = Row::new();
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   719
        let mut refcard = VecDeque::new();   // reference vec to check
0
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   720
        for i in 1..=7 {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   721
            let cval = i+5;
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   722
            let card = Card::new(cval);
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   723
            // push a card into the row
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   724
            if let Some(cards) = row.push_or_collect(card) {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   725
                // got the overflow
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   726
                println!("Got overflow row at {}!", i);
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   727
                assert!( i == 6, "Overflow at wrong position: {} != 6", i );
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   728
                // we need to get the proper vec
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   729
                assert!( cards.iter().all( |item| refcard.contains(item) ), "Got cards {:?}", cards ); 
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   730
            } else {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   731
                println!("push success {}", i);
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   732
            }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   733
            // remember the correct vec for checking
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   734
            let card = Card::new(cval);
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   735
            refcard.push_back(card);
0
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   736
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   737
            // check card value
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   738
            assert!( row.last_card_value() == cval, "Last card value mismatch: got {} vs expected {}", row.last_card_value(), cval );
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   739
        }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   740
        assert!( row.cards.len() == 2, "Row contains wrong amount of cards: {}", row.cards.len() );
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   741
    }
1
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   742
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   743
    #[test]
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   744
    fn sort_cards() {
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   745
        let mut cards: VecDeque<Card> = VecDeque::new();
1
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   746
        let mut rng = rand::thread_rng();
3
3d9dba5b16e0 v1.00 - single file
Peter Gervai <grin@grin.hu>
parents: 2
diff changeset
   747
        for _ in 1..50 {
1
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   748
            let n = rng.gen_range(1..104);
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   749
            cards.push_back( Card::new(n) );
1
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   750
        }
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   751
        cards.make_contiguous().sort();
1
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   752
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   753
        for i in 1..cards.len() {
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   754
            assert!( cards[i-1].value <= cards[i].value, "Bad ordering: {} > {}", cards[i-1].value,cards[i].value );
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   755
        }
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   756
    }
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   757
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   758
    #[test]
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   759
    fn check_closest_row() {
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   760
        let table = generate_table();
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   761
        let pcard = PlayerCard{ player_id: 42, card: Card::new(42) };
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   762
        let closest = table.get_closest_row(&pcard);
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   763
        assert_eq!( closest, Some(3) ); // index from 0
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   764
    }
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   765
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   766
    #[test]
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   767
    fn check_smallest_player_card() {
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   768
        let mut table = generate_table();
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   769
        
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   770
        let mut player_id = 1;
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   771
        vec![103, 8, 71, 93, 6].into_iter().for_each(|c| {
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   772
            table.lay_player_card( Card::new(c), player_id);
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   773
            player_id += 1;
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   774
        });
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   775
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   776
        let smallest = table.get_smallest_player_card();
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   777
        assert_eq!( smallest, PlayerCard{ player_id: 5, card: Card::new(6) } );
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   778
    }
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   779
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   780
    fn generate_table() -> Table  {
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   781
        let mut row_cards = VecDeque::new();
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   782
        vec![5,7,10,33,70].into_iter().for_each(|c| row_cards.push_back( Card::new(c) ));
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   783
        let table = Table::new(row_cards);
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   784
        table
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   785
    }
0
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   786
}
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   787
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   788
/*
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   789
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   790
- 1-104 lap, 
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   791
    - *5 - 2
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   792
    - *0 - 3
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   793
    - NN - 5
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   794
    - 55 - 7
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   795
- deck
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   796
- jatekosok; kezben tartott lapok; elvitt lapok; pontszamok; counter: elvitt sorok, okrok, total pont
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   797
-- keveres (keveresek szama)
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   798
-- osztas: mindenki 10 lap
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   799
-- start sorok: 5 kartya
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   800
- jatek (jatekok szama)
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   801
-- mindenki a felso lapot kiteszi
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   802
-- szamsorrendben felkerulnek, aki viszi, viszi
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   803
--- ha kisebb, akkor dontes
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   804
---- ha van legkevesebb, viszi
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   805
---- ha tobb min van, random valaszt
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   806
- osszesites
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   807
-- okrok, elvitt sorok
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   808
-- keveresek szama, jatekok szama, eltelt ido
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   809
- deck osszegyujtes
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   810
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   811
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   812
*/