I wrote a program that simulates a tennisgame. I made the points count from 0-4, where getting 4 points wins you the game, I did this just as a temporary solution. Now that I am pretty much finished I can't figure out how to actually change the points to count 0, 15, 30, 40.
Here is my class:
class Players: #define class players
ratio = 0
def __init__(self, name, winningProb, wonGames, playedGames):
self.name = name
self.winningProb = winningProb
self.wonGames = wonGames
self.playedGames = playedGames
if playedGames == 0:
self.ratio = 0
else:
self.ratio = self.wonGames/self.playedGames
self.score = 0
def showPlayerInfo(self):
print(self.name, self.wonGames, self.playedGames, self.winningProb)
def playerInfo(self, position):
return [str(position), self.name, str(self.wonGames), str(self.playedGames), str(self.winningProb)]
def getWinningProb(self):
return self.winningProb
def getScore(self):
return self.score
def setScore(self, score):
self.score = score
def getName(self):
return self.name
def getWonGames(self):
return self.wonGames
def getPlayedGames(self):
return self.playedGames
Function that defines a game:
def playGame(players, displayBoardPerBall, displayBoardPerGame, flag, pauseAfterBalls):
ballCounter = 0
while True:
if flag == 1:
flag = 2
if isWonTheBall(players[0]):
players[0].setScore(players[0].getScore() + 1)
else:
players[1].setScore(players[1].getScore() + 1)
elif flag == 2:
flag == 1
if isWonTheBall(players[1]):
players[1].setScore(players[1].getScore() + 1)
else:
players[0].setScore(players[0].getScore() + 1)
ballCounter += 1
if displayBoardPerBall:
print("\nBall scores until now")
print(players[0].name, players[0].getScore())
print(players[1].name, players[1].getScore())
print()
if pauseAfterBalls == ballCounter:
ballCounter = 0
nothing = input("Game Paused. Enter Any Letter To Continue.")
#whoWon stores 0 if nobody won
whoWon = checkGameWinner(players)
if whoWon!=0:
print("\n\nPlayer",whoWon+1,"won this game.")
players[0].setScore(0)
players[1].setScore(0)
return whoWon
if players[0].getScore() == 3 and players[1].getScore() == 3:
print("\nDEUCE!!\n")
Not sure if this function is helpful, but it just checks who won the game:
def checkGameWinner(players):
if players[0].getScore() == 4 and players[1].getScore() <= 2:
return 1
if players[1].getScore() == 4 and players[0].getScore() <= 2:
return 2
if players[1].getScore() > 2 and players[0].getScore() > 2:
if players[1].getScore() >= players[0].getScore()+2:
return 2
elif players[0].getScore() >= players[1].getScore()+2:
return 1
return 0
Any help is appreciated! I tried by figuring out an equation so that it would count 0,15,30,40 but could not figure out an equation that would work.
If you more of the code, perhaps the main-function, just comment and I will post it.