-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPassage.cs
More file actions
74 lines (63 loc) · 2.14 KB
/
Passage.cs
File metadata and controls
74 lines (63 loc) · 2.14 KB
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace CYOA
{
public class Passage
{
private const string _menuDelimiter = "===";
private const string _passwordDelimiter = "***";
private readonly string Text;
private readonly IMenu Menu;
public Passage(string text, IMenu menu)
{
Text = text;
Menu = menu;
}
public Passage(string filepath)
{
var lines = File.ReadAllLines(filepath).ToList();
int i = 0;
while (i < lines.Count)
{
if (lines[i] == _menuDelimiter || lines[i] == _passwordDelimiter) break;
Text += lines[i] + "\n";
i++;
}
i++;
if (lines.Any(line => line == _menuDelimiter)) Menu = BuildMenu(lines, i);
else if (lines.Any(line => line == _passwordDelimiter)) Menu = BuildPassword(filepath);
else Menu = BuildConitnue(filepath);
}
public string Display()
{
Settings.Color(FontColor.DEFAULT);
Console.WriteLine(Text);
return Menu != null ? Menu.Display() : "ERROR";
}
private Menu BuildConitnue(string filepath)
{
var filename = filepath.Substring(filepath.LastIndexOf('/') + 1);
filename = filename.Substring(0, filename.LastIndexOf('.'));
var fileId = int.Parse(filename) + 1;
return new Menu("", new List<MenuChoice> { new MenuChoice("Continue", fileId.ToString()) });
}
private Menu BuildMenu(List<string> lines, int index)
{
var prompt = lines[index];
index++;
List<MenuChoice> choices = new List<MenuChoice>();
while (index < lines.Count)
{
choices.Add(new MenuChoice(lines[index], lines[index + 1]));
index += 2;
}
return new Menu(prompt, choices);
}
private IMenu BuildPassword(string filepath)
{
return new PasswordMenu(filepath);
}
}
}