| 4 |
|
|
| 5 |
uses |
uses |
| 6 |
System.SysUtils, System.Types, System.UITypes, System.Classes, |
System.SysUtils, System.Types, System.UITypes, System.Classes, |
| 7 |
System.Variants, |
System.Variants, Generics.Collections, |
| 8 |
FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.Menus, |
FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.Menus, |
| 9 |
System.Math, FMX.Objects, FMX.StdCtrls; |
System.Math, FMX.Objects, FMX.StdCtrls; |
| 10 |
|
|
| 11 |
const |
const |
| 12 |
Count = 8; |
bmp_count = 8; |
| 13 |
|
|
| 14 |
type |
type |
| 15 |
TStoneType = (stNone, stWhite, stBlack, stError, stEffect); |
TStoneType = (stNone, stWhite, stBlack, stError, stEffect); |
| 19 |
Left, Top: integer; |
Left, Top: integer; |
| 20 |
end; |
end; |
| 21 |
|
|
| 22 |
TGridData = array [0 .. Count - 1] of array [0 .. Count - 1] of TStoneType; |
TGridData = array [0 .. bmp_count - 1] of array [0 .. bmp_count - 1] |
| 23 |
|
of TStoneType; |
| 24 |
|
|
| 25 |
TPlayer = class(TObject) |
TPlayer = class(TObject) |
| 26 |
private |
private |
| 34 |
TStoneGrid = class(TObject) |
TStoneGrid = class(TObject) |
| 35 |
private |
private |
| 36 |
FStrings: TGridData; |
FStrings: TGridData; |
| 37 |
FBuffer: array [0 .. Count * Count - 4] of TGridData; |
FBuffer: array [0 .. bmp_count * bmp_count - 4] of TGridData; |
| 38 |
FTurnNumber: integer; |
FTurnNumber: integer; |
| 39 |
FTurnIndex: integer; |
FTurnIndex: integer; |
| 40 |
FActive: Boolean; |
FActive: Boolean; |
| 41 |
FList: TList; |
FList: TList<TEffectData>; |
| 42 |
FEffectStone: TStoneType; |
FEffectStone: TStoneType; |
| 43 |
FIndex_X: integer; |
FIndex_X: integer; |
| 44 |
FIndex_Y: integer; |
FIndex_Y: integer; |
| 52 |
constructor Create; |
constructor Create; |
| 53 |
destructor Destroy; override; |
destructor Destroy; override; |
| 54 |
procedure Clear; |
procedure Clear; |
| 55 |
function CalScore(Stone: TStoneType; X, Y: integer; out Score: integer): Boolean; |
function CalScore(Stone: TStoneType; X, Y: integer; |
| 56 |
|
out Score: integer): Boolean; |
| 57 |
function CanSetStone(Stone: TStoneType; X, Y: integer; Reverse: Boolean; |
function CanSetStone(Stone: TStoneType; X, Y: integer; Reverse: Boolean; |
| 58 |
const Visible: Boolean = false): Boolean; |
const Visible: Boolean = false): Boolean; |
| 59 |
function NextStone(Stone: TStoneType; var Pos: TPoint): Boolean; |
function NextStone(Stone: TStoneType; var Pos: TPoint): Boolean; |
| 91 |
Image1: TImage; |
Image1: TImage; |
| 92 |
Image2: TImage; |
Image2: TImage; |
| 93 |
Image3: TImage; |
Image3: TImage; |
| 94 |
Image4: TImage; |
MenuItem13: TMenuItem; |
| 95 |
Image5: TImage; |
MenuItem14: TMenuItem; |
| 96 |
|
MenuItem15: TMenuItem; |
| 97 |
procedure FormCreate(Sender: TObject); |
procedure FormCreate(Sender: TObject); |
| 98 |
procedure FormDestroy(Sender: TObject); |
procedure FormDestroy(Sender: TObject); |
| 99 |
procedure Timer1Timer(Sender: TObject); |
procedure Timer1Timer(Sender: TObject); |
| 148 |
end; |
end; |
| 149 |
end; |
end; |
| 150 |
|
|
| 151 |
function TStoneGrid.CalScore(Stone: TStoneType; X, Y: integer; out Score: integer): Boolean; |
function TStoneGrid.CalScore(Stone: TStoneType; X, Y: integer; |
| 152 |
|
out Score: integer): Boolean; |
| 153 |
var |
var |
| 154 |
i, j: integer; |
i, j: integer; |
| 155 |
|
loop: integer; |
| 156 |
const |
const |
| 157 |
wast: array [0 .. 11] of TPoint = ((X: 1; Y: 0), (X: 6; Y: 0), (X: 0; |
waste: array [1 .. 12] of TPoint = ((X: 1; Y: 0), (X: 6; Y: 0), (X: 0; Y: 1), |
| 158 |
Y: 1), (X: 1; Y: 1), (X: 6; Y: 1), (X: 7; Y: 1), (X: 0; Y: 6), (X: 1; Y: 6), |
(X: 1; Y: 1), (X: 6; Y: 1), (X: 7; Y: 1), (X: 0; Y: 6), (X: 1; Y: 6), (X: 6; |
| 159 |
(X: 6; Y: 6), (X: 7; Y: 6), (X: 1; Y: 7), (X: 6; Y: 7)); |
Y: 6), (X: 7; Y: 6), (X: 1; Y: 7), (X: 6; Y: 7)); |
| 160 |
worth: array [0 .. 3] of TPoint = ((X: 0; Y: 0), (X: 7; Y: 0), (X: 0; |
worth: array [1 .. 4] of TPoint = ((X: 0; Y: 0), (X: 7; Y: 0), (X: 0; Y: 7), |
| 161 |
Y: 7), (X: 7; Y: 7)); |
(X: 7; Y: 7)); |
| 162 |
|
label Last; |
| 163 |
|
procedure Hard; |
| 164 |
|
var |
| 165 |
|
m, n: integer; |
| 166 |
|
begin |
| 167 |
|
if loop > 2 then |
| 168 |
|
Exit; |
| 169 |
|
inc(loop); |
| 170 |
|
for m := 0 to bmp_count - 1 do |
| 171 |
|
for n := 0 to bmp_count - 1 do |
| 172 |
|
begin |
| 173 |
|
if CanSetStone(Stone, m, n, true) = true then |
| 174 |
|
begin |
| 175 |
|
inc(Score, AddScore(m, n, worth)); |
| 176 |
|
if FTurnIndex + 1 < 50 then |
| 177 |
|
dec(Score, AddScore(m, n, waste)); |
| 178 |
|
case Stone of |
| 179 |
|
stBlack: |
| 180 |
|
Stone := stWhite; |
| 181 |
|
stWhite: |
| 182 |
|
Stone := stBlack; |
| 183 |
|
end; |
| 184 |
|
Hard; |
| 185 |
|
end; |
| 186 |
|
FStrings := FBuffer[FTurnIndex + 1]; |
| 187 |
|
end; |
| 188 |
|
end; |
| 189 |
|
|
| 190 |
begin |
begin |
| 191 |
if CanSetStone(Stone, X, Y, true) = true then |
if CanSetStone(Stone, X, Y, true) = true then |
| 192 |
begin |
begin |
| 193 |
Score := 0; |
Score := 0; |
| 194 |
result:=true; |
result := true; |
| 195 |
if FTurnIndex < 50 then |
if FTurnIndex < 50 then |
| 196 |
inc(Score, AddScore(X, Y, wast)); |
inc(Score, AddScore(X, Y, waste)); |
| 197 |
dec(Score, AddScore(X, Y, worth)); |
dec(Score, AddScore(X, Y, worth)); |
| 198 |
case Stone of |
case Stone of |
| 199 |
stBlack: |
stBlack: |
| 201 |
stWhite: |
stWhite: |
| 202 |
Stone := stBlack; |
Stone := stBlack; |
| 203 |
end; |
end; |
| 204 |
for i := 0 to Count - 1 do |
if (Form1.MenuItem14.IsChecked = true) and (FTurnIndex + 1 >= 60) then |
| 205 |
for j := 0 to Count - 1 do |
begin |
| 206 |
|
FBuffer[FTurnIndex + 1] := FStrings; |
| 207 |
|
loop := 0; |
| 208 |
|
Hard; |
| 209 |
|
end; |
| 210 |
|
for i := 0 to bmp_count - 1 do |
| 211 |
|
for j := 0 to bmp_count - 1 do |
| 212 |
if CanSetStone(Stone, i, j, false) = true then |
if CanSetStone(Stone, i, j, false) = true then |
| 213 |
begin |
begin |
| 214 |
inc(Score); |
inc(Score); |
| 225 |
var |
var |
| 226 |
i: integer; |
i: integer; |
| 227 |
p: Boolean; |
p: Boolean; |
| 228 |
q: ^TEffectData; |
q: TEffectData; |
| 229 |
procedure Method(m, n: integer); |
procedure Method(m, n: integer); |
| 230 |
var |
var |
| 231 |
s: TStoneType; |
s: TStoneType; |
| 232 |
j: integer; |
j: integer; |
| 233 |
k: Integer; |
k: integer; |
| 234 |
begin |
begin |
| 235 |
if p = false then |
if p = false then |
| 236 |
Exit; |
Exit; |
| 256 |
if Visible = true then |
if Visible = true then |
| 257 |
begin |
begin |
| 258 |
FEffectStone := Stone; |
FEffectStone := Stone; |
| 259 |
New(q); |
q.Left := X + m * j; |
| 260 |
q^.Left := X + m * j; |
q.Top := Y + n * j; |
| 261 |
q^.Top := Y + n * j; |
q.X := 0; |
| 262 |
q^.X := 0; |
q.Y := 0; |
|
q^.Y := 0; |
|
| 263 |
FList.Add(q); |
FList.Add(q); |
| 264 |
SetStrings(q^.Left, q^.Top, stEffect); |
SetStrings(q.Left, q.Top, stEffect); |
| 265 |
for k := 1 to 10 do |
for k := 1 to 10 do |
| 266 |
begin |
begin |
| 267 |
Sleep(15); |
Sleep(15); |
| 306 |
var |
var |
| 307 |
i, j: integer; |
i, j: integer; |
| 308 |
begin |
begin |
|
for i := 0 to FList.Count - 1 do |
|
|
Dispose(FList[i]); |
|
| 309 |
FList.Clear; |
FList.Clear; |
| 310 |
for i := 0 to Count - 1 do |
for i := 0 to bmp_count - 1 do |
| 311 |
for j := 0 to Count - 1 do |
for j := 0 to bmp_count - 1 do |
| 312 |
Strings[i, j] := stNone; |
Strings[i, j] := stNone; |
| 313 |
Strings[3, 3] := stBlack; |
Strings[3, 3] := stBlack; |
| 314 |
Strings[4, 4] := stBlack; |
Strings[4, 4] := stBlack; |
| 322 |
constructor TStoneGrid.Create; |
constructor TStoneGrid.Create; |
| 323 |
begin |
begin |
| 324 |
inherited; |
inherited; |
| 325 |
FList := TList.Create; |
FList := TList<TEffectData>.Create; |
| 326 |
end; |
end; |
| 327 |
|
|
| 328 |
destructor TStoneGrid.Destroy; |
destructor TStoneGrid.Destroy; |
|
var |
|
|
i: integer; |
|
| 329 |
begin |
begin |
|
for i := 0 to FList.Count - 1 do |
|
|
Dispose(FList[i]); |
|
| 330 |
FList.Free; |
FList.Free; |
| 331 |
inherited; |
inherited; |
| 332 |
end; |
end; |
| 347 |
|
|
| 348 |
function TStoneGrid.GetStrings(X, Y: integer): TStoneType; |
function TStoneGrid.GetStrings(X, Y: integer): TStoneType; |
| 349 |
begin |
begin |
| 350 |
if (X >= 0) and (X < Count) and (Y >= 0) and (Y < Count) then |
if (X >= 0) and (X < bmp_count) and (Y >= 0) and (Y < bmp_count) then |
| 351 |
result := FStrings[X, Y] |
result := FStrings[X, Y] |
| 352 |
else |
else |
| 353 |
result := stError; |
result := stError; |
| 361 |
|
|
| 362 |
function TStoneGrid.ListExecute: Boolean; |
function TStoneGrid.ListExecute: Boolean; |
| 363 |
var |
var |
|
p: ^TEffectData; |
|
| 364 |
i: integer; |
i: integer; |
| 365 |
|
s: TEffectData; |
| 366 |
begin |
begin |
| 367 |
if FList.Count = 0 then |
if FList.Count = 0 then |
| 368 |
result := false |
result := false |
| 369 |
else |
else |
| 370 |
begin |
begin |
| 371 |
for i := 0 to FList.Count - 1 do |
i := 0; |
| 372 |
|
while i < FList.Count do |
| 373 |
begin |
begin |
| 374 |
p := FList[i]; |
s := FList[i]; |
| 375 |
if p^.X < FIndex_X - 1 then |
if s.X < FIndex_X - 1 then |
| 376 |
p^.X := p^.X + 1 |
s.X := s.X + 1 |
| 377 |
else if p^.Y < FIndex_Y - 1 then |
else if s.Y < FIndex_Y - 1 then |
| 378 |
begin |
begin |
| 379 |
p^.X := 0; |
s.X := 0; |
| 380 |
p^.Y := p^.Y + 1; |
s.Y := s.Y + 1; |
| 381 |
end |
end |
| 382 |
else |
else |
| 383 |
begin |
begin |
| 384 |
SetStrings(p^.Left, p^.Top, FEffectStone); |
SetStrings(s.Left, s.Top, FEffectStone); |
| 385 |
Dispose(p); |
FList.Delete(i); |
| 386 |
FList[i] := nil; |
inc(i); |
| 387 |
|
continue; |
| 388 |
end; |
end; |
| 389 |
|
FList[i] := s; |
| 390 |
|
inc(i); |
| 391 |
end; |
end; |
|
for i := FList.Count - 1 downto 0 do |
|
|
if FList[i] = nil then |
|
|
FList.Delete(i); |
|
| 392 |
if FList.Count = 0 then |
if FList.Count = 0 then |
| 393 |
begin |
begin |
| 394 |
inc(FTurnIndex); |
inc(FTurnIndex); |
| 407 |
var |
var |
| 408 |
i, j, m, n: integer; |
i, j, m, n: integer; |
| 409 |
begin |
begin |
| 410 |
result:=false; |
result := false; |
| 411 |
n:=0; |
n := 0; |
| 412 |
for i := 0 to Count - 1 do |
for i := 0 to bmp_count - 1 do |
| 413 |
for j := 0 to Count - 1 do |
for j := 0 to bmp_count - 1 do |
| 414 |
if (CalScore(Stone, i, j, m) = true) and ((result = false)or(m < n)) then |
if (CalScore(Stone, i, j, m) = true) and ((result = false) or (m < n)) |
| 415 |
|
then |
| 416 |
begin |
begin |
| 417 |
if result = false then |
if result = false then |
| 418 |
result:=true; |
result := true; |
| 419 |
n:=m; |
n := m; |
| 420 |
Pos := Point(i, j); |
Pos := Point(i, j); |
| 421 |
end; |
end; |
| 422 |
end; |
end; |
| 423 |
|
|
| 424 |
procedure TStoneGrid.Paint(Canvas: TCanvas); |
procedure TStoneGrid.Paint(Canvas: TCanvas); |
| 425 |
var |
var |
| 426 |
i: integer; |
k: integer; |
|
k, m, n: integer; |
|
| 427 |
s: TBitmap; |
s: TBitmap; |
| 428 |
p: ^TEffectData; |
p: TEffectData; |
| 429 |
begin |
begin |
|
m := Form1.Image3.Bitmap.Width; |
|
|
n := Form1.Image3.Bitmap.Height; |
|
| 430 |
k := Form1.Size; |
k := Form1.Size; |
| 431 |
for i := 0 to FList.Count - 1 do |
if FEffectStone = stBlack then |
| 432 |
|
s := Form1.Image1.Bitmap |
| 433 |
|
else |
| 434 |
|
s := Form1.Image2.Bitmap; |
| 435 |
|
for p in FList do |
| 436 |
begin |
begin |
| 437 |
p := FList[i]; |
Canvas.DrawBitmap(s, RectF(p.X * 50, p.Y * 50, (p.X + 1) * 50, |
| 438 |
if FEffectStone = stBlack then |
(p.Y + 1) * 50), RectF(p.Left * k, p.Top * k, (p.Left + 1) * k, |
| 439 |
s := Form1.Image1.Bitmap |
(p.Top + 1) * k), 1); |
|
else |
|
|
s := Form1.Image2.Bitmap; |
|
|
Canvas.DrawBitmap(s, RectF(p^.X * m, p^.Y * n, (p^.X + 1) * m, |
|
|
(p^.Y + 1) * n), RectF(p^.Left * k, p^.Top * k, (p^.Left + 1) * k, |
|
|
(p^.Top + 1) * k), 1); |
|
| 440 |
end; |
end; |
| 441 |
end; |
end; |
| 442 |
|
|
| 460 |
|
|
| 461 |
procedure TStoneGrid.SetStrings(X, Y: integer; const Value: TStoneType); |
procedure TStoneGrid.SetStrings(X, Y: integer; const Value: TStoneType); |
| 462 |
begin |
begin |
| 463 |
if (X >= 0) and (X < Count) and (Y >= 0) and (Y < Count) then |
if (X >= 0) and (X < bmp_count) and (Y >= 0) and (Y < bmp_count) then |
| 464 |
FStrings[X, Y] := Value; |
FStrings[X, Y] := Value; |
| 465 |
end; |
end; |
| 466 |
|
|
| 505 |
var |
var |
| 506 |
i, j: integer; |
i, j: integer; |
| 507 |
begin |
begin |
| 508 |
for i := 0 to Count - 1 do |
for i := 0 to bmp_count - 1 do |
| 509 |
for j := 0 to Count - 1 do |
for j := 0 to bmp_count - 1 do |
| 510 |
if StoneGrid.CanSetStone(Index.Stone, i, j, false) = true then |
if StoneGrid.CanSetStone(Index.Stone, i, j, false) = true then |
| 511 |
begin |
begin |
| 512 |
result := true; |
result := true; |
| 524 |
begin |
begin |
| 525 |
m := 0; |
m := 0; |
| 526 |
n := 0; |
n := 0; |
| 527 |
for i := 0 to Count - 1 do |
for i := 0 to bmp_count - 1 do |
| 528 |
for j := 0 to Count - 1 do |
for j := 0 to bmp_count - 1 do |
| 529 |
case StoneGrid[i, j] of |
case StoneGrid[i, j] of |
| 530 |
stBlack: |
stBlack: |
| 531 |
inc(m); |
inc(m); |
| 540 |
else |
else |
| 541 |
s := 'Draw:' + #13#10; |
s := 'Draw:' + #13#10; |
| 542 |
StoneGrid.GameOver; |
StoneGrid.GameOver; |
| 543 |
Showmessage(s + '(Player1) ' + m.ToString+ #13#10 + '(Player2) ' + |
Showmessage(s + '(Player1) ' + m.ToString + #13#10 + '(Player2) ' + |
| 544 |
n.ToString); |
n.ToString); |
| 545 |
end |
end |
| 546 |
else |
else |
| 628 |
begin |
begin |
| 629 |
if StoneGrid.Active = false then |
if StoneGrid.Active = false then |
| 630 |
StoneGrid.Paint(Canvas); |
StoneGrid.Paint(Canvas); |
| 631 |
for i := 0 to Count - 1 do |
for i := 0 to bmp_count - 1 do |
| 632 |
begin |
begin |
| 633 |
for j := 0 to Count - 1 do |
for j := 0 to bmp_count - 1 do |
| 634 |
begin |
begin |
| 635 |
case StoneGrid.Strings[i, j] of |
case StoneGrid.Strings[i, j] of |
| 636 |
stWhite: |
stWhite: |
| 637 |
Canvas.DrawBitmap(Image4.Bitmap, RectF(0, 0, Image4.Bitmap.Width, |
Canvas.DrawBitmap(Image3.Bitmap, RectF(100, 0, 150, 50), |
| 638 |
Image4.Bitmap.Height), RectF(i * Size, j * Size, (i + 1) * Size, |
RectF(i * Size, j * Size, (i + 1) * Size, (j + 1) * Size), 1); |
|
(j + 1) * Size), 1); |
|
| 639 |
stBlack: |
stBlack: |
| 640 |
Canvas.DrawBitmap(Image3.Bitmap, RectF(0, 0, Image3.Bitmap.Width, |
Canvas.DrawBitmap(Image3.Bitmap, RectF(50, 0, 100, 50), |
| 641 |
Image3.Bitmap.Height), RectF(i * Size, j * Size, (i + 1) * Size, |
RectF(i * Size, j * Size, (i + 1) * Size, (j + 1) * Size), 1); |
|
(j + 1) * Size), 1); |
|
| 642 |
stEffect: |
stEffect: |
| 643 |
continue; |
continue; |
| 644 |
else |
else |
| 645 |
Canvas.DrawBitmap(Image5.Bitmap, RectF(0, 0, Image5.Bitmap.Width, |
Canvas.DrawBitmap(Image3.Bitmap, RectF(0, 0, 50, 50), |
| 646 |
Image5.Bitmap.Height), RectF(i * Size, j * Size, (i + 1) * Size, |
RectF(i * Size, j * Size, (i + 1) * Size, (j + 1) * Size), 1); |
|
(j + 1) * Size), 1); |
|
| 647 |
end; |
end; |
| 648 |
Canvas.DrawLine(PointF(0, j * Size), PointF(Count * Size, j * Size), 1); |
Canvas.DrawLine(PointF(0, j * Size), PointF(bmp_count * Size, |
| 649 |
|
j * Size), 1); |
| 650 |
end; |
end; |
| 651 |
Canvas.DrawLine(PointF(i * Size, 0), PointF(i * Size, Size * Count), 1); |
Canvas.DrawLine(PointF(i * Size, 0), PointF(i * Size, Size * bmp_count), 1); |
| 652 |
end; |
end; |
| 653 |
Canvas.DrawLine(PointF(Count * Size, 0), |
Canvas.DrawLine(PointF(bmp_count * Size, 0), PointF(bmp_count * Size, |
| 654 |
PointF(Count * Size, Count * Size), 1); |
bmp_count * Size), 1); |
| 655 |
Canvas.DrawLine(PointF(0, Count * Size), |
Canvas.DrawLine(PointF(0, bmp_count * Size), PointF(bmp_count * Size, |
| 656 |
PointF(Count * Size, Count * Size), 1); |
bmp_count * Size), 1); |
| 657 |
end; |
end; |
| 658 |
|
|
| 659 |
procedure TForm1.PaintBox1Resize(Sender: TObject); |
procedure TForm1.PaintBox1Resize(Sender: TObject); |
| 660 |
begin |
begin |
| 661 |
Size := Min(ClientWidth, ClientHeight) div Count; |
Size := Min(ClientWidth, ClientHeight) div bmp_count; |
| 662 |
end; |
end; |
| 663 |
|
|
| 664 |
procedure TForm1.FormCreate(Sender: TObject); |
procedure TForm1.FormCreate(Sender: TObject); |
| 665 |
begin |
begin |
| 666 |
ClientWidth := 50 * Count; |
ClientWidth := 400; |
| 667 |
ClientHeight := 50 * Count; |
ClientHeight := 400; |
| 668 |
StoneGrid := TStoneGrid.Create; |
StoneGrid := TStoneGrid.Create; |
| 669 |
StoneGrid.ImageCount(Form1.Image1.Bitmap.Width div Form1.Image3.Bitmap.Width, |
StoneGrid.ImageCount(6, 5); |
|
Form1.Image1.Bitmap.Height div Form1.Image3.Bitmap.Height); |
|
| 670 |
Player1 := TPlayer.Create; |
Player1 := TPlayer.Create; |
| 671 |
Player2 := TPlayer.Create; |
Player2 := TPlayer.Create; |
| 672 |
Player1.Stone := stBlack; |
Player1.Stone := stBlack; |
| 709 |
|
|
| 710 |
procedure TForm1.FormResize(Sender: TObject); |
procedure TForm1.FormResize(Sender: TObject); |
| 711 |
begin |
begin |
| 712 |
Size := Min(ClientWidth, ClientHeight) div Count; |
Size := Min(ClientWidth, ClientHeight) div bmp_count; |
| 713 |
PaintTo(Canvas); |
PaintTo(Canvas); |
| 714 |
end; |
end; |
| 715 |
|
|