- Timestamp:
- Oct 5, 2019, 2:00:50 PM (5 years ago)
- Location:
- trunk
- Files:
-
- 10 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Forms/UFormMain.lfm
r11 r20 20 20 Caption = 'Game' 21 21 object MenuItemNew: TMenuItem 22 Action = Core.AGameNew 22 Action = Core.ANew 23 end 24 object MenuItem2: TMenuItem 25 Action = Core.AUndo 23 26 end 24 27 object MenuItem1: TMenuItem -
trunk/Forms/UFormMain.pas
r19 r20 16 16 MainMenu1: TMainMenu; 17 17 MenuItem1: TMenuItem; 18 MenuItem2: TMenuItem; 18 19 MenuItemNew: TMenuItem; 19 20 MenuItemExit: TMenuItem; … … 48 49 49 50 procedure TFormMain.FormKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState); 50 var51 MovedCount: Integer;52 51 begin 53 52 if Core.Game.Running and not Core.Game.Moving then begin 54 MovedCount := 0;55 53 case Key of 56 37: MovedCount :=Core.Game.MoveAll(drLeft);57 38: MovedCount :=Core.Game.MoveAll(drUp);58 39: MovedCount :=Core.Game.MoveAll(drRight);59 40: MovedCount :=Core.Game.MoveAll(drDown);54 37: Core.Game.MoveAll(drLeft); 55 38: Core.Game.MoveAll(drUp); 56 39: Core.Game.MoveAll(drRight); 57 40: Core.Game.MoveAll(drDown); 60 58 end; 61 if MovedCount > 0 then Core.Game.FillRandomTile;62 if not Core.Game.CanMove and (Core.Game.Board.GetEmptyTilesCount = 0) then63 Core.Game.GameOver;64 if (not Core.Game.Won) and (Core.Game.Board.GetHighestTileValue >= 2048) then65 Core.Game.Win;66 59 end; 67 60 end; -
trunk/Forms/UFormNew.lfm
r15 r20 25 25 ItemHeight = 0 26 26 Items.Strings = ( 27 '2 x 2' 27 28 '3 x 3' 28 29 '4 x 4' -
trunk/Forms/UFormNew.pas
r17 r20 45 45 procedure TFormNew.Load(Game: TGame); 46 46 begin 47 ComboBoxSize.ItemIndex := Game.Board.Size.X - 3;47 ComboBoxSize.ItemIndex := Game.Board.Size.X - 2; 48 48 end; 49 49 50 50 procedure TFormNew.Save(Game: TGame); 51 51 begin 52 Game.Board.Size := Point( 3 + ComboBoxSize.ItemIndex, 3+ ComboBoxSize.ItemIndex);52 Game.Board.Size := Point(2 + ComboBoxSize.ItemIndex, 2 + ComboBoxSize.ItemIndex); 53 53 end; 54 54 -
trunk/Languages/Game2048.cs.po
r19 r20 22 22 msgstr "Ukončít" 23 23 24 #: tcore.agamenew.caption 25 msgctxt "tcore.agamenew.caption" 24 #: tcore.anew.caption 25 #, fuzzy 26 msgctxt "tcore.anew.caption" 26 27 msgid "New..." 27 28 msgstr "Nová..." … … 31 32 msgid "Settings" 32 33 msgstr "Nastavení" 34 35 #: tcore.aundo.caption 36 msgid "Undo" 37 msgstr "" 33 38 34 39 #: tformabout.buttonclose.caption … … 149 154 msgid "You won! Do you want to continue to play?" 150 155 msgstr "Vyhrál jsi! Chceš pokračovat ve hře?" 156 -
trunk/Languages/Game2048.po
r19 r20 12 12 msgstr "" 13 13 14 #: tcore.a gamenew.caption15 msgctxt "tcore.a gamenew.caption"14 #: tcore.anew.caption 15 msgctxt "tcore.anew.caption" 16 16 msgid "New..." 17 17 msgstr "" … … 20 20 msgctxt "tcore.asettings.caption" 21 21 msgid "Settings" 22 msgstr "" 23 24 #: tcore.aundo.caption 25 msgid "Undo" 22 26 msgstr "" 23 27 -
trunk/UCore.lfm
r18 r20 43 43 left = 420 44 44 top = 184 45 object A GameNew: TAction45 object ANew: TAction 46 46 Caption = 'New...' 47 OnExecute = AGameNewExecute 47 OnExecute = ANewExecute 48 ShortCut = 113 48 49 end 49 50 object AExit: TAction … … 58 59 Caption = 'Settings' 59 60 OnExecute = ASettingsExecute 61 ShortCut = 120 62 end 63 object AUndo: TAction 64 Caption = 'Undo' 65 OnExecute = AUndoExecute 66 ShortCut = 114 60 67 end 61 68 end -
trunk/UCore.lrj
r11 r20 1 1 {"version":1,"strings":[ 2 {"hash":88908046,"name":"tcore.a gamenew.caption","sourcebytes":[78,101,119,46,46,46],"value":"New..."},2 {"hash":88908046,"name":"tcore.anew.caption","sourcebytes":[78,101,119,46,46,46],"value":"New..."}, 3 3 {"hash":315140,"name":"tcore.aexit.caption","sourcebytes":[69,120,105,116],"value":"Exit"}, 4 4 {"hash":4691652,"name":"tcore.aabout.caption","sourcebytes":[65,98,111,117,116],"value":"About"}, 5 {"hash":213582195,"name":"tcore.asettings.caption","sourcebytes":[83,101,116,116,105,110,103,115],"value":"Settings"} 5 {"hash":213582195,"name":"tcore.asettings.caption","sourcebytes":[83,101,116,116,105,110,103,115],"value":"Settings"}, 6 {"hash":378031,"name":"tcore.aundo.caption","sourcebytes":[85,110,100,111],"value":"Undo"} 6 7 ]} -
trunk/UCore.pas
r19 r20 15 15 TCore = class(TDataModule) 16 16 AAbout: TAction; 17 AUndo: TAction; 17 18 ASettings: TAction; 18 19 ActionList1: TActionList; 19 20 AExit: TAction; 20 A GameNew: TAction;21 ANew: TAction; 21 22 ApplicationInfo1: TApplicationInfo; 22 23 PersistentForm1: TPersistentForm; … … 25 26 procedure AAboutExecute(Sender: TObject); 26 27 procedure AExitExecute(Sender: TObject); 27 procedure A GameNewExecute(Sender: TObject);28 procedure ANewExecute(Sender: TObject); 28 29 procedure ASettingsExecute(Sender: TObject); 30 procedure AUndoExecute(Sender: TObject); 29 31 procedure DataModuleCreate(Sender: TObject); 30 32 procedure DataModuleDestroy(Sender: TObject); … … 33 35 public 34 36 Game: TGame; 37 procedure UpdateInterface; 35 38 procedure LoadConfig; 36 39 procedure SaveConfig; … … 68 71 end; 69 72 73 procedure TCore.AUndoExecute(Sender: TObject); 74 begin 75 Game.Undo; 76 end; 77 70 78 procedure TCore.AAboutExecute(Sender: TObject); 71 79 begin … … 84 92 end; 85 93 86 procedure TCore.A GameNewExecute(Sender: TObject);94 procedure TCore.ANewExecute(Sender: TObject); 87 95 begin 88 96 FormNew := TFormNew.Create(nil); … … 107 115 begin 108 116 FormMain.Redraw; 117 UpdateInterface; 118 end; 119 120 procedure TCore.UpdateInterface; 121 begin 122 AUndo.Enabled := Game.CanUndo;; 109 123 end; 110 124 -
trunk/UGame.pas
r19 r20 55 55 FRunning: Boolean; 56 56 FScore: Integer; 57 FCanUndo: Boolean; 58 FBoardUndo: TBoard; 59 function CanMoveDirection(Direction: TDirection): Boolean; 57 60 function GetTileColor(Value: Integer): TColor; 58 61 procedure SetScore(AValue: Integer); 59 62 procedure DoChange; 60 63 procedure RenderTile(Canvas: TCanvas; Tile: TTile; TileRect: TRect); 64 procedure GameOver; 65 procedure Win; 66 function FillRandomTile: Integer; 61 67 public 62 68 Board: TBoard; 63 69 TopScore: Integer; 64 70 AnimationDuration: Integer; 65 Won: Boolean; 66 procedure GameOver; 67 procedure Win; 68 function FillRandomTile: Integer; 71 WinScore: Integer; 72 function CanUndo: Boolean; 73 procedure Undo; 69 74 function CanMove: Boolean; 70 75 procedure Assign(Source: TGame); … … 146 151 CurrentContext := RegContext; 147 152 153 WriteInteger('SizeX', Size.X); 154 WriteInteger('SizeY', Size.Y); 148 155 Value := ''; 149 156 for Y := 0 to Size.Y - 1 do begin … … 171 178 CurrentContext := RegContext; 172 179 180 Size := Point(ReadIntegerWithDefault('SizeX', 4), ReadIntegerWithDefault('SizeY', 4)); 173 181 Items := TStringList.Create; 174 182 Items.Delimiter := ','; … … 257 265 procedure TGame.Win; 258 266 begin 259 if not Won then begin 260 Won := True; 261 if MessageDlg(SWinCaption, SWinMessage, mtConfirmation, 262 mbYesNo, 0) = mrNo then begin 263 Running := False; 264 end; 267 if MessageDlg(SWinCaption, SWinMessage, mtConfirmation, 268 mbYesNo, 0) = mrNo then begin 269 Running := False; 265 270 end; 266 271 end; … … 281 286 282 287 function TGame.CanMove: Boolean; 283 var 284 TempGame: TGame; 285 begin 286 Result := False; 287 TempGame := TGame.Create; 288 try 289 TempGame.Assign(Self); 290 TempGame.AnimationDuration := 0; 291 Result := TempGame.MoveAll(drDown) > 0; 292 if Result then Exit; 293 Result := TempGame.MoveAll(drUp) > 0; 294 if Result then Exit; 295 Result := TempGame.MoveAll(drRight) > 0; 296 if Result then Exit; 297 Result := TempGame.MoveAll(drLeft) > 0; 298 finally 299 TempGame.Free; 300 end; 288 begin 289 Result := CanMoveDirection(drLeft) or CanMoveDirection(drRight) or 290 CanMoveDirection(drUp) or CanMoveDirection(drDown); 301 291 end; 302 292 303 293 procedure TGame.Assign(Source: TGame); 304 var305 X, Y: Integer;306 294 begin 307 295 FScore := Source.FScore; 308 296 TopScore := Source.TopScore; 309 297 AnimationDuration := Source.AnimationDuration; 310 Won := Source.Won;311 298 Board.Assign(Source.Board); 312 299 end; … … 316 303 I: Integer; 317 304 begin 305 FCanUndo := False; 318 306 Board.Clear; 319 307 Score := 0; 320 Won := False;321 308 Running := True; 322 309 for I := 0 to 1 do FillRandomTile; … … 413 400 end; 414 401 402 function TGame.CanUndo: Boolean; 403 begin 404 Result := FCanUndo; 405 end; 406 407 procedure TGame.Undo; 408 begin 409 if CanUndo then begin 410 Board.Assign(FBoardUndo); 411 FCanUndo := False; 412 FRunning := CanMove; 413 DoChange; 414 end; 415 end; 416 417 function TGame.CanMoveDirection(Direction: TDirection): Boolean; 418 var 419 StartPoint: TPoint; 420 AreaSize: TPoint; 421 Increment: TPoint; 422 P: TPoint; 423 PNew: TPoint; 424 PI: TPoint; 425 begin 426 Result := False; 427 case Direction of 428 drLeft: begin 429 StartPoint := Point(1, 0); 430 AreaSize := Point(Board.Size.X - 2, Board.Size.Y - 1); 431 Increment := Point(1, 1); 432 end; 433 drUp: begin 434 StartPoint := Point(0, 1); 435 AreaSize := Point(Board.Size.X - 1, Board.Size.Y - 2); 436 Increment := Point(1, 1); 437 end; 438 drRight: begin 439 StartPoint := Point(Board.Size.X - 2, 0); 440 AreaSize := Point(Board.Size.X - 2, Board.Size.Y - 1); 441 Increment := Point(-1, 1); 442 end; 443 drDown: begin 444 StartPoint := Point(0, Board.Size.Y - 2); 445 AreaSize := Point(Board.Size.X - 1, Board.Size.Y - 2); 446 Increment := Point(1, -1); 447 end; 448 end; 449 450 PI.Y := 0; 451 while PI.Y <= AreaSize.Y do begin 452 PI.X := 0; 453 while PI.X <= AreaSize.X do begin 454 P := Point(StartPoint.X + PI.X * Increment.X, StartPoint.Y + PI.Y * Increment.Y); 455 PNew.X := P.X + DirectionDiff[Direction].X; 456 PNew.Y := P.Y + DirectionDiff[Direction].Y; 457 if IsValidPos(PNew) then begin 458 if (Board.Tiles[P.Y, P.X].Value <> 0) then begin 459 if (Board.Tiles[PNew.Y, PNew.X].Value = 0) or 460 (Board.Tiles[PNew.Y, PNew.X].Value = Board.Tiles[P.Y, P.X].Value) then begin 461 Result := True; 462 Break; 463 end; 464 end; 465 P.X := PNew.X; 466 P.Y := PNew.Y; 467 PNew.X := P.X + DirectionDiff[Direction].X; 468 PNew.Y := P.Y + DirectionDiff[Direction].Y; 469 end; 470 Inc(PI.X); 471 end; 472 if Result then Break; 473 Inc(PI.Y); 474 end; 475 end; 415 476 416 477 function TGame.MoveAll(Direction: TDirection): Integer; … … 430 491 Time: TDateTime; 431 492 Part: Double; 493 HighestValue: Integer; 432 494 begin 433 495 FMoving := True; 496 HighestValue := Board.GetHighestTileValue; 497 FBoardUndo.Assign(Board); 498 FCanUndo := True; 434 499 //Diff := DirectionDiff[Direction]; 435 500 case Direction of … … 535 600 end; 536 601 Result := MovedCount; 602 603 // Update state after move 604 if MovedCount > 0 then FillRandomTile; 605 if not CanMove and (Board.GetEmptyTilesCount = 0) then 606 GameOver; 607 if (HighestValue < WinScore) and 608 (Board.GetHighestTileValue >= WinScore) then Win; 609 537 610 FMoving := False; 538 611 end; … … 560 633 WriteInteger('TopScore', TopScore); 561 634 WriteInteger('AnimationDuration', AnimationDuration); 562 WriteInteger('SizeX', Board.Size.X);563 WriteInteger('SizeY', Board.Size.Y);564 635 WriteInteger('Score', Score); 565 636 WriteBool('GameRunning', FRunning); 566 WriteBool(' Won', Won);637 WriteBool('CanUndo', FCanUndo); 567 638 finally 568 639 Free; 569 640 end; 570 Board.SaveToRegistry(RegContext); 641 FBoardUndo.SaveToRegistry(TRegistryContext.Create(RegContext.RootKey, RegContext.Key + '\BoardUndo')); 642 Board.SaveToRegistry(TRegistryContext.Create(RegContext.RootKey, RegContext.Key + '\Board')); 571 643 end; 572 644 … … 576 648 try 577 649 CurrentContext := RegContext; 578 Board.Size := Point(ReadIntegerWithDefault('SizeX', 4), ReadIntegerWithDefault('SizeY', 4));579 650 AnimationDuration := ReadIntegerWithDefault('AnimationDuration', 30); 580 651 TopScore := ReadIntegerWithDefault('TopScore', 0); 581 652 Score := ReadIntegerWithDefault('Score', 0); 582 653 FRunning := ReadBoolWithDefault('GameRunning', False); 583 Won := ReadBoolWithDefault('Won', False);654 FCanUndo := ReadBoolWithDefault('CanUndo', False); 584 655 finally 585 656 Free; 586 657 end; 587 Board.LoadFromRegistry(RegContext); 658 FBoardUndo.LoadFromRegistry(TRegistryContext.Create(RegContext.RootKey, RegContext.Key + '\BoardUndo')); 659 Board.LoadFromRegistry(TRegistryContext.Create(RegContext.RootKey, RegContext.Key + '\Board')); 588 660 end; 589 661 … … 591 663 begin 592 664 AnimationDuration := 30; 665 WinScore := 2048; 593 666 Board := TBoard.Create; 667 FBoardUndo := TBoard.Create; 594 668 end; 595 669 596 670 destructor TGame.Destroy; 597 671 begin 672 FreeAndNil(FBoardUndo); 598 673 FreeAndNil(Board); 599 674 inherited;
Note:
See TracChangeset
for help on using the changeset viewer.