Changeset 7
- Timestamp:
- Nov 22, 2012, 7:09:17 AM (12 years ago)
- Location:
- trunk
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk
- Property svn:ignore
-
old new 3 3 Lazes.lps 4 4 Lazes.dbg 5 Lazes
-
- Property svn:ignore
-
trunk/Forms/ufrmmain1.lfm
r6 r7 5 5 Width = 459 6 6 Caption = 'Lazes - Lazarus Mazes' 7 ClientHeight = 3 707 ClientHeight = 366 8 8 ClientWidth = 459 9 9 Constraints.MinHeight = 300 10 10 Constraints.MinWidth = 300 11 11 Menu = MainMenu1 12 OnClose = FormClose 12 13 OnCreate = FormCreate 13 14 OnDestroy = FormDestroy … … 3337 3338 } 3338 3339 Stretch = True 3339 Visible = False3340 3340 end 3341 3341 object pbMaze: TPaintBox … … 3356 3356 Caption = '&File' 3357 3357 object MenuItem2: TMenuItem 3358 Action = FileExit13358 Action = AFileExit 3359 3359 end 3360 3360 end … … 3362 3362 Caption = '&Maze' 3363 3363 object MenuItem7: TMenuItem 3364 Action = acNewMaze3364 Action = AMazeNew 3365 3365 end 3366 3366 object MenuItem4: TMenuItem 3367 Action = actMetricsPopUp 3367 Action = AMetricsPopUp 3368 end 3369 object MenuItem9: TMenuItem 3370 Action = AMazeEnlarge 3371 end 3372 object MenuItem10: TMenuItem 3373 Action = AMazeShrink 3368 3374 end 3369 3375 object MenuItem5: TMenuItem … … 3371 3377 end 3372 3378 object MenuItem6: TMenuItem 3373 Action = acSolve 3379 Action = AMazeSolve 3380 end 3381 object MenuItem8: TMenuItem 3382 Action = ATerminate 3374 3383 end 3375 3384 end 3376 3385 end 3377 3386 object ActionList1: TActionList 3378 left = 883387 left = 96 3379 3388 top = 8 3380 object FileExit1: TFileExit3389 object AFileExit: TFileExit 3381 3390 Category = 'File' 3382 3391 Caption = 'E&xit' 3383 3392 Hint = 'Exit' 3384 3393 end 3385 object actMetricsPopUp: TAction3394 object AMetricsPopUp: TAction 3386 3395 Category = 'Maze' 3387 3396 Caption = '&Change maze metrics' 3388 OnExecute = actMetricsPopUpExecute3397 OnExecute = AMetricsPopUpExecute 3389 3398 ShortCut = 113 3390 3399 end 3391 object acSolve: TAction3400 object AMazeSolve: TAction 3392 3401 Category = 'Maze' 3393 3402 Caption = '&Solve maze' 3394 OnExecute = acSolveExecute3403 OnExecute = AMazeSolveExecute 3395 3404 ShortCut = 112 3396 3405 end 3397 object acNewMaze: TAction3406 object AMazeNew: TAction 3398 3407 Category = 'Maze' 3399 3408 Caption = '&Generate new maze' 3400 OnExecute = acNewMazeExecute3409 OnExecute = AMazeNewExecute 3401 3410 ShortCut = 116 3411 end 3412 object AMazeEnlarge: TAction 3413 Category = 'Maze' 3414 Caption = '&Enlarge' 3415 OnExecute = AMazeEnlargeExecute 3416 ShortCut = 114 3417 end 3418 object AMazeShrink: TAction 3419 Category = 'Maze' 3420 Caption = '&Shrink' 3421 OnExecute = AMazeShrinkExecute 3422 ShortCut = 115 3423 end 3424 object ATerminate: TAction 3425 Category = 'Maze' 3426 Caption = 'Terminate' 3427 OnExecute = ATerminateExecute 3428 ShortCut = 27 3402 3429 end 3403 3430 end … … 3405 3432 Interval = 10 3406 3433 OnTimer = TimerDrawTimer 3407 left = 1 523434 left = 176 3408 3435 top = 8 3409 3436 end -
trunk/Forms/ufrmmain1.pas
r6 r7 31 31 32 32 TfrmMain1 = class(TForm) 33 acSolve: TAction; 34 acNewMaze: TAction; 35 actMetricsPopUp: TAction; 33 ATerminate: TAction; 34 AMazeEnlarge: TAction; 35 AMazeShrink: TAction; 36 AMazeSolve: TAction; 37 AMazeNew: TAction; 38 AMetricsPopUp: TAction; 36 39 ActionList1: TActionList; 37 FileExit1: TFileExit;40 AFileExit: TFileExit; 38 41 imgBackground: TImage; 39 42 MainMenu1: TMainMenu; 40 43 MenuItem1: TMenuItem; 44 MenuItem10: TMenuItem; 41 45 MenuItem2: TMenuItem; 42 46 MenuItem3: TMenuItem; … … 45 49 MenuItem6: TMenuItem; 46 50 MenuItem7: TMenuItem; 51 MenuItem8: TMenuItem; 52 MenuItem9: TMenuItem; 47 53 pbMaze: TPaintBox; 48 54 TimerDraw: TTimer; 49 55 50 procedure acNewMazeExecute(Sender: TObject); 51 procedure acSolveExecute(Sender: TObject); 52 procedure actMetricsPopUpExecute(Sender: TObject); 56 procedure AMazeEnlargeExecute(Sender: TObject); 57 procedure AMazeNewExecute(Sender: TObject); 58 procedure AMazeShrinkExecute(Sender: TObject); 59 procedure AMazeSolveExecute(Sender: TObject); 60 procedure AMetricsPopUpExecute(Sender: TObject); 61 procedure ATerminateExecute(Sender: TObject); 62 procedure FormClose(Sender: TObject; var CloseAction: TCloseAction); 53 63 procedure FormCreate(Sender: TObject); 54 64 procedure FormDestroy(Sender: TObject); … … 64 74 MazeIsSolved: boolean; 65 75 RedrawPending: Boolean; 76 DisableActions: Boolean; 77 Solved: boolean; 78 TerminateAction: Boolean; 66 79 procedure GenerateNewMaze; 67 80 procedure ResizeMaze(const pdx, pdy: integer); 68 81 procedure SetMazeMetrics(AValue: TMazeUpdateInfo); 82 procedure TravelMaze(CurPos: TCellPoint); 83 procedure TryMove(const pFrom: TCellPoint; const pDirection: TDirection); 69 84 public 70 85 property MazeMetrics: TMazeUpdateInfo read FMazeMetrics write SetMazeMetrics; 71 86 procedure Redraw; 87 procedure UpdateInterface; 72 88 end; 73 89 … … 104 120 // And generate a demo maze 105 121 GenerateNewMaze; 122 123 UpdateInterface; 106 124 end; 107 125 … … 114 132 procedure TfrmMain1.FormMouseWheelDown(Sender: TObject; Shift: TShiftState; MousePos: TPoint; var Handled: Boolean); 115 133 begin 116 ResizeMaze(-1,-1);134 AMazeShrink.Execute; 117 135 end; 118 136 119 137 procedure TfrmMain1.FormMouseWheelUp(Sender: TObject; Shift: TShiftState; MousePos: TPoint; var Handled: Boolean); 120 138 begin 121 ResizeMaze(1,1); 122 end; 123 124 procedure TfrmMain1.acSolveExecute(Sender: TObject); 125 126 var Solved: boolean; 127 128 procedure TravelMaze(CurPos: TCellPoint); 129 130 procedure TryMove(const pFrom: TCellPoint; const pDirection: TDirection); 131 const DirMoves: array[TDirection] of TPoint = ((x: 0; y:-1), // North 132 (x: 0; y:+1), // South 133 (x:+1; y:0 ), // East 134 (x:-1; y:0 )); // West 135 var NewPos: TCellPoint; 139 AMazeEnlarge.Execute; 140 end; 141 142 procedure TfrmMain1.TryMove(const pFrom: TCellPoint; const pDirection: TDirection); 143 const 144 DirMoves: array[TDirection] of TPoint = 145 ((x: 0; y:-1), // North 146 (x: 0; y:+1), // South 147 (x:+1; y:0 ), // East 148 (x:-1; y:0 )); // West 149 var 150 NewPos: TCellPoint; 151 begin 152 if TerminateAction then Exit; 153 154 // Direction at all possible? 155 with Maze.GetCell(pFrom) do 156 if not CanGo[pDirection] then Exit; 157 158 // Movement is possible! 159 NewPos.Row := pFrom.Row + DirMoves[pDirection].y; 160 NewPos.Col := pFrom.Col + DirMoves[pDirection].x; 161 162 // Check if the next cell is not visited before. 163 with Maze.GetCell(NewPos) do 164 if State = csExit then 165 begin 166 Solved := true; 167 Exit; 168 end 169 else if State = csVisited then 170 Exit 171 else 136 172 begin 137 // Direction at all possible? 138 with Maze.GetCell(pFrom) do 139 if not CanGo[pDirection] then EXIT; 140 141 // Movement is possible! 142 NewPos.Row := pFrom.Row + DirMoves[pDirection].y; 143 NewPos.Col := pFrom.Col + DirMoves[pDirection].x; 144 145 // Check if the next cell is not visited before. 146 with Maze.GetCell(NewPos) do 147 if State = csExit then 173 State := csVisited; // Been here 174 // Tag := 1; // Visited 175 // State := csStart; 176 MazePainter.IsDirty := true; // Force repaint with new cell populated 177 Redraw; 178 Application.ProcessMessages; 179 Sleep(10); 180 181 // Start travelling from here 182 if not Solved then 183 TravelMaze(NewPos); 184 185 if TerminateAction then Exit; 186 187 // If still not solved, backtrack 188 if not Solved then 148 189 begin 149 Solved := true; 150 Exit 151 end 152 else if State = csVisited then 153 Exit 154 else 155 begin 156 State := csVisited; // Been here 157 // Tag := 1; // Visited 158 // State := csStart; 159 MazePainter.IsDirty := true; // Force repaint with new cell populated 160 Redraw; 161 Application.ProcessMessages; 162 Sleep(10); 163 164 // Start travelling from here 165 if not Solved then 166 TravelMaze(NewPos); 167 168 // If still not solved, backtrack 169 if not Solved then 170 begin 171 State := csEmpty; 172 // Tag := 0; 173 MazePainter.IsDirty := true; // Force repaint with new cell populated 174 Redraw; 175 Application.ProcessMessages; 176 Sleep(10); 177 end; 178 end; 190 State := csEmpty; 191 // Tag := 0; 192 MazePainter.IsDirty := true; // Force repaint with new cell populated 193 Redraw; 194 Application.ProcessMessages; 195 Sleep(10); 196 end; 179 197 end; 180 181 begin 182 // Check all 4 directions for possible moves 183 if not Solved then TryMove(CurPos, dirEast); 184 if not Solved then TryMove(CurPos, dirSouth); 185 if not Solved then TryMove(CurPos, dirWest); 186 if not Solved then TryMove(CurPos, dirNorth); 187 end; 188 198 end; 199 200 procedure TfrmMain1.TravelMaze(CurPos: TCellPoint); 201 begin 202 // Check all 4 directions for possible moves 203 if not Solved then TryMove(CurPos, dirEast); 204 if not Solved then TryMove(CurPos, dirSouth); 205 if not Solved then TryMove(CurPos, dirWest); 206 if not Solved then TryMove(CurPos, dirNorth); 207 end; 208 209 procedure TfrmMain1.AMazeSolveExecute(Sender: TObject); 189 210 begin 190 211 // If the maze was already solved, dont do it again … … 195 216 end; 196 217 197 // Reset the tags, they will be used for backtracking 198 Maze.ResetTags; 199 200 // Not done yet... 201 Solved := false; 202 203 // Travel all cells until the end is found 204 TravelMaze(Maze.GetStartPosition); 205 MazeIsSolved := true; 206 207 // Found, so give a victorious message! 208 MessageDlg('Hurrah! Found the exit!!!!', mtInformation, [mbOK], 0); 209 end; 210 211 procedure TfrmMain1.acNewMazeExecute(Sender: TObject); 218 try 219 DisableActions := True; 220 TerminateAction := False; 221 UpdateInterface; 222 223 Maze.SetStartCell(0, 0); 224 225 // Reset the tags, they will be used for backtracking 226 Maze.ResetTags; 227 Maze.ResetState; 228 229 // Not done yet... 230 Solved := false; 231 232 // Travel all cells until the end is found 233 TravelMaze(Maze.GetStartPosition); 234 if not TerminateAction then begin 235 MazeIsSolved := true; 236 237 // Found, so give a victorious message! 238 MessageDlg('Hurrah! Found the exit!!!!', mtInformation, [mbOK], 0); 239 end; 240 finally 241 DisableActions := False; 242 UpdateInterface; 243 end; 244 end; 245 246 procedure TfrmMain1.AMazeNewExecute(Sender: TObject); 212 247 begin 213 248 GenerateNewMaze; 214 249 end; 215 250 216 procedure TfrmMain1.actMetricsPopUpExecute(Sender: TObject); 251 procedure TfrmMain1.AMazeShrinkExecute(Sender: TObject); 252 begin 253 ResizeMaze(-1,-1); 254 end; 255 256 procedure TfrmMain1.AMazeEnlargeExecute(Sender: TObject); 257 begin 258 ResizeMaze(1,1); 259 end; 260 261 procedure TfrmMain1.AMetricsPopUpExecute(Sender: TObject); 217 262 begin 218 263 frmScaling.Show; 264 end; 265 266 procedure TfrmMain1.ATerminateExecute(Sender: TObject); 267 begin 268 TerminateAction := True; 269 end; 270 271 procedure TfrmMain1.FormClose(Sender: TObject; var CloseAction: TCloseAction); 272 begin 273 ATerminate.Execute; 219 274 end; 220 275 … … 254 309 255 310 // Set lower right hand corner as exit (top left is start by default) 256 Maze.Cell[ Maze.Height-1, Maze.Width-1].State := csExit;311 Maze.Cell[Maze.Height - 1, Maze.Width - 1].State := csExit; 257 312 258 313 // Paint the maze … … 295 350 end; 296 351 352 procedure TfrmMain1.UpdateInterface; 353 begin 354 AMazeNew.Enabled := not DisableActions; 355 AMazeSolve.Enabled := not DisableActions; 356 AMetricsPopUp.Enabled := not DisableActions; 357 if DisableActions then frmScaling.Hide; 358 AMazeEnlarge.Enabled := not DisableActions; 359 AMazeShrink.Enabled := not DisableActions; 360 ATerminate.Enabled := DisableActions; 361 end; 362 297 363 end. 298 364 -
trunk/Lazes.lpi
r5 r7 81 81 <local> 82 82 <FormatVersion Value="1"/> 83 <LaunchingApplication PathPlusParams="\usr\bin\xterm -T 'Lazarus Run Output' -e $(LazarusDir)\tools\runwait.sh $(TargetCmdLine)"/> 83 84 </local> 84 85 </RunParams> … … 107 108 <IsPartOfProject Value="True"/> 108 109 <HasResources Value="True"/> 109 <UnitName Value="ufrm scaling"/>110 <UnitName Value="ufrmScaling"/> 110 111 </Unit2> 111 112 <Unit3> -
trunk/class/maze.pas
r1 r7 83 83 84 84 procedure ResetTags(const pValue: integer = 0); 85 procedure ResetState; 85 86 function GetCell(const pPosition: TCellPoint): TMazeCell; 86 87 function StartCell: TMazeCell; … … 211 212 212 213 procedure TMaze.ResetTags(const pValue: integer); 213 var row,col: integer; 214 var 215 row,col: integer; 214 216 begin 215 217 for row := 0 to Height-1 do 216 218 for col := 0 to Width-1 do 217 FMaze[row][col].Tag := pValue 219 FMaze[row][col].Tag := pValue; 220 end; 221 222 procedure TMaze.ResetState; 223 var 224 row,col: integer; 225 begin 226 for row := 0 to Height - 1 do 227 for col := 0 to Width - 1 do 228 FMaze[row, col].State := csEmpty; 218 229 end; 219 230 -
trunk/class/mazepainter.pas
r6 r7 70 70 constructor TMazePainter.Create(const pMaze: TMaze; pCanvas: TCanvas); 71 71 begin 72 Bmp := TBitMap.Create; 73 72 74 // Init the default drawing width and height 73 75 FCellDrawWidth := 15; … … 91 93 destructor TMazePainter.Destroy; 92 94 begin 93 bmp.Free;95 FreeAndNil(Bmp); 94 96 inherited Destroy; 95 97 end; … … 122 124 if isDirty then 123 125 begin 124 //FreeAndNil(bmp);125 //bmp := TBitMap.Create;126 126 bmp.SetSize(Width + 1, Height + 1); 127 127 Canvas := bmp.Canvas;
Note:
See TracChangeset
for help on using the changeset viewer.