Created
November 30, 2012 18:03
-
-
Save basak/4177408 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#!/usr/bin/python3 | |
STARTING_SCORE = ('0', '0') | |
# key: previous score; value: new score if player 0 scores a point from that | |
POINT_LOOKUP = { | |
('0', '0'): ('15', '0'), | |
('0', '15'): ('15', '15'), | |
('0', '30'): ('15', '30'), | |
('0', '40'): ('15', '40'), | |
('15', '0'): ('30', '0'), | |
('15', '15'): ('30', '15'), | |
('15', '30'): ('30', '30'), | |
('15', '40'): ('30', '40'), | |
('30', '0'): ('40', '0'), | |
('30', '15'): ('40', '15'), | |
('30', '30'): ('40', '30'), | |
('30', '40'): ('40', '40'), | |
('40', '0'): ('WON', '0'), | |
('40', '15'): ('WON', '15'), | |
('40', '30'): ('WON', '30'), | |
('40', '40'): ('ADV', '40'), | |
('40', 'ADV'): ('40', '40'), | |
('ADV', '40'): ('WON', '40'), | |
} | |
def score_after_point(old_score, player_who_scored): | |
if player_who_scored == 0: | |
return POINT_LOOKUP[old_score] | |
elif player_who_scored == 1: | |
reversed_old_score = tuple(reversed(old_score)) | |
reversed_new_score = POINT_LOOKUP[reversed_old_score] | |
new_score = tuple(reversed(reversed_new_score)) | |
return new_score | |
def human_score_name(score): | |
if score == ('40', '40'): | |
return 'DEUCE' | |
elif score[0] == 'WON': | |
return 'PLAYER 0 WINS' | |
elif score[1] == 'WON': | |
return 'PLAYER 1 WINS' | |
else: | |
return '%s-%s' % score | |
def game_finished(score): | |
return 'WON' in score | |
def test_player0_scored(): | |
assert score_after_point(('0', '0'), 0) == ('15', '0') | |
def test_player1_scored(): | |
assert score_after_point(('0', '0'), 1) == ('0', '15') | |
def test_deuce(): | |
assert human_score_name(('40', '40')) == 'DEUCE' | |
def test_normal_score(): | |
assert human_score_name(('15', '15')) == '15-15' | |
def test_player0_won(): | |
assert human_score_name(('WON', '0')) == 'PLAYER 0 WINS' | |
def test_player1_won(): | |
assert human_score_name(('0', 'WON')) == 'PLAYER 1 WINS' | |
def test_not_finished(): | |
assert not game_finished(('0', '0')) | |
def test_player0_finished(): | |
assert game_finished(('WON', '0')) | |
def test_player1_finished(): | |
assert game_finished(('0', 'WON')) | |
if __name__ == '__main__': | |
score = STARTING_SCORE | |
while not game_finished(score): | |
print("Score: %s" % human_score_name(score)) | |
scoring_player_str = input("Which player scored a point (0/1)? ") | |
if scoring_player_str not in ['0', '1']: | |
print("Player must be 0 or 1") | |
continue | |
score = score_after_point(score, int(scoring_player_str)) | |
print(human_score_name(score)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment