-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tennis.php
96 lines (85 loc) · 2.59 KB
/
Tennis.php
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
93
94
95
96
<?php
/*Tennis.php file created by Anupam Khosla*/
function computeGameState(string $nameP1, string $nameP2, array $wins) {
//check for input first. If wrong input then throw error.
//array can only have nameP1 or nameP2
foreach($wins as $value) {
if( ($value != $nameP1) && ($value != $nameP2) ) {
die("Wrong input parameter $wins. It must have values which are either player1's name or player2's name");
}
}
$scoreP1 = 0; //initial score
$scoreP2 = 0;
$state = NULL; //state to be calculated after
function score_incrm($nameP, $nameP1, $nameP2, &$scoreP1, &$scoreP2){ //every call will set score
if($nameP == $nameP1) {
switch(true) {
case ($scoreP1 == 0):
$scoreP1 = 15;
break;
case ($scoreP1 == 15):
$scoreP1 = 30;
break;
case ($scoreP1 > 15):
$scoreP1 = $scoreP1 + 10;
break;
}
}
else if($nameP == $nameP2) {
switch(true) {
case ($scoreP2 == 0):
$scoreP2 = 15;
break;
case ($scoreP2 == 15):
$scoreP2 = 30;
break;
case ($scoreP2 > 15):
$scoreP2 = $scoreP2 + 10;
break;
}
}
}
foreach($wins as $value) {
//get total score for both players in $scoreP1 and $scoreP2
score_incrm($value, $nameP1, $nameP2, $scoreP1, $scoreP2);
}
if(max($scoreP1, $scoreP2) > 40) { //Someone crossed 40; we have a proper state
if($scoreP1 == $scoreP2) {
//Tie
$state = "DEUCE";
}
else if( ($scoreP1 > $scoreP2) && ($scoreP1 - $scoreP2 > 10) ) {
//P1 wins
$state = $nameP1." WINS";
}
else if( ($scoreP1 > $scoreP2) && ($scoreP1 - $scoreP2 <= 10) ) {
//P1 has advantag
$state = $nameP1." ADVANTAGE";
}
else if( ($scoreP2 > $scoreP1) && ($scoreP2 - $scoreP1 > 10) ) {
//P2 wins
$state = $nameP2." WINS";
}
else if( ($scoreP2 > $scoreP1) && ($scoreP2 - $scoreP1 <= 10) ) {
//P2 has advantag
$state = $nameP2." ADVANTAGE";
}
}
if( max($scoreP1, $scoreP2) <= 40 ) { //if no one crosses 40 then show normal score
//no one got to 40, so lets see if theres a draw
if($scoreP1 == $scoreP2) {
$state = $scoreP1."a";
}
else {
$state = $nameP1." ".$scoreP1." - ".$nameP2." ".$scoreP2;
}
}
return $state;
}
/*Tests*/
//You can change the $wins array to test if the output matches the expected result.
echo "Player1: Bob, Player2 : Anna" . "<br>";
echo computeGameState("Bob", "Anna", ["Bob", "Bob", "Bob", ]);
// input for $wins like ["Bob", "Bob", "Bob", "Bob", "Anna", "Anna", "Anna", "Anna", "Anna"] are not considered
// as Bob would have won and the game would not continue. But the output we'll get is `Anna Advantage`
?>