Skip to content

Commit

Permalink
added Julia Implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabrielle Gasse committed Nov 20, 2021
1 parent 1289bce commit 8d783b0
Show file tree
Hide file tree
Showing 8 changed files with 377 additions and 0 deletions.
26 changes: 26 additions & 0 deletions julia/Manifest.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# This file is machine-generated - editing it directly is not advised

[[Base64]]
uuid = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"

[[InteractiveUtils]]
deps = ["Markdown"]
uuid = "b77e0a4c-d291-57a0-90e8-8db25a27a240"

[[Logging]]
uuid = "56ddb016-857b-54e1-b83d-db4d58db5568"

[[Markdown]]
deps = ["Base64"]
uuid = "d6f4376e-aef5-505a-96c1-9c027394607a"

[[Random]]
deps = ["Serialization"]
uuid = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"

[[Serialization]]
uuid = "9e88b42a-f829-5b0c-bbe9-9e923198166b"

[[Test]]
deps = ["InteractiveUtils", "Logging", "Random", "Serialization"]
uuid = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
7 changes: 7 additions & 0 deletions julia/Project.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
name = "Tenniskata"
uuid = "e8ea7c87-1fb4-4e26-a338-fb47bba8a06d"
authors = ["Gabrielle Gasse <[email protected]>"]
version = "1.0.0"

[deps]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
24 changes: 24 additions & 0 deletions julia/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Tennis in [Julia](https://julialang.org/)

## Installation

Start the Julia REPL with the current directory as project root.
```
julia --project=@.
```

Once you are in REPL, use the `Pkg` module to download the project's dependencies (the `Test` module).
```julia
using Pkg

Pkg.instantiate()
```

## Running Tests

Using the Julia REPL, use the `Pkg` module to run the tests.
```Julia
using Pkg

Pkg.test()
```
6 changes: 6 additions & 0 deletions julia/src/Tenniskata.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module Tenniskata

won_point(game, playerName::String) = error("Not implemented.")
get_score(game) = error("Not implemented.")

end
62 changes: 62 additions & 0 deletions julia/src/Tenniskata1.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
mutable struct TennisGame1
m_score1::Int
m_score2::Int
player1Name::String
player2Name::String

TennisGame1(player1Name, player2Name) = new(0, 0, player1Name, player2Name)
end

function Tenniskata.won_point(game::TennisGame1, playerName::String)
if playerName == "player1"
game.m_score1 += 1
else
game.m_score2 += 1
end
end

function Tenniskata.get_score(game::TennisGame1)
score = ""
tempScore = 0
if game.m_score1 == game.m_score2
if game.m_score1 == 0
score = "Love-All"
elseif game.m_score1 == 1
score = "Fifteen-All"
elseif game.m_score1 == 2
score = "Thirty-All"
else
score = "Deuce"
end
elseif game.m_score1 >= 4 || game.m_score2 >= 4
minusResult = game.m_score1 - game.m_score2
if minusResult == 1
score = "Advantage player1"
elseif minusResult == -1
score = "Advantage player2"
elseif minusResult >= 2
score = "Win for player1"
else
score = "Win for player2"
end
else
for i = 1:2
if i == 1
tempScore = game.m_score1
else
score *= "-"
tempScore = game.m_score2
end
if tempScore == 0
score *= "Love"
elseif tempScore == 1
score *= "Fifteen"
elseif tempScore == 2
score *= "Thirty"
elseif tempScore == 3
score *= "Forty"
end
end
end
return score
end
125 changes: 125 additions & 0 deletions julia/src/Tenniskata2.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
mutable struct TennisGame2
P1point::Int
P2point::Int

P1res::String
P2res::String
player1Name::String
player2Name::String

TennisGame2(player1Name, player2Name) = new(0, 0, "", "", player1Name, player2Name)
end

function Tenniskata.get_score(game::TennisGame2)
score = ""
if game.P1point == game.P2point && game.P1point < 4
if game.P1point == 0
score = "Love"
end
if game.P1point == 1
score = "Fifteen"
end
if game.P1point == 2
score = "Thirty"
end
score *= "-All"
end
if game.P1point == game.P2point && game.P1point >= 3
score = "Deuce"
end

if game.P1point > 0 && game.P2point == 0
if game.P1point == 1
game.P1res = "Fifteen"
end
if game.P1point == 2
game.P1res = "Thirty"
end
if game.P1point == 3
game.P1res = "Forty"
end

game.P2res = "Love"
score = game.P1res * "-" * game.P2res
end
if game.P2point > 0 && game.P1point == 0
if game.P2point == 1
game.P2res = "Fifteen"
end
if game.P2point == 2
game.P2res = "Thirty"
end
if game.P2point == 3
game.P2res = "Forty"
end

game.P1res = "Love"
score = game.P1res * "-" * game.P2res
end

if game.P1point > game.P2point && game.P1point < 4
if game.P1point == 2
game.P1res = "Thirty"
end
if game.P1point == 3
game.P1res = "Forty"
end
if game.P2point == 1
game.P2res = "Fifteen"
end
if game.P2point == 2
game.P2res = "Thirty"
end
score = game.P1res * "-" * game.P2res
end
if game.P2point > game.P1point && game.P2point < 4
if game.P2point == 2
game.P2res = "Thirty"
end
if game.P2point == 3
game.P2res = "Forty"
end
if game.P1point == 1
game.P1res = "Fifteen"
end
if game.P1point == 2
game.P1res = "Thirty"
end
score = game.P1res * "-" * game.P2res
end

if game.P1point > game.P2point && game.P2point >= 3
score = "Advantage player1"
end

if game.P2point > game.P1point && game.P1point >= 3
score = "Advantage player2"
end

if game.P1point >= 4 && game.P2point >= 0 && (game.P1point-game.P2point) >= 2
score = "Win for player1"
end
if game.P2point >= 4 && game.P1point >= 0 && (game.P2point-game.P1point) >= 2
score = "Win for player2"
end
return score
end

set_p1_score(game::TennisGame2, number::Int) = for _ = 1:number p1_score(game) end
set_p2_score(game::TennisGame2, number::Int) = for _ = 1:number p2_score(game) end

function p1_score(game::TennisGame2)
game.P1point += 1
end

function p2_score(game::TennisGame2)
game.P2point += 1
end

function Tenniskata.won_point(game::TennisGame2, player::String)
if player == "player1"
p1_score(game)
else
p2_score(game)
end
end
40 changes: 40 additions & 0 deletions julia/src/Tenniskata3.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
mutable struct TennisGame3
p2::Int
p1::Int
p1N::String
p2N::String

TennisGame3(p1N, p2N) = new(0, 0, p1N, p2N)
end

function Tenniskata.get_score(game::TennisGame3)
if game.p1 < 4 && game.p2 < 4 && !(game.p1+game.p2 == 6)
p = Dict(0 => "Love", 1 => "Fifteen", 2 => "Thirty", 3 => "Forty")
s = p[game.p1]
if game.p1 == game.p2
return s * "-All"
end
return s * "-" * p[game.p2]
else
if game.p1 == game.p2
return "Deuce"
end
if game.p1 > game.p2
s = game.p1N
else
s = game.p2N
end
if (game.p1-game.p2)*(game.p1-game.p2) == 1
return "Advantage " * s
end
return "Win for " * s
end
end

function Tenniskata.won_point(game::TennisGame3, playerName::String)
if playerName == "player1"
game.p1 += 1
else
game.p2 += 1
end
end
87 changes: 87 additions & 0 deletions julia/test/runtests.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
include("../src/Tenniskata.jl")
include("../src/Tenniskata1.jl")
include("../src/Tenniskata2.jl")
include("../src/Tenniskata3.jl")

using Test

struct TestDataSample
player1Score::Int
player2Score::Int
expectedScore::String
end

testData = [
TestDataSample(0, 0, "Love-All"),
TestDataSample(1, 1, "Fifteen-All"),
TestDataSample(2, 2, "Thirty-All"),
TestDataSample(3, 3, "Deuce"),
TestDataSample(4, 4, "Deuce"),

TestDataSample(1, 0, "Fifteen-Love"),
TestDataSample(0, 1, "Love-Fifteen"),
TestDataSample(2, 0, "Thirty-Love"),
TestDataSample(0, 2, "Love-Thirty"),
TestDataSample(3, 0, "Forty-Love"),
TestDataSample(0, 3, "Love-Forty"),
TestDataSample(4, 0, "Win for player1"),
TestDataSample(0, 4, "Win for player2"),

TestDataSample(2, 1, "Thirty-Fifteen"),
TestDataSample(1, 2, "Fifteen-Thirty"),
TestDataSample(3, 1, "Forty-Fifteen"),
TestDataSample(1, 3, "Fifteen-Forty"),
TestDataSample(4, 1, "Win for player1"),
TestDataSample(1, 4, "Win for player2"),

TestDataSample(3, 2, "Forty-Thirty"),
TestDataSample(2, 3, "Thirty-Forty"),
TestDataSample(4, 2, "Win for player1"),
TestDataSample(2, 4, "Win for player2"),

TestDataSample(4, 3, "Advantage player1"),
TestDataSample(3, 4, "Advantage player2"),
TestDataSample(5, 4, "Advantage player1"),
TestDataSample(4, 5, "Advantage player2"),
TestDataSample(15, 14, "Advantage player1"),
TestDataSample(14, 15, "Advantage player2"),

TestDataSample(6, 4, "Win for player1"),
TestDataSample(4, 6, "Win for player2"),
TestDataSample(16, 14, "Win for player1"),
TestDataSample(14, 16, "Win for player2")
]

function setScore(game, sample::TestDataSample)
highestScore = max(sample.player1Score, sample.player2Score)

for i = 0:highestScore
if i < sample.player1Score
Tenniskata.won_point(game, "player1")
end

if i < sample.player2Score
Tenniskata.won_point(game, "player2")
end
end

@test Tenniskata.get_score(game) == sample.expectedScore
end

@testset "Tenniskata1" begin
@testset "$sample" for sample in testData
setScore(TennisGame1("player1", "player2"), sample)
end
end

@testset "Tenniskata2" begin
@testset "$sample" for sample in testData
setScore(TennisGame2("player1", "player2"), sample)
end
end

@testset "Tenniskata3" begin
@testset "$sample" for sample in testData
setScore(TennisGame3("player1", "player2"), sample)
end
end

0 comments on commit 8d783b0

Please sign in to comment.