src/main.rs
author Peter Gervai <grin@grin.hu>
Sun, 29 Jan 2023 23:59:45 +0100
changeset 1 5f81068a8a88
parent 0 a95b84125269
child 2 950660fddec0
permissions -rw-r--r--
Progressing....
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
1
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
     4
use core::{fmt, panic};
0
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() {
1
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
    10
    game();
0
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
1
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
    64
impl Eq for Card {} 
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
    65
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
    66
impl PartialOrd for Card {
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
    67
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
    68
        match self.value.partial_cmp(&other.value) {
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
    69
            Some(core::cmp::Ordering::Equal) => {None}
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
    70
            ord => return ord,
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
    71
        }
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
    72
    }
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
    73
}
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
    74
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
    75
impl Ord for Card {
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
    76
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
    77
        self.value.cmp(&other.value)
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
    78
    }
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
    79
}
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
    80
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
    81
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
    82
#[derive(Debug)]
0
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    83
struct Deck {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    84
    content: VecDeque<Card>,
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    85
}
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    86
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    87
impl Deck {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    88
    fn new_empty() -> Self {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    89
        Deck {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    90
            content: VecDeque::new(),
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    91
        }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    92
    }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    93
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    94
    fn new() -> Self {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    95
        let content = (1..104).into_iter().map( |n| Card::new(n) ).collect();
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    96
        Deck {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    97
            content,
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
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   101
    fn shuffle( &mut self ) {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   102
        let mut rng = rand::thread_rng();
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   103
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   104
        // shufflers:
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   105
        // * naive: swap cards n times
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   106
        // * kgb: half the deck, take 1..4 cards sequentially from each
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   107
        // * grin: take 1..6 from front and put at bottom
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   108
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   109
        // naive shuffle: exchange random cards
1
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   110
        for _i in 1..=500 {
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   111
            let c1 = rng.gen_range(0 .. self.content.len());
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   112
            let c2 = rng.gen_range(0 .. self.content.len());
0
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   113
            if c1 != c2 {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   114
                self.content.swap(c1, c2);
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   115
            }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   116
        }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   117
    }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   118
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   119
    // get top card from deck
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   120
    fn pop( &mut self ) -> Option<Card> {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   121
        self.content.pop_front() 
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   122
    }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   123
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   124
    // put a card into the bottom of the deck
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   125
    fn push( &mut self, c: Card ) {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   126
        self.content.push_back(c);
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   127
    }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   128
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   129
    fn len( &self ) -> usize {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   130
        self.content.len()
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   131
    }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   132
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   133
    fn get_nth( &mut self, n: usize ) -> Card {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   134
        if let Some(c) = self.content.remove(n) {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   135
            c
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   136
        } else {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   137
            panic!("get_nth: index {} out of bounds ({})!", n, self.content.len());
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   138
        }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   139
    }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   140
}
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   141
1
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   142
#[derive(Debug)]
0
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   143
struct Player {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   144
    name: String,
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   145
    hand: Deck,
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   146
    pile: Vec<Card>,
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   147
    game_point: i32,
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   148
    total_point: i32,
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   149
    rows_busted: i32,
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   150
}
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   151
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   152
impl Player {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   153
    fn new(name: String)->Self {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   154
        Player {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   155
            name,
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   156
            hand: Deck::new_empty(),
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   157
            pile: Vec::new(),
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   158
            game_point: 0,
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   159
            total_point: 0,
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   160
            rows_busted: 0,
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   161
        }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   162
    }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   163
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   164
    // get one card from th dealer
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   165
    fn get_card( &mut self, card: Card ) {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   166
        self.hand.push(card);
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   167
    }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   168
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   169
    // throw a card from hand to the table
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   170
    fn throw_card( &mut self )->Card {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   171
        if let Some(c) = self.hand.pop() {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   172
            c
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   173
        } else {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   174
            panic!("throw_card: Player {} has no card in hand!", self.name);
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   175
        }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   176
    }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   177
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   178
    // get a busted row of cards
1
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   179
    fn get_pile( &mut self, cards: VecDeque<Card> ) {
0
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   180
        for c in cards.into_iter() {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   181
            self.game_point += c.points as i32;
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   182
            self.pile.push(c);
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   183
        }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   184
        self.rows_busted += 1;
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
    // ask the player their score
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   188
    fn tell_points( self ) -> i32 {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   189
        self.game_point
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   190
    }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   191
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   192
    // give back cards from the pile
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   193
    fn give_pile( &mut self ) -> Vec<Card> {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   194
        mem::take( &mut self.pile )
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   195
        // same effect:
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   196
        // self.pile.drain(..).collect()
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   197
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   198
        // very cumbersome manual fiddling (also reverted...)
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   199
/*         let mut throw: Vec<Card> = Vec::new();
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   200
        for _i in 0 .. self.pile.len() {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   201
            throw.push( self.pile.pop().unwrap() );
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   202
        }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   203
        throw
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   204
 */    
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   205
    }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   206
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   207
    // I can do this just because I *throw away* c!
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   208
    // doesn't work if I want to use it.
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   209
/*     fn _gimme_pile(self)->Self {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   210
        for c in &self.pile {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   211
            println!("Throw {} ", c);
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   212
        }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   213
        self
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   214
    }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   215
 */
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   216
    fn close_round( &mut self ) {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   217
        if self.hand.len() > 0 {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   218
            panic!("Closing round when {} has {} cards in hand", self.name, self.hand.len());
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   219
        }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   220
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   221
        if self.pile.len() > 0 {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   222
            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
   223
        }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   224
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   225
        self.total_point += self.game_point;
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   226
        self.game_point = 0;
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   227
    }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   228
}
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   229
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   230
// a row of cards on the table (max 5)
1
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   231
#[derive(Debug)]
0
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   232
struct Row {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   233
    cards: VecDeque<Card>,
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   234
}
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   235
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   236
impl Row {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   237
    const MAX_LEN: usize = 5;
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   238
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   239
    fn new() -> Self {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   240
        Row {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   241
            cards: VecDeque::new(),
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   242
        }
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
    fn push_or_collect( &mut self, card: Card ) -> Option<VecDeque<Card>> {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   246
        if self.cards.len() < Self::MAX_LEN {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   247
            self.cards.push_back(card);
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   248
            None
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   249
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   250
        } else {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   251
            // row overflow
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   252
            let row_cards = mem::take( &mut self.cards );
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   253
            self.cards.push_back(card);
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   254
            if self.cards.len() != 1 {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   255
                panic!("New row must have one card, not {}", self.cards.len());
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   256
            }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   257
            Some(row_cards)
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   258
        }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   259
    }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   260
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   261
    fn last_card_value(&self) -> i8 {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   262
        println!("last_card_value: cards {:?}, len {}", self.cards, self.cards.len());
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   263
        self.cards.get( self.cards.len()-1 ).unwrap().value
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
1
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   267
#[derive(Debug)]
0
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   268
struct PlayerCard {
1
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   269
    player_id: i32,
0
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   270
    card: Card,
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   271
}
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   272
1
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   273
impl PlayerCard {
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   274
    fn get_player(&self) -> i32 {
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   275
        self.player_id
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   276
    }
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   277
}
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   278
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   279
impl PartialEq for PlayerCard {
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   280
    fn eq(&self, other: &Self) -> bool {
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   281
        self.card == other.card
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   282
    }
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   283
}
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   284
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   285
impl PartialOrd for PlayerCard {
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   286
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   287
        match self.card.partial_cmp(&other.card) {
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   288
            Some(core::cmp::Ordering::Equal) => {None}
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   289
            ord => return ord,
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   290
        }
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   291
    }
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   292
}
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   293
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   294
#[derive(Debug)]
0
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   295
struct Table {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   296
    rows: Vec<Row>,
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   297
    player_cards: Vec<PlayerCard>, // owned by a player
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   298
}
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   299
1
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   300
impl Table {
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   301
    fn new(row_cards: Vec<Card>) -> Self {
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   302
        let mut rows = Vec::new();
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   303
        for card in row_cards {
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   304
            // create a new row then put a card into it
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   305
            let mut row = Row::new();
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   306
            if let Some(c) = row.push_or_collect(card) {
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   307
                panic!("Freshly created row overflowed");
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   308
            }
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   309
            rows.push( row );
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   310
        }
0
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   311
1
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   312
        Table {
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   313
            rows,
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   314
            player_cards: Vec::new(),
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   315
        }
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   316
    }
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   317
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   318
    fn lay_player_card( &mut self, card: Card, player_id: i32 ) {
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   319
        self.player_cards.push( PlayerCard { player_id, card } );
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   320
    }
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   321
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   322
    fn sort_cards( &mut self ) {
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   323
        self.player_cards.sort_by( |a,b| b.card.cmp(&a.card) );
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   324
    }
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   325
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   326
    fn get_smallest_player_card( &mut self ) -> PlayerCard {
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   327
        self.player_cards.pop().expect("out of player cards on table")
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   328
    }
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   329
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   330
    fn get_closest_row( &self, pcard: &PlayerCard ) -> Option<i8> {
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   331
        // get the row id with last card closest smaller to players'
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   332
        todo!()
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   333
    }
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   334
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   335
    fn put_card_into_row( &mut self, pcard: PlayerCard, row_id: i8 ) -> Option<VecDeque<Card>> {
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   336
        self.rows[row_id as usize].push_or_collect(pcard.card)
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   337
    }
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   338
}
0
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   339
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   340
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   341
fn game() {
1
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   342
    let mut cnt_shuffle = 0;
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   343
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   344
    let mut deck = Deck::new();
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   345
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   346
    let player_names = vec![ "grin", "moni", "icbalint", "orsi", "topi", "kgb", "zsu", "csilla" ];
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   347
    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
   348
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   349
    let player_count = players.len(); // pc - 1
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   350
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   351
    for _cnt_game in 1..=2 {
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   352
        deck.shuffle();
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   353
        cnt_shuffle += 1;
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   354
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   355
        // dealing
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   356
        for i in 1..=10 {
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   357
            for player in 0 .. player_count {
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   358
                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
   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
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   362
        // we need 5 crds from deck
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   363
        let mut cards = Vec::new();
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   364
        for i in 1..=5 {
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   365
            cards.push( deck.pop().expect("deck empty before starting the game") );
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   366
        }
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   367
        println!("We push 5 cards to rows: {:?}\n", cards);
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   368
        let mut table = Table::new(cards);
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   369
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   370
        // DEBUG
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   371
        println!("Table: {:?}\n", table);
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   372
        println!("PLayers: {:?}\n", players);
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   373
        println!("Deck: {:?}\n", deck);
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   374
        
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   375
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   376
        // playing
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   377
        for turn in 1..=10 {
0
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   378
1
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   379
            // everyone puts a card face down
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   380
            for player in 0 .. player_count {
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   381
                let player_id: i32 = player.try_into().unwrap();
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   382
                // get a card from the player
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   383
                let topcard = players[player].throw_card();
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   384
                // put it on the table ("turned face down")
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   385
                table.lay_player_card( topcard, player_id );
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   386
            }
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   387
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   388
            // process cards
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   389
            table.sort_cards();
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   390
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   391
            let smallest = table.get_smallest_player_card();
0
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   392
1
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   393
            let closest_row = table.get_closest_row(&smallest);
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   394
            match closest_row {
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   395
                Some(rowid) => {
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   396
                    let player_id: usize = smallest.player_id.try_into().unwrap();
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   397
                    let overflow = table.put_card_into_row(smallest, rowid);
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   398
                    if let Some(cards) = overflow {
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   399
                        // row is full, got pile
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   400
                        // player gets pile, card gets into row head
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   401
                        players[ player_id ].get_pile( cards );
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   402
                    }
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   403
                },
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   404
                None => {
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   405
                    // card too small, need to pick row!
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   406
                    todo!();
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   407
                }
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   408
            }
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   409
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   410
        }
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   411
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   412
        // end of round
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   413
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   414
        // recollect deck
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   415
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   416
        panic!("FInish now.");
0
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   417
    }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   418
}
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   419
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   420
#[cfg(test)]
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   421
mod tests {
1
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   422
    use core::panic;
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   423
    use std::collections::VecDeque;
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   424
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   425
    use rand::Rng;
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   426
0
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   427
    use crate::{Card, Player, Row};
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   428
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   429
    #[test]
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   430
    fn card_values() {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   431
        let card_values = vec![1,2,5,10,33,55,77];
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   432
        let card_points = vec![1,1,2,3, 5, 7, 5];
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   433
        for i in 1 .. card_values.len() {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   434
            let c = Card::new( card_values[i] );
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   435
            let p = c.points;
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   436
            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
   437
        }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   438
    }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   439
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   440
    #[test]
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   441
    fn player_take_pile() {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   442
        // create a player
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   443
        let mut p = Player::new("bob".to_string());
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   444
        // create a pile
1
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   445
        let mut pile = VecDeque::new();
0
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   446
        let mut refpile = Vec::new();
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   447
        for i in 5..10 {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   448
            let c = Card::new(i);
1
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   449
            pile.push_back(c);
0
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   450
            let c = Card::new(i);
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   451
            refpile.push(c);
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   452
        }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   453
        // add the pile to player
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   454
        p.get_pile(pile);
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   455
        assert!( p.rows_busted == 1 );
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   456
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   457
        // get back the pile from player
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   458
        // p = p.gimme_pile();
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   459
        let pile = p.give_pile();
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   460
        
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   461
        // the pile we got shall be same as the pile we gave
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   462
        // 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
   463
        assert!( pile.iter().all( |item| refpile.contains(item)) );
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   464
1
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   465
        let mut pile = VecDeque::new();
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   466
        for i in 4..=9 {
0
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   467
            let c = Card::new(i);
1
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   468
            pile.push_back(c);
0
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   469
        }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   470
        p.get_pile(pile);
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   471
        assert!( p.rows_busted == 2 );
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   472
    }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   473
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   474
    #[test]
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   475
    fn row_push() {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   476
        let mut row = Row::new();
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   477
        let mut refcard = Vec::new();   // reference vec to check
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   478
        for i in 1..=7 {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   479
            let cval = i+5;
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   480
            let card = Card::new(cval);
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   481
            // push a card into the row
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   482
            if let Some(cards) = row.push_or_collect(card) {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   483
                // got the overflow
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   484
                println!("Got overflow row at {}!", i);
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   485
                assert!( i == 6, "Overflow at wrong position: {} != 6", i );
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   486
                // we need to get the proper vec
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   487
                assert!( cards.iter().all( |item| refcard.contains(item) ), "Got cards {:?}", cards ); 
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   488
            } else {
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   489
                println!("push success {}", i);
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   490
            }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   491
            // remember the correct vec for checking
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   492
            let card = Card::new(cval);
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   493
            refcard.push(card);
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   494
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   495
            // check card value
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   496
            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
   497
        }
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   498
        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
   499
    }
1
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   500
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   501
    #[test]
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   502
    fn sort_cards() {
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   503
        let mut cards: Vec<Card> = Vec::new();
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   504
        let mut rng = rand::thread_rng();
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   505
        for i in 1..50 {
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   506
            let n = rng.gen_range(1..104);
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   507
            cards.push( Card::new(n) );
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   508
        }
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   509
        cards.sort();
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   510
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   511
        for i in 1..cards.len() {
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   512
            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
   513
        }
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   514
    }
5f81068a8a88 Progressing....
Peter Gervai <grin@grin.hu>
parents: 0
diff changeset
   515
0
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   516
}
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   517
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   518
/*
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   519
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   520
- 1-104 lap, 
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   521
    - *5 - 2
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   522
    - *0 - 3
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   523
    - NN - 5
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   524
    - 55 - 7
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   525
- deck
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   526
- jatekosok; kezben tartott lapok; elvitt lapok; pontszamok; counter: elvitt sorok, okrok, total pont
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   527
-- keveres (keveresek szama)
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   528
-- osztas: mindenki 10 lap
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   529
-- start sorok: 5 kartya
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   530
- jatek (jatekok szama)
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   531
-- mindenki a felso lapot kiteszi
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   532
-- szamsorrendben felkerulnek, aki viszi, viszi
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   533
--- ha kisebb, akkor dontes
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   534
---- ha van legkevesebb, viszi
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   535
---- ha tobb min van, random valaszt
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   536
- osszesites
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   537
-- okrok, elvitt sorok
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   538
-- keveresek szama, jatekok szama, eltelt ido
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   539
- deck osszegyujtes
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   540
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   541
a95b84125269 Add repo.
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   542
*/