Refactor Clicker game

This commit is contained in:
Geir Okkenhaug Jerstad 2025-01-08 11:30:08 +01:00
parent 13af9e740b
commit 6e80cd34cc
3 changed files with 64 additions and 27 deletions

View file

@ -0,0 +1,55 @@
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)
{
Points = points;
PointsPerClick = pointsPerClick;
PointsPerClickIncrease = pointsPerClickIncrease;
}
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()
{
Points -= 10;
PointsPerClick += PointsPerClickIncrease;
}
void SuperUpgrade()
{
Points -= 100;
PointsPerClickIncrease++;
}
void Exit()
{
Environment.Exit(0);
}
}

View file

@ -0,0 +1,6 @@
namespace ClickerOOP;
public interface ICommand
{
public void Run();
}

View file

@ -1,28 +1,4 @@
int points = 0;
int pointsPerClick = 1;
int pointsPerClickIncrease = 1;
using ClickerOOP;
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') Environment.Exit(0);
else if (command == ' ') points += pointsPerClick;
else if (command == 'k' && points >= 10)
{
points -= 10;
pointsPerClick += pointsPerClickIncrease;
}
else if (command == 's' && points >= 100)
{
points -= 100;
pointsPerClickIncrease++;
}
}
var game = new ClickerGame(0,1,1);
game.Run();