From 85921091f9db1d0e5884bdcbd78935b05cacf839 Mon Sep 17 00:00:00 2001 From: Geir Okkenhaug Jerstad Date: Wed, 8 Jan 2025 13:03:52 +0100 Subject: [PATCH] Checked out the solution --- Emne 3/ClickerOOP/ClickerOOP/Click.cs | 16 ++++++ Emne 3/ClickerOOP/ClickerOOP/ClickerGame.cs | 55 ++++++-------------- Emne 3/ClickerOOP/ClickerOOP/Commands.cs | 33 ++++++++++++ Emne 3/ClickerOOP/ClickerOOP/ExitCommand.cs | 11 ++++ Emne 3/ClickerOOP/ClickerOOP/ICommand.cs | 1 + Emne 3/ClickerOOP/ClickerOOP/Program.cs | 18 ++++++- Emne 3/ClickerOOP/ClickerOOP/SuperUpgrade.cs | 17 ++++++ Emne 3/ClickerOOP/ClickerOOP/Upgrade.cs | 18 +++++++ 8 files changed, 127 insertions(+), 42 deletions(-) create mode 100644 Emne 3/ClickerOOP/ClickerOOP/Click.cs create mode 100644 Emne 3/ClickerOOP/ClickerOOP/Commands.cs create mode 100644 Emne 3/ClickerOOP/ClickerOOP/ExitCommand.cs create mode 100644 Emne 3/ClickerOOP/ClickerOOP/SuperUpgrade.cs create mode 100644 Emne 3/ClickerOOP/ClickerOOP/Upgrade.cs diff --git a/Emne 3/ClickerOOP/ClickerOOP/Click.cs b/Emne 3/ClickerOOP/ClickerOOP/Click.cs new file mode 100644 index 0000000..27cee85 --- /dev/null +++ b/Emne 3/ClickerOOP/ClickerOOP/Click.cs @@ -0,0 +1,16 @@ +namespace ClickerOOP; + +public class Click: ICommand +{ + private ClickerGame _game; + public char Character { get; } = ' '; + + public Click(ClickerGame game) + { + _game = game; + } + public void Run() + { + _game.Click(); + } +} \ No newline at end of file diff --git a/Emne 3/ClickerOOP/ClickerOOP/ClickerGame.cs b/Emne 3/ClickerOOP/ClickerOOP/ClickerGame.cs index 145f1cb..7613e71 100644 --- a/Emne 3/ClickerOOP/ClickerOOP/ClickerGame.cs +++ b/Emne 3/ClickerOOP/ClickerOOP/ClickerGame.cs @@ -1,55 +1,30 @@ namespace ClickerOOP; -public class ClickerGame : ICommand -{ - public int Points {get; private set;} - public int PointsPerClick {get; private set;} - public int PointsPerClickIncrease {get; private set;} - - public ClickerGame(int points, int pointsPerClick, int pointsPerClickIncrease) +public class ClickerGame +{ + public int Points { get; private set; } = 0; + private int _pointsPerClick = 1; + private int _pointsPerClickIncrease = 1; + public void Click() { - Points = points; - PointsPerClick = pointsPerClick; - PointsPerClickIncrease = pointsPerClickIncrease; + Points += _pointsPerClick; } - public void Run() - { - while (true) - { - Console.Clear(); - Console.WriteLine("Kommandoer:\r\n" + - "- SPACE = klikk (og få poeng)\r\n" + - "- K = kjøp oppgradering \r\nØker poeng per klikk koster 10 poeng\r\n" + - "- S = kjøp superoppgradering øker \"poeng per klikk\" for den vanlige oppgraderingen. koster 100 poeng\r\n" + - "- X = avslutt applikasjonen"); - Console.WriteLine($"Du har {Points} poeng."); - Console.WriteLine("Trykk tast for ønsket kommando."); - var command = Console.ReadKey().KeyChar; - if (command == 'x'){ Exit(); } - else if (command == ' ') { Click(); } - else if (command == 'k' && Points >= 10) { Upgrade(); } - else if (command == 's' && Points >= 100) { SuperUpgrade(); } } - } - void Click() - { - Points += PointsPerClick; - } - void Upgrade() + public void Upgrade() { + if(Points < 10) return; Points -= 10; - PointsPerClick += PointsPerClickIncrease; + _pointsPerClick += _pointsPerClickIncrease; + } - void SuperUpgrade() + public void SuperUpgrade() { + if (Points < 100) return; Points -= 100; - PointsPerClickIncrease++; - } - void Exit() - { - Environment.Exit(0); + _pointsPerClickIncrease++; } + } \ No newline at end of file diff --git a/Emne 3/ClickerOOP/ClickerOOP/Commands.cs b/Emne 3/ClickerOOP/ClickerOOP/Commands.cs new file mode 100644 index 0000000..ce0d513 --- /dev/null +++ b/Emne 3/ClickerOOP/ClickerOOP/Commands.cs @@ -0,0 +1,33 @@ +namespace ClickerOOP; + +public class Commands +{ + private ICommand[] _commands; + + public Commands(ClickerGame game) + { + _commands = new ICommand[] + { + new ExitCommand(), + new Upgrade(game), + new Click(game), + new SuperUpgrade(game), + + }; + } + + public void Run(char commandChar) + { + var command = FindCommand(commandChar); + if(command != null) command.Run(); + } + + private ICommand FindCommand(char commandChar) + { + foreach (var command in _commands) + { + if (command.Character == commandChar){ return command; } + } + return null; + } +} \ No newline at end of file diff --git a/Emne 3/ClickerOOP/ClickerOOP/ExitCommand.cs b/Emne 3/ClickerOOP/ClickerOOP/ExitCommand.cs new file mode 100644 index 0000000..2580a39 --- /dev/null +++ b/Emne 3/ClickerOOP/ClickerOOP/ExitCommand.cs @@ -0,0 +1,11 @@ +namespace ClickerOOP; + +public class ExitCommand: ICommand +{ + public char Character { get; } = 'X'; + + public void Run() + { + Environment.Exit(0); + } +} \ No newline at end of file diff --git a/Emne 3/ClickerOOP/ClickerOOP/ICommand.cs b/Emne 3/ClickerOOP/ClickerOOP/ICommand.cs index 81d79cd..082ec8e 100644 --- a/Emne 3/ClickerOOP/ClickerOOP/ICommand.cs +++ b/Emne 3/ClickerOOP/ClickerOOP/ICommand.cs @@ -3,4 +3,5 @@ namespace ClickerOOP; public interface ICommand { public void Run(); + char Character { get; } } \ No newline at end of file diff --git a/Emne 3/ClickerOOP/ClickerOOP/Program.cs b/Emne 3/ClickerOOP/ClickerOOP/Program.cs index b48376c..a53a025 100644 --- a/Emne 3/ClickerOOP/ClickerOOP/Program.cs +++ b/Emne 3/ClickerOOP/ClickerOOP/Program.cs @@ -1,4 +1,18 @@ using ClickerOOP; -var game = new ClickerGame(0,1,1); -game.Run(); +var game = new ClickerGame(); +var commands = new Commands(game); + +while (true) +{ + Console.Clear(); + Console.WriteLine("Kommandoer:\r\n" + + "- SPACE = klikk (og få poeng)\r\n" + + "- K = kjøp oppgradering \r\nØker poeng per klikk koster 10 poeng\r\n" + + "- S = kjøp superoppgradering øker \"poeng per klikk\" for den vanlige oppgraderingen. koster 100 poeng\r\n" + + "- X = avslutt applikasjonen"); + Console.WriteLine($"Du har {game.Points} poeng."); + Console.WriteLine("Trykk tast for ønsket kommando."); + var command = Console.ReadKey().KeyChar; + commands.Run(command); +} \ No newline at end of file diff --git a/Emne 3/ClickerOOP/ClickerOOP/SuperUpgrade.cs b/Emne 3/ClickerOOP/ClickerOOP/SuperUpgrade.cs new file mode 100644 index 0000000..f7090ce --- /dev/null +++ b/Emne 3/ClickerOOP/ClickerOOP/SuperUpgrade.cs @@ -0,0 +1,17 @@ +namespace ClickerOOP; + +public class SuperUpgrade: ICommand +{ + private ClickerGame _game; + public char Character { get; } = 'S'; + + public SuperUpgrade(ClickerGame game) + { + _game = game; + } + + public void Run() + { + _game.SuperUpgrade(); + } +} \ No newline at end of file diff --git a/Emne 3/ClickerOOP/ClickerOOP/Upgrade.cs b/Emne 3/ClickerOOP/ClickerOOP/Upgrade.cs new file mode 100644 index 0000000..8f71924 --- /dev/null +++ b/Emne 3/ClickerOOP/ClickerOOP/Upgrade.cs @@ -0,0 +1,18 @@ +namespace ClickerOOP; + +public class Upgrade: ICommand +{ + private ClickerGame _game; + + public Upgrade(ClickerGame game) + { + _game = game; + } + + public char Character { get; } = 'K'; + + public void Run() + { + _game.Upgrade(); + } +} \ No newline at end of file