-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCData.cs
More file actions
667 lines (598 loc) · 20.1 KB
/
CData.cs
File metadata and controls
667 lines (598 loc) · 20.1 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Windows.Forms;
namespace RapChessGui
{
public enum CGameMode { game, match, tourB, tourE, tourP, training, puzzle, edit, none }
public enum CProtocol { uci, xb, auto, unknow }
public enum CLimitKind { standard, time, depth, nodes, infinite }
public enum CColor { none, white, black }
public static class CWinMessage
{
[DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int wMsg, IntPtr wParam, IntPtr lParam);
private readonly static object locker = new object();
public static IntPtr winHandle;
public static int Message(int msg)
{
lock (locker)
{
return SendMessage(winHandle, msg, IntPtr.Zero, IntPtr.Zero);
}
}
}
public static class CPromotion
{
public static string umo;
public static int sou;
public static int des;
}
public static class CDrag
{
public static bool dragged = false;
public static int last = -1;
public static int lastSelected = -1;
public static int lastSou = -1;
public static int lastDes = -1;
public static int mouseX = 0;
public static int mouseY = 0;
public static int mouseIndex = -1;
}
public static class Global
{
public static int elo = 1500;
public static decimal value = 60;
public static string none = "None";
public static string human = "Human";
public static string puzzle = "Puzzle";
public static string limit = "Standard";
}
public static class CGames
{
public static int played = 0;
public static int draw = 0;
public static int time = 0;
public static int error = 0;
public static void NewSession()
{
played = 0;
draw = 0;
time = 0;
error = 0;
}
public static string Text
{
get
{
return $"RapChessGui Games {played} Draws {draw} Time out {time} Errors {error}";
}
}
}
public class CError : List<int>
{
int Sum()
{
int result = 0;
foreach (int i in this)
result += (i + 1);
return result;
}
public double Errors()
{
int s = Sum();
if (s == 0)
return 0;
return ((Count - 1) * 100.0) / s;
}
public void AddGame(bool error)
{
if (Count == 0)
Add(0);
if (error)
Add(0);
else if (this[Count - 1] < int.MaxValue)
this[Count - 1]++;
if (((Sum() % 100) == 0) || (Count > FormChess.formOptions.nudHistory.Value))
RemoveRange(0, 1);
}
public void LoadFromStr(string str)
{
Clear();
string[] arr = str.Split(new[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
foreach (string e in arr)
Add(Convert.ToInt32(e));
}
}
public static class CData
{
public static List<string> fileBookReader = new List<string>();
public static List<string> folderEngine = new List<string>();
public static string ProtocolToStr(CProtocol p)
{
switch (p)
{
case CProtocol.uci:
return "UCI";
case CProtocol.xb:
return "XB";
case CProtocol.auto:
return "Auto";
default:
return "Unknown";
}
}
public static string TextBeauty(string text)
{
TextInfo ti = new CultureInfo("en-US", false).TextInfo;
string p = text.Replace('_', ' ').Trim();
return ti.ToTitleCase(p);
}
public static void ComboSelect(ComboBox cb, string n)
{
int i = cb.FindStringExact(n);
if (i < 0)
i = 0;
cb.SelectedIndex = i;
}
public static CProtocol StrToProtocol(string p)
{
switch (p)
{
case "UCI":
return CProtocol.uci;
case "XB":
return CProtocol.xb;
case "Auto":
return CProtocol.auto;
default:
return CProtocol.unknow;
}
}
public static string MakeShort(string name)
{
int f = 0;
string result = string.Empty;
for (int n = 0; n < name.Length; n++)
{
char c = name[n];
if ((f == 0) || Char.IsUpper(c) || Char.IsNumber(c))
result += Char.ToUpper(c);
f++;
if (c == ' ')
f = 0;
}
return result;
}
public static void UpdateBookReader()
{
fileBookReader.Clear();
string[] arrBooks = Directory.GetFiles("Books", "*.exe");
for (int n = 0; n < arrBooks.Length; n++)
{
string fn = Path.GetFileName(arrBooks[n]);
fileBookReader.Add(fn);
}
}
public static List<string> ListExe(string path)
{
List<string> list = new List<string>();
if (Directory.Exists(path))
{
string[] filePaths = Directory.GetFiles(path, "*.exe");
for (int n = 0; n < filePaths.Length; n++)
{
string fn = Path.GetFileName(filePaths[n]);
list.Add(fn);
}
}
return list;
}
public static void FillComboBox(ComboBox cb, List<string> list)
{
cb.Items.Clear();
cb.Sorted = true;
foreach (string s in list)
cb.Items.Add(s);
cb.Sorted = false;
cb.Items.Insert(0, Global.none);
cb.SelectedIndex = 0;
}
public static void UpdateFolderEngine()
{
folderEngine.Clear();
string[] arr = Directory.GetDirectories("Engines");
for (int n = 0; n < arr.Length; n++)
{
string fn = Path.GetFileName(arr[n]);
folderEngine.Add(fn);
}
}
}
public static class CElo
{
public static int eloTotal = 4000;
public static int eloMin = 50;
public static int eloMax = eloTotal - eloMin;
public static int eloRange = eloMax - eloMin;
static double Probability(double rating1, double rating2)
{
return 1.0 / (1.0 + Math.Pow(10.0, (rating2 - rating1) / 400.0));
}
public static void EloRating(double oldEloWin, double oldEloLoose, out double newEloWin, out double newEloLoose, int gw, int gl, bool draw)
{
int Kmin = 0xf;
int Kmax = 0xf0;
if (gw < Kmin) gw = Kmin;
if (gw > Kmax) gw = Kmax;
if (gl < Kmin) gl = Kmin;
if (gl > Kmax) gl = Kmax;
double fw = (double)gw / 0xff;
double fl = (double)gl / 0xff;
fw = (1.0 - fw) * 32.0;
fl = (1.0 - fl) * 32.0;
double pro = Probability(oldEloWin, oldEloLoose);
if (draw)
{
newEloWin = oldEloWin + fw * (0.5 - pro);
newEloLoose = oldEloLoose + fl * (pro - 0.5);
}
else
{
newEloWin = oldEloWin + fw * (1 - pro);
newEloLoose = oldEloLoose - fl * (1 - pro);
if (oldEloWin - oldEloLoose > 300)
{
newEloWin = oldEloWin;
newEloLoose = oldEloLoose;
}
}
if (newEloWin > eloMax)
newEloWin = eloMax;
if (newEloLoose > eloMax)
newEloLoose = eloMax;
if (newEloWin < eloMin)
newEloWin = eloMin;
if (newEloLoose < eloMin)
newEloLoose = eloMin;
}
public static void EloRating(double oldEloWin, double oldEloLoose, out int newEloWin, out int newEloLoose, int Ga, int Gb, bool draw)
{
EloRating(oldEloWin, oldEloLoose, out double dNewEloA, out double dNewEloB, Ga, Gb, draw);
newEloWin = Convert.ToInt32(dNewEloA);
newEloLoose = Convert.ToInt32(dNewEloB);
}
public static double EloOpt(double ratingPlayer1, double ratingPlayer2, double gw, double gl, double gd)
{
double Kmin = 0xf;
double Kmax = 0xf0;
double games = gw + gl + gd;
if (games < Kmin)
games = Kmin;
if (games > Kmax)
games = Kmax;
double factor = (1.0 - games / 0xff) * 32.0;
double probabilityWinPlayer1 = Probability(ratingPlayer1, ratingPlayer2);
ratingPlayer1 += gw * factor * (1.0 - probabilityWinPlayer1);
ratingPlayer1 += gl * factor * -probabilityWinPlayer1;
ratingPlayer1 += gd * factor * (0.5 - probabilityWinPlayer1);
if (ratingPlayer1 < eloMin)
ratingPlayer1 = eloMin;
if (ratingPlayer1 > eloMax)
ratingPlayer1 = eloMax;
return ratingPlayer1;
}
}
public class CModeTournament
{
public bool rotate = true;
public int reps = 0;
public int left = 0;
public string first = String.Empty;
public string opponent = String.Empty;
public string clicked = String.Empty;
public void NewGame()
{
rotate = true;
reps = 0;
left = 0;
opponent = String.Empty;
CGames.NewSession();
}
}
public class CLimitValue
{
public static CLimitKind defKind = CLimitKind.standard;
public static int defValue = 10;
public CLimitKind kind = defKind;
public int baseVal = defValue;
public int baseInc = 0;
public static CLimitKind StrToLimit(string kind)
{
switch (kind)
{
case "Standard":
return CLimitKind.standard;
case "Time":
return CLimitKind.time;
case "Depth":
return CLimitKind.depth;
case "Nodes":
return CLimitKind.nodes;
default:
return CLimitKind.infinite;
}
}
public static string LimitToStr(CLimitKind kind)
{
switch (kind)
{
case CLimitKind.standard:
return "Standard";
case CLimitKind.depth:
return "Depth";
case CLimitKind.nodes:
return "Nodes";
case CLimitKind.time:
return "Time";
default:
return "Infinite";
}
}
public static int GetIncrement(CLimitKind kind)
{
switch (kind)
{
case CLimitKind.standard:
return 15;
case CLimitKind.depth:
return 1;
case CLimitKind.nodes:
return 100000;
case CLimitKind.infinite:
return 0;
default:
return 100;
}
}
public void SetLimit(string str)
{
kind = StrToLimit(str);
}
public string GetLimit()
{
return LimitToStr(kind);
}
public void SetValue(int v)
{
int inc = GetIncrement();
if (inc > 0)
baseVal = v / inc;
else
baseVal = 0;
}
public int GetValue()
{
int inc = GetIncrement();
return baseVal > 0 ? baseVal * inc : inc;
}
public int GetBaseInc()
{
if (kind == CLimitKind.standard)
return baseInc;
return 0;
}
public int GetUciValue()
{
int result = baseVal * GetIncrement();
if (result < 0)
result = 1;
if (kind == CLimitKind.standard)
result *= 1000;
return result;
}
public int GetIncrement()
{
return GetIncrement(kind);
}
public string GetUci()
{
int v = GetValue();
switch (kind)
{
case CLimitKind.standard:
return $"go wtime {v} btime {v} winc 0 binc 0";
case CLimitKind.depth:
return $"go depth {v}";
case CLimitKind.nodes:
return $"nodes {v}";
case CLimitKind.infinite:
return "infinite";
default:
return $"go movetime {v}";
}
}
public string GetTip()
{
switch (kind)
{
case CLimitKind.standard:
return "Base for whole game in seconds";
case CLimitKind.depth:
return "Depth in half-moves";
case CLimitKind.nodes:
return "Maximum nodes per move";
case CLimitKind.infinite:
return "Infinite mode until click stop";
default:
return "Time per move in miliseconds";
}
}
public string ShortName()
{
string mode = GetLimit();
string result = mode[0].ToString();
if (mode != "Infinite")
result = $"{result}{baseVal}";
return $" {result}";
}
public string LongName()
{
int inc = GetIncrement();
int bVal = baseVal * inc;
int bInc = baseInc;
string mode = GetLimit();
if (mode == "Standard")
{
int i1 = bVal / 60;
int i2 = (bVal % 60) / 15;
int i3 = bInc / 1000;
int i4 = (bInc % 1000) / 250;
string[] q = new string[4] { "", "¼", "½", "¾" };
int t = bVal + (bInc * 60) / 1000;
int m = bVal / 4;
string tim = $"{i1}{q[i2]}+{i3}{q[i4]}";
if (t > 21600)
return $"Mail {tim}";
if (t > 1800)
return $"Classical {tim}";
if (t > 600)
return $"Rapid {tim}";
if (t > 180)
return $"Blitz {tim}";
if (t > 30)
return $"Bullet {tim}";
return $"UltraBullet {tim}";
}
if (mode != "Infinite")
return $"{mode} {baseVal}";
return mode;
}
}
public class CElement
{
public int position = 0;
public string name = string.Empty;
public CHisElo history = new CHisElo();
public int Elo
{
get
{
return history.Last();
}
set
{
if (history.Count == 0)
history.Add(value);
else
history[history.Count - 1] = value;
}
}
public virtual int ClearHistory()
{
int elo = Elo;
history.Clear();
Elo = elo;
return CModeTournamentE.tourList.DeletePlayer(name);
}
public double EvaluateOpponent3(CElement second, double listCount, CTourList tourList)
{
double sElo = second.Elo;
double allGames = tourList.CountGames(name);
int curGames = tourList.CountGames(second.name, name, out int gw, out int gl, out int gd) + 1;
int sPro = (gw * 200 + gd * 100) / curGames - 100;
double r = curGames == 0 ? 0 : (gw - gl) / curGames;
double nElo = sElo + r * Math.Abs(Elo - sElo);
double ratioElo = (Math.Abs(sElo - nElo) / CElo.eloRange);
double maxCount = Math.Sqrt(allGames * 2) + 1;
double maxRange = Math.Min(listCount + 1, maxCount);
double avgCount = allGames / maxRange;
double delCount = (avgCount * 2) / maxRange;
double optCount = maxCount - second.position * delCount + 1;
double ratioCount = allGames == 0 ? 0 : (optCount - curGames) / maxCount;
double ratioDistance = (listCount - second.position) / listCount;
double ratioOrder = allGames == 0 ? 0 : (sPro > 0) == (sElo < Elo) ? 1.0 : (sPro == 0) ? 0.8 : (sElo == Elo) ? 0.2 : 0;
return ratioCount + ratioDistance + ratioElo + ratioOrder * 2;
}
public double EvaluateOpponent(CElement second, double listCount, CTourList tourList)
{
double sElo = second.Elo;
double allGames = tourList.CountGames(name);
double curGames = tourList.CountGames(second.name, name, out int gw, out int gl, out int gd);
double sPro = curGames == 0 ? 0 : (gw - gl) / curGames;
double maxCount = Math.Sqrt(allGames * 2) + 1;
double optCount = maxCount * (listCount - second.position) / listCount;
double ratioCount = allGames == 0 ? 0 : (optCount - curGames) / maxCount;
double ratioOrder = curGames == 0 ? 0 : (sPro > 0) == (Elo > sElo) ? 1.0 : (sPro == 0) ? 0.8 : (sElo == Elo) ? 0.2 : 0;
//FormChess.log.Add($"Opponent {second.name} Optimal {optCount:0.00} Count {ratioCount:0.00} Order {ratioOrder:0.00} Total {ratioCount+ ratioOrder:0.00}");
return ratioCount + ratioOrder;
}
public void SetElo(string e)
{
if (int.TryParse(e, out int elo))
Elo = elo;
}
}
public class ListViewComparer : System.Collections.IComparer
{
readonly private int ColumnNumber;
readonly private SortOrder SortOrder;
public ListViewComparer(int column_number,
SortOrder sort_order)
{
ColumnNumber = column_number;
SortOrder = sort_order;
}
public int Compare(object object_x, object object_y)
{
ListViewItem item_x = object_x as ListViewItem;
ListViewItem item_y = object_y as ListViewItem;
string string_x;
if (item_x.SubItems.Count <= ColumnNumber)
{
string_x = String.Empty;
}
else
{
string_x = item_x.SubItems[ColumnNumber].Text;
}
string string_y;
if (item_y.SubItems.Count <= ColumnNumber)
{
string_y = String.Empty;
}
else
{
string_y = item_y.SubItems[ColumnNumber].Text;
}
int result;
if (double.TryParse(string_x, out double double_x) && double.TryParse(string_y, out double double_y))
{
result = double_x.CompareTo(double_y);
}
else
{
if (DateTime.TryParse(string_x, out DateTime date_x) &&
DateTime.TryParse(string_y, out DateTime date_y))
{
result = date_x.CompareTo(date_y);
}
else
{
result = string_x.CompareTo(string_y);
}
}
if (SortOrder == SortOrder.Ascending)
{
return result;
}
else
{
return -result;
}
}
}
}