-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
123 lines (108 loc) · 2.76 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
Console.Title = "The Long Game";
Console.Write("Welcome to The Long Game, please enter your username to get started: ");
string? username = Console.ReadLine()!;
//Lets make sure the user enters a name - otherwise we can't give them a score.
while(username is null || username == "")
{
Console.WriteLine("You must supply a username to play");
Console.Write("What is your username?: ");
username = Console.ReadLine()!;
}
//Create the player and the game.
LongGame game = new(new User(username));
//Let's play!
if (game.Player.NewPlayer)
{
Console.WriteLine($"""
Welcome {game.Player.Username},
As this is your first time with us your score starts with {game.Player.Score}.
The rules are simple. Keep typing to increase your score, every key press is worth 1 point
To exit and save your progress press "Enter".
""");
}
else
{
if (game.Player.ScoreReset)
{
Console.WriteLine($"Welcome back {game.Player.Username}, " +
$"your previous score has been reset and you are starting again at {game.Player.Score}.");
}
else
{
Console.WriteLine($"Welcome back {game.Player.Username}, last time you scored {game.Player.Score} - You know the rules, begin...");
}
}
game.Run();
//Game & User Classes
public class LongGame
{
public User Player { get; init; }
public bool GameOver { get; private set; }
public LongGame(User username)
{
Player = username;
}
public void Run()
{
ConsoleKey userKey;
while (!GameOver)
{
userKey = Console.ReadKey().Key;
if (userKey == ConsoleKey.Enter)
{
Player.SavePlayerScore();
GameOver = true;
}
else
{
Player.IncreaseScore();
}
Console.Clear();
Console.WriteLine($"Score: {Player.Score}");
}
}
public bool EndGame() => GameOver = true;
}
public class User
{
public string Username { get; init; }
public int Score { get; private set; }
public bool NewPlayer { get; private set; }
public bool ScoreReset { get; private set; }
public User(string username)
{
Username = FormatUsername(username);
Score = LoadPlayerScore();
}
private string FormatUsername(string username)
{
username = username.ToLower();
return char.ToUpper(username[0]) + username.Remove(0, 1);
}
public int IncreaseScore() => Score++;
//Checks to see if the user has played before and loads their previous score.
private int LoadPlayerScore()
{
if (File.Exists($"{Username}.txt"))
{
string[] fileContents = File.ReadAllLines($"{Username}.txt");
bool parsed = int.TryParse(fileContents[0], out int score);
if (!parsed)
{
ScoreReset = true;
return 0;
}
return score;
}
else
{
NewPlayer = true;
return 0;
}
}
//Save the players current score to a file of their username.
public void SavePlayerScore()
{
File.WriteAllText($"{Username}.txt", Score.ToString());
}
}