diff --git a/bin/TennisGame.class b/bin/TennisGame.class index 2da4e7a..e463dc5 100644 Binary files a/bin/TennisGame.class and b/bin/TennisGame.class differ diff --git a/bin/TennisGameTest.class b/bin/TennisGameTest.class index b202db2..9e3a25d 100644 Binary files a/bin/TennisGameTest.class and b/bin/TennisGameTest.class differ diff --git a/src/TennisGame.java b/src/TennisGame.java index 327f284..e890e2d 100644 --- a/src/TennisGame.java +++ b/src/TennisGame.java @@ -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); @@ -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 ; } } \ No newline at end of file diff --git a/tests/TennisGameTest.java b/tests/TennisGameTest.java index 8674eba..5359b0d 100644 --- a/tests/TennisGameTest.java +++ b/tests/TennisGameTest.java @@ -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(); @@ -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(); @@ -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); + + } + + }