diff --git a/Emne 3/Calc/Calc.sln b/Emne 3/Calc/Calc.sln new file mode 100644 index 0000000..2c8c2ed --- /dev/null +++ b/Emne 3/Calc/Calc.sln @@ -0,0 +1,22 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Calc", "Calc\Calc.csproj", "{316D55FE-FAD0-4686-BD29-68E4B6B8DBE8}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Calculator.Test", "Calculator.Test\Calculator.Test.csproj", "{57206669-909C-4445-B3B9-2444902E1C20}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {316D55FE-FAD0-4686-BD29-68E4B6B8DBE8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {316D55FE-FAD0-4686-BD29-68E4B6B8DBE8}.Debug|Any CPU.Build.0 = Debug|Any CPU + {316D55FE-FAD0-4686-BD29-68E4B6B8DBE8}.Release|Any CPU.ActiveCfg = Release|Any CPU + {316D55FE-FAD0-4686-BD29-68E4B6B8DBE8}.Release|Any CPU.Build.0 = Release|Any CPU + {57206669-909C-4445-B3B9-2444902E1C20}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {57206669-909C-4445-B3B9-2444902E1C20}.Debug|Any CPU.Build.0 = Debug|Any CPU + {57206669-909C-4445-B3B9-2444902E1C20}.Release|Any CPU.ActiveCfg = Release|Any CPU + {57206669-909C-4445-B3B9-2444902E1C20}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection +EndGlobal diff --git a/Emne 3/Calc/Calc/Calc.csproj b/Emne 3/Calc/Calc/Calc.csproj new file mode 100644 index 0000000..2f4fc77 --- /dev/null +++ b/Emne 3/Calc/Calc/Calc.csproj @@ -0,0 +1,10 @@ + + + + Exe + net8.0 + enable + enable + + + diff --git a/Emne 3/Calc/Calc/Calculator.cs b/Emne 3/Calc/Calc/Calculator.cs new file mode 100644 index 0000000..410c2b7 --- /dev/null +++ b/Emne 3/Calc/Calc/Calculator.cs @@ -0,0 +1,29 @@ +namespace Calc; + +public class Calculator +{ + public int LeggSammen(int a, int b) + { + return a + b; + } + + public int TrekkFra(int a, int b) + { + return a - b; + } + + public int Multipliser(int a, int b) + { + return a * b; + } + + public int Divider(int a, int b) + { + if (b == 0) + { + throw new ArgumentException("Cannot divide by zero."); + } + return a / b; + } + +} \ No newline at end of file diff --git a/Emne 3/Calc/Calc/Program.cs b/Emne 3/Calc/Calc/Program.cs new file mode 100644 index 0000000..e5dff12 --- /dev/null +++ b/Emne 3/Calc/Calc/Program.cs @@ -0,0 +1,3 @@ +// See https://aka.ms/new-console-template for more information + +Console.WriteLine("Hello, World!"); \ No newline at end of file diff --git a/Emne 3/Calc/Calculator.Test/Calculator.Test.csproj b/Emne 3/Calc/Calculator.Test/Calculator.Test.csproj new file mode 100644 index 0000000..5b568ba --- /dev/null +++ b/Emne 3/Calc/Calculator.Test/Calculator.Test.csproj @@ -0,0 +1,28 @@ + + + + net8.0 + enable + enable + + false + true + + + + + + + + + + + + + + + + + + + diff --git a/Emne 3/Calc/Calculator.Test/UnitTest1.cs b/Emne 3/Calc/Calculator.Test/UnitTest1.cs new file mode 100644 index 0000000..7a5ad80 --- /dev/null +++ b/Emne 3/Calc/Calculator.Test/UnitTest1.cs @@ -0,0 +1,43 @@ +namespace Calculator.Test; + +public class Tests +{ + [Test] + public void TestLeggSammen() + { + var calc = new Calc.Calculator(); + var sum = calc.LeggSammen(1, 2); + Assert.That(sum, Is.EqualTo(3)); + } + + [Test] + public void TestTrekkFra() + { + var calc = new Calc.Calculator(); + var sum = calc.TrekkFra(2, 1); + Assert.That(sum, Is.EqualTo(1)); + } + + [Test] + public void TestMultipliser() + { + var calc = new Calc.Calculator(); + var sum = calc.Multipliser(2, 2); + Assert.That(sum, Is.EqualTo(4)); + } + + [Test] + public void TestDivision() + { + var calc = new Calc.Calculator(); + var sum = calc.Divider(4, 2); + Assert.That(sum, Is.EqualTo(2)); + } + + [Test] + public void TestDivisionByZero() + { + var calc = new Calc.Calculator(); + Assert.Throws(() => calc.Divider(4, 0)); + } +} \ No newline at end of file diff --git a/Emne 3/StigespilletTDD/GameTest/GameTest.csproj b/Emne 3/StigespilletTDD/GameTest/GameTest.csproj new file mode 100644 index 0000000..a228818 --- /dev/null +++ b/Emne 3/StigespilletTDD/GameTest/GameTest.csproj @@ -0,0 +1,28 @@ + + + + net8.0 + enable + enable + + false + true + + + + + + + + + + + + + + + + + + + diff --git a/Emne 3/StigespilletTDD/GameTest/UnitTest1.cs b/Emne 3/StigespilletTDD/GameTest/UnitTest1.cs new file mode 100644 index 0000000..de9c676 --- /dev/null +++ b/Emne 3/StigespilletTDD/GameTest/UnitTest1.cs @@ -0,0 +1,12 @@ +namespace GameTest; +using StigespilletTDD + +public class Tests +{ + + [Test] + public void () + { + Assert.Pass(); + } +} \ No newline at end of file diff --git a/Emne 3/StigespilletTDD/StigespilletTDD.sln b/Emne 3/StigespilletTDD/StigespilletTDD.sln new file mode 100644 index 0000000..66f4581 --- /dev/null +++ b/Emne 3/StigespilletTDD/StigespilletTDD.sln @@ -0,0 +1,22 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StigespilletTDD", "StigespilletTDD\StigespilletTDD.csproj", "{EA04112E-4F91-4EFF-98A6-09C05202F318}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StigespilletTDD.test", "StigespilletTDD.test\StigespilletTDD.test.csproj", "{BCE58F6B-442E-4977-A26E-10E39AE516BD}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {EA04112E-4F91-4EFF-98A6-09C05202F318}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {EA04112E-4F91-4EFF-98A6-09C05202F318}.Debug|Any CPU.Build.0 = Debug|Any CPU + {EA04112E-4F91-4EFF-98A6-09C05202F318}.Release|Any CPU.ActiveCfg = Release|Any CPU + {EA04112E-4F91-4EFF-98A6-09C05202F318}.Release|Any CPU.Build.0 = Release|Any CPU + {BCE58F6B-442E-4977-A26E-10E39AE516BD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {BCE58F6B-442E-4977-A26E-10E39AE516BD}.Debug|Any CPU.Build.0 = Debug|Any CPU + {BCE58F6B-442E-4977-A26E-10E39AE516BD}.Release|Any CPU.ActiveCfg = Release|Any CPU + {BCE58F6B-442E-4977-A26E-10E39AE516BD}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection +EndGlobal diff --git a/Emne 3/StigespilletTDD/StigespilletTDD.test/StigespilletTDD.test.csproj b/Emne 3/StigespilletTDD/StigespilletTDD.test/StigespilletTDD.test.csproj new file mode 100644 index 0000000..a228818 --- /dev/null +++ b/Emne 3/StigespilletTDD/StigespilletTDD.test/StigespilletTDD.test.csproj @@ -0,0 +1,28 @@ + + + + net8.0 + enable + enable + + false + true + + + + + + + + + + + + + + + + + + + diff --git a/Emne 3/StigespilletTDD/StigespilletTDD.test/UnitTest1.cs b/Emne 3/StigespilletTDD/StigespilletTDD.test/UnitTest1.cs new file mode 100644 index 0000000..b983cd4 --- /dev/null +++ b/Emne 3/StigespilletTDD/StigespilletTDD.test/UnitTest1.cs @@ -0,0 +1,72 @@ + +namespace StigespilletTDD.test; +using StigespilletTDD; + +public class Tests +{ + [Test] + public void TestStartAt0() + { + var game = new Game(4); + int position = game.GetPlayerPosition(0); + Assert.AreEqual(0, position); + + } + [Test] + public void TestInitialMove() + { + var game = new Game(4); + game.Move(1, 4); + var position = game.GetPlayerPosition(0); + Assert.AreEqual(4, position); + } + + [Test] + public void TestMultiplePlayerPosition() + { + var game = new Game(2); + game.Move(0, 3); + game.Move(1, 4); + Assert.AreEqual(3, game.GetPlayerPosition(0)); + Assert.AreEqual(4, game.GetPlayerPosition(1)); + } + + [TestCase(1,40)] + [TestCase(36,52)] + [TestCase(24,5)] + public void TestLadder(int posFrom, int posTo) + { + var game = new Game(1); + game.Move(0,posFrom); + Assert.AreEqual(posTo, game.GetPlayerPosition(0)); + } + + [Test] + public void TestDiceMax6() + { + + } + + [Test] + public void TestNotWinning() + { + var game = new Game(1); + game.Move(0, 2); + var winner = game.GetWinner(); + Assert.IsNull(winner); + } + + [Test] + public void TestWinning() + { + var game = new Game(1); + game.Move(0, 1); + game.Move(0, 3); + game.Move(0, 3); + game.Move(0, 6); + game.Move(0, 2); + var winner = game.GetWinner(); + Assert.AreEqual(0, winner); + + } +} \ No newline at end of file diff --git a/Emne 3/StigespilletTDD/StigespilletTDD/Game.cs b/Emne 3/StigespilletTDD/StigespilletTDD/Game.cs new file mode 100644 index 0000000..0a46f31 --- /dev/null +++ b/Emne 3/StigespilletTDD/StigespilletTDD/Game.cs @@ -0,0 +1,63 @@ +namespace StigespilletTDD; + +public class Game +{ + private int[] _positions; + private readonly Ladder[] _ladders; + public Game(int playerCount) + { + _positions = new int[playerCount]; + _ladders = new[] + { + new Ladder(1, 40), + new Ladder(8, 10), + new Ladder(36, 52), + new Ladder(43, 62), + new Ladder(49, 79), + new Ladder(65, 82), + new Ladder(68, 85), + + new Ladder(87, 70), + new Ladder(74, 12), + new Ladder(64, 27), + new Ladder(56, 37), + new Ladder(42, 30), + new Ladder(33, 3), + new Ladder(24, 5), + }; + } + + public int GetPlayerPosition(int playerIndex) + { + return _positions[playerIndex]; + } + + public void Move(int playerIndex, int moveCount) + { + var pos = _positions[playerIndex] += moveCount; + var ladder = FindLadder(pos); + if (ladder != null) + { + _positions[playerIndex] = ladder.PositionTo; + } + + } + + private Ladder FindLadder(int pos) + { + return _ladders.FirstOrDefault(l => l.PositionFrom == pos); + } + + public int? GetWinner() + { + if (GetPlayerPosition(0) == 90) + { + return 0; + } + else + { + return null; + } + + } +} \ No newline at end of file diff --git a/Emne 3/StigespilletTDD/StigespilletTDD/Ladder.cs b/Emne 3/StigespilletTDD/StigespilletTDD/Ladder.cs new file mode 100644 index 0000000..34f7ee8 --- /dev/null +++ b/Emne 3/StigespilletTDD/StigespilletTDD/Ladder.cs @@ -0,0 +1,13 @@ +namespace StigespilletTDD; + +public class Ladder +{ + public int PositionFrom { get; set; } + public int PositionTo { get; set; } + + public Ladder(int positionFrom, int positionTo) + { + PositionFrom = positionFrom; + PositionTo = positionTo; + } +} \ No newline at end of file diff --git a/Emne 3/StigespilletTDD/StigespilletTDD/Program.cs b/Emne 3/StigespilletTDD/StigespilletTDD/Program.cs new file mode 100644 index 0000000..e5dff12 --- /dev/null +++ b/Emne 3/StigespilletTDD/StigespilletTDD/Program.cs @@ -0,0 +1,3 @@ +// See https://aka.ms/new-console-template for more information + +Console.WriteLine("Hello, World!"); \ No newline at end of file diff --git a/Emne 3/StigespilletTDD/StigespilletTDD/StigespilletTDD.csproj b/Emne 3/StigespilletTDD/StigespilletTDD/StigespilletTDD.csproj new file mode 100644 index 0000000..2f4fc77 --- /dev/null +++ b/Emne 3/StigespilletTDD/StigespilletTDD/StigespilletTDD.csproj @@ -0,0 +1,10 @@ + + + + Exe + net8.0 + enable + enable + + +