Skip to content

Commit

Permalink
add tennis7 in cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
emilybache committed Aug 16, 2024
1 parent cafa8e0 commit 99a8057
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 1 deletion.
8 changes: 7 additions & 1 deletion cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,10 @@ add_executable(tennis6_google_test
tennis6.cc
gtest_all.cpp)
target_link_libraries(tennis6_google_test GTest::gtest_main)
gtest_discover_tests(tennis6_google_test)
gtest_discover_tests(tennis6_google_test)

add_executable(tennis7_google_test
tennis7.cc
gtest_all.cpp)
target_link_libraries(tennis7_google_test GTest::gtest_main)
gtest_discover_tests(tennis7_google_test)
73 changes: 73 additions & 0 deletions cpp/tennis7.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#include <string>

std::string tennis_score(int player1Score, int player2Score)
{
std::string result = "";
std::string player1Name = "player1";
std::string player2Name = "player2";

if (player1Score == player2Score)
{
// tie score
switch (player1Score)
{
case 0: result += "Love-All"; break;
case 1: result += "Fifteen-All"; break;
case 2: result += "Thirty-All"; break;
default: result += "Deuce"; break;
}
}
else if (player1Score >= 4 || player2Score >= 4)
{
// end-game score
if (player1Score - player2Score == 1)
{
result += "Advantage " + player1Name;
}
else if (player1Score - player2Score == -1)
{
result += "Advantage " + player2Name;
}
else if (player1Score - player2Score >= 2)
{
result += "Win for " + player1Name;
}
else
{
result += "Win for " + player2Name;
}
} else {
// regular score
switch (player1Score) {
case 0:
result += "Love";
break;
case 1:
result += "Fifteen";
break;
case 2:
result += "Thirty";
break;
default:
result += "Forty";
break;
}
result += "-";
switch (player2Score) {
case 0:
result += "Love";
break;
case 1:
result += "Fifteen";
break;
case 2:
result += "Thirty";
break;
default:
result += "Forty";
break;
}
}

return result;
}

0 comments on commit 99a8057

Please sign in to comment.