-
Notifications
You must be signed in to change notification settings - Fork 0
/
Admin_EndGameRelatedFunctionality.cs
149 lines (122 loc) · 4.9 KB
/
Admin_EndGameRelatedFunctionality.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
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static Chess___netanel.ChessUtilities;
namespace Chess___netanel
{
partial class Admin
{
int[] endGameAfterMath = new int[2]; //[0] how the game ended - 0 regular ending | 1 draw | 2 for win on time | 3 for resignation
//[1] losing color - 1 for white | 2 for black
//these three functions are triggered by UI button events.
public void DrawRequested()
{
didBlackRequestDraw = blackTimer.Enabled;
UIForm.ToggleVisibilityDrawRequestPanel(didBlackRequestDraw ? "White" : "Black");
}
public void fiftyMoveRuleEndgame()
{
endGameAfterMath[0] = 1;
gameEndsOnDraw("---Draw: 50 move repetition rule---");
}
public void GameEndsOnResign()
{
bool blackResigned = blackTimer.Enabled;
string resignMessage;
resignMessage = (blackResigned ? "Black" : "White") + " Resigned!";
endGameAfterMath[0] = 3; // resignation
endGameAfterMath[1] = blackResigned ? 2 : 1; // 2 black lost, 1 white lost
sendEndGameToUI(resignMessage);
}
public void DrawAccepted()
{
gameEndsOnDraw("draw - requested by " + (didBlackRequestDraw ? "Black" : "White"));
}
private void gameEndsOnCheckMate(Colors loser)
{
endGameAfterMath[0] = 0; //checkmate
endGameAfterMath[1] = (loser == Colors.Black) ? 2 : 1;
string losermessage = ((loser == Colors.Black) ? "Black" : "White") + " lost - checkmate!";
sendEndGameToUI(losermessage);
}
private void gameEndsOnTime(Colors loser)
{
InsufficientMaterialTag timeWinnerMaterial = calculateMaterialForOneSide(collectionsOfPiecesOnBoard[(int)getOppositeColor(loser)]);
bool isTimeWinnerInsuf =
(timeWinnerMaterial != InsufficientMaterialTag.Sufficient)
&&
((timeWinnerMaterial == InsufficientMaterialTag.King)
||
(timeWinnerMaterial == calculateMaterialForOneSide(collectionsOfPiecesOnBoard[(int)loser])));
if (isTimeWinnerInsuf)
{
gameEndsOnDraw("---Draw: Out of time vs. insufficient material---");
return;
}
string losermessage = ((loser == Colors.Black) ? "Black" : "White") + " lost on time!";
endGameAfterMath[1] = (loser == Colors.Black) ? 2 : 1;
sendEndGameToUI(losermessage);
}
private void gameEndsOnDraw(string message)
{
endGameAfterMath[0] = 1;
sendEndGameToUI(message);
}
private void staleMateEndGame()
{
endGameAfterMath[0] = 1;
sendEndGameToUI("---Draw: stalemate---");
}
private void threeRepetitionRuleEndGame()
{
endGameAfterMath[0] = 1;
sendEndGameToUI("---Draw: 3 move repetition rule---");
}
private void insufficientMaterialEndgame()
{
endGameAfterMath[0] = 1;
sendEndGameToUI("---Draw: Insufficient material---");
}
private void sendEndGameToUI(string message)
{
StopTheClocksExclemationPoint();
Invocation2 delg = UIForm.TriggerEndGameForm;
string result = "";
bool whiteLost = endGameAfterMath[1] == 1;
string losingColor = whiteLost ? "White" : "Black";
switch (endGameAfterMath[0])
{
case 1: //game ends on draw
result += "1/2-1/2";
break;
case 2: //game ends on time
break;
case 3: //game ends on resignation
result += losingColor + " " + "resigned. " + (whiteLost ? "0:1" : "1:0");
break;
case 0: //check mate
result += "# " + (whiteLost ? "0:1" : "1:0");
break;
}
moveList.Add(new ChessMove { IsEndGameInfo = true, EndGameInfo = result });
UIForm.Invoke(delg, new EndGamePackage { Message = message, MoveList = moveListToString() });
}
private string moveListToString()
{
string result = "";
int iterator = 1;
for (int i = 0; i < moveList.Count; i += 2)
{
result += iterator + "." + " " + moveList[i].ToString();
if ((i + 1) < moveList.Count)
result += " " + moveList[i + 1].ToString();
result += "\r\n";
iterator++;
}
return result;
}
}
delegate void Invocation2(EndGamePackage pack);
}