- Timestamp:
- Oct 6, 2019, 9:53:39 PM (5 years ago)
- Location:
- trunk
- Files:
-
- 7 added
- 12 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Forms/UFormMain.lfm
r28 r29 32 32 end 33 33 end 34 object MenuItemTools: TMenuItem 35 Caption = 'Tools' 36 object MenuItem4: TMenuItem 37 Action = Core.AComputer 38 end 39 end 34 40 object MenuItemHelp: TMenuItem 35 41 Caption = 'Help' -
trunk/Forms/UFormMain.lrj
r28 r29 2 2 {"hash":217976,"name":"tformmain.caption","sourcebytes":[50,48,52,56],"value":"2048"}, 3 3 {"hash":317493,"name":"tformmain.menuitemgame.caption","sourcebytes":[71,97,109,101],"value":"Game"}, 4 {"hash":5989939,"name":"tformmain.menuitemtools.caption","sourcebytes":[84,111,111,108,115],"value":"Tools"}, 4 5 {"hash":322608,"name":"tformmain.menuitemhelp.caption","sourcebytes":[72,101,108,112],"value":"Help"} 5 6 ]} -
trunk/Forms/UFormMain.pas
r28 r29 18 18 MenuItem2: TMenuItem; 19 19 MenuItem3: TMenuItem; 20 MenuItem4: TMenuItem; 21 MenuItemTools: TMenuItem; 20 22 MenuItemNew: TMenuItem; 21 23 MenuItemExit: TMenuItem; … … 35 37 public 36 38 procedure Redraw; 39 procedure UpdateInterface; 37 40 end; 38 41 … … 53 56 if Core.Game.Running and not Core.Game.Moving then begin 54 57 case Key of 55 37: Core.Game.MoveAll (drLeft);56 38: Core.Game.MoveAll (drUp);57 39: Core.Game.MoveAll (drRight);58 40: Core.Game.MoveAll (drDown);58 37: Core.Game.MoveAllAndUpdate(drLeft); 59 38: Core.Game.MoveAllAndUpdate(drUp); 60 39: Core.Game.MoveAllAndUpdate(drRight); 61 40: Core.Game.MoveAllAndUpdate(drDown); 59 62 end; 60 63 end; … … 85 88 if not Core.Game.Running then 86 89 Core.Game.New; 90 UpdateInterface; 87 91 end; 88 92 … … 100 104 end; 101 105 106 procedure TFormMain.UpdateInterface; 107 var 108 I: Integer; 109 ToolsVisible: Boolean; 110 begin 111 ToolsVisible := False; 112 for I := 0 to MenuItemTools.Count - 1 do 113 if MenuItemTools.Items[I].Enabled then begin 114 ToolsVisible := True; 115 Break; 116 end; 117 MenuItemTools.Visible := ToolsVisible; 118 end; 119 102 120 end. 103 121 -
trunk/Game2048.lpi
r28 r29 82 82 </Item2> 83 83 </RequiredPackages> 84 <Units Count=" 8">84 <Units Count="9"> 85 85 <Unit0> 86 86 <Filename Value="Game2048.lpr"/> … … 130 130 <IsPartOfProject Value="True"/> 131 131 <ComponentName Value="FormHelp"/> 132 <HasResources Value="True"/> 132 133 <ResourceBaseClass Value="Form"/> 133 134 </Unit7> 135 <Unit8> 136 <Filename Value="Forms/UFormComputer.pas"/> 137 <IsPartOfProject Value="True"/> 138 <ComponentName Value="FormComputer"/> 139 <HasResources Value="True"/> 140 <ResourceBaseClass Value="Form"/> 141 </Unit8> 134 142 </Units> 135 143 </ProjectOptions> … … 173 181 </Options> 174 182 </Linking> 183 <Other> 184 <CustomOptions Value="-dDEBUG"/> 185 </Other> 175 186 </CompilerOptions> 176 187 <Debugging> -
trunk/Game2048.lpr
r28 r29 8 8 {$ENDIF} 9 9 Interfaces, SysUtils,// this includes the LCL widgetset 10 Forms, UGame, Common, UFormSettings, UFormMain, UCore, UFormHelp 10 Forms, UGame, Common, UFormSettings, UFormMain, UCore, UFormHelp, 11 UFormComputer 11 12 { you can add units after this }; 12 13 -
trunk/Languages/Game2048.cs.po
r28 r29 17 17 msgstr "O aplikaci" 18 18 19 #: tcore.acomputer.caption 20 msgctxt "tcore.acomputer.caption" 21 msgid "AI" 22 msgstr "AI" 23 19 24 #: tcore.aexit.caption 20 25 msgctxt "tcore.aexit.caption" … … 62 67 msgstr " " 63 68 69 #: tformcomputer.buttonsolve.caption 70 msgid "Solve" 71 msgstr "Vyřešit" 72 73 #: tformcomputer.caption 74 msgctxt "tformcomputer.caption" 75 msgid "AI" 76 msgstr "AI" 77 64 78 #: tformhelp.caption 65 79 msgctxt "tformhelp.caption" … … 80 94 msgstr "Nápověda" 81 95 96 #: tformmain.menuitemtools.caption 97 msgid "Tools" 98 msgstr "Nástroje" 99 82 100 #: tformnew.buttoncancel.caption 83 101 msgctxt "tformnew.buttoncancel.caption" … … 125 143 msgstr "Jazyk:" 126 144 145 #: ucore.sgameovercaption 146 msgctxt "ucore.sgameovercaption" 147 msgid "Lost" 148 msgstr "Prohra" 149 150 #: ucore.sgameovermessage 151 msgctxt "ucore.sgameovermessage" 152 msgid "Game over!" 153 msgstr "Konec hry!" 154 155 #: ucore.swincaption 156 msgctxt "ucore.swincaption" 157 msgid "Win" 158 msgstr "Vítězství" 159 160 #: ucore.swinmessage 161 msgctxt "ucore.swinmessage" 162 msgid "You reached %d and won! You can continue to play to get higher score." 163 msgstr "Dosáhl jsi %d a vyhrál! Můžeš pokračovat ve hře k dosažení vyššího skóre." 164 127 165 #: uformabout.slicense 128 166 msgid "License" … … 165 203 msgstr "Změna jazyka" 166 204 167 #: ugame.sgameovercaption168 msgid "Lost"169 msgstr "Prohra"170 171 #: ugame.sgameovermessage172 msgid "Game over!"173 msgstr "Konec hry!"174 175 205 #: ugame.sscore 176 206 msgid "Score" … … 180 210 msgid "Top score" 181 211 msgstr "Nejvyšší skóre" 182 183 #: ugame.swincaption184 msgid "Win"185 msgstr "Vítězství"186 187 #: ugame.swinmessage188 msgid "You reached %d and won! You can continue to play to get higher score."189 msgstr "Dosáhl jsi %d a vyhrál! Můžeš pokračovat ve hře k dosažení vyššího skóre." -
trunk/Languages/Game2048.po
r28 r29 7 7 msgstr "" 8 8 9 #: tcore.acomputer.caption 10 msgctxt "tcore.acomputer.caption" 11 msgid "AI" 12 msgstr "" 13 9 14 #: tcore.aexit.caption 10 15 msgctxt "tcore.aexit.caption" … … 52 57 msgstr "" 53 58 59 #: tformcomputer.buttonsolve.caption 60 msgid "Solve" 61 msgstr "" 62 63 #: tformcomputer.caption 64 msgctxt "tformcomputer.caption" 65 msgid "AI" 66 msgstr "" 67 54 68 #: tformhelp.caption 55 69 msgctxt "tformhelp.caption" … … 70 84 msgstr "" 71 85 86 #: tformmain.menuitemtools.caption 87 msgid "Tools" 88 msgstr "" 89 72 90 #: tformnew.buttoncancel.caption 73 91 msgctxt "tformnew.buttoncancel.caption" … … 115 133 msgstr "" 116 134 135 #: ucore.sgameovercaption 136 msgctxt "ucore.sgameovercaption" 137 msgid "Lost" 138 msgstr "" 139 140 #: ucore.sgameovermessage 141 msgctxt "ucore.sgameovermessage" 142 msgid "Game over!" 143 msgstr "" 144 145 #: ucore.swincaption 146 msgctxt "ucore.swincaption" 147 msgid "Win" 148 msgstr "" 149 150 #: ucore.swinmessage 151 msgctxt "ucore.swinmessage" 152 msgid "You reached %d and won! You can continue to play to get higher score." 153 msgstr "" 154 117 155 #: uformabout.slicense 118 156 msgid "License" … … 155 193 msgstr "" 156 194 157 #: ugame.sgameovercaption158 msgid "Lost"159 msgstr ""160 161 #: ugame.sgameovermessage162 msgid "Game over!"163 msgstr ""164 165 195 #: ugame.sscore 166 196 msgid "Score" … … 171 201 msgstr "" 172 202 173 #: ugame.swincaption174 msgid "Win"175 msgstr ""176 177 #: ugame.swinmessage178 msgid "You reached %d and won! You can continue to play to get higher score."179 msgstr ""180 -
trunk/UCore.lfm
r28 r29 71 71 ShortCut = 112 72 72 end 73 object AComputer: TAction 74 Caption = 'AI' 75 OnExecute = AComputerExecute 76 ShortCut = 115 77 end 73 78 end 74 79 end -
trunk/UCore.lrj
r28 r29 5 5 {"hash":213582195,"name":"tcore.asettings.caption","sourcebytes":[83,101,116,116,105,110,103,115],"value":"Settings"}, 6 6 {"hash":378031,"name":"tcore.aundo.caption","sourcebytes":[85,110,100,111],"value":"Undo"}, 7 {"hash":322608,"name":"tcore.ahelp.caption","sourcebytes":[72,101,108,112],"value":"Help"} 7 {"hash":322608,"name":"tcore.ahelp.caption","sourcebytes":[72,101,108,112],"value":"Help"}, 8 {"hash":1113,"name":"tcore.acomputer.caption","sourcebytes":[65,73],"value":"AI"} 8 9 ]} -
trunk/UCore.pas
r28 r29 7 7 uses 8 8 Classes, SysUtils, UTheme, UPersistentForm, UApplicationInfo, UTranslator, 9 URegistry, UGame, ActnList, Forms, Controls ;9 URegistry, UGame, ActnList, Forms, Controls, Dialogs; 10 10 11 11 type … … 15 15 TCore = class(TDataModule) 16 16 AAbout: TAction; 17 AComputer: TAction; 17 18 AHelp: TAction; 18 19 AUndo: TAction; … … 26 27 Translator1: TTranslator; 27 28 procedure AAboutExecute(Sender: TObject); 29 procedure AComputerExecute(Sender: TObject); 28 30 procedure AExitExecute(Sender: TObject); 29 31 procedure AHelpExecute(Sender: TObject); … … 35 37 private 36 38 procedure GameChange(Sender: TObject); 39 procedure GameWin(Sender: TObject); 40 procedure GameOver(Sender: TObject); 37 41 public 38 42 Game: TGame; … … 50 54 51 55 uses 52 UFormMain, UFormSettings, UFormAbout, UFormNew, UFormHelp; 56 UFormMain, UFormSettings, UFormAbout, UFormNew, UFormHelp, UFormComputer; 57 58 resourcestring 59 SGameOverCaption = 'Lost'; 60 SGameOverMessage = 'Game over!'; 61 SWinCaption = 'Win'; 62 SWinMessage = 'You reached %d and won! You can continue to play to get higher score.'; 53 63 54 64 { TCore } … … 66 76 Game.Board.Size := Point(4, 4); 67 77 Game.OnChange := GameChange; 78 Game.OnWin := GameWin; 68 79 LoadConfig; 69 80 end; … … 92 103 finally 93 104 FreeAndNil(FormAbout); 105 end; 106 end; 107 108 procedure TCore.AComputerExecute(Sender: TObject); 109 begin 110 FormComputer := TFormComputer.Create(nil); 111 try 112 FormComputer.ShowModal; 113 finally 114 FreeAndNil(FormComputer); 94 115 end; 95 116 end; … … 136 157 end; 137 158 159 procedure TCore.GameWin(Sender: TObject); 160 begin 161 MessageDlg(SWinCaption, Format(SWinMessage, [Game.WinScore]), mtInformation, [mbOk], 0); 162 end; 163 164 procedure TCore.GameOver(Sender: TObject); 165 begin 166 MessageDlg(SGameOverCaption, SGameOverMessage, mtInformation, [mbOK], 0); 167 end; 168 138 169 procedure TCore.UpdateInterface; 139 170 begin 140 171 AUndo.Enabled := Game.CanUndo; 172 {$IFDEF DEBUG} 173 AComputer.Enabled := True; 174 {$ELSE} 175 AComputer.Enabled := False; 176 {$ENDIF} 141 177 end; 142 178 -
trunk/UGame.pas
r28 r29 36 36 procedure Clear; 37 37 procedure ClearMerged; 38 function GetValueSum: Integer; 38 39 function GetHighestTileValue: Integer; 39 40 function GetEmptyTilesCount: Integer; … … 53 54 FMoving: Boolean; 54 55 FOnChange: TNotifyEvent; 56 FOnGameOver: TNotifyEvent; 57 FOnWin: TNotifyEvent; 55 58 FRunning: Boolean; 56 59 FScore: Integer; 57 60 FCanUndo: Boolean; 58 61 FBoardUndo: TBoard; 59 function CanMoveDirection(Direction: TDirection): Boolean;60 62 function GetTileColor(Value: Integer): TColor; 61 63 procedure SetScore(AValue: Integer); … … 73 75 function CanUndo: Boolean; 74 76 procedure Undo; 77 function CanMergeDirection(Direction: TDirection): Boolean; 78 function CanMoveDirection(Direction: TDirection): Boolean; 75 79 function CanMove: Boolean; 76 80 procedure Assign(Source: TGame); … … 78 82 procedure Render(Canvas: TCanvas; CanvasSize: TPoint); 79 83 procedure MoveAll(Direction: TDirection); 84 procedure MoveAllAndUpdate(Direction: TDirection); 80 85 procedure MoveTile(SourceTile, TargetTile: TTile); 81 86 function IsValidPos(Pos: TPoint): Boolean; … … 87 92 property Running: Boolean read FRunning write FRunning; 88 93 property OnChange: TNotifyEvent read FOnChange write FOnChange; 94 property OnWin: TNotifyEvent read FOnWin write FOnWin; 95 property OnGameOver: TNotifyEvent read FOnGameOver write FOnGameOver; 89 96 property Moving: Boolean read FMoving; 97 end; 98 99 TGames = class(TFPGObjectList<TGame>) 90 100 end; 91 101 … … 94 104 (X: -1; Y: 0), (X: 0; Y: -1), (X: 1; Y: 0), (X: 0; Y: 1) 95 105 ); 106 DirectionText: array[TDirection] of string = ('Left', 'Up', 'Right', 'Down'); 96 107 97 108 resourcestring 98 SGameOverCaption = 'Lost';99 SGameOverMessage = 'Game over!';100 SWinCaption = 'Win';101 SWinMessage = 'You reached %d and won! You can continue to play to get higher score.';102 109 SScore = 'Score'; 103 110 STopScore = 'Top score'; … … 212 219 end; 213 220 221 function TBoard.GetValueSum: Integer; 222 var 223 X, Y: Integer; 224 begin 225 Result := 0; 226 for Y := 0 to Size.Y - 1 do 227 for X := 0 to Size.X - 1 do 228 Inc(Result, Tiles[Y, X].Value); 229 end; 230 214 231 function TBoard.GetEmptyTilesCount: Integer; 215 232 var … … 260 277 procedure TGame.GameOver; 261 278 begin 262 if Running then MessageDlg(SGameOverCaption, SGameOverMessage, mtInformation, [mbOK], 0);279 if Running and Assigned(FOnGameOver) then FOnGameOver(Self); 263 280 Running := False; 264 281 end; … … 266 283 procedure TGame.Win; 267 284 begin 268 MessageDlg(SWinCaption, Format(SWinMessage, [WinScore]), mtInformation, [mbOk], 0);285 if Assigned(FOnWin) then FOnWin(Self); 269 286 end; 270 287 … … 293 310 procedure TGame.Assign(Source: TGame); 294 311 begin 295 FScore := Source.FScore; 312 Board.Assign(Source.Board); 313 FBoardUndo.Assign(Source.FBoardUndo); 314 FCanUndo := Source.FCanUndo; 296 315 TopScore := Source.TopScore; 297 316 AnimationDuration := Source.AnimationDuration; 298 Board.Assign(Source.Board); 317 WinScore := Source.WinScore; 318 UndoEnabled := Source.UndoEnabled; 319 FScore := Source.FScore; 320 FRunning := Source.FRunning; 299 321 end; 300 322 … … 412 434 FRunning := CanMove; 413 435 DoChange; 436 end; 437 end; 438 439 function TGame.CanMergeDirection(Direction: TDirection): Boolean; 440 var 441 StartPoint: TPoint; 442 AreaSize: TPoint; 443 Increment: TPoint; 444 P: TPoint; 445 PNew: TPoint; 446 PI: TPoint; 447 I: Integer; 448 begin 449 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 473 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; 481 if IsValidPos(PNew) then begin 482 if (Board.Tiles[PNew.Y, PNew.X].Value = 0) then begin 483 Board.Tiles[PNew.Y, PNew.X].Value := Board.Tiles[P.Y, P.X].Value; 484 Board.Tiles[P.Y, P.X].Value := 0; 485 end else 486 if (Board.Tiles[P.Y, P.X].Value <> 0) then begin 487 if Board.Tiles[PNew.Y, PNew.X].Value = Board.Tiles[P.Y, P.X].Value then begin 488 Result := True; 489 Break; 490 end; 491 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 end; 497 Inc(PI.X); 498 end; 499 if Result then Break; 500 Inc(PI.Y); 501 end; 414 502 end; 415 503 end; … … 490 578 Time: TDateTime; 491 579 Part: Double; 492 HighestValue: Integer;493 580 begin 494 581 if not CanMoveDirection(Direction) then Exit; 495 582 FMoving := True; 496 HighestValue := Board.GetHighestTileValue;497 583 FBoardUndo.Assign(Board); 498 584 FCanUndo := True; … … 596 682 DoChange; 597 683 end; 598 599 // Update state after move 684 FMoving := False; 685 end; 686 687 procedure TGame.MoveAllAndUpdate(Direction: TDirection); 688 var 689 HighestValue: Integer; 690 begin 691 HighestValue := Board.GetHighestTileValue; 692 MoveAll(Direction); 600 693 FillRandomTile; 601 694 if not CanMove and (Board.GetEmptyTilesCount = 0) then … … 603 696 if (HighestValue < WinScore) and 604 697 (Board.GetHighestTileValue >= WinScore) then Win; 605 606 FMoving := False;607 698 end; 608 699
Note:
See TracChangeset
for help on using the changeset viewer.