diff --git a/Sources/Tests/unitTest.sh b/Sources/Tests/unitTest.sh index d2b37f9..966ce8a 100644 --- a/Sources/Tests/unitTest.sh +++ b/Sources/Tests/unitTest.sh @@ -13,7 +13,7 @@ function testItCanProvideSecondPlayersName () { } function testItCanGetScoreForAPlayer () { - standings=$'John - Michael\nJohn\nMichael\nJohn' + standings=$'John\nMichael\nJohn' assertEquals '2' `getScoreFor 'John' "$standings"` } @@ -46,4 +46,4 @@ function testItCanOutputWinnerForFirstPlayer () { } ## Call and Run all Tests -. "../shunit2-2.1.6/src/shunit2" \ No newline at end of file +. "../shunit2-2.1.6/src/shunit2" diff --git a/Sources/functions.sh b/Sources/functions.sh index 4de513a..8a420c5 100644 --- a/Sources/functions.sh +++ b/Sources/functions.sh @@ -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]} +} diff --git a/Sources/tennisGame.sh b/Sources/tennisGame.sh old mode 100644 new mode 100755 index 238a0ef..6c1a804 --- a/Sources/tennisGame.sh +++ b/Sources/tennisGame.sh @@ -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