Changeset 30
- Timestamp:
- Oct 12, 2019, 9:35:25 PM (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/UGame.pas
r29 r30 23 23 24 24 TTiles = class(TFPGObjectList<TTile>) 25 end; 26 27 { TArea } 28 29 TArea = record 30 P1, P2: TPoint; 31 function Increment: TPoint; 32 function Create(P1, P2: TPoint): TArea; overload; 33 function Create(X1, Y1, X2, Y2: Integer): TArea; overload; 25 34 end; 26 35 … … 67 76 procedure Win; 68 77 function FillRandomTile(Value4Change: Double = 0.1): Integer; 78 function GetMoveArea(Direction: TDirection): TArea; 69 79 public 70 80 Board: TBoard; … … 112 122 implementation 113 123 124 { TArea } 125 126 function TArea.Increment: TPoint; 127 begin 128 Result := Point(Sign(P2.X - P1.X), Sign(P2.Y - P1.Y)); 129 end; 130 131 function TArea.Create(P1, P2: TPoint): TArea; 132 begin 133 Result.P1 := P1; 134 Result.P2 := P2; 135 end; 136 137 function TArea.Create(X1, Y1, X2, Y2: Integer): TArea; 138 begin 139 Result.P1 := Point(X1, Y1); 140 Result.P2 := Point(X2, Y2); 141 end; 142 114 143 { TBoard } 115 144 … … 302 331 end; 303 332 333 function TGame.GetMoveArea(Direction: TDirection): TArea; 334 begin 335 case Direction of 336 drLeft: begin 337 Result := TArea.Create(1, 0, Board.Size.X - 1, Board.Size.Y - 1); 338 end; 339 drUp: begin 340 Result := TArea.Create(0, 1, Board.Size.X - 1, Board.Size.Y - 1); 341 end; 342 drRight: begin 343 Result := TArea.Create(Board.Size.X - 2, 0, 0, Board.Size.Y - 1); 344 end; 345 drDown: begin 346 Result := TArea.Create(0, Board.Size.Y - 2, Board.Size.X - 1, 0); 347 end; 348 end; 349 end; 350 304 351 function TGame.CanMove: Boolean; 305 352 begin … … 439 486 function TGame.CanMergeDirection(Direction: TDirection): Boolean; 440 487 var 441 StartPoint: TPoint;442 AreaSize: TPoint;443 Increment: TPoint;444 488 P: TPoint; 445 489 PNew: TPoint; 446 PI: TPoint;447 490 I: Integer; 491 Area: TArea; 448 492 begin 449 493 Result := False; 450 case Direction of 451 drLeft: begin 452 StartPoint := Point(1, 0); 453 AreaSize := Point(Board.Size.X - 2, Board.Size.Y - 1); 454 Increment := Point(1, 1); 455 end; 456 drUp: begin 457 StartPoint := Point(0, 1); 458 AreaSize := Point(Board.Size.X - 1, Board.Size.Y - 2); 459 Increment := Point(1, 1); 460 end; 461 drRight: begin 462 StartPoint := Point(Board.Size.X - 2, 0); 463 AreaSize := Point(Board.Size.X - 2, Board.Size.Y - 1); 464 Increment := Point(-1, 1); 465 end; 466 drDown: begin 467 StartPoint := Point(0, Board.Size.Y - 2); 468 AreaSize := Point(Board.Size.X - 1, Board.Size.Y - 2); 469 Increment := Point(1, -1); 470 end; 471 end; 472 494 Area := GetMoveArea(Direction); 473 495 for I := 0 to Max(Board.Size.X, Board.Size.Y) - 1 do begin 474 PI.Y := 0; 475 while PI.Y <= AreaSize.Y do begin 476 PI.X := 0; 477 while PI.X <= AreaSize.X do begin 478 P := Point(StartPoint.X + PI.X * Increment.X, StartPoint.Y + PI.Y * Increment.Y); 479 PNew.X := P.X + DirectionDiff[Direction].X; 480 PNew.Y := P.Y + DirectionDiff[Direction].Y; 496 P := Area.P1; 497 while P.Y <> Area.P2.Y do begin 498 P.X := Area.P1.X; 499 while P.X <> Area.P2.X do begin 500 PNew := P + DirectionDiff[Direction]; 481 501 if IsValidPos(PNew) then begin 482 502 if (Board.Tiles[PNew.Y, PNew.X].Value = 0) then begin … … 490 510 end; 491 511 end; 492 P.X := PNew.X;493 P.Y := PNew.Y;494 PNew.X := P.X + DirectionDiff[Direction].X;495 PNew.Y := P.Y + DirectionDiff[Direction].Y;496 512 end; 497 Inc(P I.X);513 Inc(P.X, Area.Increment.Y); 498 514 end; 499 515 if Result then Break; 500 Inc(P I.Y);516 Inc(P.Y, Area.Increment.Y); 501 517 end; 502 518 end; … … 505 521 function TGame.CanMoveDirection(Direction: TDirection): Boolean; 506 522 var 507 StartPoint: TPoint;508 AreaSize: TPoint;509 Increment: TPoint;510 523 P: TPoint; 511 524 PNew: TPoint; 512 PI: TPoint;525 Area: TArea; 513 526 begin 514 527 Result := False; 515 case Direction of 516 drLeft: begin 517 StartPoint := Point(1, 0); 518 AreaSize := Point(Board.Size.X - 2, Board.Size.Y - 1); 519 Increment := Point(1, 1); 520 end; 521 drUp: begin 522 StartPoint := Point(0, 1); 523 AreaSize := Point(Board.Size.X - 1, Board.Size.Y - 2); 524 Increment := Point(1, 1); 525 end; 526 drRight: begin 527 StartPoint := Point(Board.Size.X - 2, 0); 528 AreaSize := Point(Board.Size.X - 2, Board.Size.Y - 1); 529 Increment := Point(-1, 1); 530 end; 531 drDown: begin 532 StartPoint := Point(0, Board.Size.Y - 2); 533 AreaSize := Point(Board.Size.X - 1, Board.Size.Y - 2); 534 Increment := Point(1, -1); 535 end; 536 end; 537 538 PI.Y := 0; 539 while PI.Y <= AreaSize.Y do begin 540 PI.X := 0; 541 while PI.X <= AreaSize.X do begin 542 P := Point(StartPoint.X + PI.X * Increment.X, StartPoint.Y + PI.Y * Increment.Y); 543 PNew.X := P.X + DirectionDiff[Direction].X; 544 PNew.Y := P.Y + DirectionDiff[Direction].Y; 528 Area := GetMoveArea(Direction); 529 P := Area.P1; 530 while P.Y <> Area.P2.Y + Area.Increment.Y do begin 531 P.X := Area.P1.X; 532 while P.X <> Area.P2.X + Area.Increment.X do begin 533 PNew := P + DirectionDiff[Direction]; 545 534 if IsValidPos(PNew) then begin 546 535 if (Board.Tiles[P.Y, P.X].Value <> 0) then begin … … 551 540 end; 552 541 end; 553 P.X := PNew.X;554 P.Y := PNew.Y;555 PNew.X := P.X + DirectionDiff[Direction].X;556 PNew.Y := P.Y + DirectionDiff[Direction].Y;557 542 end; 558 Inc(P I.X);543 Inc(P.X, Area.Increment.X); 559 544 end; 560 545 if Result then Break; 561 Inc(P I.Y);546 Inc(P.Y, Area.Increment.Y); 562 547 end; 563 548 end; … … 565 550 procedure TGame.MoveAll(Direction: TDirection); 566 551 var 567 StartPoint: TPoint;568 AreaSize: TPoint;569 Increment: TPoint;570 MoveDirection: TPoint;571 552 P: TPoint; 572 553 PNew: TPoint; 573 PI: TPoint;574 554 X, Y: Integer; 575 555 I: Integer; … … 578 558 Time: TDateTime; 579 559 Part: Double; 560 Area: TArea; 580 561 begin 581 562 if not CanMoveDirection(Direction) then Exit; … … 583 564 FBoardUndo.Assign(Board); 584 565 FCanUndo := True; 585 //Diff := DirectionDiff[Direction]; 586 case Direction of 587 drLeft: begin 588 StartPoint := Point(1, 0); 589 AreaSize := Point(Board.Size.X - 2, Board.Size.Y - 1); 590 Increment := Point(1, 1); 591 MoveDirection := Point(-1, 0); 592 end; 593 drUp: begin 594 StartPoint := Point(0, 1); 595 AreaSize := Point(Board.Size.X - 1, Board.Size.Y - 2); 596 Increment := Point(1, 1); 597 MoveDirection := Point(0, -1); 598 end; 599 drRight: begin 600 StartPoint := Point(Board.Size.X - 2, 0); 601 AreaSize := Point(Board.Size.X - 2, Board.Size.Y - 1); 602 Increment := Point(-1, 1); 603 MoveDirection := Point(1, 0); 604 end; 605 drDown: begin 606 StartPoint := Point(0, Board.Size.Y - 2); 607 AreaSize := Point(Board.Size.X - 1, Board.Size.Y - 2); 608 Increment := Point(1, -1); 609 MoveDirection := Point(0, 1); 610 end; 611 end; 566 Area := GetMoveArea(Direction); 612 567 Board.ClearMerged; 613 568 for I := 0 to Max(Board.Size.X, Board.Size.Y) - 1 do begin 614 PI.Y := 0;615 569 for Y := 0 to Board.Size.Y - 1 do 616 570 for X := 0 to Board.Size.X - 1 do begin … … 619 573 end; 620 574 621 while PI.Y <= AreaSize.Y do begin 622 PI.X := 0; 623 while PI.X <= AreaSize.X do begin 624 P := Point(StartPoint.X + PI.X * Increment.X, StartPoint.Y + PI.Y * Increment.Y); 625 PNew.X := P.X + DirectionDiff[Direction].X; 626 PNew.Y := P.Y + DirectionDiff[Direction].Y; 575 P := Area.P1; 576 while P.Y <> Area.P2.Y + Area.Increment.Y do begin 577 P.X := Area.P1.X; 578 while P.X <> Area.P2.X + Area.Increment.X do begin 579 PNew := P + DirectionDiff[Direction]; 627 580 if IsValidPos(PNew) then begin 628 581 if (Board.Tiles[P.Y, P.X].NewValue <> 0) then begin … … 644 597 end; 645 598 end; 646 P.X := PNew.X;647 P.Y := PNew.Y;648 PNew.X := P.X + DirectionDiff[Direction].X;649 PNew.Y := P.Y + DirectionDiff[Direction].Y;650 599 end; 651 Inc(P I.X);600 Inc(P.X, Area.Increment.X); 652 601 end; 653 Inc(P I.Y);602 Inc(P.Y, Area.Increment.Y); 654 603 end; 655 604 … … 665 614 for X := 0 to Board.Size.X - 1 do begin 666 615 if Board.Tiles[Y, X].Moving then 667 Board.Tiles[Y, X].Shift := Point(Trunc(Part * MoveDirection.X * 100),668 Trunc(Part * MoveDirection.Y * 100));616 Board.Tiles[Y, X].Shift := Point(Trunc(Part * DirectionDiff[Direction].X * 100), 617 Trunc(Part * DirectionDiff[Direction].Y * 100)); 669 618 end; 670 619 DoChange;
Note:
See TracChangeset
for help on using the changeset viewer.