Нет описания

ai.rs 3.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. use game;
  2. use deck;
  3. use card;
  4. // Each player looks at his game and termines if he takes or not, returns the taker's id
  5. pub fn auctions(players: &Vec<game::Player>) -> i32 {
  6. let potentials: Vec<(i32, i32)> = players.iter()
  7. .map(|ref player| (player.id, evaluate_hand_potential(&player.hand)))
  8. .collect();
  9. let &(taker_id, _) = match potentials.iter().max_by_key(|&&(_, potential)| potential) {
  10. None => panic!("no players"),
  11. Some(x) => x,
  12. };
  13. taker_id
  14. }
  15. pub fn evaluate_hand_potential(hand: &deck::Hand) -> i32 {
  16. hand.iter().fold(0, |acc, card| acc + card::potential_value(card))
  17. }
  18. pub fn exchange_dog(mut players: &mut Vec<game::Player>,
  19. dog: &mut deck::Hand,
  20. game: &mut game::Game) {
  21. let ref mut taker = match players.iter_mut().find(|player| player.id == game.taker_id) {
  22. None => panic!("cannot find the taker"),
  23. Some(player) => player,
  24. };
  25. // Now we proceed to exchange the cards in the dog
  26. // We adopt a simple and naïve strategy
  27. // We let the low cards in the dog and exchange useful for random low others
  28. let mut new_dog: Vec<(card::Card, i32)> = Vec::new();
  29. dog.clone()
  30. .iter()
  31. .fold((), |_, &card| {
  32. let to_exchange = match card {
  33. card::Card::Trump(_) => false,
  34. card::Card::Face(card::Face { suit: _, symbol }) => {
  35. match symbol {
  36. card::Symbol::Jack | card::Symbol::Knight | card::Symbol::Queen |
  37. card::Symbol::King => false,
  38. _ => true,
  39. }
  40. }
  41. card::Card::Fool => false,
  42. };
  43. if to_exchange {
  44. // Performs the exchange
  45. dog.remove(&card);
  46. new_dog.push((card, taker.id));
  47. }
  48. });
  49. // We exchange the remaining cards with cards in the hand of the taker
  50. dog.clone().iter().fold((), |_, &dog_card| {
  51. let taker_hand = taker.hand.clone();
  52. let exchange_card = taker_hand.iter().filter(|&&card| dog_card > card).next();
  53. match exchange_card {
  54. None => (),
  55. Some(&exchange_card) => {
  56. dog.remove(&dog_card);
  57. taker.hand.insert(dog_card);
  58. taker.hand.remove(&exchange_card);
  59. new_dog.push((exchange_card, taker.id));
  60. }
  61. };
  62. });
  63. //Then for the remaining cards in the dog we simply transfer them to the new dog
  64. dog.clone().iter().fold((), |_, &card| {
  65. dog.remove(&card);
  66. new_dog.push((card, taker.id));
  67. });
  68. // Finaly the new dog goes into the attack's heap
  69. game.attack_heap.append(&mut new_dog);
  70. }
  71. // Select a card to play among the valid cards
  72. pub fn select_card(_: &game::Player, valid_cards: &deck::Hand, _: &game::Heap) -> card::Card {
  73. match valid_cards.iter().next() {
  74. None => panic!("no valid card to play"),
  75. Some(card) => card.clone(),
  76. }
  77. }