-
Notifications
You must be signed in to change notification settings - Fork 0
/
PokeShiriManager.cs
110 lines (89 loc) · 2.8 KB
/
PokeShiriManager.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
using System;
using System.Collections.Generic;
using System.IO;
namespace Artemis.PokemonShiritori
{
public enum Result
{
Done,
WrongInitial,
AlreadySaid,
NoPokemon,
Finish
}
public class PokeShiriManager
{
private static Dictionary<char, List<string>> _namesList;
private Dictionary<char, List<string>> _saidList = new Dictionary<char, List<string>>();
public char nextChar;
public PokeShiriManager()
{
SetRandomInit();
//nextChar = 'a';
for (char c = 'a'; c <= 'z'; c++)
{
_saidList.Add(c, new List<string>());
}
if(_namesList == null)
{
FillDictionary();
}
}
public Result Say(string name)
{
name = name.ToLower().Trim();
if(name[0] != nextChar)
return Result.WrongInitial;
if(_saidList[nextChar].Contains(name))
return Result.AlreadySaid;
if(!_namesList[nextChar].Contains(name))
return Result.NoPokemon;
_saidList[nextChar].Add(name);
/*nextChar = name[name.Length - 1];
for (int i = 2; !InitAvailable(nextChar); i++)
{
nextChar = name[name.Length - i];
if (i > name.Length)
return Result.Finish;
}*/
/*{
int i = 1;
do
{
nextChar = name[name.Length - i];
i++;
if(i > name.Length)
return Result.Finish;
} while(!InitAvailable(nextChar));
}*/
for (int i = 1; i <= name.Length; i++)
{
nextChar = name[name.Length - i];
if(InitAvailable(nextChar))
return Result.Done;
}
return Result.Finish;
}
public void SetRandomInit()
{
nextChar = (char)new Random().Next(0x61, 0x7b);
}
private bool InitAvailable(char init) =>
(_namesList[init].Count > _saidList[init].Count);
private static void FillDictionary()
{
_namesList = new Dictionary<char, List<string>>();
var names = File.ReadAllLines("pokemonlist.txt");
int nameIndex = 0;
for (char c = 'a'; c <= 'z'; c++)
{
var temp = new List<string>();
for(; nameIndex < names.Length && names[nameIndex][0] == c; nameIndex++)
{
temp.Add(names[nameIndex]);
}
_namesList.Add(c, temp);
}
}
}
}