src/main.rs
author Peter Gervai <grin@grin.hu>
Sun, 29 Jan 2023 21:41:10 +0100
changeset 0 a95b84125269
child 1 5f81068a8a88
permissions -rw-r--r--
Add repo.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
     1
// vigyazz6! autplayer
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
     2
//
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
     3
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
     4
use core::fmt;
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
     5
use std::collections::VecDeque;
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
     6
use rand::Rng;
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
     7
use std::mem;
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
     8
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
     9
fn main() {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    10
    println!("Hello, world!");
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    11
}
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    12
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    13
#[derive(Debug)]
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    14
struct Card {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    15
    value: i8,
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    16
    points: i8,
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    17
}
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    18
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    19
impl Card {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    20
    fn new(value: i8)->Self {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    21
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    22
        let mut points = 0;
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    23
        if value % 10 == 5 {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    24
            // ends with 5 = 2 point
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    25
            points = 2;
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    26
            // println!("*5 add 1, val={}, pt={}", value, points);
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    27
        }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    28
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    29
        if value % 10 == 0 {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    30
            // ends with 0 = 3 point
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    31
            points = 3;
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    32
            // println!("*0 add 2, val={}, pt={}", value, points);
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    33
        }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    34
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    35
        if value % 10 == value / 10 {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    36
            // same numbers = 5 points (55=7)
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    37
            points += 5;
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    38
            // println!("NN add 5, val={}, pt={}", value, points);
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    39
        }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    40
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    41
        if points == 0 {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    42
            points = 1;
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    43
        }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    44
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    45
        Card {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    46
            value,
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    47
            points,
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    48
        }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    49
    }
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
impl fmt::Display for Card {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    53
    fn fmt( &self, f: &mut fmt::Formatter ) -> fmt::Result {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    54
        write!(f, "(Card {}, points {})", self.value, self.points)
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
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    58
impl PartialEq for Card {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    59
    fn eq(&self, other: &Self) -> bool {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    60
        self.value == other.value
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
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    64
struct Deck {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    65
    content: VecDeque<Card>,
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
impl Deck {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    69
    fn new_empty() -> Self {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    70
        Deck {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    71
            content: VecDeque::new(),
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
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    75
    fn new() -> Self {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    76
        let content = (1..104).into_iter().map( |n| Card::new(n) ).collect();
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    77
        Deck {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    78
            content,
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    79
        }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    80
    }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    81
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    82
    fn shuffle( &mut self ) {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    83
        let mut rng = rand::thread_rng();
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    84
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    85
        // shufflers:
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    86
        // * naive: swap cards n times
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    87
        // * kgb: half the deck, take 1..4 cards sequentially from each
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    88
        // * grin: take 1..6 from front and put at bottom
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    89
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    90
        // naive shuffle: exchange random cards
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    91
        for _i in 1..500 {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    92
            let c1 = rng.gen_range(1 .. self.content.len());
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    93
            let c2 = rng.gen_range(1 .. self.content.len());
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    94
            if c1 != c2 {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    95
                self.content.swap(c1, c2);
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
    }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    99
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   100
    // get top card from deck
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   101
    fn pop( &mut self ) -> Option<Card> {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   102
        self.content.pop_front() 
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
    // put a card into the bottom of the deck
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   106
    fn push( &mut self, c: Card ) {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   107
        self.content.push_back(c);
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   108
    }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   109
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   110
    fn len( &self ) -> usize {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   111
        self.content.len()
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 get_nth( &mut self, n: usize ) -> Card {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   115
        if let Some(c) = self.content.remove(n) {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   116
            c
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   117
        } else {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   118
            panic!("get_nth: index {} out of bounds ({})!", n, self.content.len());
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   119
        }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   120
    }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   121
}
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   122
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   123
struct Player {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   124
    name: String,
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   125
    hand: Deck,
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   126
    pile: Vec<Card>,
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   127
    game_point: i32,
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   128
    total_point: i32,
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   129
    rows_busted: i32,
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   130
}
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   131
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   132
impl Player {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   133
    fn new(name: String)->Self {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   134
        Player {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   135
            name,
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   136
            hand: Deck::new_empty(),
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   137
            pile: Vec::new(),
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   138
            game_point: 0,
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   139
            total_point: 0,
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   140
            rows_busted: 0,
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   141
        }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   142
    }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   143
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   144
    // get one card from th dealer
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   145
    fn get_card( &mut self, card: Card ) {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   146
        self.hand.push(card);
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   147
    }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   148
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   149
    // throw a card from hand to the table
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   150
    fn throw_card( &mut self )->Card {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   151
        if let Some(c) = self.hand.pop() {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   152
            c
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   153
        } else {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   154
            panic!("throw_card: Player {} has no card in hand!", self.name);
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   155
        }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   156
    }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   157
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   158
    // get a busted row of cards
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   159
    fn get_pile( &mut self, cards: Vec<Card> ) {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   160
        for c in cards.into_iter() {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   161
            self.game_point += c.points as i32;
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   162
            self.pile.push(c);
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   163
        }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   164
        self.rows_busted += 1;
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   165
    }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   166
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   167
    // ask the player their score
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   168
    fn tell_points( self ) -> i32 {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   169
        self.game_point
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   170
    }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   171
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   172
    // give back cards from the pile
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   173
    fn give_pile( &mut self ) -> Vec<Card> {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   174
        mem::take( &mut self.pile )
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   175
        // same effect:
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   176
        // self.pile.drain(..).collect()
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   177
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   178
        // very cumbersome manual fiddling (also reverted...)
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   179
/*         let mut throw: Vec<Card> = Vec::new();
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   180
        for _i in 0 .. self.pile.len() {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   181
            throw.push( self.pile.pop().unwrap() );
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   182
        }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   183
        throw
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   184
 */    
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   185
    }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   186
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   187
    // I can do this just because I *throw away* c!
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   188
    // doesn't work if I want to use it.
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   189
/*     fn _gimme_pile(self)->Self {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   190
        for c in &self.pile {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   191
            println!("Throw {} ", c);
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   192
        }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   193
        self
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
    fn close_round( &mut self ) {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   197
        if self.hand.len() > 0 {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   198
            panic!("Closing round when {} has {} cards in hand", self.name, self.hand.len());
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   199
        }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   200
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   201
        if self.pile.len() > 0 {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   202
            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
   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
        self.total_point += self.game_point;
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   206
        self.game_point = 0;
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   207
    }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   208
}
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   209
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   210
// a row of cards on the table (max 5)
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   211
struct Row {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   212
    cards: VecDeque<Card>,
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   213
}
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   214
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   215
impl Row {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   216
    const MAX_LEN: usize = 5;
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   217
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   218
    fn new() -> Self {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   219
        Row {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   220
            cards: VecDeque::new(),
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   221
        }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   222
    }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   223
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   224
    fn push_or_collect( &mut self, card: Card ) -> Option<VecDeque<Card>> {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   225
        if self.cards.len() < Self::MAX_LEN {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   226
            self.cards.push_back(card);
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   227
            None
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   228
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   229
        } else {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   230
            // row overflow
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   231
            let row_cards = mem::take( &mut self.cards );
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   232
            self.cards.push_back(card);
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   233
            if self.cards.len() != 1 {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   234
                panic!("New row must have one card, not {}", self.cards.len());
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   235
            }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   236
            Some(row_cards)
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   237
        }
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
    fn last_card_value(&self) -> i8 {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   241
        println!("last_card_value: cards {:?}, len {}", self.cards, self.cards.len());
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   242
        self.cards.get( self.cards.len()-1 ).unwrap().value
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   243
    }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   244
}
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   245
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   246
struct PlayerCard {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   247
    player_id: u32,
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   248
    card: Card,
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   249
}
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   250
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   251
struct Table {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   252
    rows: Vec<Row>,
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   253
    player_cards: Vec<PlayerCard>, // owned by a player
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   254
}
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   255
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   256
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   257
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   258
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   259
fn game() {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   260
/*
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   261
    let mut deck = deck_init();
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   262
    let mut players = Vec::new();
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   263
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   264
    for cnt_game in (1..100) {
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
     */
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   268
}
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   269
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   270
#[cfg(test)]
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   271
mod tests {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   272
    use crate::{Card, Player, Row};
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
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   290
        let mut pile = Vec::new();
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   291
        let mut refpile = Vec::new();
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);
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   294
            pile.push(c);
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   295
            let c = Card::new(i);
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   296
            refpile.push(c);
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   297
        }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   298
        // add the pile to player
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   299
        p.get_pile(pile);
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   300
        assert!( p.rows_busted == 1 );
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();
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   304
        let pile = p.give_pile();
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
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   308
        assert!( pile.iter().all( |item| refpile.contains(item)) );
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   309
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   310
        let mut pile = Vec::new();
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   311
        for i in 4..9 {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   312
            let c = Card::new(i);
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   313
            pile.push(c);
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   314
        }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   315
        p.get_pile(pile);
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   316
        assert!( p.rows_busted == 2 );
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   317
    }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   318
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   319
    #[test]
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   320
    fn row_push() {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   321
        let mut row = Row::new();
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   322
        let mut refcard = Vec::new();   // reference vec to check
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   323
        for i in 1..=7 {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   324
            let cval = i+5;
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   325
            let card = Card::new(cval);
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   326
            // push a card into the row
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   327
            if let Some(cards) = row.push_or_collect(card) {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   328
                // got the overflow
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   329
                println!("Got overflow row at {}!", i);
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   330
                assert!( i == 6, "Overflow at wrong position: {} != 6", i );
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   331
                // we need to get the proper vec
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   332
                assert!( cards.iter().all( |item| refcard.contains(item) ), "Got cards {:?}", cards ); 
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   333
            } else {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   334
                println!("push success {}", i);
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   335
            }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   336
            // remember the correct vec for checking
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   337
            let card = Card::new(cval);
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   338
            refcard.push(card);
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   339
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   340
            // check card value
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   341
            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
   342
        }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   343
        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
   344
    }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   345
}
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   346
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   347
/*
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   348
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   349
- 1-104 lap, 
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   350
    - *5 - 2
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   351
    - *0 - 3
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   352
    - NN - 5
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   353
    - 55 - 7
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   354
- deck
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   355
- jatekosok; kezben tartott lapok; elvitt lapok; pontszamok; counter: elvitt sorok, okrok, total pont
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   356
-- keveres (keveresek szama)
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   357
-- osztas: mindenki 10 lap
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   358
-- start sorok: 5 kartya
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   359
- jatek (jatekok szama)
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   360
-- mindenki a felso lapot kiteszi
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   361
-- szamsorrendben felkerulnek, aki viszi, viszi
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   362
--- ha kisebb, akkor dontes
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   363
---- ha van legkevesebb, viszi
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   364
---- ha tobb min van, random valaszt
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   365
- osszesites
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   366
-- okrok, elvitt sorok
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   367
-- keveresek szama, jatekok szama, eltelt ido
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   368
- deck osszegyujtes
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   369
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   370
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   371
*/