From 3351cc63ba1a5dee386e1f11287f127f25a98bc5 Mon Sep 17 00:00:00 2001 From: Ethan du Toit Date: Wed, 28 Aug 2024 00:39:15 +1000 Subject: [PATCH] game --- src/main.rs | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 71 insertions(+), 4 deletions(-) diff --git a/src/main.rs b/src/main.rs index 8bc803a..98d6dcd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -75,7 +75,8 @@ fn printHand(state: &GameState) { } } -fn deal(human: usize, trump: Option) { +fn deal(human: usize, trump: Option) -> (u8, u8) { + let player_names: [&str; 4] = ["South", "West", "North", "East"]; match trump { Some(x) => println!("The trump is {:?}", x), None => println!("There is no trump"), @@ -115,10 +116,14 @@ fn deal(human: usize, trump: Option) { .unwrap(); let mut player: usize = 0; let mut ais: [Option; 4] = [None, None, None, None]; - for _turn in 0..52 { + for turn in 0..52 { let card_played: Card = if player == human { // we'll do the other stuff later printHand(&states[player as usize]); + match &states[player as usize].trick[0] { + Some(x) => println!("The suit led is {:?}.", x.suit), + None => println!("Your lead."), + } println!("What do you play? "); readCard() } else { @@ -166,7 +171,11 @@ fn deal(human: usize, trump: Option) { } } ais[player] = Some(node); - println!("{} played {}!", player, numToCard(best_move as u8)); + println!( + "{} played {}!", + player_names[((player + 4) - human) % 4], + numToCard(best_move as u8) + ); numToCard(best_move as u8) }; for i in 0..4 { @@ -184,9 +193,64 @@ fn deal(human: usize, trump: Option) { states[i] = state_transit(&states[i], card_played); } player = states[0].player as usize; + if states[0].trick[0].is_none() { + if turn != 51 { + println!("New trick."); + } + println!( + "Now N-S have {} tricks, and E-W have {}.", + states[0].tricksWonBy0, + (13 - states[0].sizes[0]) - states[0].tricksWonBy0 + ); + } + } + if (states[0].tricksWonBy0 > 6) { + println!( + "Yay! Your team won more than 6 tricks -- in fact, you won {}. So you get {} points!", + states[0].tricksWonBy0, + states[0].tricksWonBy0 - 6 + ); + (states[0].tricksWonBy0 - 6, 0) + } else { + println!("Aw... The other team won more than 6 tricks -- they won {}. Therefore, they get {} points.", 13 - states[0].tricksWonBy0, (7 - states[0].tricksWonBy0)); + (0, 7 - states[0].tricksWonBy0) } } +fn game() { + println!("Let's play Whist! You are South, and your partner is North. Your opponents are West and East."); + let player_names: [&str; 4] = ["South", "West", "North", "East"]; + let trumpSeq = [ + Some(Suit::Hearts), + Some(Suit::Spades), + Some(Suit::Diamonds), + Some(Suit::Clubs), + None, + ]; + let mut points = (0, 0); + let mut first = random::() % 4; + let mut trump = 0; + while points.0 < 7 && points.1 < 7 { + println!("N-S has {} points, E-W has {}.", points.0, points.1); + println!("New deal! {} goes first.", player_names[first]); + let new_points = deal(4 - first, trumpSeq[trump]); + points = (points.0 + new_points.0, points.1 + new_points.1); + trump += 1; + trump %= 5; + first += 1; + first %= 4; + } + if points.0 >= 7 { + println!("Congratulations! You won!"); + } else { + println!("Sorry... you lose."); + } + println!( + "N-S finished with {} points, E-W with {}.", + points.0, points.1 + ); +} + fn rankOf(c: Card) -> u8 { c.rank } @@ -645,7 +709,10 @@ fn simulate(state: &GameState, hands: &mut [Vec; 4]) -> i8 { } fn main() { - deal(0, Some(Suit::Hearts)); + //deal(0, Some(Suit::Hearts)); + game(); + return; + let mut played = bitarr!(0; 52); let mut node = new_node(GameState { player: 0,