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....

// vigyazz6! autplayer
//

use core::{fmt, panic};
use std::collections::VecDeque;
use rand::Rng;
use std::mem;

fn main() {
    game();
}

#[derive(Debug)]
struct Card {
    value: i8,
    points: i8,
}

impl Card {
    fn new(value: i8)->Self {

        let mut points = 0;
        if value % 10 == 5 {
            // ends with 5 = 2 point
            points = 2;
            // println!("*5 add 1, val={}, pt={}", value, points);
        }

        if value % 10 == 0 {
            // ends with 0 = 3 point
            points = 3;
            // println!("*0 add 2, val={}, pt={}", value, points);
        }

        if value % 10 == value / 10 {
            // same numbers = 5 points (55=7)
            points += 5;
            // println!("NN add 5, val={}, pt={}", value, points);
        }

        if points == 0 {
            points = 1;
        }

        Card {
            value,
            points,
        }
    }
}

impl fmt::Display for Card {
    fn fmt( &self, f: &mut fmt::Formatter ) -> fmt::Result {
        write!(f, "(Card {}, points {})", self.value, self.points)
    }
}

impl PartialEq for Card {
    fn eq(&self, other: &Self) -> bool {
        self.value == other.value
    }
}

impl Eq for Card {} 

impl PartialOrd for Card {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        match self.value.partial_cmp(&other.value) {
            Some(core::cmp::Ordering::Equal) => {None}
            ord => return ord,
        }
    }
}

impl Ord for Card {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        self.value.cmp(&other.value)
    }
}


#[derive(Debug)]
struct Deck {
    content: VecDeque<Card>,
}

impl Deck {
    fn new_empty() -> Self {
        Deck {
            content: VecDeque::new(),
        }
    }

    fn new() -> Self {
        let content = (1..104).into_iter().map( |n| Card::new(n) ).collect();
        Deck {
            content,
        }
    }

    fn shuffle( &mut self ) {
        let mut rng = rand::thread_rng();

        // shufflers:
        // * naive: swap cards n times
        // * kgb: half the deck, take 1..4 cards sequentially from each
        // * grin: take 1..6 from front and put at bottom

        // naive shuffle: exchange random cards
        for _i in 1..=500 {
            let c1 = rng.gen_range(0 .. self.content.len());
            let c2 = rng.gen_range(0 .. self.content.len());
            if c1 != c2 {
                self.content.swap(c1, c2);
            }
        }
    }

    // get top card from deck
    fn pop( &mut self ) -> Option<Card> {
        self.content.pop_front() 
    }

    // put a card into the bottom of the deck
    fn push( &mut self, c: Card ) {
        self.content.push_back(c);
    }

    fn len( &self ) -> usize {
        self.content.len()
    }

    fn get_nth( &mut self, n: usize ) -> Card {
        if let Some(c) = self.content.remove(n) {
            c
        } else {
            panic!("get_nth: index {} out of bounds ({})!", n, self.content.len());
        }
    }
}

#[derive(Debug)]
struct Player {
    name: String,
    hand: Deck,
    pile: Vec<Card>,
    game_point: i32,
    total_point: i32,
    rows_busted: i32,
}

impl Player {
    fn new(name: String)->Self {
        Player {
            name,
            hand: Deck::new_empty(),
            pile: Vec::new(),
            game_point: 0,
            total_point: 0,
            rows_busted: 0,
        }
    }

    // get one card from th dealer
    fn get_card( &mut self, card: Card ) {
        self.hand.push(card);
    }

    // throw a card from hand to the table
    fn throw_card( &mut self )->Card {
        if let Some(c) = self.hand.pop() {
            c
        } else {
            panic!("throw_card: Player {} has no card in hand!", self.name);
        }
    }

    // get a busted row of cards
    fn get_pile( &mut self, cards: VecDeque<Card> ) {
        for c in cards.into_iter() {
            self.game_point += c.points as i32;
            self.pile.push(c);
        }
        self.rows_busted += 1;
    }

    // ask the player their score
    fn tell_points( self ) -> i32 {
        self.game_point
    }

    // give back cards from the pile
    fn give_pile( &mut self ) -> Vec<Card> {
        mem::take( &mut self.pile )
        // same effect:
        // self.pile.drain(..).collect()

        // very cumbersome manual fiddling (also reverted...)
/*         let mut throw: Vec<Card> = Vec::new();
        for _i in 0 .. self.pile.len() {
            throw.push( self.pile.pop().unwrap() );
        }
        throw
 */    
    }

    // I can do this just because I *throw away* c!
    // doesn't work if I want to use it.
/*     fn _gimme_pile(self)->Self {
        for c in &self.pile {
            println!("Throw {} ", c);
        }
        self
    }
 */
    fn close_round( &mut self ) {
        if self.hand.len() > 0 {
            panic!("Closing round when {} has {} cards in hand", self.name, self.hand.len());
        }

        if self.pile.len() > 0 {
            panic!("Closing round when {} stil have pile with {} cards", self.name, self.pile.len());
        }

        self.total_point += self.game_point;
        self.game_point = 0;
    }
}

// a row of cards on the table (max 5)
#[derive(Debug)]
struct Row {
    cards: VecDeque<Card>,
}

impl Row {
    const MAX_LEN: usize = 5;

    fn new() -> Self {
        Row {
            cards: VecDeque::new(),
        }
    }

    fn push_or_collect( &mut self, card: Card ) -> Option<VecDeque<Card>> {
        if self.cards.len() < Self::MAX_LEN {
            self.cards.push_back(card);
            None

        } else {
            // row overflow
            let row_cards = mem::take( &mut self.cards );
            self.cards.push_back(card);
            if self.cards.len() != 1 {
                panic!("New row must have one card, not {}", self.cards.len());
            }
            Some(row_cards)
        }
    }

    fn last_card_value(&self) -> i8 {
        println!("last_card_value: cards {:?}, len {}", self.cards, self.cards.len());
        self.cards.get( self.cards.len()-1 ).unwrap().value
    }
}

#[derive(Debug)]
struct PlayerCard {
    player_id: i32,
    card: Card,
}

impl PlayerCard {
    fn get_player(&self) -> i32 {
        self.player_id
    }
}

impl PartialEq for PlayerCard {
    fn eq(&self, other: &Self) -> bool {
        self.card == other.card
    }
}

impl PartialOrd for PlayerCard {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        match self.card.partial_cmp(&other.card) {
            Some(core::cmp::Ordering::Equal) => {None}
            ord => return ord,
        }
    }
}

#[derive(Debug)]
struct Table {
    rows: Vec<Row>,
    player_cards: Vec<PlayerCard>, // owned by a player
}

impl Table {
    fn new(row_cards: Vec<Card>) -> Self {
        let mut rows = Vec::new();
        for card in row_cards {
            // create a new row then put a card into it
            let mut row = Row::new();
            if let Some(c) = row.push_or_collect(card) {
                panic!("Freshly created row overflowed");
            }
            rows.push( row );
        }

        Table {
            rows,
            player_cards: Vec::new(),
        }
    }

    fn lay_player_card( &mut self, card: Card, player_id: i32 ) {
        self.player_cards.push( PlayerCard { player_id, card } );
    }

    fn sort_cards( &mut self ) {
        self.player_cards.sort_by( |a,b| b.card.cmp(&a.card) );
    }

    fn get_smallest_player_card( &mut self ) -> PlayerCard {
        self.player_cards.pop().expect("out of player cards on table")
    }

    fn get_closest_row( &self, pcard: &PlayerCard ) -> Option<i8> {
        // get the row id with last card closest smaller to players'
        todo!()
    }

    fn put_card_into_row( &mut self, pcard: PlayerCard, row_id: i8 ) -> Option<VecDeque<Card>> {
        self.rows[row_id as usize].push_or_collect(pcard.card)
    }
}


fn game() {
    let mut cnt_shuffle = 0;

    let mut deck = Deck::new();

    let player_names = vec![ "grin", "moni", "icbalint", "orsi", "topi", "kgb", "zsu", "csilla" ];
    let mut players: Vec<Player> = player_names.iter().map( |n| Player::new(n.to_string()) ).collect();

    let player_count = players.len(); // pc - 1

    for _cnt_game in 1..=2 {
        deck.shuffle();
        cnt_shuffle += 1;

        // dealing
        for i in 1..=10 {
            for player in 0 .. player_count {
                players[player].get_card( deck.pop().expect("Deck is empty while dealing to players") );
            }
        }

        // we need 5 crds from deck
        let mut cards = Vec::new();
        for i in 1..=5 {
            cards.push( deck.pop().expect("deck empty before starting the game") );
        }
        println!("We push 5 cards to rows: {:?}\n", cards);
        let mut table = Table::new(cards);

        // DEBUG
        println!("Table: {:?}\n", table);
        println!("PLayers: {:?}\n", players);
        println!("Deck: {:?}\n", deck);
        

        // playing
        for turn in 1..=10 {

            // everyone puts a card face down
            for player in 0 .. player_count {
                let player_id: i32 = player.try_into().unwrap();
                // get a card from the player
                let topcard = players[player].throw_card();
                // put it on the table ("turned face down")
                table.lay_player_card( topcard, player_id );
            }

            // process cards
            table.sort_cards();

            let smallest = table.get_smallest_player_card();

            let closest_row = table.get_closest_row(&smallest);
            match closest_row {
                Some(rowid) => {
                    let player_id: usize = smallest.player_id.try_into().unwrap();
                    let overflow = table.put_card_into_row(smallest, rowid);
                    if let Some(cards) = overflow {
                        // row is full, got pile
                        // player gets pile, card gets into row head
                        players[ player_id ].get_pile( cards );
                    }
                },
                None => {
                    // card too small, need to pick row!
                    todo!();
                }
            }

        }

        // end of round

        // recollect deck

        panic!("FInish now.");
    }
}

#[cfg(test)]
mod tests {
    use core::panic;
    use std::collections::VecDeque;

    use rand::Rng;

    use crate::{Card, Player, Row};

    #[test]
    fn card_values() {
        let card_values = vec![1,2,5,10,33,55,77];
        let card_points = vec![1,1,2,3, 5, 7, 5];
        for i in 1 .. card_values.len() {
            let c = Card::new( card_values[i] );
            let p = c.points;
            assert!(p == card_points[i], "card={} card points={} i={} expected point={}", card_values[i], p, i, card_points[i]);
        }
    }

    #[test]
    fn player_take_pile() {
        // create a player
        let mut p = Player::new("bob".to_string());
        // create a pile
        let mut pile = VecDeque::new();
        let mut refpile = Vec::new();
        for i in 5..10 {
            let c = Card::new(i);
            pile.push_back(c);
            let c = Card::new(i);
            refpile.push(c);
        }
        // add the pile to player
        p.get_pile(pile);
        assert!( p.rows_busted == 1 );

        // get back the pile from player
        // p = p.gimme_pile();
        let pile = p.give_pile();
        
        // the pile we got shall be same as the pile we gave
        // this check is O(n^2), doesn't matter for less than 100 items
        assert!( pile.iter().all( |item| refpile.contains(item)) );

        let mut pile = VecDeque::new();
        for i in 4..=9 {
            let c = Card::new(i);
            pile.push_back(c);
        }
        p.get_pile(pile);
        assert!( p.rows_busted == 2 );
    }

    #[test]
    fn row_push() {
        let mut row = Row::new();
        let mut refcard = Vec::new();   // reference vec to check
        for i in 1..=7 {
            let cval = i+5;
            let card = Card::new(cval);
            // push a card into the row
            if let Some(cards) = row.push_or_collect(card) {
                // got the overflow
                println!("Got overflow row at {}!", i);
                assert!( i == 6, "Overflow at wrong position: {} != 6", i );
                // we need to get the proper vec
                assert!( cards.iter().all( |item| refcard.contains(item) ), "Got cards {:?}", cards ); 
            } else {
                println!("push success {}", i);
            }
            // remember the correct vec for checking
            let card = Card::new(cval);
            refcard.push(card);

            // check card value
            assert!( row.last_card_value() == cval, "Last card value mismatch: got {} vs expected {}", row.last_card_value(), cval );
        }
        assert!( row.cards.len() == 2, "Row contains wrong amount of cards: {}", row.cards.len() );
    }

    #[test]
    fn sort_cards() {
        let mut cards: Vec<Card> = Vec::new();
        let mut rng = rand::thread_rng();
        for i in 1..50 {
            let n = rng.gen_range(1..104);
            cards.push( Card::new(n) );
        }
        cards.sort();

        for i in 1..cards.len() {
            assert!( cards[i-1].value <= cards[i].value, "Bad ordering: {} > {}", cards[i-1].value,cards[i].value );
        }
    }

}

/*

- 1-104 lap, 
    - *5 - 2
    - *0 - 3
    - NN - 5
    - 55 - 7
- deck
- jatekosok; kezben tartott lapok; elvitt lapok; pontszamok; counter: elvitt sorok, okrok, total pont
-- keveres (keveresek szama)
-- osztas: mindenki 10 lap
-- start sorok: 5 kartya
- jatek (jatekok szama)
-- mindenki a felso lapot kiteszi
-- szamsorrendben felkerulnek, aki viszi, viszi
--- ha kisebb, akkor dontes
---- ha van legkevesebb, viszi
---- ha tobb min van, random valaszt
- osszesites
-- okrok, elvitt sorok
-- keveresek szama, jatekok szama, eltelt ido
- deck osszegyujtes


*/