You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
davidrobert99 941955e507 small corrections and fix typos 2 years ago
..
README.md small corrections and fix typos 2 years ago

README.md

borrow box

Instructions

In this exercise, a CRUD functionality will have to be created. Therefore the following functions will have to be defined :

  • new, which receives two players and initializes them with a name and a score. This function should return the structure wrapped in a Box.

  • read_winner, which returns a tuple with the name and the score of the player who is winning. In case none of the players are winning, it should return the same tuple with the string "Same score! tied" and the tied score.

  • update_score, which receives the name of a player. This function should increment the score of the player. The score should only be increased if it does not pass the nbr_of_games. Example: if the nbr_of_games is 3 then the winner of the game should be the one who is the best out of three games. So if one play has 2 wins then he is the winner and the function should not increase the score anymore for either player.

  • delete, which takes the ownership of the boxed game and returns a string: "game deleted: id -> 0".

Notions

Expected Functions

#[derive(Debug, Clone, Eq, PartialEq)]
pub struct Game {
    pub id: u32,
    pub p1: (String, u16),
    pub p2: (String, u16),
    pub nbr_of_games: u16
}

impl Game {
    pub fn new(i: u32, pl1: String, pl2: String, n: u16) -> Box<Game> {

    }
    pub fn read_winner(&self) -> (String, u16) {

    }
    pub fn update_score(&mut self, user_name: String) {

    }
    pub fn delete(self) -> String {

    }
}

Usage

Here is a program to test your functions,

use borrow_box::*;

fn main() {
    let mut game = Game::new(0, String::from("Joao"), String::from("Susana"), 5);
    println!("{:?}", game.read_winner());
    // output : ("Same score! tied", 0)

    game.update_score(String::from("Joao"));
    game.update_score(String::from("Joao"));
    game.update_score(String::from("Susana"));
    game.update_score(String::from("Susana"));
    println!("{:?}", game.read_winner());
    // output : ("Same score! tied", 2)

    game.update_score(String::from("Joao"));
    // this one will not count because it already 5 games played, the nbr_of_games
    game.update_score(String::from("Susana"));

    println!("{:?}", game.read_winner());
    // output : ("Joao", 3)

    println!("{:?}", game.delete());
    // output : "game deleted: id -> 0"

    // game.read_winner();
    // this will give error
    // because the game was dropped, no longer exists on the heap
}

And its output:

$ cargo run
("Same score! tied", 0)
("Same score! tied", 2)
("Joao", 3)
"game deleted: id -> 0"
$