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
5 changes: 5 additions & 0 deletions bin/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/TennisGame.class
/TennisGameException.class
/TennisGameTest.class
/TennisGameTest2.class
/AllTests.class
Binary file modified bin/TennisGame.class
Binary file not shown.
Binary file removed bin/TennisGameTest.class
Binary file not shown.
87 changes: 47 additions & 40 deletions src/TennisGame.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,56 +2,67 @@
// NOTE THAT it may contain bugs
// Write unit tests in TennisGameTest.java and try to find the errors in the code

public class TennisGame {
public class TennisGame
{
private int player1Points;
private int player2Points;

private boolean gameEnded;

public TennisGame() {
public TennisGame()
{
player1Points = 0;
player2Points = 0;
gameEnded = false ;
}

private void checkGameEnded() {
private void checkGameEnded()
{
if (player1Points>=4 && player1Points-player2Points>=2)
gameEnded = true;
else if (player2Points>=4 && player2Points-player1Points>=2)
gameEnded = true;
}

private String getScore(int points) {
switch (points) {
case 0: return "love";
case 1: return "15" ;
case 2: return "30" ;
case 3: return "40";
default: return "40" ;

private String getAScore(int points)
{
switch (points)
{
case 0: return "love";
case 1: return "15" ;
case 2: return "30" ;
case 3: return "40";
default: return "40" ;
}
}

public void player1Scored() throws TennisGameException {
if (gameEnded) {
public void player1Scored() throws TennisGameException
{
if (gameEnded)
{
throw new TennisGameException();
}
else {
else
{
player1Points++;
checkGameEnded();
}
}

public void player2Scored() throws TennisGameException {
if (gameEnded) {
public void player2Scored() throws TennisGameException
{
if (gameEnded)
{
throw new TennisGameException();
}
else {
else
{
player2Points++;
checkGameEnded();
}
}

public String getScore() {
public String getScore()
{
// Here is the format of the scores:
// "love - love"
// "15 - 15"
Expand All @@ -66,26 +77,22 @@ public String getScore() {
// "player2 has advantage"
// "player1 wins"
// "player2 wins"

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

if (gameEnded) {
if (player1Points > player2Points)
return "player1 wins";
else
return "player2 wins";
}

if (player1Points >= 4 && player1Points == player2Points)
return "deuce";

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

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

return player2Score + " - " + player1Score ;
String player1Score = getAScore(player1Points);
String player2Score = getAScore(player2Points);
if (gameEnded)
{
if (player1Points > player2Points)
return "player1 wins";
else
return "player2 wins";
}
if (player1Points >= 3 && player1Points == player2Points)
return "deuce";
else if (player1Points >= 4 && player1Points - player2Points == 1)
return "player1 has advantage";
else if (player2Points >= 4 && player2Points - player1Points == 1)
return "player2 has advantage";
return player1Score + " - " + player2Score ;
//return player2Score + " - " + player1Score ;
}
}
3 changes: 2 additions & 1 deletion src/TennisGameException.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

public class TennisGameException extends Exception {
public class TennisGameException extends Exception
{

}
67 changes: 0 additions & 67 deletions tests/TennisGameTest.java

This file was deleted.

Loading