feihung

Statki - 20.12.2014, 9:08

Dec 20th, 2014
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 7.54 KB | None | 0 0
  1. unit Unit1;
  2.  
  3. interface
  4.  
  5. uses
  6.   Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  7.   Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.Menus, Vcl.StdCtrls, Vcl.ExtCtrls,
  8.   Vcl.Grids;
  9.  
  10. type
  11.   TForm1 = class(TForm)
  12.     MainMenu1: TMainMenu;
  13.     Game1: TMenuItem;
  14.     NewGame: TMenuItem;
  15.     Label1: TLabel;
  16.     PlayerGrid: TStringGrid;
  17.     OpponentGrid: TStringGrid;
  18.     Button1: TButton;
  19.     Button2: TButton;
  20.  
  21.     procedure NewGameClick(Sender: TObject);
  22.     procedure PlayerGridClick(Sender: TObject);
  23.     procedure OpponentGridClick(Sender: TObject);
  24.     procedure PlayerDrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect;
  25.       State: TGridDrawState);
  26.     procedure OpponentDrawCell(Sender: TObject; ACol, ARow: Integer;
  27.       Rect: TRect; State: TGridDrawState);
  28.     procedure Button1Click(Sender: TObject);
  29.     procedure Button2Click(Sender: TObject);
  30.   private
  31.     { Private declarations }
  32.   public
  33.     { Public declarations }
  34.  
  35.   end;
  36. const n = 8;
  37. var
  38.   Form1: TForm1;
  39.   PlayerArray, OpponentArray : array[1..n,1..n] of string;
  40.   GameState, EndGame, Counter : integer;
  41.   Hidden : boolean;
  42.   ShotClicked : boolean;
  43.   i,j : integer;
  44.  
  45. implementation
  46.  
  47. {$R *.dfm}
  48.  
  49. procedure ClearGrid();
  50. begin
  51.   for i := 1 to n do
  52.   begin
  53.     for j := 1 to n do
  54.     begin
  55.       Form1.PlayerGrid.Cells[i,j] := '';
  56.       Form1.OpponentGrid.Cells[i,j] := '';
  57.     end;
  58.   end;
  59. end;
  60.  
  61. procedure ClearArray();
  62. begin
  63.   for i := 1 to n do
  64.   begin
  65.     for j := 1 to n do
  66.     begin
  67.       PlayerArray[i,j] := '';
  68.       OpponentArray[i,j] := '';
  69.     end;
  70.   end;
  71. end;
  72.  
  73. procedure ShowArray( ArrayName : string );
  74. begin
  75.   for i := 1 to n do
  76.   begin
  77.     for j := 1 to n do
  78.     begin
  79.       if ArrayName = 'player' then
  80.       begin
  81.         Form1.PlayerGrid.Cells[i,j] := PlayerArray[i,j];
  82.       end else if ArrayName = 'opponent' then
  83.       begin
  84.         Form1.PlayerGrid.Cells[i,j] := OpponentArray[i,j];
  85.       end;
  86.     end;
  87.   end;
  88. end;
  89.  
  90. procedure ShowOpponentArray( ArrayName : string );
  91. begin
  92.   for i := 1 to n do
  93.   begin
  94.     for j := 1 to n do
  95.     begin
  96.       if ArrayName = 'player' then
  97.       begin
  98.         Form1.OpponentGrid.Cells[i,j] := PlayerArray[i,j];
  99.       end else if ArrayName = 'opponent' then
  100.       begin
  101.         Form1.OpponentGrid.Cells[i,j] := OpponentArray[i,j];
  102.       end;
  103.     end;
  104.   end;
  105. end;
  106.  
  107. procedure DisplayGrids ( variable : boolean );
  108. begin
  109.   if variable = true then
  110.   begin
  111.     Form1.PlayerGrid.Visible := true;
  112.     Form1.OpponentGrid.Visible := true;
  113.   end else
  114.   begin
  115.     Form1.PlayerGrid.Visible := false;
  116.     Form1.OpponentGrid.Visible := false;
  117.   end;
  118. end;
  119.  
  120. procedure CheckWin ();
  121. var
  122.   IsWinA, IsWinB : boolean;
  123. begin
  124.   IsWinA := true;
  125.   IsWinB := true;
  126.   for i := 1 to n do
  127.   begin
  128.     for j := 1 to n do
  129.     begin
  130.       if PlayerArray[i,j] = '1' then
  131.         IsWinA := false;
  132.       if OpponentArray[i,j] = '1' then
  133.         IsWinB := false;
  134.     end;
  135.   end;
  136.   if (IsWinA = true) then
  137.   begin
  138.     Form1.Label1.Caption := 'Koniec gry, wygrał gracz 2.';
  139.     Form1.Button2.Visible := false;
  140.     Form1.OpponentGrid.Enabled := false;
  141.     DisplayGrids(true);
  142.     GameState := 99;
  143.   end else if (IsWinB = true) then
  144.   begin
  145.     Form1.Label1.Caption := 'Koniec gry, wygrał gracz 1.';
  146.     Form1.Button2.Visible := false;
  147.     Form1.OpponentGrid.Enabled := false;
  148.     DisplayGrids(true);
  149.     GameState := 99;
  150.   end;
  151. end;
  152.  
  153. procedure TForm1.Button1Click(Sender: TObject);
  154. begin
  155.   inc(GameState);
  156.   ClearGrid();
  157.   if (GameState = 3) then
  158.   begin
  159.     Form1.Label1.Caption := 'Statki ustawione, aby rozpocząć grę naciśnij przycisk start.';
  160.     Form1.PlayerGrid.Left := 112;
  161.     DisplayGrids(false);
  162.     Form1.Button1.Visible := false;
  163.     Form1.Button2.Caption := 'Start';
  164.     Form1.Button2.Visible := true;
  165.   end;
  166. end;
  167.  
  168. procedure TForm1.Button2Click(Sender: TObject);
  169. begin
  170.   Form1.Button2.Caption := 'Kontynuuj';
  171.   if (Counter mod 2 = 1) then
  172.   begin
  173.       Form1.Label1.Caption := 'Ruch gracza pierwszego.';
  174.       DisplayGrids(true);
  175.       ShowArray('player');
  176.       ShowOpponentArray('opponent');
  177.  
  178.   end else
  179.   begin
  180.       Form1.Label1.Caption := 'Ruch gracza drugiego.';
  181.       DisplayGrids(true);
  182.       ShowArray('opponent');
  183.       ShowOpponentArray('player');
  184.   end;
  185.   CheckWin();
  186.   inc(Counter);
  187. end;
  188.  
  189.  
  190. procedure TForm1.NewGameClick(Sender: TObject);
  191. begin
  192.   GameState := 1;
  193.   Counter := 1;
  194.   ClearArray();
  195.   ClearGrid();
  196.   Form1.Button1.Visible := true;
  197.   Form1.PlayerGrid.Left := 112 + 140;
  198.   Form1.PlayerGrid.Visible := true;
  199.   Form1.Button2.Visible := false;
  200.   Form1.Label1.Caption := 'Ustaw statki na planszy i naciśnij kontynuuj';
  201.  
  202. end;
  203.  
  204. procedure TForm1.PlayerDrawCell(Sender: TObject; ACol, ARow: Integer;
  205.   Rect: TRect; State: TGridDrawState);
  206. begin
  207.     with PlayerGrid, Canvas do
  208.     begin
  209.       if Cells[ACol, ARow] = '1' then
  210.       begin
  211.         Brush.Color := clBlack
  212.       end
  213.       else if Cells[ACol, ARow] = '2' then
  214.       begin
  215.         Brush.Color := clGreen;
  216.       end
  217.       else if Cells[ACol, ARow] = '3' then
  218.       begin
  219.         Brush.Color := clRed;
  220.       end
  221.       else
  222.         Brush.Color := clWhite;
  223.       FillRect(Rect);
  224.     end;
  225.  
  226. end;
  227.  
  228. procedure TForm1.PlayerGridClick(Sender: TObject);
  229. begin
  230.   if (GameState = 1) then
  231.   begin
  232.  
  233.     with PlayerGrid do
  234.     begin
  235.       if Cells[Col, Row] = '' then
  236.       begin
  237.         Cells[Col, Row] := '1';
  238.         PlayerArray[Col, Row] := '1';
  239.         ShowMessage(inttostr(Col) + inttostr(Row));
  240.       end
  241.       else
  242.       begin
  243.         Cells[Col, Row] := '';
  244.         PlayerArray[Col, Row] := '';
  245.       end;
  246.     end;
  247.  
  248.   end else if (GameState = 2) then
  249.   begin
  250.  
  251.     with PlayerGrid do
  252.     begin
  253.       if Cells[Col, Row] = '' then
  254.       begin
  255.         Cells[Col, Row] := '1';
  256.         OpponentArray[Col, Row] := '1';
  257.       end
  258.       else
  259.       begin
  260.         Cells[Col, Row] := '';
  261.         OpponentArray[Col, Row] := '';
  262.       end;
  263.     end;
  264.  
  265.   end;
  266. end;
  267.  
  268. procedure TForm1.OpponentDrawCell(Sender: TObject; ACol, ARow: Integer;
  269.   Rect: TRect; State: TGridDrawState);
  270. begin
  271.   with OpponentGrid, Canvas do
  272.   begin
  273.       if Cells[ACol, ARow] = '1' then
  274.       begin
  275.         Brush.Color := clBlack;
  276.       end
  277.       else if Cells[ACol, ARow] = '2' then
  278.       begin
  279.         Brush.Color := clGreen;
  280.       end
  281.       else if Cells[ACol, ARow] = '3' then
  282.       begin
  283.         Brush.Color := clRed;
  284.       end
  285.       else
  286.         Brush.Color := clWhite;
  287.       FillRect(Rect);
  288.   end;
  289. end;
  290.  
  291. procedure TForm1.OpponentGridClick(Sender: TObject);
  292. begin
  293.   if (Counter mod 2 = 0) then
  294.   begin
  295.     // Player 1 shot, write on Player 2 array
  296.     with OpponentGrid do
  297.     begin
  298.       if ( OpponentArray[Col, Row] = '1' ) or ( OpponentArray[Col, Row] = '3' ) then
  299.       begin
  300.         OpponentArray[Col, Row] := '3';
  301.         Form1.Label1.Caption := 'Trafiony!';
  302.       end else
  303.       begin
  304.         OpponentArray[Col, Row] := '2';
  305.         Form1.Label1.Caption := 'Pudło!';
  306.       end;
  307.     end;
  308.     ShowOpponentArray('opponent');
  309.   end else
  310.   begin
  311.     // Player 2 shot, write on Player 1 array
  312.     with OpponentGrid do
  313.     begin
  314.       if ( PlayerArray[Col, Row] = '1' ) or ( PlayerArray[Col, Row] = '3' ) then
  315.       begin
  316.         PlayerArray[Col, Row] := '3';
  317.         Form1.Label1.Caption := 'Trafiony!';
  318.       end else
  319.       begin
  320.         PlayerArray[Col, Row] := '2';
  321.         Form1.Label1.Caption := 'Pudło!';
  322.       end;
  323.     end;
  324.     ShowOpponentArray('player');
  325.   end;
  326.   DisplayGrids(false);
  327.   CheckWin();
  328. end;
  329.  
  330. end.
Advertisement
Add Comment
Please, Sign In to add comment