forked from emilybache/Tennis-Refactoring-Kata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTennisGame1.c
92 lines (86 loc) · 2.29 KB
/
TennisGame1.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <stdlib.h>
#include <string.h>
#include "TennisGame.h"
struct TennisGame
{
int m_score1;
int m_score2;
const char* player1Name;
const char* player2Name;
char score[18];
};
struct TennisGame* TennisGame_Create(const char* player1Name, const char* player2Name)
{
struct TennisGame* newGame = malloc(sizeof(struct TennisGame));
newGame->m_score1 = 0;
newGame->m_score2 = 0;
newGame->player1Name = player1Name;
newGame->player2Name = player2Name;
return newGame;
}
void TennisGame_WonPoint(struct TennisGame* game, const char* playerName)
{
if (strcmp(playerName, "player1") == 0)
game->m_score1 += 1;
else
game->m_score2 += 1;
}
const char* TennisGame_GetScore(struct TennisGame* game)
{
game->score[0] = '\0';
int tempScore = 0;
if (game->m_score1 == game->m_score2)
{
switch (game->m_score1)
{
case 0:
strcpy(game->score, "Love-All");
break;
case 1:
strcpy(game->score, "Fifteen-All");
break;
case 2:
strcpy(game->score, "Thirty-All");
break;
default:
strcpy(game->score, "Deuce");
break;
}
}
else if (game->m_score1 >= 4 || game->m_score2 >= 4)
{
int minusResult = game->m_score1 - game->m_score2;
if (minusResult == 1)
strcpy(game->score, "Advantage player1");
else if (minusResult == -1)
strcpy(game->score, "Advantage player2");
else if (minusResult >= 2)
strcpy(game->score, "Win for player1");
else
strcpy(game->score, "Win for player2");
}
else
{
for (int i = 1; i < 3; i++)
{
if (i == 1) tempScore = game->m_score1;
else { strcat(game->score, "-"); tempScore = game->m_score2; }
switch (tempScore)
{
case 0:
strcat(game->score, "Love");
break;
case 1:
strcat(game->score, "Fifteen");
break;
case 2:
strcat(game->score, "Thirty");
break;
case 3:
strcat(game->score, "Forty");
break;
}
}
}
return game->score;
}