src/main.rs
author Peter Gervai <grin@grin.hu>
Sat, 04 Feb 2023 22:51:10 +0100
changeset 6 9cdd932eceda
parent 5 0dd7f2c9fd81
child 8 91154fe07100
permissions -rw-r--r--
Added tag v1.00 for changeset 0dd7f2c9fd81
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)]
5
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
     2
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
     3
// vigyazz6! autoplayer
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
     4
//
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
     5
// This is a program automagically playing a card game called "Vigyazz6!".
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
     6
//
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
     7
// The rules are rather simple, but I'mrather lazy to reprint it in detail,
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
     8
// so it'll be brief.
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
     9
// 
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
    10
// The code is based on a deck of 104 numbered cards, all of them having
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
    11
// a face `value` and punishment `points` (the less the better).
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
    12
// There are 2 to 10 players (but above 8 the code shall be modified to
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
    13
// deal less cards), and the game has 5 rows of maximum 5 cards on the
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
    14
// table. 
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
    15
// All players throw cards at once, and they are put in the row with 
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
    16
// closest less value; if there is none, they have to pick and take
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
    17
// any row. When a row is full (5) they have to take the row and their
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
    18
// card starts a new row head.
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
    19
// When players are out of cards the least points collected wins.
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
    20
//
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
    21
// Important: I do not use `unsafe` code, this is all tightly controlled.
0
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    22
//
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    23
5
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
    24
/// We use panic in several places where the code shall not go. 
4
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents: 3
diff changeset
    25
use core::panic;
5
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
    26
/// `VecDeque` is a Vec which can be pushed/popped at both ends
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
    27
/// `time::Instant` is used to measure running time.
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
    28
/// `cmp::Reverse` used for reverse sorting.
3
3d9dba5b16e0 v1.00 - single file
Peter Gervai <grin@grin.hu>
parents: 2
diff changeset
    29
use std::{collections::VecDeque, time::Instant, cmp::Reverse};
4
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents: 3
diff changeset
    30
5
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
    31
/// We use this `pretty_env_logger` as a logging framework.
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
    32
extern crate pretty_env_logger;
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
    33
#[macro_use] extern crate log;
4
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents: 3
diff changeset
    34
5
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
    35
// I import structures from my module files to use here.
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
    36
use crate::{deck::Deck, player::{Player, PlayerCard}, table::Table};
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
    37
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
    38
// Here we read the files and "get to know" what's in them. This isn't including, only scanning.
4
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents: 3
diff changeset
    39
mod deck;
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents: 3
diff changeset
    40
mod table;
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents: 3
diff changeset
    41
mod player;
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents: 3
diff changeset
    42
mod card;
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents: 3
diff changeset
    43
mod row;
0
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    44
5
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
    45
// We play this many game rounds. You want to keep this under 3 for any debug level above "warnings".
4
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents: 3
diff changeset
    46
const GAME_ROUNDS: i32 = 100_000;
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
    47
0
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    48
fn main() {
5
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
    49
    // Env variable makes the logger to log more or less:
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
    50
    // RUST_LOG=debug cargo run
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
    51
    pretty_env_logger::init();
5
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
    52
    info!("Program starting."); // info level logging
1
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
    53
    game();
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
    54
    info!("End of run.");
0
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    55
}
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    56
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    57
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
    58
/*** Game ****/
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
    59
5
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
    60
/// `GameStat` structure collects global statistics about the run.
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
    61
struct GameStat {
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
    62
    game_count: i64,
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
    63
    shuffle_count: i64,
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
    64
    start_time: Instant, // use start_time.elapsed().as_nanos() or .as_secs()
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
    65
}
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
    66
5
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
    67
/// Play all the game rounds.
0
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    68
fn game() {
5
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
    69
    // Whole game statistics stored here
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
    70
    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
    71
5
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
    72
    // Create the game deck with all the card (ordered). Open box. Remove shrink wrap. :-)
1
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
    73
    let mut deck = Deck::new();
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
    74
5
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
    75
    // Create the players (up to 8 w/o changing the hand cards)
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
    76
    let player_names = vec![ "peter", "moni", "icbalint", "orsi", "topi", "kgb", "zsu", "csilla" ];
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
    77
    // Iterate on player names, create Player struct from them and collect them into a Vec'tor.
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
    78
    // (On the borrow level we don't consume the player_names vector [using `iter()` instead of `into_iter()`].)
1
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
    79
    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
    80
5
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
    81
    // We use this often, so take it now.
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
    82
    let player_count = players.len(); // = <number of players> - 1
1
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
    83
5
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
    84
    // Start playing - GAME_ROUNDS rounds.
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
    85
    for cnt_game in 1..=GAME_ROUNDS {
5
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
    86
        debug!("Game round {} starts", cnt_game); // debug level logging
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
    87
        // Shuffle the deck (using various methods)
1
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
    88
        deck.shuffle();
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
    89
        stats.shuffle_count += 1;
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
    90
        stats.game_count += 1;
1
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
    91
5
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
    92
        // dealing 10 cards for each player
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
    93
        debug!("Dealing.");
3
3d9dba5b16e0 v1.00 - single file
Peter Gervai <grin@grin.hu>
parents: 2
diff changeset
    94
        for _i in 1..=10 {
1
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
    95
            for player in 0 .. player_count {
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
    96
                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
    97
            }
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
    98
        }
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
    99
5
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
   100
        // we need 5 cards from deck to build the rows
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   101
        debug!("Building the rows.");
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   102
        let mut cards = VecDeque::new();
5
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
   103
        // This is another way to write "for _ in (1..=5) { ... }"
3
3d9dba5b16e0 v1.00 - single file
Peter Gervai <grin@grin.hu>
parents: 2
diff changeset
   104
        (1..=5).for_each(|_| {
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   105
            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
   106
        });
5
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
   107
        trace!("We pushed 5 cards to rows: {:?}\n", cards);
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
   108
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
   109
        // Create the Table (contains the Deck, the Rows (with 5 starting `cards`) and occasional cards thrown by Players)
1
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   110
        let mut table = Table::new(cards);
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   111
5
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
   112
/*      debug!("Table: {:?}\n", table);
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
   113
        debug!("Players: {:?}\n", players);
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
   114
        debug!("Deck: {:?}\n", deck);
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   115
 */        
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   116
        debug!("We have the table ready: {:?}", table);
1
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   117
5
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
   118
        // playing one game, players taking turns
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   119
        debug!("Players start taking turns");
5
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
   120
        // Since players have 10 cards dealt we have exactly 10 turns.
1
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   121
        for turn in 1..=10 {
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   122
            debug!("Turn {}!", turn);
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   123
            trace!("The table: {:?}", table);
0
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   124
5
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
   125
            // Everyone puts a card "face down" (Rust promised not to peek).
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
   126
            // If I used the `players.for_each()` form I would hit the borrow checker:
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
   127
            // `players` would be repeatedly borrowed mutable and program wouldn't compile.
1
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   128
            for player in 0 .. player_count {
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   129
                // get a card from the player
5
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
   130
                let player_id: i32 = player.try_into().unwrap();    // convert `usize` array index into `i32` [that was an unnecessary hassle: in fact I should rewrite this using usize everywhere]
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
   131
                let topcard = players[player].throw_card();         // If Player [module] was smart this would be a chosen card, but it would need player
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
   132
                                                                    // not just brains but possibility to peek at the rows. This wasn't in the plan, so 
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
   133
                                                                    // we just take the top card in hand.
1
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   134
                // put it on the table ("turned face down")
5
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
   135
                table.lay_player_card( topcard, player_id );        // We actually move the card variable with its ownership everywhere, so no &refs.
1
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   136
            }
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   137
5
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
   138
            // Process cards on the Table.
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   139
            debug!("The Table process the throws.");
5
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
   140
            // Sort the cards by `value` the players have thrown.
1
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   141
            table.sort_cards();
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   142
5
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
   143
            // Pick them one by one, from smallest to larger ones
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   144
            while table.has_player_cards() {
5
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
   145
                let smallest: PlayerCard = table.get_smallest_player_card(); // I just wrote the PlayerCard type to show; Rust is smart enough to know.
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   146
                trace!("Take smallest card {:?}", &smallest);
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   147
5
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
   148
                // Find the row with the closest smallest card, or None if smaller than any row head.
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
   149
                let closest_row = table.get_closest_row(&smallest); // That is an Option<usize>.
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   150
                trace!("Choose closest row: {:?}", closest_row);
0
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   151
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   152
                match closest_row {
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   153
                    Some(rowid) => {
5
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
   154
                        // We have to put it at the end of `rowid`.
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   155
                        debug!("Putting down card into row {}", rowid);
5
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
   156
                        let player_id: usize = smallest.player_id.try_into().unwrap(); // By "just" indexing `players` (by `player_id`) I have avoided borrowing it and get into trouble later.
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
   157
                        // Try to put the card to the end of the row, or collect full row first
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
   158
                        let overflow = table.put_card_into_row(smallest, rowid); // And this is Option<VecDeque<Card>>.
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   159
                        if let Some(cards) = overflow {
5
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
   160
                            // Row is full, we got pile ("bust")
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
   161
                            debug!("Row is busted, {} collects", players[player_id].get_name());    
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
   162
                            // Player gets pile, thrown card was put into row head
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   163
                            players[ player_id ].give_pile( cards );
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   164
                        }
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   165
                    },
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   166
                    None => {
5
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
   167
                        // Card too small, need to pick any row to take!
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
   168
                        let player_id: usize = smallest.player_id.try_into().unwrap(); // We _directly_ access `PlayerCard.player_id` structure member, because borrow checker wasn't helping my mental hygiene.
4
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents: 3
diff changeset
   169
                        debug!("Too small from {}, picking row", players[player_id].get_name());
5
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
   170
                        // Pick any row to take
4
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents: 3
diff changeset
   171
                        let rowid = players[ player_id ].pick_row_for_small_card(table.peek_rows(), &smallest.card);
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   172
                        trace!("Picked row {}", rowid);
5
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
   173
                        // take the row cards ("bust")
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   174
                        let cards = table.take_row(rowid);
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   175
                        trace!("Took cards: {:?}", cards);
5
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
   176
                        // and give them to the player who owns the card
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   177
                        players[ player_id ].give_pile( cards );
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   178
                        // put new card in the row
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   179
                        let overflow = table.put_card_into_row(smallest, rowid);
3
3d9dba5b16e0 v1.00 - single file
Peter Gervai <grin@grin.hu>
parents: 2
diff changeset
   180
                        if let Some(_) = overflow {
5
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
   181
                            // If this fires then I have fucked something up. :-)
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   182
                            panic!("Player took whole row and it's already full");
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   183
                        }
1
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   184
                    }
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   185
                }
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   186
            }
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   187
        }
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   188
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   189
        // end of round
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   190
        info!("Round finished, len is {} ??sec", stats.start_time.elapsed().as_micros());
1
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   191
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   192
        debug!("End of round, counting and collecting back piles");
5
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
   193
        // We collect the winner(s) and their score here
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   194
        let mut winners: Vec<usize> = Vec::new();
5
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
   195
        let mut winscore: i32 = i32::MAX-1; // Getting facy here about "larger than any valid value".
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   196
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   197
        for i in 0..player_count {
4
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents: 3
diff changeset
   198
            info!("Player {} has {} points", players[i].get_name(), players[i].get_points());
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   199
4
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents: 3
diff changeset
   200
            if players[i].get_points() < winscore  {
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents: 3
diff changeset
   201
                trace!("New winner {} with score {}", players[i].get_name(), players[i].get_points());
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   202
                winners.clear();
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   203
                winners.push(i);
4
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents: 3
diff changeset
   204
                winscore = players[i].get_points();
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   205
            
4
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents: 3
diff changeset
   206
            } else if players[i].get_points() == winscore {
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents: 3
diff changeset
   207
                trace!("New co-winner {} with score {}", players[i].get_name(), players[i].get_points());
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   208
                winners.push(i);
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   209
            }
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   210
            trace!("The list of winners is {:?}", winners);
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   211
5
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
   212
            // get pile from Player
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   213
            let cards = players[i].get_pile();
5
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
   214
            // and give it back to the Deck
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   215
            deck.push_cards(cards);
5
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
   216
            // close Player round and update stats
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   217
            players[i].close_round();
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   218
        }
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   219
5
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
   220
        // collect remaining Cards from Table
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   221
        deck.push_cards( table.collect_rows() );
4
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents: 3
diff changeset
   222
        trace!("Shall have full deck now, len {}", deck.len());
1
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   223
5
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
   224
        // This is a Vec of `player_id`s, i is a &ref
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   225
        for i in winners.iter() {
5
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
   226
            players[*i].inc_wins(); // Increment win count of &Vec<usize> player_id
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   227
        }
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   228
5
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
   229
        // Collect the names of the winners.
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
   230
        let mut finals = Vec::new();
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   231
        for i in winners {
4
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents: 3
diff changeset
   232
            finals.push( players[i].get_name() );
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   233
        }
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   234
        info!("The winner(s): {:?}", &finals);
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   235
    }
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   236
5
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
   237
    // Do the type conversions _very_ visibly. `as_micros()` results `u128`
3
3d9dba5b16e0 v1.00 - single file
Peter Gervai <grin@grin.hu>
parents: 2
diff changeset
   238
    let elapsed_micro: f64 = stats.start_time.elapsed().as_micros() as f64;
5
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
   239
    // and `GAME_ROUNDS` is `i32`. We'll use `elapsed_micro/game_rounds` later.
3
3d9dba5b16e0 v1.00 - single file
Peter Gervai <grin@grin.hu>
parents: 2
diff changeset
   240
    let game_rounds: f64 = GAME_ROUNDS.into();
5
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
   241
    // ???which is same as the slightly uglier:
3
3d9dba5b16e0 v1.00 - single file
Peter Gervai <grin@grin.hu>
parents: 2
diff changeset
   242
    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
   243
3d9dba5b16e0 v1.00 - single file
Peter Gervai <grin@grin.hu>
parents: 2
diff changeset
   244
    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
   245
        stats.start_time.elapsed().as_micros(), 
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   246
        stats.start_time.elapsed().as_secs(),
3
3d9dba5b16e0 v1.00 - single file
Peter Gervai <grin@grin.hu>
parents: 2
diff changeset
   247
        elapsed_micro / game_rounds, 
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   248
        stats.game_count,
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   249
        stats.shuffle_count,
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   250
    );
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   251
3
3d9dba5b16e0 v1.00 - single file
Peter Gervai <grin@grin.hu>
parents: 2
diff changeset
   252
    // 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
   253
    // players.sort_by( |a, b| b.wins.partial_cmp(&a.wins).unwrap() );                  // DESC wins
5
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
   254
    players.sort_by_cached_key( |x| Reverse(x.get_wins()) );                            // DESC wins (caching is just for the show); using std::cmp::Reverse
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   255
5
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
   256
    // Print out totals
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   257
    for i in 0..players.len() {
5
0dd7f2c9fd81 Comments. Lot of Comments.
Peter Gervai <grin@grin.hu>
parents: 4
diff changeset
   258
        let p = &players[i]; // the rest looks simpler by using this. (Seems to confuse rust-analyzer in codium though.)
4
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents: 3
diff changeset
   259
        println!("Player {} has wins {}, score {} (busted {} times)", 
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents: 3
diff changeset
   260
            p.get_name(), 
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents: 3
diff changeset
   261
            p.get_wins(), 
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents: 3
diff changeset
   262
            p.get_total_points(), 
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents: 3
diff changeset
   263
            p.get_rows_busted());
0
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   264
    }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   265
}
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   266
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   267
#[cfg(test)]
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   268
mod tests {
1
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   269
    use std::collections::VecDeque;
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   270
    use rand::Rng;
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   271
4
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents: 3
diff changeset
   272
    use crate::{card::Card, player::{Player, PlayerCard}, row::Row, table::Table};
0
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   273
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   274
    #[test]
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   275
    fn card_values() {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   276
        let card_values = vec![1,2,5,10,33,55,77];
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   277
        let card_points = vec![1,1,2,3, 5, 7, 5];
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   278
        for i in 1 .. card_values.len() {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   279
            let c = Card::new( card_values[i] );
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   280
            let p = c.points;
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   281
            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
   282
        }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   283
    }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   284
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   285
    #[test]
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   286
    fn player_take_pile() {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   287
        // create a player
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   288
        let mut p = Player::new("bob".to_string());
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   289
        // create a pile
1
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   290
        let mut pile = VecDeque::new();
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   291
        let mut refpile = VecDeque::new();
0
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   292
        for i in 5..10 {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   293
            let c = Card::new(i);
1
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   294
            pile.push_back(c);
0
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   295
            let c = Card::new(i);
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   296
            refpile.push_back(c);
0
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   297
        }
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   298
        // give the pile to player
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   299
        p.give_pile(pile);
4
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents: 3
diff changeset
   300
        assert!( p.get_rows_busted() == 1 );
0
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   301
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   302
        // get back the pile from player
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   303
        // p = p.gimme_pile();
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   304
        let pile = p.get_pile();
0
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   305
        
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   306
        // the pile we got shall be same as the pile we gave
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   307
        // 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
   308
        assert_eq!( pile, refpile );
0
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   309
        assert!( pile.iter().all( |item| refpile.contains(item)) );
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   310
1
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   311
        let mut pile = VecDeque::new();
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   312
        for i in 4..=9 {
0
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   313
            let c = Card::new(i);
1
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   314
            pile.push_back(c);
0
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   315
        }
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   316
        p.give_pile(pile);
4
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents: 3
diff changeset
   317
        assert!( p.get_rows_busted() == 2 );
0
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   318
    }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   319
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   320
    #[test]
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   321
    fn row_push() {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   322
        let mut row = Row::new();
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   323
        let mut refcard = VecDeque::new();   // reference vec to check
0
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   324
        for i in 1..=7 {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   325
            let cval = i+5;
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   326
            let card = Card::new(cval);
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   327
            // push a card into the row
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   328
            if let Some(cards) = row.push_or_collect(card) {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   329
                // got the overflow
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   330
                println!("Got overflow row at {}!", i);
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   331
                assert!( i == 6, "Overflow at wrong position: {} != 6", i );
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   332
                // we need to get the proper vec
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   333
                assert!( cards.iter().all( |item| refcard.contains(item) ), "Got cards {:?}", cards ); 
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   334
            } else {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   335
                println!("push success {}", i);
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   336
            }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   337
            // remember the correct vec for checking
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   338
            let card = Card::new(cval);
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   339
            refcard.push_back(card);
0
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   340
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   341
            // check card value
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   342
            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
   343
        }
4
a2f0cb2b5c13 Split main into module files.
Peter Gervai <grin@grin.hu>
parents: 3
diff changeset
   344
        assert!( row.len() == 2, "Row contains wrong amount of cards: {}", row.len() );
0
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   345
    }
1
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   346
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   347
    #[test]
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   348
    fn sort_cards() {
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   349
        let mut cards: VecDeque<Card> = VecDeque::new();
1
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   350
        let mut rng = rand::thread_rng();
3
3d9dba5b16e0 v1.00 - single file
Peter Gervai <grin@grin.hu>
parents: 2
diff changeset
   351
        for _ in 1..50 {
1
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   352
            let n = rng.gen_range(1..104);
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   353
            cards.push_back( Card::new(n) );
1
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   354
        }
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   355
        cards.make_contiguous().sort();
1
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   356
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   357
        for i in 1..cards.len() {
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   358
            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
   359
        }
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   360
    }
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   361
2
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   362
    #[test]
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   363
    fn check_closest_row() {
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   364
        let table = generate_table();
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   365
        let pcard = PlayerCard{ player_id: 42, card: Card::new(42) };
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   366
        let closest = table.get_closest_row(&pcard);
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   367
        assert_eq!( closest, Some(3) ); // index from 0
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   368
    }
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   369
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   370
    #[test]
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   371
    fn check_smallest_player_card() {
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   372
        let mut table = generate_table();
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   373
        
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   374
        let mut player_id = 1;
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   375
        vec![103, 8, 71, 93, 6].into_iter().for_each(|c| {
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   376
            table.lay_player_card( Card::new(c), player_id);
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   377
            player_id += 1;
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   378
        });
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   379
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   380
        let smallest = table.get_smallest_player_card();
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   381
        assert_eq!( smallest, PlayerCard{ player_id: 5, card: Card::new(6) } );
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   382
    }
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   383
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   384
    fn generate_table() -> Table  {
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   385
        let mut row_cards = VecDeque::new();
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   386
        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
   387
        let table = Table::new(row_cards);
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   388
        table
950660fddec0 Final version :-)
Peter Gervai <grin@grin.hu>
parents: 1
diff changeset
   389
    }
0
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   390
}
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   391
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   392
/*
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   393
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   394
- 1-104 lap, 
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   395
    - *5 - 2
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   396
    - *0 - 3
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   397
    - NN - 5
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   398
    - 55 - 7
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   399
- deck
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   400
- jatekosok; kezben tartott lapok; elvitt lapok; pontszamok; counter: elvitt sorok, okrok, total pont
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   401
-- keveres (keveresek szama)
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   402
-- osztas: mindenki 10 lap
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   403
-- start sorok: 5 kartya
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   404
- jatek (jatekok szama)
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   405
-- mindenki a felso lapot kiteszi
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   406
-- szamsorrendben felkerulnek, aki viszi, viszi
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   407
--- ha kisebb, akkor dontes
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   408
---- ha van legkevesebb, viszi
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   409
---- ha tobb min van, random valaszt
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   410
- osszesites
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   411
-- okrok, elvitt sorok
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   412
-- keveresek szama, jatekok szama, eltelt ido
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   413
- deck osszegyujtes
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   414
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   415
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   416
*/