|
Oct
13
|
|
J’ai eu envie de faire un peu de coloration syntaxique sur mon blog… dans un langage que je n’avais pas encore testé avec GeSHiW…
alors, j’ai ressorti un vieux dossier :
- il y a deux ans et demi, un défi avait eu lieu pour développer un jeu de Puissance 4W
- il fallait juste utiliser Delphi
Voici le source que j’avais présenté un peu trop tard :
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 | program wp4c; {$APPTYPE CONSOLE} uses Windows, SysUtils; {$R wp4c.res} type TAuthorizedChars = set of Char; const BG = 1; FG = 15; J1 = 12; J2 = 14; COL = 10; WINJ1 = 3; WINJ2 = 4; DRAW = 5; var hci : THandle; // handle de l'input clavier nextPlayer : Smallint; // Joueur qui va jouer ou 0 en fin de partie key : char; // Touche frappée en cours de jeu lastRows : string; // Hauteur de jetons par colonne lastRow : Smallint; // Dernière ligne jouée lastCol : Smallint; // Dernière colonne jouée grid : array[1..7] of array[1..6] of Smallint; // Matrice du jeu (0=vide;1|2=J1|J2) procedure SetColor(fg: byte; bg: byte); begin SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),fg + (bg * 16)); end; procedure ClrScr; var position : TCoord; h : HWND; sbi : TConsoleScreenBufferInfo; len: LongWord; nbr_ecrits: LongWord; begin position.X := 0; position.Y := 0; h := GetStdHandle(STD_OUTPUT_HANDLE); GetConsoleScreenBufferInfo(h, sbi); len := sbi.dwSize.x * sbi.dwSize.y; FillConsoleOutputCharacter(h, #32, len, position, nbr_ecrits); FillConsoleOutputAttribute(h, sbi.wAttributes, len, position, nbr_ecrits); SetConsoleCursorPosition(h, position); end; {$ENDREGION} function GetKey(authorized: TAuthorizedChars): char; var noer : DWord; buffer : TInputRecord; begin FlushConsoleInputBuffer(hci); repeat noer := 0; ReadConsoleInput(hci, buffer, 1, noer); until (buffer.EventType = KEY_EVENT) and (buffer.Event.KeyEvent.AsciiChar in authorized); WriteLn(buffer.Event.KeyEvent.AsciiChar); Sleep(250); Result := buffer.Event.KeyEvent.AsciiChar; end; procedure PrintGrid(); var x, y : Smallint; begin ClrScr; for y := 6 downto 1 do begin for x := 1 to 7 do begin SetColor(FG, BG); Write('|'); case grid[x,y] of 1: begin SetColor(J1, BG); end; 2: begin SetColor(J2, BG); end else begin SetColor(BG, BG); end; end; Write('o'); end; SetColor(FG, BG); Writeln('|'); end; for x := 1 to 7 do begin if (x = lastCol) then begin SetColor(COL, BG); end else begin SetColor(FG, BG); end; Write(' ' + IntToStr(x)); end; Writeln(''); end; procedure InitGame(); var x, y : Smallint; begin // Affichage SetColor(FG, BG); ClrScr; Writeln('Qui commence ? (1|2) '); // Données nextPlayer := StrToInt(GetKey(['1', '2'])); for y := 1 to 6 do begin for x := 1 to 7 do begin grid[x,y] := 0; end; end; lastRows := '0000000'; lastCol := 0; PrintGrid; end; function HasWin():Boolean; function HowMany(dX, dY: Smallint): Smallint; var x, y : Smallint; begin Result := 0; x := lastCol + dX; y := lastRow + dY; while ((1<=x) AND (x<=7) AND (1<=y) AND (y<=6)) do begin if (grid[x, y] = nextPlayer) then begin Inc(Result); x := x + dX; y := y + dY; end else begin Exit; end; end; end; begin // Vertical if (HowMany(0, -1) >= 3) then begin Result := True; end // Horizontal else if ((HowMany(1, 0)+HowMany(-1, 0)) >= 3) then begin Result := True; end // SO - NE else if ((HowMany(-1, -1)+HowMany(1, 1)) >= 3) then begin Result := True; end // NO - SE else if ((HowMany(-1, 1)+HowMany(1, -1)) >= 3) then begin Result := True; end // Pas gagnant ! else begin Result := False; end; end; //----------------------------------------------------------------------- begin // Récupération du handle du clavier hci := GetStdHandle(STD_INPUT_HANDLE); repeat // Nouvelle grille InitGame; // Début concrêt de la partie while ((nextPlayer = 1) or (nextPlayer = 2)) do begin case nextPlayer of 1: begin SetColor(J1, BG); Write('Joueur 1 ? (1-7|0) '); end; 2: begin SetColor(J2, BG); Write('Joueur 2 ? (1-7|0) '); end; end; key := GetKey(['0','1','2','3','4','5','6','7']); case key of '0': begin nextPlayer := 0; end else begin lastCol := StrToInt(key); lastRow := StrToIntDef(lastRows[lastCol], 6) + 1; // Colonne pleine ? if (lastRow<>7) then begin // Ajouter dernier jeton / colonne lastRows[lastCol] := IntToStr(lastRow)[1]; // Ajouter jeton grid[lastCol, lastRow] := nextPlayer; Beep; // Vérifie si le joueur gagné if HasWin then begin nextPlayer := nextPlayer + 2; end else begin // On vérifie si le jeu est rempli if lastRows = '6666666' then begin nextPlayer := DRAW; end else begin // Changer de joueur // 2->1 1->2 Dec(nextPlayer); nextPlayer := 1 - nextPlayer; Inc(nextPlayer); end; end; PrintGrid; end; end; end; end; Writeln(''); // Si un joueur a gagné case nextPlayer of WINJ1: // J1 gagne begin SetColor(J1, BG); Writeln('Le joueur 1 gagne !'); end; WINJ2: // J2 gagne begin SetColor(J2, BG); Writeln('Le joueur 2 gagne !'); end; DRAW: // Match nul begin SetColor(FG, BG); Writeln('Match nul !'); end; end; // Rejouer ? SetColor(FG, BG); Writeln('Rejouer ? (O|N) '); until (LowerCase(GetKey(['o','O','n','N'])) = 'n') end. |
- L’application compilée peut-être téléchargée sur cette page :
https://www.whiler.com/freewares/ - Les sources et les explications sont sur celle-ci :
https://whiler.developpez.com/defis/borland/codegear/bds/delphi/puissance4/ - Les résultats du défi :
https://delphi.developpez.com/defi/puissance4/




Derniers commentaires