forked from emilybache/Tennis-Refactoring-Kata
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
split tennis exercises into separate files
- Loading branch information
1 parent
c01a56c
commit 3accb74
Showing
6 changed files
with
207 additions
and
196 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
Oops, something went wrong.