Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use rand::Rng;
- use std::borrow::Cow;
- use uuid::Uuid;
- const ROUNDS: usize = 9;
- const GAMES_PER_ROUND: usize = 5;
- /*
- 1 тур 1 - (10) 2 - 9 3 - 8 4 - 7 5 - 6
- 2 тур (10) - 6 7 - 5 8 - 4 9 - 3 1 - 2
- 3 тур 2 - (10) 3 - 1 4 - 9 5 - 8 6 - 7
- 4 тур (10) - 7 8 - 6 9 - 5 1 - 4 2 - 3
- 5 тур 3 - (10) 4 - 2 5 - 1 6 - 9 7 - 8
- 6 тур (10) - 8 9 - 7 1 - 6 2 - 5 3 - 4
- 7 тур 4 - (10) 5 - 3 6 - 2 7 - 1 8 - 9
- 8 тур (10) - 9 1 - 8 2 - 7 3 - 6 4 - 5
- 9 тур 5 - (10) 6 - 4 7 - 3 8 - 2 9 - 1
- */
- const BRACKETS: [[(usize, usize); GAMES_PER_ROUND]; ROUNDS] = [
- [(0, 9), (1, 8), (2, 7), (3, 6), (4, 5)],
- [(9, 5), (6, 4), (7, 3), (8, 2), (0, 1)],
- [(1, 9), (2, 0), (3, 8), (4, 7), (5, 6)],
- [(9, 6), (7, 5), (8, 4), (0, 3), (1, 2)],
- [(2, 9), (3, 1), (4, 0), (5, 8), (6, 7)],
- [(9, 7), (8, 6), (0, 5), (1, 4), (2, 3)],
- [(3, 9), (4, 2), (5, 1), (6, 0), (7, 8)],
- [(9, 8), (0, 7), (1, 6), (2, 5), (3, 4)],
- [(4, 9), (5, 3), (6, 2), (7, 1), (8, 0)],
- ];
- #[derive(Debug, Clone)]
- struct Team<'a> {
- id: Uuid,
- name: Cow<'a, str>,
- game_list : Vec<Game<'a>>,
- }
- impl<'a> Team<'a> {
- fn new<S>(name: S) -> Self
- where
- S: Into<Cow<'a, str>>,
- {
- let id = Uuid::new_v4();
- Team {
- id,
- name : name.into(),
- game_list : Vec::new(),
- }
- }
- fn add_game(&mut self, game : &Game<'a>) {
- self.game_list.push(game.to_owned());
- }
- }
- #[derive(Clone, Debug)]
- struct Teams<'a> {
- teams: Vec<Team<'a>>,
- }
- impl<'a> Teams<'a> {
- fn new(teams: &[Team<'a>]) -> Self {
- Teams {
- teams: teams.to_vec(),
- }
- }
- fn print(self) {
- println!("{:#?}", self.teams);
- }
- }
- #[derive(Debug, Clone)]
- struct Game<'a> {
- id: Uuid,
- team_home_id: &'a Uuid,
- team_away_id: &'a Uuid,
- team_home_score: u8,
- team_away_score: u8,
- }
- impl<'a> Game<'a> {
- fn new(team_home_id: &'a Uuid, team_away_id: &'a Uuid) -> Self {
- let id = Uuid::new_v4();
- let (team_home_score, team_away_score) = generate_score();
- Game {
- id,
- team_home_id,
- team_away_id,
- team_home_score,
- team_away_score,
- }
- }
- }
- fn generate_score() -> (u8, u8) {
- let mut rng = rand::thread_rng();
- let team_home_score = rng.gen_range(0u8, 10);
- let team_away_score = rng.gen_range(0u8, 10);
- (team_home_score, team_away_score)
- }
- fn round<'a>(teams : &'a mut Teams<'a>, round_id : usize) {
- let round_value = BRACKETS[round_id];
- for (t1, t2) in round_value.to_vec() {
- let team1 = &teams.teams[t1].id;
- let team2 = &teams.teams[t2].id;
- let game = Game::new(team1, team2);
- teams.teams[t1].add_game(&game);
- teams.teams[t2].add_game(&game);
- }
- }
- fn main() {
- let team1 = Team::new("team1");
- let team2 = Team::new("team2");
- let team3 = Team::new("team3");
- let team4 = Team::new("team4");
- let team5 = Team::new("team5");
- let team6 = Team::new("team6");
- let team7 = Team::new("team7");
- let team8 = Team::new("team8");
- let team9 = Team::new("team9");
- let team10 = Team::new("team10");
- let mut teams = Teams::new(&[
- team1, team2, team3, team4, team5, team6, team7, team8, team9, team10,
- ]);
- round(&mut teams, 3);
- teams.print();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement