-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMistakeList.cs
More file actions
130 lines (115 loc) · 3.43 KB
/
MistakeList.cs
File metadata and controls
130 lines (115 loc) · 3.43 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
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
123
124
125
126
127
128
129
130
using System;
using System.Collections.Generic;
using System.IO;
namespace RapChessGui
{
class EMistake
{
public string fen = string.Empty;
public string moves = string.Empty;
public int rep = 2;
public string SaveToStr()
{
return $"{fen},{moves},{rep}";
}
public bool LoadFromStr(string str)
{
string[] strArr = str.Split(',');
if (strArr.Length < 3)
return false;
fen = strArr[0];
moves = strArr[1];
int.TryParse(strArr[2], out rep);
return true;
}
public string GetMove(int i)
{
string[] arrs = moves.Split();
if (i >= arrs.Length)
return string.Empty;
return arrs[i];
}
}
internal class MistakeList : List<EMistake>
{
readonly string fileName = @"Data\mistakes.csv";
public MistakeList()
{
LoadFromFile();
}
public void Add(string fen, string moves)
{
EMistake em = new EMistake() { fen = fen, moves = moves };
Insert(0, em);
SaveToFile();
}
///<summary>
///deletes the given fens
///</summary>
public bool Delete(string fen)
{
for (int n = Count - 1; n >= 0; n--)
if (this[n].fen == fen)
{
RemoveAt(n);
return true;
}
return false;
}
public void SaveToFile()
{
using (FileStream fs = File.Open(fileName, FileMode.Create, FileAccess.Write, FileShare.None))
using (StreamWriter sw = new StreamWriter(fs))
{
int c = 0;
int limit = (int)FormChess.formOptions.nudPuzzleRepetition.Value;
foreach (EMistake em in this)
if (em.rep > 0)
{
sw.WriteLine(em.SaveToStr());
if ((limit > 0) && (++c >= limit))
break;
}
}
}
public bool LoadFromFile()
{
Clear();
if (!File.Exists(fileName))
return false;
using (FileStream fs = File.Open(fileName, FileMode.OpenOrCreate, FileAccess.Read, FileShare.Read))
using (StreamReader reader = new StreamReader(fs))
{
int limit = (int)FormChess.formOptions.nudPuzzleRepetition.Value;
string line = String.Empty;
while ((line = reader.ReadLine()) != null)
{
EMistake em = new EMistake();
if (em.LoadFromStr(line))
Add(em);
if ((limit > 0) && (Count >= limit))
break;
}
}
return Count > 0;
}
public void Success(bool fail)
{
EMistake em = this[0];
if (fail)
em.rep = 2;
else
em.rep--;
RemoveAt(0);
Add(em);
SaveToFile();
}
void SortEpd()
{
Sort(delegate (EMistake m1, EMistake m2)
{
return String.Compare(m1.fen, m2.fen, StringComparison.Ordinal);
});
}
}
}