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
|
unit UCedule;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes,
System.Generics.Collections, System.Math, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ComCtrls, Vcl.Buttons;
type
TPartie = class(TObject)
private
FVisiteur: String;
FHote: String;
public
property Visiteur: String read FVisiteur write FVisiteur;
property Hote: String read FHote write FHote;
end;
TParties = class(TObjectList<TPartie>)
public
function AjouterPartie(UnVisiteur, UnHote: String): TPartie;
end;
TJour = class(TObject)
private
FDate: TDate;
FParties: TParties;
public
property Date: TDate read FDate write FDate;
property Parties: TParties read FParties write FParties;
end;
TCedule = class(TObjectList<TJour>)
public
function AjouterJour: TJour;
end;
TForm1 = class(TForm)
Memo1: TMemo;
MonthCalendar1: TMonthCalendar;
BitBtn1: TBitBtn;
procedure BitBtn1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure MonthCalendar1Click(Sender: TObject);
{ procedure MonthCalendar1Click(Sender: TObject);}
private
{ Déclarations privées }
public
{ Déclarations publiques }
end;
var
Form1: TForm1;
Cedule: TCedule;
Bingo: Boolean;
DebutSaison, FinSaison: TDate;
implementation
{$R *.dfm}
function TParties.AjouterPartie(UnVisiteur, UnHote: String): TPartie;
begin
Result:=TPartie.Create;
Result.Visiteur := UnVisiteur;
Result.Hote := UnHote;
Add(Result);
end;
function TCedule.AjouterJour: TJour;
begin
Result := TJour.Create;
Add(Result);
end;
procedure TForm1.BitBtn1Click(Sender: TObject);
function CreerDesParties(DesEquipes: TStrings): TParties;
var
i, j: Cardinal;
begin
Result:=TParties.Create;
for i:=1 to Pred(DesEquipes.Count) do
begin
j:=0;
while (j+i)<DesEquipes.Count do
begin
Result.AjouterPartie(DesEquipes[j+i], DesEquipes[j]);
Inc(j);
end;
end;
end;
function VerifDispo(UneEquipe: String; UnJour: TJour): Boolean;
var
i: Cardinal;
begin
Result:=True;
for i:=0 to Pred(UnJour.Parties.Count) do
if (UnJour.Parties[i].Visiteur=UneEquipe)or(UnJour.Parties[i].Hote=UneEquipe) then
begin
Result:=False;
Break
end;
end;
function VerifDispoEx(UneEquipe: String; UnJour: TJour): Boolean;
var
i: Cardinal;
begin
Result:=True;
for i:=0 to Pred(UnJour.Parties.Count) do
if (UnJour.Parties[i].Visiteur=UneEquipe) then
begin
Result:=False;
Break
end;
end;
function VerifDispoDom(UneEquipe: String; UnJour: TJour): Boolean;
var
i, j: Cardinal;
begin
Result:=True;
j:=0;
for i:=0 to Pred(UnJour.Parties.Count) do
if (UnJour.Parties[i].Hote=UneEquipe) then
begin
Inc(j);
if j=2 then
begin
Result:=False;
Break
end;
end;
end;
function VerifQuatre(UnePartie: TPartie; UneCedule: TCedule; UnJour: Cardinal): Boolean;
var i, j, EV, EH: Cardinal;
begin
Result:=True;
EV:=0;
EH:=0;
for i:=UnJour to Min(Pred(UneCedule.Count), UnJour+3) do
begin
if Assigned(UneCedule[i].Parties) then
for j:=0 to Pred(UneCedule[i].Parties.Count) do
begin
if (UneCedule[i].Parties[j].Visiteur=UnePartie.Visiteur) or (UneCedule[i].Parties[j].Hote=UnePartie.Visiteur) then
Inc(EV);
if (UneCedule[i].Parties[j].Visiteur=UnePartie.Hote) or (UneCedule[i].Parties[j].Hote=UnePartie.Hote) then
Inc(EH);
if (EV>3)or(EH>3)then
Break;
end;
if (EV>3)or(EH>3)then
begin
Result:=False;
Break;
end;
end;
end;
var
i: Cardinal;
j, JourTentatif: Integer;
Parties: TParties;
CeduleTemp: TCedule;
Possible: Boolean;
begin
if Memo1.Lines.Count<2 then
begin
ShowMessage('Inscrivez au moins deux équipes');
Exit
end;
Parties:=CreerDesParties(Memo1.Lines);
Memo1.Clear;
CeduleTemp:=TCedule.Create;
DebutSaison:=MonthCalendar1.Date;
CeduleTemp.AjouterJour;
for i:=0 to Pred(Parties.Count) do
begin
JourTentatif:=0;
Possible:=False;
while not Possible do
begin
Possible:=True;
{Vérifier si le visiteur joue la veille, ce jour-là ou le lendemain}
for j:=Max(Pred(JourTentatif), 0) to Min(Pred(CeduleTemp.Count), Succ(JourTentatif)) do
begin
if Assigned(CeduleTemp[j].Parties) then
begin
Possible:=VerifDispo(Parties[i].Visiteur, CeduleTemp[j]);
if not Possible then
Break;
end;
end;
if Possible then
{Vérifier si l'hôte joue à l'extérieur, la veille, ce jour-là ou le lendemain}
for j:=Max(Pred(JourTentatif), 0) to Min(Pred(CeduleTemp.Count), Succ(JourTentatif)) do
begin
if Assigned(CeduleTemp[j].Parties) then
begin
Possible:=VerifDispoEx(Parties[i].Hote, CeduleTemp[j]);
if not Possible then
Break;
end;
end;
if Possible and Assigned(CeduleTemp[JourTentatif].Parties) then
{Vérifier si l'hôte joue plus d'une partie ce jour-là}
if CeduleTemp[JourTentatif].Parties.Count>1 then
Possible:=VerifDispoDom(Parties[i].Hote, CeduleTemp[JourTentatif]);
{Vérifier le nombre de parties en quatre jours}
if Possible then
for j:=Max(0, JourTentatif-3) to JourTentatif do
if VerifQuatre(Parties[i], CeduleTemp, j)=False then
begin
Possible:=False;
break;
end;
if not Possible then
begin
if JourTentatif=Pred(CeduleTemp.Count) then
CeduleTemp.AjouterJour;
Inc(JourTentatif);
end;
end;
if not Assigned(CeduleTemp[JourTentatif].Parties)then
CeduleTemp[JourTentatif].Parties:=TParties.Create;
CeduleTemp[JourTentatif].Parties.AjouterPartie(Parties[i].Visiteur, Parties[i].Hote);
end;
Parties.Free;
j:=0;
for i:=Pred(CeduleTemp.Count)downto 0 do
begin
Cedule.AjouterJour;
Cedule[j]:=CeduleTemp[i];
Cedule[j].Date:=DebutSaison+j;
Inc(j);
end;
FinSaison:=DebutSaison+j;
CeduleTemp.Free;
for i:=0 to Pred(Cedule[0].Parties.Count) do
Memo1.Lines.Add(Cedule[0].Parties[i].Visiteur+' @ '+Cedule[0].Parties[i].Hote);
Caption:=FormatDateTime('d mmmm yyyy', Cedule[0].Date);
Bingo:=True;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Cedule:=TCedule.Create;
Bingo:=False;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
Cedule.Free;
end;
procedure TForm1.MonthCalendar1Click(Sender: TObject);
var
i: Cardinal;
begin
Caption:=FormatDateTime('d mmmm yyyy', MonthCalendar1.Date);
if Bingo then
begin
Memo1.Clear;
if MonthCalendar1.Date<DebutSaison then
Memo1.Text:='La saison commence le '+FormatDateTime('d mmmm yyyy', DebutSaison)
else
if MonthCalendar1.Date>=FinSaison then
Memo1.Text:='La saison est terminée depuis le '+FormatDateTime('d mmmm yyyy', FinSaison-1)
else
if not Assigned(Cedule[Trunc(MonthCalendar1.Date-DebutSaison)].Parties)then
Memo1.Text:='Pas de partie aujourd''hui'
else
for i:=0 to Pred(Cedule[Trunc(MonthCalendar1.Date-DebutSaison)].Parties.Count) do
Memo1.Lines.Add(Cedule[Trunc(MonthCalendar1.Date-DebutSaison)].Parties[i].Visiteur+' @ '+Cedule[Trunc(MonthCalendar1.Date-DebutSaison)].Parties[i].Hote);
end;
end;
end. |
Partager