Skip to content

Commit

Permalink
split tennis exercises into separate files
Browse files Browse the repository at this point in the history
  • Loading branch information
emilybache committed Jan 30, 2023
1 parent c01a56c commit 3accb74
Show file tree
Hide file tree
Showing 6 changed files with 207 additions and 196 deletions.
169 changes: 0 additions & 169 deletions python/tennis.py
Original file line number Diff line number Diff line change
@@ -1,172 +1,3 @@
# -*- coding: utf-8 -*-

class TennisGame1:

def __init__(self, player1Name, player2Name):
self.player1Name = player1Name
self.player2Name = player2Name
self.p1points = 0
self.p2points = 0

def won_point(self, playerName):
if playerName == self.player1Name:
self.p1points += 1
else:
self.p2points += 1

def score(self):
result = ""
tempScore=0
if (self.p1points==self.p2points):
result = {
0 : "Love-All",
1 : "Fifteen-All",
2 : "Thirty-All",
}.get(self.p1points, "Deuce")
elif (self.p1points>=4 or self.p2points>=4):
minusResult = self.p1points-self.p2points
if (minusResult==1):
result ="Advantage " + self.player1Name
elif (minusResult ==-1):
result ="Advantage " + self.player2Name
elif (minusResult>=2):
result = "Win for " + self.player1Name
else:
result ="Win for " + self.player2Name
else:
for i in range(1,3):
if (i==1):
tempScore = self.p1points
else:
result+="-"
tempScore = self.p2points
result += {
0 : "Love",
1 : "Fifteen",
2 : "Thirty",
3 : "Forty",
}[tempScore]
return result


class TennisGame2:
def __init__(self, player1Name, player2Name):
self.player1Name = player1Name
self.player2Name = player2Name
self.p1points = 0
self.p2points = 0

def won_point(self, playerName):
if playerName == self.player1Name:
self.P1Score()
else:
self.P2Score()

def score(self):
result = ""
if (self.p1points == self.p2points and self.p1points < 3):
if (self.p1points==0):
result = "Love"
if (self.p1points==1):
result = "Fifteen"
if (self.p1points==2):
result = "Thirty"
result += "-All"
if (self.p1points==self.p2points and self.p1points>2):
result = "Deuce"

P1res = ""
P2res = ""
if (self.p1points > 0 and self.p2points==0):
if (self.p1points==1):
P1res = "Fifteen"
if (self.p1points==2):
P1res = "Thirty"
if (self.p1points==3):
P1res = "Forty"

P2res = "Love"
result = P1res + "-" + P2res
if (self.p2points > 0 and self.p1points==0):
if (self.p2points==1):
P2res = "Fifteen"
if (self.p2points==2):
P2res = "Thirty"
if (self.p2points==3):
P2res = "Forty"

P1res = "Love"
result = P1res + "-" + P2res


if (self.p1points>self.p2points and self.p1points < 4):
if (self.p1points==2):
P1res="Thirty"
if (self.p1points==3):
P1res="Forty"
if (self.p2points==1):
P2res="Fifteen"
if (self.p2points==2):
P2res="Thirty"
result = P1res + "-" + P2res
if (self.p2points>self.p1points and self.p2points < 4):
if (self.p2points==2):
P2res="Thirty"
if (self.p2points==3):
P2res="Forty"
if (self.p1points==1):
P1res="Fifteen"
if (self.p1points==2):
P1res="Thirty"
result = P1res + "-" + P2res

if (self.p1points > self.p2points and self.p2points >= 3):
result = "Advantage " + self.player1Name

if (self.p2points > self.p1points and self.p1points >= 3):
result = "Advantage " + self.player2Name

if (self.p1points>=4 and self.p2points>=0 and (self.p1points-self.p2points)>=2):
result = "Win for " + self.player1Name
if (self.p2points>=4 and self.p1points>=0 and (self.p2points-self.p1points)>=2):
result = "Win for " + self.player2Name
return result

def SetP1Score(self, number):
for i in range(number):
self.P1Score()

def SetP2Score(self, number):
for i in range(number):
self.P2Score()

def P1Score(self):
self.p1points +=1


def P2Score(self):
self.p2points +=1

class TennisGame3:
def __init__(self, player1Name, player2Name):
self.p1N = player1Name
self.p2N = player2Name
self.p1 = 0
self.p2 = 0

def won_point(self, n):
if n == self.p1N:
self.p1 += 1
else:
self.p2 += 1

def score(self):
if (self.p1 < 4 and self.p2 < 4) and (self.p1 + self.p2 < 6):
p = ["Love", "Fifteen", "Thirty", "Forty"]
s = p[self.p1]
return s + "-All" if (self.p1 == self.p2) else s + "-" + p[self.p2]
else:
if (self.p1 == self.p2):
return "Deuce"
s = self.p1N if self.p1 > self.p2 else self.p2N
return "Advantage " + s if ((self.p1-self.p2)*(self.p1-self.p2) == 1) else "Win for " + s
49 changes: 49 additions & 0 deletions python/tennis1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# -*- coding: utf-8 -*-

class TennisGame1:

def __init__(self, player1Name, player2Name):
self.player1Name = player1Name
self.player2Name = player2Name
self.p1points = 0
self.p2points = 0

def won_point(self, playerName):
if playerName == self.player1Name:
self.p1points += 1
else:
self.p2points += 1

def score(self):
result = ""
tempScore=0
if (self.p1points==self.p2points):
result = {
0 : "Love-All",
1 : "Fifteen-All",
2 : "Thirty-All",
}.get(self.p1points, "Deuce")
elif (self.p1points>=4 or self.p2points>=4):
minusResult = self.p1points-self.p2points
if (minusResult==1):
result ="Advantage " + self.player1Name
elif (minusResult ==-1):
result ="Advantage " + self.player2Name
elif (minusResult>=2):
result = "Win for " + self.player1Name
else:
result ="Win for " + self.player2Name
else:
for i in range(1,3):
if (i==1):
tempScore = self.p1points
else:
result+="-"
tempScore = self.p2points
result += {
0 : "Love",
1 : "Fifteen",
2 : "Thirty",
3 : "Forty",
}[tempScore]
return result
99 changes: 99 additions & 0 deletions python/tennis2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
# -*- coding: utf-8 -*-

class TennisGame2:
def __init__(self, player1Name, player2Name):
self.player1Name = player1Name
self.player2Name = player2Name
self.p1points = 0
self.p2points = 0

def won_point(self, playerName):
if playerName == self.player1Name:
self.P1Score()
else:
self.P2Score()

def score(self):
result = ""
if (self.p1points == self.p2points and self.p1points < 3):
if (self.p1points==0):
result = "Love"
if (self.p1points==1):
result = "Fifteen"
if (self.p1points==2):
result = "Thirty"
result += "-All"
if (self.p1points==self.p2points and self.p1points>2):
result = "Deuce"

P1res = ""
P2res = ""
if (self.p1points > 0 and self.p2points==0):
if (self.p1points==1):
P1res = "Fifteen"
if (self.p1points==2):
P1res = "Thirty"
if (self.p1points==3):
P1res = "Forty"

P2res = "Love"
result = P1res + "-" + P2res
if (self.p2points > 0 and self.p1points==0):
if (self.p2points==1):
P2res = "Fifteen"
if (self.p2points==2):
P2res = "Thirty"
if (self.p2points==3):
P2res = "Forty"

P1res = "Love"
result = P1res + "-" + P2res


if (self.p1points>self.p2points and self.p1points < 4):
if (self.p1points==2):
P1res="Thirty"
if (self.p1points==3):
P1res="Forty"
if (self.p2points==1):
P2res="Fifteen"
if (self.p2points==2):
P2res="Thirty"
result = P1res + "-" + P2res
if (self.p2points>self.p1points and self.p2points < 4):
if (self.p2points==2):
P2res="Thirty"
if (self.p2points==3):
P2res="Forty"
if (self.p1points==1):
P1res="Fifteen"
if (self.p1points==2):
P1res="Thirty"
result = P1res + "-" + P2res

if (self.p1points > self.p2points and self.p2points >= 3):
result = "Advantage " + self.player1Name

if (self.p2points > self.p1points and self.p1points >= 3):
result = "Advantage " + self.player2Name

if (self.p1points>=4 and self.p2points>=0 and (self.p1points-self.p2points)>=2):
result = "Win for " + self.player1Name
if (self.p2points>=4 and self.p1points>=0 and (self.p2points-self.p1points)>=2):
result = "Win for " + self.player2Name
return result

def SetP1Score(self, number):
for i in range(number):
self.P1Score()

def SetP2Score(self, number):
for i in range(number):
self.P2Score()

def P1Score(self):
self.p1points +=1


def P2Score(self):
self.p2points +=1
25 changes: 25 additions & 0 deletions python/tennis3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# -*- coding: utf-8 -*-

class TennisGame3:
def __init__(self, player1Name, player2Name):
self.p1N = player1Name
self.p2N = player2Name
self.p1 = 0
self.p2 = 0

def won_point(self, n):
if n == self.p1N:
self.p1 += 1
else:
self.p2 += 1

def score(self):
if (self.p1 < 4 and self.p2 < 4) and (self.p1 + self.p2 < 6):
p = ["Love", "Fifteen", "Thirty", "Forty"]
s = p[self.p1]
return s + "-All" if (self.p1 == self.p2) else s + "-" + p[self.p2]
else:
if (self.p1 == self.p2):
return "Deuce"
s = self.p1N if self.p1 > self.p2 else self.p2N
return "Advantage " + s if ((self.p1-self.p2)*(self.p1-self.p2) == 1) else "Win for " + s
31 changes: 17 additions & 14 deletions python/tennis_test.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
# -*- coding: utf-8 -*-

import pytest
from tennis import TennisGame1, TennisGame2, TennisGame3
from tennis3 import TennisGame3
from tennis2 import TennisGame2
from tennis1 import TennisGame1

from tennis_unittest import test_cases, play_game

class TestTennis:

@pytest.mark.parametrize('p1Points p2Points score p1Name p2Name'.split(), test_cases)
def test_get_score_game1(self, p1Points, p2Points, score, p1Name, p2Name):
game = play_game(TennisGame1, p1Points, p2Points, p1Name, p2Name)
assert score == game.score()
@pytest.mark.parametrize('p1Points p2Points score p1Name p2Name'.split(), test_cases)
def test_get_score_game1(p1Points, p2Points, score, p1Name, p2Name):
game = play_game(TennisGame1, p1Points, p2Points, p1Name, p2Name)
assert score == game.score()

@pytest.mark.parametrize('p1Points p2Points score p1Name p2Name'.split(), test_cases)
def test_get_score_game2(self, p1Points, p2Points, score, p1Name, p2Name):
game = play_game(TennisGame2, p1Points, p2Points, p1Name, p2Name)
assert score == game.score()

@pytest.mark.parametrize('p1Points p2Points score p1Name p2Name'.split(), test_cases)
def test_get_score_game3(self, p1Points, p2Points, score, p1Name, p2Name):
game = play_game(TennisGame3, p1Points, p2Points, p1Name, p2Name)
assert score == game.score()
@pytest.mark.parametrize('p1Points p2Points score p1Name p2Name'.split(), test_cases)
def test_get_score_game2(p1Points, p2Points, score, p1Name, p2Name):
game = play_game(TennisGame2, p1Points, p2Points, p1Name, p2Name)
assert score == game.score()


@pytest.mark.parametrize('p1Points p2Points score p1Name p2Name'.split(), test_cases)
def test_get_score_game3(p1Points, p2Points, score, p1Name, p2Name):
game = play_game(TennisGame3, p1Points, p2Points, p1Name, p2Name)
assert score == game.score()
Loading

0 comments on commit 3accb74

Please sign in to comment.