Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified bin/TennisGame.class
Binary file not shown.
Binary file modified bin/TennisGameTest.class
Binary file not shown.
39 changes: 21 additions & 18 deletions src/TennisGame.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,20 @@ public void player2Scored() throws TennisGameException {

public String getScore() {
// Here is the format of the scores:
// "love - love"
// "15 - 15"
// "30 - 30"
// "deuce"
// "15 - love", "love - 15"
// "30 - love", "love - 30"
// "40 - love", "love - 40"
// "30 - 15", "15 - 30"
// "40 - 15", "15 - 40"
// "player1 has advantage"
// "player2 has advantage"
// "player1 wins"
// "player2 wins"
// "love - love" 0-0
// "15 - 15" 1-1
// "30 - 30" 2-2
// "deuce" 3-3
// "15 - love", "love - 15" 1-0,0-1
// "30 - love", "love - 30" 2-0,0-2
// "40 - love", "love - 40" 3-0,0-3
// "30 - 15", "15 - 30" 2-1,1-2
// "40 - 15", "15 - 40" 3-1,1-3
// "player1 has advantage" 4-3
// "player2 has advantage" 3-4
// "player1 wins" 4-0,4-1,4-2,5-3
// "player2 wins" 0-4,1-4,2-4,3-5

String player1Score = getScore(player1Points);
String player2Score = getScore(player2Points);

Expand All @@ -77,15 +77,18 @@ public String getScore() {
return "player2 wins";
}

if (player1Points >= 4 && player1Points == player2Points)
return "deuce";
// original fails as 40-40 when both have scored 3 points because deuce is already at 3-3)
if (player1Points >= 3 && player1Points == player2Points)
return "deuce";

if (player1Points >= 4 && player1Points - player2Points == 1)
return "player1 has advantage";

if (player2Points > 4 && player2Points - player1Points == 1)
// original was missing =, resulting in a 40-40 score instead of advantage being reported
if (player2Points >= 4 && player2Points - player1Points == 1)
return "player2 has advantage";

return player2Score + " - " + player1Score ;
// testing found that player scores were listed in reverse order
return player1Score + " - " + player2Score ;
}
}
166 changes: 161 additions & 5 deletions tests/TennisGameTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class TennisGameTest {
// "player2 has advantage"
// "player1 wins"
// "player2 wins"
@Ignore
@Test
public void testTennisGame_Start() {
//Arrange
TennisGame game = new TennisGame();
Expand All @@ -31,7 +31,7 @@ public void testTennisGame_Start() {
}

@Test
public void testTennisGame_EahcPlayerWin4Points_Score_Deuce() throws TennisGameException {
public void testTennisGame_EachPlayerWin4Points_Score_Deuce() throws TennisGameException {
//Arrange
TennisGame game = new TennisGame();

Expand All @@ -55,13 +55,169 @@ public void testTennisGame_EahcPlayerWin4Points_Score_Deuce() throws TennisGameE
public void testTennisGame_Player1WinsPointAfterGameEnded_ResultsException() throws TennisGameException {
//Arrange
TennisGame game = new TennisGame();
String score;
//Act
game.player1Scored();
game.player1Scored();
game.player1Scored();
game.player1Scored();


//Act
// This statement should cause an exception
game.player1Scored();
}
// This statement should cause an exception according to teacher. Why? Should't the program have ended already by this time?
game.player1Scored();
score=game.getScore();
System.out.println(score);

//Assert
assertEquals("failure when game has already ended", "player1 wins", score);

}

@Test // tests 3-3 deuce
public void deuce_testi() throws TennisGameException
{
//Arrange
TennisGame game = new TennisGame();
String score;
int i;
for (i=0; i<3; i++)
{
game.player2Scored();
game.player1Scored();
}
//Act

score = game.getScore() ;


// Assert
assertEquals("Deuce fails", "deuce", score);

}

@Test // tests 1-0 15-love - apparently score was reported in the wrong fashion
public void fifteen_love_test() throws TennisGameException
{
//Arrange
TennisGame game = new TennisGame();
String score;
//Act
game.player1Scored();
score = game.getScore() ;


// Assert
assertEquals("fails", "15 - love", score);

}

@Test // tests 1-0 15-love - apparently score was reported in the wrong fashion
public void love_fifteen_test() throws TennisGameException
{
//Arrange
TennisGame game = new TennisGame();
String score;
//Act
game.player2Scored();
score = game.getScore() ;


// Assert
assertEquals("fails", "love - 15", score);
}

// There would normally be different tests for 30-15, 15-30, 30-30 etc, but they hardly seem necessary.
// could be tested in a loop that prints each score as the game progresses.

@Test
public void player1_wins_test() throws TennisGameException
{
//Arrange
TennisGame game = new TennisGame();
String score;
int i;
//Act
for (i=0;i<3;i++)
{
game.player1Scored();

}
game.player2Scored();
game.player1Scored();
score = game.getScore() ;


// Assert
assertEquals("fails", "player1 wins", score);
}



@Test
public void player2_advantage() throws TennisGameException
{
//Arrange
TennisGame game = new TennisGame();
String score;
int i;
//Act
for (i=0;i<3;i++)
{
game.player1Scored();
game.player2Scored();
}
// score is now 40-40 (i.e. 3-3)
game.player2Scored();
score = game.getScore();

// Assert
assertEquals("fails", "player2 has advantage", score);

}

@Test
public void player1_advantage() throws TennisGameException
{
//Arrange
TennisGame game = new TennisGame();
String score;
int i;
//Act
for (i=0;i<3;i++)
{
game.player1Scored();
game.player2Scored();
}
// score is now 40-40 (i.e. 3-3)
game.player1Scored();
score = game.getScore();

// Assert
assertEquals("fails", "player1 has advantage", score);

}

@Test
public void player2_wins_test() throws TennisGameException
{
//Arrange
TennisGame game = new TennisGame();
String score;
int i;
//Act
for (i=0;i<3;i++)
{
game.player2Scored();
}
game.player1Scored();
game.player2Scored();
score = game.getScore() ;

// Assert
assertEquals("fails", "player2 wins", score);

}


}