Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Sources/Tests/unitTest.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ function testItCanProvideSecondPlayersName () {
}

function testItCanGetScoreForAPlayer () {
standings=$'John - Michael\nJohn\nMichael\nJohn'
standings=$'John\nMichael\nJohn'
assertEquals '2' `getScoreFor 'John' "$standings"`
}

Expand Down Expand Up @@ -46,4 +46,4 @@ function testItCanOutputWinnerForFirstPlayer () {
}

## Call and Run all Tests
. "../shunit2-2.1.6/src/shunit2"
. "../shunit2-2.1.6/src/shunit2"
67 changes: 32 additions & 35 deletions Sources/functions.sh
Original file line number Diff line number Diff line change
@@ -1,56 +1,53 @@
### functions.sh ###

function getFirstPlayerFrom () {
echo $1 | sed -e 's/-.*//'
echo $1 | sed 's/ - .*//'
}

function getSecondPlayerFrom () {
echo $1 | sed -e 's/.*-//'
echo $1 | sed 's/^.* - //'
}

function getScoreFor () {
player=$1
standings=$2
totalMatches=$(echo "$standings" | grep $player | wc -l)
echo $(($totalMatches-1))
player=$1
standings=$2
echo `echo "$standings" | grep "$player" | wc -l`
}

function displayScore () {
firstPlayerName=$1; firstPlayerScore=$2
secondPlayerName=$3; secondPlayerScore=$4

if outOfRegularScore $firstPlayerScore $secondPlayerScore; then
checkEquality $firstPlayerScore $secondPlayerScore
checkAdvantageFor $firstPlayerName $firstPlayerScore $secondPlayerScore
checkAdvantageFor $secondPlayerName $secondPlayerScore $firstPlayerScore
else
echo "$1: `convertToTennisScore $2` - $3: `convertToTennisScore $4`"
fi
}
firstPlayerName=$1; firstPlayerScore=$2
secondPlayerName=$3; secondPlayerScore=$4

function checkAdvantageFor () {
if [ $2 -gt $3 ]; then
if [ `expr $2 - $3` -gt 1 ]; then
echo "$1: Winner"
else
echo "$1: Advantage"
fi
fi
if outOfRegularScore $firstPlayerScore $secondPlayerScore ; then
checkEquality $firstPlayerScore $secondPlayerScore
checkAdvantageFor $firstPlayerName $firstPlayerScore $secondPlayerScore
checkAdvantageFor $secondPlayerName $secondPlayerScore $firstPlayerScore
else
echo "$firstPlayerName: $(convertToTennisScore $firstPlayerScore)" \
"- $secondPlayerName: $(convertToTennisScore $secondPlayerScore)"
fi
}

function outOfRegularScore () {
[ $1 -gt 2 ] && [ $2 -gt 2 ]
return $?
[ $1 -gt 2 ] && [ $2 -gt 2 ]
return $?
}

function checkEquality () {
if [ $1 -eq $2 ]; then
echo "Deuce"
fi
if [ $1 -eq $2 ]; then
echo 'Deuce'
fi
}

function convertToTennisScore () {
declare -a scoreMap=('0' '15' '30' '40')
echo ${scoreMap[$1]};
function checkAdvantageFor () {
if [ $2 -gt $3 ]; then
if [ `expr $2 - $3` -gt 1 ]; then
echo "$1: Winner"
else
echo "$1: Advantage"
fi
fi
}

function convertToTennisScore () {
declare -a scoreMap=(0 15 30 40)
echo ${scoreMap[$1]}
}
24 changes: 13 additions & 11 deletions Sources/tennisGame.sh
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@

. ./functions.sh

playersLine=`head -n 1 $1`
echo "$playersLine"
firstPlayer=`getFirstPlayerFrom "$playersLine"`
secondPlayer=`getSecondPlayerFrom "$playersLine"`
dump=`cat $1`

playersLine=`echo "$dump" | sed -n '1p'`
firstPlayerName=`getFirstPlayerFrom "$playersLine"`
secondPlayerName=`getSecondPlayerFrom "$playersLine"`

wholeScoreFileContent=`cat $1`
totalNoOfLines=`echo "$wholeScoreFileContent" | wc -l`
for currentLine in `seq 2 $totalNoOfLines`
do
firstPlayerScore=$(getScoreFor $firstPlayer "`echo \"$wholeScoreFileContent\" | head -n $currentLine`")
secondPlayerScore=$(getScoreFor $secondPlayer "`echo \"$wholeScoreFileContent\" | head -n $currentLine`")
displayScore $firstPlayer $firstPlayerScore $secondPlayer $secondPlayerScore
gameLog=`echo "$dump" | sed -n '1!p'`

echo "$playersLine"
echo "$gameLog" | while read line; do
let firstPlayerScore=firstPlayerScore+`getScoreFor "$firstPlayerName" "$line"`
let secondPlayerScore=secondPlayerScore+`getScoreFor "$secondPlayerName" "$line"`
displayScore "$firstPlayerName" $firstPlayerScore \
"$secondPlayerName" $secondPlayerScore
done