Skip to content

Commit

Permalink
Implement TennisGame3 and TennisGame2 in rust
Browse files Browse the repository at this point in the history
  • Loading branch information
balp committed Sep 5, 2023
1 parent f196fb3 commit 32df71d
Show file tree
Hide file tree
Showing 5 changed files with 264 additions and 68 deletions.
75 changes: 8 additions & 67 deletions rust/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,72 +1,13 @@
mod tennisgame1;
mod tennisgame2;
mod tennisgame3;

pub use crate::tennisgame1::TennisGame1;
pub use crate::tennisgame2::TennisGame2;
pub use crate::tennisgame3::TennisGame3;

pub trait TennisGame {
fn clear(&mut self);
fn won_point(&mut self, player_name: &str);
fn get_score(&self) -> String;
}

#[derive(Default)]
pub struct TennisGame1 {
score1: u8,
score2: u8,
_player1_name: String,
_player2_name: String,
}
impl TennisGame1 {
pub fn new() -> Self {
TennisGame1::default()
}
}
impl TennisGame for TennisGame1 {
fn clear(&mut self) {
self.score1 = 0;
self.score2 = 0;
}
fn won_point(&mut self, player_name: &str) {
if player_name == "player1" {
self.score1 += 1;
} else {
self.score2 += 1;
}
}
fn get_score(&self) -> String {
match (self.score1, self.score2) {
(a, b) if a == b => match a {
0 => return "Love-All".to_owned(),
1 => return "Fifteen-All".to_owned(),
2 => return "Thirty-All".to_owned(),
_ => return "Deuce".to_owned(),
},
(a, b) if a >= 4 || b >= 4 => {
let minus_result = self.score1 as i8 - self.score2 as i8;
if minus_result == 1 {
return "Advantage player1".to_owned();
} else if minus_result == -1i8 {
return "Advantage player2".to_owned();
} else if minus_result >= 2 {
return "Win for player1".to_owned();
}
"Win for player2".to_owned()
}
_ => {
let mut temp_score: u8;
let mut score = String::new();
for i in 1..3 {
if i == 1 {
temp_score = self.score1;
} else {
score.push_str("-");
temp_score = self.score2;
}
match temp_score {
0 => score.push_str("Love"),
1 => score.push_str("Fifteen"),
2 => score.push_str("Thirty"),
3 => score.push_str("Forty"),
_ => {}
}
}
return score;
}
}
}
}
70 changes: 70 additions & 0 deletions rust/src/tennisgame1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
use crate::TennisGame;

#[derive(Default)]
pub struct TennisGame1 {
score1: u8,
score2: u8,
_player1_name: String,
_player2_name: String,
}

impl TennisGame1 {
pub fn new() -> Self {
TennisGame1::default()
}
}

impl TennisGame for TennisGame1 {
fn clear(&mut self) {
self.score1 = 0;
self.score2 = 0;
}
fn won_point(&mut self, player_name: &str) {
if player_name == "player1" {
self.score1 += 1;
} else {
self.score2 += 1;
}
}
fn get_score(&self) -> String {
match (self.score1, self.score2) {
(a, b) if a == b => match a {
0 => return "Love-All".to_owned(),
1 => return "Fifteen-All".to_owned(),
2 => return "Thirty-All".to_owned(),
_ => return "Deuce".to_owned(),
},
(a, b) if a >= 4 || b >= 4 => {
let minus_result = self.score1 as i8 - self.score2 as i8;
if minus_result == 1 {
return "Advantage player1".to_owned();
} else if minus_result == -1i8 {
return "Advantage player2".to_owned();
} else if minus_result >= 2 {
return "Win for player1".to_owned();
}
"Win for player2".to_owned()
}
_ => {
let mut temp_score: u8;
let mut score = String::new();
for i in 1..3 {
if i == 1 {
temp_score = self.score1;
} else {
score.push_str("-");
temp_score = self.score2;
}
match temp_score {
0 => score.push_str("Love"),
1 => score.push_str("Fifteen"),
2 => score.push_str("Thirty"),
3 => score.push_str("Forty"),
_ => {}
}
}
return score;
}
}
}
}
113 changes: 113 additions & 0 deletions rust/src/tennisgame2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
use crate::TennisGame;

#[derive(Default)]
pub struct TennisGame2 {
p1points: u8,
p2points: u8,
_player1_name: String,
_player2_name: String,
}

impl TennisGame2 {
pub fn new() -> Self {
Self {
p1points: 0,
p2points: 0,
_player1_name: "player1".to_string(),
_player2_name: "player2".to_string(),
}
}
fn P1Score(&mut self) {
self.p1points += 1;
}
fn P2Score(&mut self) {
self.p2points += 1;
}
}

impl TennisGame for TennisGame2 {
fn clear(&mut self) {
self.p1points = 0;
self.p2points = 0;
}
fn won_point(&mut self, player_name: &str) {
if player_name == self._player1_name {
self.P1Score();
} else {
self.P2Score();
}
}
fn get_score(&self) -> String {
let mut result = "".to_string();
if (self.p1points == self.p2points) && (self.p1points < 3) {
let s = match self.p1points {
0 => "Love",
1 => "Fifteen",
2 => "Thirty",
_ => "",
};
result = s.to_string() + "-All";
}
if (self.p1points == self.p2points) && (self.p1points > 2) {
result = "Deuce".to_string()
}
if (self.p1points > 0) && (self.p2points == 0) {
let s = match self.p1points {
1 => "Fifteen",
2 => "Thirty",
3 => "Forty",
_ => "",
};
result = s.to_string() + "-Love"
} else if (self.p2points > 0) && (self.p1points == 0) {
let s = match self.p2points {
1 => "Fifteen",
2 => "Thirty",
3 => "Forty",
_ => "",
};
result = "Love-".to_string() + s
} else if (self.p1points > self.p2points) && (self.p1points < 4) {
let p1res = match self.p1points {
2 => "Thirty",
3 => "Forty",
_ => "",
};
let p2res = match self.p2points {
1 => "Fifteen",
2 => "Thirty",
_ => "",
};
result = p1res.to_string() + "-" + p2res
} else if (self.p2points > self.p1points) && (self.p2points < 4) {
let p1res = match self.p1points {
1 => "Fifteen",
2 => "Thirty",
_ => "",
};
let p2res = match self.p2points {
2 => "Thirty",
3 => "Forty",
_ => "",
};
result = p1res.to_string() + "-" + p2res
}
if (self.p1points > self.p2points) && (self.p2points >= 3) {
result = "Advantage player1".to_string()
}
if (self.p2points > self.p1points) && (self.p1points >= 3) {
result = "Advantage player2".to_string()
}
if (self.p1points >= 4 && self.p2points >= 0)
&& (self.p1points as i16 - self.p2points as i16) >= 2
{
result = "Win for player1".to_string()
}
if (self.p2points >= 4 && self.p1points >= 0)
&& (self.p2points as i16 - self.p1points as i16) >= 2
{
result = "Win for player2".to_string()
}
return result;
}
}
60 changes: 60 additions & 0 deletions rust/src/tennisgame3.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
use crate::TennisGame;

#[derive(Default)]
pub struct TennisGame3 {
p1: u8,
p2: u8,
p1n: String,
p2n: String,
}

impl TennisGame3 {
pub fn new() -> Self {
Self {
p1: 0,
p2: 0,
p1n: "player1".to_string(),
p2n: "player2".to_string(),
}
}
}

impl TennisGame for TennisGame3 {
fn clear(&mut self) {
self.p1 = 0;
self.p2 = 0;
}
fn won_point(&mut self, player_name: &str) {
if player_name == "player1" {
self.p1 += 1;
} else {
self.p2 += 1;
}
}
fn get_score(&self) -> String {
if (self.p1 < 4 && self.p2 < 4) && (self.p1 + self.p2 < 6) {
let p = ["Love", "Fifteen", "Thirty", "Forty"];
let s = p[self.p1 as usize];
return s.to_string()
+ "-"
+ if self.p1 == self.p2 {
"All"
} else {
p[self.p2 as usize]
};
} else {
if self.p1 == self.p2 {
return "Deuce".to_string();
}
let s = if self.p1 > self.p2 {
self.p1n.clone()
} else {
self.p2n.clone()
};
if (self.p1 as i16 - self.p2 as i16) * (self.p1 as i16 - self.p2 as i16) == 1 {
return "Advantage ".to_string() + s.as_str();
}
return "Win for ".to_string() + s.as_str();
}
}
}
14 changes: 13 additions & 1 deletion rust/tests/unit.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
mod tests {
use rust::{TennisGame, TennisGame1};
use rust::{TennisGame, TennisGame1, TennisGame2, TennisGame3};

fn all_scores() -> Vec<(u8, u8, &'static str)> {
vec![
Expand Down Expand Up @@ -60,4 +60,16 @@ mod tests {
let mut game = TennisGame1::new();
run(&mut game);
}

#[test]
fn test_game2() {
let mut game = TennisGame2::new();
run(&mut game);
}

#[test]
fn test_game3() {
let mut game = TennisGame3::new();
run(&mut game);
}
}

0 comments on commit 32df71d

Please sign in to comment.