Advertisement
Guest User

Untitled

a guest
Nov 25th, 2014
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 14.25 KB | None | 0 0
  1. unit UnitGeral;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  7.   StdCtrls, LogicaJogo, Menus, ComCtrls, Clipbrd, ActnList,
  8.   System.Actions, UnitFrame2;
  9.  
  10. const
  11.   MM_DOMOVE = WM_USER + 1;
  12.   MM_DEBUG = WM_USER + 2;
  13.   MM_IS_ANIMATION = WM_USER + 3;
  14.  
  15. type
  16.   TMode = (mdMachineWhite, mdMachineBlack, mdTwoMachine, mdView);
  17.  
  18.   TJogoHistorico = class
  19.   private
  20.     function GetPartyView: TListView;
  21.     function GetPositionFrame: TPositionFrame;
  22.   private
  23.     FPositions: array[0..255] of TPosition;
  24.     FMoveNo: Integer;
  25.     procedure AddBlackMove(const Move: string);
  26.     procedure AddWhiteMove(const Move: string);
  27.     property PositionFrame: TPositionFrame read GetPositionFrame;
  28.     property PartyView: TListView read GetPartyView;
  29.   public
  30.     procedure NewGame;
  31.     procedure AddMove(NewPosition: TPosition);
  32.     procedure Undo;
  33.     property MoveNo: Integer read FMoveNo write FMoveNo;
  34.   end;
  35.  
  36.   TMainForm = class(TForm)
  37.     PositionFrame: TPositionFrame;
  38.     Memo: TMemo;
  39.     MainMenu: TMainMenu;
  40.     GameMenu: TMenuItem;
  41.     NewItem: TMenuItem;
  42.     Separator1: TMenuItem;
  43.     BeginerItem: TMenuItem;
  44.     IntermediateItem: TMenuItem;
  45.     ExpertItem: TMenuItem;
  46.     Separator2: TMenuItem;
  47.     ExitItem: TMenuItem;
  48.     ModeMenu: TMenuItem;
  49.     MachineWhiteItem: TMenuItem;
  50.     MachineBlackItem: TMenuItem;
  51.     TwoMachineItem: TMenuItem;
  52.     ViewItem: TMenuItem;
  53.     Separator3: TMenuItem;
  54.     FlipBoardItem: TMenuItem;
  55.     PartyView: TListView;
  56.     DebugMenu: TMenuItem;
  57.     SetPositionItem: TMenuItem;
  58.     AddToLibraryItem: TMenuItem;
  59.     CopyGameItem: TMenuItem;
  60.     Separator4: TMenuItem;
  61.     UndoMoveItem: TMenuItem;
  62.     ActionList: TActionList;
  63.     NewGameAction: TAction;
  64.     BeginerAction: TAction;
  65.     IntermediateAction: TAction;
  66.     ExpertAction: TAction;
  67.     UndoMoveAction: TAction;
  68.     ExitAction: TAction;
  69.     MachineWhiteAction: TAction;
  70.     MachineBlackAction: TAction;
  71.     TwoMachineAction: TAction;
  72.     ViewGameAction: TAction;
  73.     FlipBoardAction: TAction;
  74.     SetPositionAction: TAction;
  75.     AddToLibraryAction: TAction;
  76.     CopyGameAction: TAction;
  77.     procedure FormShow(Sender: TObject);
  78.     procedure SelectCellBtnClick(Sender: TObject);
  79.     procedure FormResize(Sender: TObject);
  80.     procedure FormCreate(Sender: TObject);
  81.     procedure FormDestroy(Sender: TObject);
  82.     procedure UndoMoveItemClick(Sender: TObject);
  83.     procedure NewGameActionExecute(Sender: TObject);
  84.     procedure LevelActionExecute(Sender: TObject);
  85.     procedure UndoMoveActionExecute(Sender: TObject);
  86.     procedure ActionListUpdate(Action: TBasicAction; var Handled: Boolean);
  87.     procedure ExitActionExecute(Sender: TObject);
  88.     procedure MachineWhiteActionExecute(Sender: TObject);
  89.     procedure MachineBlackActionExecute(Sender: TObject);
  90.     procedure TwoMachineActionExecute(Sender: TObject);
  91.     procedure ViewGameActionExecute(Sender: TObject);
  92.     procedure FlipBoardActionExecute(Sender: TObject);
  93.     procedure SetPositionActionExecute(Sender: TObject);
  94.     procedure AddToLibraryActionExecute(Sender: TObject);
  95.     procedure CopyGameActionExecute(Sender: TObject);
  96.     procedure PositionFrameTimerTimer(Sender: TObject);
  97.   private
  98.     FDeep: Integer;
  99.     FGameHistory: TJogoHistorico;
  100.     FMode: TMode;
  101.     FThreadHandle: THandle;
  102.     procedure AcceptMove(Sender: TObject; const NewPosition: TPosition);
  103.     procedure TuneState;
  104.     procedure StopThinking;
  105.     procedure DoMove(var Message: TMessage); message MM_DOMOVE;
  106.     procedure DoDebug(var Message: TMessage); message MM_DEBUG;
  107.     procedure IsAnimation(var Message: TMessage); message MM_IS_ANIMATION;
  108.     property Mode: TMode read FMode;
  109.     property Deep: Integer read FDeep write FDeep;
  110.     property ThreadHandle: THandle read FThreadHandle write FThreadHandle;
  111.     property GameHistory: TJogoHistorico read FGameHistory write FGameHistory;
  112.     procedure Deselect(Action: TAction; const Category: string);
  113.   end;
  114.  
  115. var
  116.   MainForm: TMainForm;
  117.  
  118. implementation
  119.  
  120. uses TaticasJogo;
  121.  
  122. {$R *.DFM}
  123.  
  124. function Thinker(APosition: Pointer): Integer;
  125. var
  126.   Position: TPosition;
  127.   Estimate: Integer;
  128. begin
  129.   Position := TPosition(APosition^);
  130.   SelectMove(Position, MainForm.Deep, Estimate);
  131.   SendMessage(MainForm.Handle, MM_DOMOVE, Integer(@Position), Estimate);
  132.   Result := 0;
  133. end;
  134.  
  135. procedure TMainForm.FormShow(Sender: TObject);
  136. var
  137.   Position: TPosition;
  138. begin
  139.   LoadLib;
  140.   Position := TabelaInicial;
  141.   PositionFrame.Debug := Memo.Lines;
  142.   PositionFrame.OnAcceptMove := AcceptMove;
  143.   NewGameAction.Execute;
  144.   BeginerAction.Execute;
  145.   MachineBlackAction.Execute;
  146. end;
  147.  
  148. procedure TMainForm.SelectCellBtnClick(Sender: TObject);
  149. begin
  150.   PositionFrame.SelectCell(1, 6);
  151. end;
  152.  
  153. procedure TMainForm.AcceptMove(Sender: TObject; const NewPosition: TPosition);
  154. var
  155.   St: string;
  156. begin
  157.   GameHistory.AddMove(NewPosition);
  158.   PositionFrame.DefinirPosicao(NewPosition);
  159.   St := GameOver(NewPosition);
  160.   if St <> '' then
  161.   begin
  162.     ShowMessage(St);
  163.     PositionFrame.AcceptMove := False;
  164.     Exit;
  165.   end;
  166.   TuneState;
  167. end;
  168.  
  169. procedure TMainForm.FormResize(Sender: TObject);
  170. begin
  171.   PositionFrame.Left := 3;
  172.   PositionFrame.Top := 3;
  173.   Memo.Left := 3;
  174.   Memo.Top := PositionFrame.Top + PositionFrame.Height + 3;
  175.   Memo.Width := ClientWidth - 6;
  176.   Memo.Height := ClientHeight - PositionFrame.Height - 9;
  177.   PartyView.Left := PositionFrame.Left + PositionFrame.Width + 3;
  178.   PartyView.Width := ClientWidth - PositionFrame.Width - 9;
  179.   PartyView.Top := 3;
  180.   PartyView.Height := PositionFrame.Height;
  181.   PartyView.Columns[0].Width := 30;
  182.   PartyView.Columns[1].Width := (PartyView.Width - 40) div 2;
  183.   PartyView.Columns[2].Width := (PartyView.Width - 40) div 2;
  184. end;
  185.  
  186. procedure TMainForm.DoMove(var Message: TMessage);
  187. var
  188.   NewPosition: TPosition;
  189. begin
  190.   NewPosition := TPosition(Pointer(Message.WParam)^);
  191.   CloseHandle(ThreadHandle);
  192.   ThreadHandle := 0;
  193.   AcceptMove(nil, NewPosition);
  194. end;
  195.  
  196. procedure TMainForm.FormCreate(Sender: TObject);
  197. begin
  198.   FMode := mdMachineBlack;
  199.   Memo.Clear;
  200.   DoubleBuffered := True;
  201.   FGameHistory := TJogoHistorico.Create;
  202. end;
  203.  
  204. procedure TMainForm.TuneState;
  205. var
  206.   RunThinker: Boolean;
  207.   ThreadId: Cardinal;
  208.   Index: Integer;
  209.   V: Integer;
  210. begin
  211.   if ThreadHandle <> 0 then StopThinking;
  212.   PositionFrame.AcceptMove := (Mode = mdView)
  213.     or ((Mode = mdMachineWhite) and (PositionFrame.Position.Active = ActiveBlack))
  214.     or ((Mode = mdMachineBlack) and (PositionFrame.Position.Active = ActiveWhite));
  215.   RunThinker := (Mode = mdTwoMachine)
  216.     or ((Mode = mdMachineWhite) and (PositionFrame.Position.Active = ActiveWhite))
  217.     or ((Mode = mdMachineBlack) and (PositionFrame.Position.Active = ActiveBlack));
  218.   if DebugMenu.Visible then
  219.   begin
  220.     Index := Lib.IndexOf(FormatPosition(PositionFrame.Position));
  221.     if Index <> -1 then
  222.     begin
  223.       V := Integer(Lib.Objects[Index]);
  224.       Memo.Lines.Add(Format('Theory = %.3f', [V/200]));
  225.     end;
  226.   end;
  227.   if not RunThinker then Exit;
  228.   ThreadHandle := BeginThread(nil, 8*4096, @Thinker, @PositionFrame.Position, CREATE_SUSPENDED, ThreadId);
  229.   SetThreadPriority(ThreadHandle, THREAD_PRIORITY_BELOW_NORMAL);
  230.   ResumeThread(ThreadHandle);
  231. end;
  232.  
  233. procedure TMainForm.DoDebug(var Message: TMessage);
  234. var
  235.   Position: PPosition;
  236. begin
  237.   if not DebugMenu.Visible then Exit;
  238.   if Message.WPAram = 0 then
  239.   begin
  240.     Memo.Clear;
  241.     Exit;
  242.   end;
  243.  
  244.   Position := Pointer(Message.WPAram);
  245.   Memo.Lines.Add(Format('E=%d N=%.3f M=%s',
  246.     [Message.LParam, Message.LParam/200, GetLastMove(Position^)]));
  247. end;
  248.  
  249. procedure TMainForm.IsAnimation(var Message: TMessage);
  250. begin
  251.   if PositionFrame.Animate
  252.     then Message.Result := 1
  253.     else Message.Result := 0
  254. end;
  255.  
  256. const
  257.   MAX_LEN = 60;
  258.  
  259. procedure TMainForm.StopThinking;
  260. begin
  261.   TerminateThread(ThreadHandle, 0);
  262.   CloseHandle(ThreadHandle);
  263.   ThreadHandle := 0;
  264. end;
  265.  
  266. procedure TMainForm.FormDestroy(Sender: TObject);
  267. begin
  268.   FreeAndNil(FGameHistory);
  269. end;
  270.  
  271. procedure TMainForm.UndoMoveItemClick(Sender: TObject);
  272. begin
  273. end;
  274.  
  275. { TGameHistory }
  276.  
  277. procedure TJogoHistorico.AddWhiteMove(const Move: string);
  278. var
  279.   NewItem: TListItem;
  280. begin
  281.   NewItem := PartyView.Items.Add;
  282.   NewItem.Caption := IntToStr((MoveNo div 2) + 1);
  283.   NewItem.Subitems.Add(Move);
  284.   PartyView.Selected := NewItem;
  285.   PartyView.Selected.MakeVisible(False);
  286. end;
  287.  
  288. procedure TJogoHistorico.AddBlackMove(const Move: string);
  289. var
  290.   Item: TListItem;
  291. begin
  292.   Assert(MainForm.PartyView.Items.Count > 0);
  293.   Item := PartyView.Items[PartyView.Items.Count-1];
  294.   Item.Subitems.Add(Move);
  295.   PartyView.Selected := Item;
  296.   PartyView.Selected.MakeVisible(False);
  297. end;
  298.  
  299. procedure TJogoHistorico.AddMove(NewPosition: TPosition);
  300. var
  301.   Move: string;
  302. begin
  303.   Move := GetLastMove(NewPosition);
  304.   if Move <> '' then
  305.     if FPositions[MoveNo].Active = ActiveWhite
  306.       then AddWhiteMove(Move)
  307.       else AddBlackMove(Move);
  308.   MoveNo := MoveNo + 1;
  309.   FPositions[MoveNo] := NewPosition;
  310. end;
  311.  
  312. procedure TJogoHistorico.NewGame;
  313. begin
  314.  
  315.   MoveNo := 0; // Define o numero de Movimentos para 0
  316.   PartyView.Items.Clear; // Limpa o Log Lateral
  317.  
  318.   // Obtem as posições como a padrão da TabelaInicial
  319.   FPositions[0] := TabelaInicial;
  320.   // Define as Posições no Frame Secundario
  321.   PositionFrame.DefinirPosicao(TabelaInicial)
  322.  
  323. end;
  324.  
  325. function TJogoHistorico.GetPartyView: TListView;
  326. begin
  327.   Result := MainForm.PartyView;
  328. end;
  329.  
  330. function TJogoHistorico.GetPositionFrame: TPositionFrame;
  331. begin
  332.   Result := MainForm.PositionFrame;
  333. end;
  334.  
  335. procedure TJogoHistorico.Undo;
  336. var
  337.   Last: Integer;
  338.   Item: TListItem;
  339. begin
  340.   Assert(MoveNo > 0);
  341.   MainForm.ViewItem.Click;
  342.   MoveNo := MoveNo - 1;
  343.   PositionFrame.DefinirPosicao(FPositions[MoveNo], False);
  344.   Last := PartyView.Items.Count-1;
  345.   Assert(Last >= 0);
  346.   Item := PartyView.Items[Last];
  347.   if Item.SubItems.Count > 1
  348.     then Item.SubItems.Delete(1)
  349.     else PartyView.Items.Delete(Last);
  350. end;
  351.  
  352. procedure TMainForm.NewGameActionExecute(Sender: TObject);
  353. begin
  354.   StopThinking;
  355.   GameHistory.NewGame;
  356.   if Mode in [mdMachineWhite, mdTwoMachine] then MachineBlackItem.Click;
  357.   PositionFrame.AcceptMove := True;
  358. end;
  359.  
  360. procedure TMainForm.PositionFrameTimerTimer(Sender: TObject);
  361. begin
  362.   PositionFrame.TimerTimer(Sender);
  363. end;
  364.  
  365. procedure TMainForm.Deselect(Action: TAction; const Category: string);
  366. var
  367.   I: Integer;
  368. begin
  369.   for I := 0 to ActionList.ActionCount - 1 do
  370.   begin
  371.     if ActionList.Actions[I].Category <> Category then Continue;
  372.     if ActionList.Actions[I] = Action then Continue;
  373.     (ActionList.Actions[I] as TAction).Checked := False;
  374.   end;
  375. end;
  376.  
  377. procedure TMainForm.LevelActionExecute(Sender: TObject);
  378. begin
  379.   Deselect(Sender as TAction, 'Level');
  380.   with Sender as TAction do
  381.   begin
  382.     Checked := True;
  383.     Deep := Tag;
  384.   end;
  385. end;
  386.  
  387. procedure TMainForm.UndoMoveActionExecute(Sender: TObject);
  388. begin
  389.   GameHistory.Undo;
  390. end;
  391.  
  392. procedure TMainForm.ActionListUpdate(Action: TBasicAction;
  393.   var Handled: Boolean);
  394. begin
  395.   UndoMoveAction.Enabled := GameHistory.MoveNo > 0;
  396. end;
  397.  
  398. procedure TMainForm.ExitActionExecute(Sender: TObject);
  399. begin
  400.   ViewItem.Click;
  401.   Close;
  402. end;
  403.  
  404. procedure TMainForm.MachineWhiteActionExecute(Sender: TObject);
  405. begin
  406.   Deselect(Sender as TAction, 'Mode');
  407.   (Sender as TAction).Checked := True;
  408.   if Mode = mdMachineWhite then Exit;
  409.   FMode := mdMachineWhite;
  410.   PositionFrame.FlipBoard := True;
  411.   TuneState;
  412. end;
  413.  
  414. procedure TMainForm.MachineBlackActionExecute(Sender: TObject);
  415. begin
  416.   Deselect(Sender as TAction, 'Mode');
  417.   (Sender as TAction).Checked := True;
  418.   if Mode = mdMachineBlack then Exit;
  419.   FMode := mdMachineBlack;
  420.   PositionFrame.FlipBoard := False;
  421.   TuneState;
  422. end;
  423.  
  424. procedure TMainForm.TwoMachineActionExecute(Sender: TObject);
  425. begin
  426.   Deselect(Sender as TAction, 'Mode');
  427.   (Sender as TAction).Checked := True;
  428.   if Mode = mdTwoMachine then Exit;
  429.   FMode := mdTwoMachine;
  430.   TuneState;
  431. end;
  432.  
  433. procedure TMainForm.ViewGameActionExecute(Sender: TObject);
  434. begin
  435.   Deselect(Sender as TAction, 'Mode');
  436.   (Sender as TAction).Checked := True;
  437.   if Mode = mdView then Exit;
  438.   FMode := mdView;
  439.   ViewItem.Checked := True;
  440.   if ThreadHandle <> 0 then StopThinking;
  441. end;
  442.  
  443. procedure TMainForm.FlipBoardActionExecute(Sender: TObject);
  444. begin
  445.   PositionFrame.FlipBoard := not PositionFrame.FlipBoard;
  446. end;
  447.  
  448. procedure TMainForm.SetPositionActionExecute(Sender: TObject);
  449. var
  450.   Position: TPosition;
  451. begin
  452.   ViewItem.Click;
  453.   FillChar(Position.Field, 32, $00);
  454.   Position.Field[31] := -20;
  455.   Position.Field[29] := 70;
  456.   Position.Active := ActiveWhite;
  457. //  Position.Field[0] := 20;
  458. //  Position.Field[2] := -70;
  459. //  Position.Active := ActiveBlack;
  460.   Position.MoveCount := 0;
  461.   PositionFrame.DefinirPosicao(Position);
  462. end;
  463.  
  464. procedure TMainForm.AddToLibraryActionExecute(Sender: TObject);
  465. var
  466.   V: Integer;
  467.   Estimate: string;
  468.   PositionFmt: string;
  469.   Index: Integer;
  470. begin
  471.   //DecimalSeparator := '.';
  472.   Estimate := InputBox('Input', 'Please, enter estimate', '');
  473.   if Estimate = '' then Exit;
  474.   Estimate := StringReplace(Estimate, ',', '.', []);
  475.   V := Round(200 * StrToFloat(Estimate));
  476.   PositionFmt := FormatPosition(PositionFrame.Position);
  477.   Index := Lib.IndexOf(PositionFmt);
  478.   if Index = -1 then
  479.     Lib.AddObject(PositionFmt, TObject(V))
  480.   else begin
  481.     Lib.Sorted := False;
  482.     Lib[Index] := PositionFmt;
  483.     Lib.Objects[Index] := TObject(V);
  484.     Lib.Sorted := True;
  485.   end;
  486.   SaveLib;
  487. end;
  488.  
  489. procedure TMainForm.CopyGameActionExecute(Sender: TObject);
  490. var
  491.   MoveNo: Integer;
  492.   Item: TListItem;
  493.   CurrentSt: string;
  494.   AllParty: TStringList;
  495.  
  496. procedure Add(const St: string);
  497. begin
  498.   if Length(CurrentSt) + Length(St) + 1 > MAX_LEN then
  499.   begin
  500.     AllParty.Add(CurrentSt);
  501.     CurrentSt := '';
  502.   end;
  503.   if CurrentSt <> '' then CurrentSt := CurrentSt + ' ';
  504.   CurrentSt := CurrentSt + St;
  505. end;
  506.  
  507. begin
  508.   AllParty := TStringList.Create;
  509.   try
  510.     CurrentSt := '';
  511.     for MoveNo := 0 to PartyView.Items.Count-1 do
  512.     begin
  513.       Item := PartyView.Items[MoveNo];
  514.       Add(Item.Caption + '.');
  515.       Add(Item.SubItems[0]);
  516.       if Item.SubItems.Count > 1 then
  517.         Add(Item.SubItems[1]);
  518.     end;
  519.     if CurrentSt <> '' then AllParty.Add(CurrentSt);
  520.     Clipboard.AsText := AllParty.Text;
  521.   finally
  522.     AllParty.Free;
  523.   end;
  524. end;
  525.  
  526. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement