Advertisement
feihung

Untitled

Dec 20th, 2014
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 12.91 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.     N1: TMenuItem;
  21.     ExitApplication: TMenuItem;
  22.     Options1: TMenuItem;
  23.     SecondPlayer1: TMenuItem;
  24.     Computer1: TMenuItem;
  25.  
  26.     procedure NewGameClick(Sender: TObject);
  27.     procedure PlayerGridClick(Sender: TObject);
  28.     procedure OpponentGridClick(Sender: TObject);
  29.     procedure PlayerDrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect;
  30.       State: TGridDrawState);
  31.     procedure OpponentDrawCell(Sender: TObject; ACol, ARow: Integer;
  32.       Rect: TRect; State: TGridDrawState);
  33.     procedure Button1Click(Sender: TObject);
  34.     procedure Button2Click(Sender: TObject);
  35.     procedure ExitApplicationClick(Sender: TObject);
  36.     procedure SecondPlayer1Click(Sender: TObject);
  37.     procedure Computer1Click(Sender: TObject);
  38.     procedure FormCreate(Sender: TObject);
  39.   private
  40.     { Private declarations }
  41.   public
  42.     { Public declarations }
  43.  
  44.   end;
  45. const n = 9;
  46. var
  47.   Form1: TForm1;
  48.   PlayerArray, OpponentArray : array[0..n,0..n] of string;
  49.   GameState, EndGame, Counter : integer;
  50.   Hidden : boolean;
  51.   ComputerPlay : boolean;
  52.   i,j : integer;
  53.  
  54. implementation
  55.  
  56. {$R *.dfm}
  57.  
  58. //Form Create
  59. procedure TForm1.FormCreate(Sender: TObject);
  60. begin
  61.   ComputerPlay := false;
  62. end;
  63.  
  64. procedure ClearGrid();
  65. begin
  66.   for i := 0 to n do
  67.   begin
  68.     for j := 0 to n do
  69.     begin
  70.       Form1.PlayerGrid.Cells[i,j] := '';
  71.       Form1.OpponentGrid.Cells[i,j] := '';
  72.     end;
  73.   end;
  74. end;
  75.  
  76. procedure ClearArray();
  77. begin
  78.   for i := 0 to n do
  79.   begin
  80.     for j := 0 to n do
  81.     begin
  82.       PlayerArray[i,j] := '';
  83.       OpponentArray[i,j] := '';
  84.     end;
  85.   end;
  86. end;
  87.  
  88. procedure ShowArray( ArrayName : string );
  89. begin
  90.   for i := 0 to n do
  91.   begin
  92.     for j := 0 to n do
  93.     begin
  94.       if ArrayName = 'player' then
  95.       begin
  96.         Form1.PlayerGrid.Cells[i,j] := PlayerArray[i,j];
  97.       end else if ArrayName = 'opponent' then
  98.       begin
  99.         Form1.PlayerGrid.Cells[i,j] := OpponentArray[i,j];
  100.       end;
  101.     end;
  102.   end;
  103. end;
  104.  
  105. procedure ShowOpponentArray( ArrayName : string );
  106. begin
  107.   for i := 0 to n do
  108.   begin
  109.     for j := 0 to n do
  110.     begin
  111.       if ArrayName = 'player' then
  112.       begin
  113.         Form1.OpponentGrid.Cells[i,j] := PlayerArray[i,j];
  114.       end else if ArrayName = 'opponent' then
  115.       begin
  116.         Form1.OpponentGrid.Cells[i,j] := OpponentArray[i,j];
  117.       end;
  118.     end;
  119.   end;
  120. end;
  121.  
  122. procedure DisplayGrids ( variable : boolean );
  123. begin
  124.   if variable = true then
  125.   begin
  126.     Form1.PlayerGrid.Visible := true;
  127.     Form1.OpponentGrid.Visible := true;
  128.   end else
  129.   begin
  130.     Form1.PlayerGrid.Visible := false;
  131.     Form1.OpponentGrid.Visible := false;
  132.   end;
  133. end;
  134.  
  135. procedure CheckWin ();
  136. var
  137.   IsWinA, IsWinB : boolean;
  138. begin
  139.   IsWinA := true;
  140.   IsWinB := true;
  141.   for i := 0 to n do
  142.   begin
  143.     for j := 0 to n do
  144.     begin
  145.       if PlayerArray[i,j] = '1' then
  146.         IsWinA := false;
  147.       if OpponentArray[i,j] = '1' then
  148.         IsWinB := false;
  149.     end;
  150.   end;
  151.  
  152.   if (IsWinA = true) then
  153.   begin
  154.     if (ComputerPlay = false) then
  155.     begin
  156.       Form1.Label1.Caption := 'Koniec gry, wygrał gracz 2.';
  157.     end else
  158.     begin
  159.       Form1.Label1.Caption := 'Koniec gry, wygrał komputer.';
  160.     end;
  161.   end else if (IsWinB = true) then
  162.   begin
  163.     Form1.Label1.Caption := 'Koniec gry, wygrał gracz 1.';
  164.   end;
  165.  
  166.   if (IsWinA = true) or (IsWinB = true) then
  167.   begin
  168.     Form1.Button2.Visible := false;
  169.     Form1.OpponentGrid.Enabled := false;
  170.     DisplayGrids(true);
  171.     GameState := 99;
  172.   end;
  173. end;
  174.  
  175. procedure ComputerShot();
  176. var
  177.   x,y : byte;
  178. begin
  179.   Form1.Label1.Caption := 'Ruch komputera.';
  180.   DisplayGrids(true);
  181.   randomize;
  182.   repeat
  183.     x := random(n+1);
  184.     y := random(n+1);
  185.   until ( PlayerArray[x,y] = '' ) or ( PlayerArray[x,y] = '1' );
  186.   if (PlayerArray[x,y] = '1') then
  187.   begin
  188.     PlayerArray[x,y] := '3';
  189.   end else
  190.   begin
  191.     PlayerArray[x,y] := '2';
  192.   end;
  193.   ShowArray('player');
  194.   ShowOpponentArray('opponent');
  195. end;
  196.  
  197. function isCollision ( x : integer; y : integer; orientation : integer; size : integer ) : boolean;
  198. var
  199.   i : integer;
  200.   check : boolean;
  201. begin
  202.   check := false;
  203.   if (orientation = 0) then
  204.   begin
  205.     for i := y-1 to y+size do
  206.     begin
  207.       if (OpponentArray[x,i] <> '')  then
  208.       begin
  209.         check := true;
  210.       end;
  211.     end;
  212.   end else
  213.   begin
  214.     for i:= x-1 to x+size do
  215.     begin
  216.       if (OpponentArray[i,y] <> '') then
  217.       begin
  218.         check := true;
  219.       end;
  220.     end;
  221.   end;
  222.   if check = true then
  223.   begin
  224.     result := true;
  225.   end else
  226.   begin
  227.     result := false;
  228.   end;
  229. end;
  230.  
  231. procedure placeHolder ( x : integer; y : integer );
  232. begin
  233.   if OpponentArray[x,y] = '' then
  234.   begin
  235.     OpponentArray[x,y] := '5';
  236.   end;
  237. end;
  238.  
  239. procedure placeShipInArray ( x : integer; y : integer; orientation : integer; size : integer );
  240. var
  241.   i : byte;
  242. begin
  243.   if (orientation = 0) then
  244.   begin
  245.     if ( y-1 >= 0 ) then
  246.     begin
  247. //      OpponentArray[x,y-1] := '5';
  248.       placeHolder(x,y-1);
  249.       if ( x-1 >= 0 ) then
  250.         //OpponentArray[x-1,y-1] := '5';
  251.         placeHolder(x-1,y-1);
  252.       if ( x+1 <= n ) then
  253.         //OpponentArray[x+1,y-1] := '5';
  254.         placeHolder(x+1, y-1);
  255.     end;
  256.  
  257.     for i := y to y+size-1 do
  258.     begin
  259.       OpponentArray[x,i] := '1';
  260.       placeHolder(x,i);
  261.       if ( x-1 >= 0 ) then
  262.         //OpponentArray[x-1,i] := '5';
  263.         placeHolder(x-1,i);
  264.       if ( x+1 <= n ) then
  265.         //OpponentArray[x+1,i] := '5';
  266.         placeHolder(x+1,i);
  267.  
  268.     end;
  269.     if ( y+size <= n ) then
  270.     begin
  271.       //OpponentArray[x,y+size] := '5';
  272.       placeHolder(x, y+size);
  273.       if ( x-1 >= 0 ) then
  274.         //OpponentArray[x-1,y+size] := '5';
  275.         placeHolder(x-1, y+size);
  276.       if ( x+1 <= n ) then
  277.         //OpponentArray[x+1,y+size] := '5';
  278.         placeHolder(x+1, y+size);
  279.     end;
  280.   end
  281.  
  282.   else
  283.  
  284.   begin
  285.     if ( x-1 >= 0 ) then
  286.     begin
  287.       //OpponentArray[x-1,y] := '5';
  288.       placeHolder(x-1, y);
  289.       if ( y-1 >= 0 ) then
  290.         //OpponentArray[x-1,y-1] := '5';
  291.         placeHolder(x-1, y-1);
  292.       if ( y+1 <= n ) then
  293.         //OpponentArray[x-1,y+1] := '5';
  294.         placeHolder(x-1, y+1);
  295.     end;
  296.     for i := x to x+size-1 do
  297.     begin
  298.       OpponentArray[i,y] := '1';
  299.       placeHolder(i,y);
  300.       if ( y-1 >= 0 ) then
  301.         //OpponentArray[i,y-1] := '5';
  302.         placeHolder(i, y-1);
  303.       if ( y+1 <= n ) then
  304.         //OpponentArray[i,y+1] := '5';
  305.         placeHolder(i, y+1);
  306.     end;
  307.     if ( x+size <= n ) then
  308.     begin
  309.       //OpponentArray[x+size,y] := '5';
  310.       placeHolder(x+size, y);
  311.       if ( y-1 >= 0 ) then
  312.         //OpponentArray[x+size,y-1] := '5';
  313.         placeHolder(x+size, y-1);
  314.       if ( y+1 <= n ) then
  315.         //OpponentArray[x+size,y+1] := '5';
  316.         placeHolder(x+size, y+1);
  317.     end;
  318.   end;
  319.  
  320. end;
  321.  
  322. procedure addShip ( size : integer );
  323. var
  324.   x,y,orientation : integer;
  325. begin
  326.   repeat
  327.     orientation := random(2);
  328.     if (orientation = 0) then
  329.     begin
  330.       x := random(n+1);
  331.       y := random( (n+1) - size + 1 );
  332.     end else
  333.     begin
  334.       x := random( (n+1) - size + 1 );
  335.       y := random(n+1);
  336.     end;
  337.   until isCollision(x,y, orientation, size);
  338.   placeShipInArray(x,y, orientation,size);
  339. end;
  340.  
  341. procedure TForm1.Button1Click(Sender: TObject);
  342. begin
  343.   inc(GameState);
  344.   ClearGrid();
  345.   if (GameState = 2) and (ComputerPlay = true) then
  346.   begin
  347.     addShip(4);
  348.     addShip(3);
  349. //    addShip(3);
  350. //    addShip(2);
  351. //    addShip(2);
  352. //    addShip(2);
  353.     ClearGrid();
  354.     inc(GameState);
  355.   end;
  356.   if (GameState = 3) then
  357.   begin
  358.     Form1.Label1.Caption := 'Statki ustawione, aby rozpocząć grę naciśnij przycisk start.';
  359.     Form1.PlayerGrid.Left := 112;
  360.     DisplayGrids(false);
  361.     Form1.Button1.Visible := false;
  362.     Form1.Button2.Caption := 'Start';
  363.     Form1.Button2.Visible := true;
  364.   end;
  365. end;
  366.  
  367. procedure TForm1.Button2Click(Sender: TObject);
  368. var
  369.   x,y : byte;
  370. begin
  371.   Form1.Button2.Caption := 'Kontynuuj';
  372.   if (Counter mod 2 = 1) then
  373.   begin
  374.       Form1.Label1.Caption := 'Ruch gracza pierwszego.';
  375.       DisplayGrids(true);
  376.       ShowArray('player');
  377.       ShowOpponentArray('opponent');
  378.  
  379.   end else
  380.   begin
  381.       if (ComputerPlay = false) then
  382.       begin
  383.         Form1.Label1.Caption := 'Ruch gracza drugiego.';
  384.         DisplayGrids(true);
  385.         ShowArray('opponent');
  386.         ShowOpponentArray('player');
  387.       end;
  388.   end;
  389.   CheckWin();
  390. end;
  391.  
  392. //Menu: Game
  393. procedure TForm1.NewGameClick(Sender: TObject);
  394. begin
  395.   GameState := 1;
  396.   Counter := 1;
  397.   ClearArray();
  398.   ClearGrid();
  399.   Form1.Button1.Visible := true;
  400.   Form1.PlayerGrid.Left := 112 + 140;
  401.   Form1.PlayerGrid.Visible := true;
  402.   Form1.Button2.Visible := false;
  403.   Form1.Label1.Caption := 'Ustaw statki na planszy i naciśnij kontynuuj';
  404.  
  405. end;
  406.  
  407. procedure TForm1.ExitApplicationClick(Sender: TObject);
  408. begin
  409.   Application.ShowMainForm := False;
  410.   Application.Terminate;
  411. end;
  412.  
  413. //Menu: Options
  414. procedure TForm1.SecondPlayer1Click(Sender: TObject);
  415. begin
  416.   ComputerPlay := false;
  417.   Form1.SecondPlayer1.Checked := true;
  418.   Form1.Computer1.Checked := false;
  419. end;
  420.  
  421. procedure TForm1.Computer1Click(Sender: TObject);
  422. begin
  423.   ComputerPlay := true;
  424.   Form1.SecondPlayer1.Checked := false;
  425.   Form1.Computer1.Checked := true;
  426. end;
  427.  
  428.  
  429. procedure TForm1.PlayerDrawCell(Sender: TObject; ACol, ARow: Integer;
  430.   Rect: TRect; State: TGridDrawState);
  431. begin
  432.     with PlayerGrid, Canvas do
  433.     begin
  434.       if Cells[ACol, ARow] = '1' then
  435.       begin
  436.         Brush.Color := clBlack
  437.       end
  438.       else if Cells[ACol, ARow] = '2' then
  439.       begin
  440.         Brush.Color := clGreen;
  441.       end
  442.       else if Cells[ACol, ARow] = '3' then
  443.       begin
  444.         Brush.Color := clRed;
  445.       end
  446.       else
  447.         Brush.Color := clWhite;
  448.       FillRect(Rect);
  449.     end;
  450.  
  451. end;
  452.  
  453. procedure TForm1.PlayerGridClick(Sender: TObject);
  454. begin
  455.   if (GameState = 1) then
  456.   begin
  457.  
  458.     with PlayerGrid do
  459.     begin
  460.       if Cells[Col, Row] = '' then
  461.       begin
  462.         Cells[Col, Row] := '1';
  463.         PlayerArray[Col, Row] := '1';
  464.       end
  465.       else
  466.       begin
  467.         Cells[Col, Row] := '';
  468.         PlayerArray[Col, Row] := '';
  469.       end;
  470.     end;
  471.  
  472.   end else if (GameState = 2) then
  473.   begin
  474.  
  475.     with PlayerGrid do
  476.     begin
  477.       if Cells[Col, Row] = '' then
  478.       begin
  479.         Cells[Col, Row] := '1';
  480.         OpponentArray[Col, Row] := '1';
  481.       end
  482.       else
  483.       begin
  484.         Cells[Col, Row] := '';
  485.         OpponentArray[Col, Row] := '';
  486.       end;
  487.     end;
  488.  
  489.   end;
  490. end;
  491.  
  492. procedure TForm1.OpponentDrawCell(Sender: TObject; ACol, ARow: Integer;
  493.   Rect: TRect; State: TGridDrawState);
  494. begin
  495.   with OpponentGrid, Canvas do
  496.   begin
  497.       if Cells[ACol, ARow] = '1' then
  498.       begin
  499.         Brush.Color := clBlack;
  500.       end
  501.       else if Cells[ACol, ARow] = '2' then
  502.       begin
  503.         Brush.Color := clGreen;
  504.       end
  505.       else if Cells[ACol, ARow] = '3' then
  506.       begin
  507.         Brush.Color := clRed;
  508.       end
  509.       else if Cells[ACol, ARow] = '5' then
  510.       begin
  511.         Brush.Color := clYellow;
  512.       end
  513.       else
  514.         Brush.Color := clWhite;
  515.       FillRect(Rect);
  516.   end;
  517. end;
  518.  
  519. procedure TForm1.OpponentGridClick(Sender: TObject);
  520. var
  521.   x,y : byte;
  522. begin
  523.   if (Counter mod 2 = 1) then
  524.   begin
  525.     // Player 1 shot, write on Player 2 array
  526.     with OpponentGrid do
  527.     begin
  528.       if ( OpponentArray[Col, Row] = '1' ) or ( OpponentArray[Col, Row] = '3' ) then
  529.       begin
  530.         OpponentArray[Col, Row] := '3';
  531.         Form1.Label1.Caption := 'Trafiony!';
  532.       end else
  533.       begin
  534.         OpponentArray[Col, Row] := '2';
  535.         Form1.Label1.Caption := 'Pudło!';
  536.       end;
  537.     end;
  538.     inc(Counter);
  539.     ShowOpponentArray('opponent');
  540.   end else
  541.   begin
  542.     // Player 2 shot, write on Player 1 array
  543.     with OpponentGrid do
  544.     begin
  545.       if ( PlayerArray[Col, Row] = '1' ) or ( PlayerArray[Col, Row] = '3' ) then
  546.       begin
  547.         PlayerArray[Col, Row] := '3';
  548.         Form1.Label1.Caption := 'Trafiony!';
  549.       end else
  550.       begin
  551.         PlayerArray[Col, Row] := '2';
  552.         Form1.Label1.Caption := 'Pudło!';
  553.       end;
  554.     end;
  555.     inc(Counter);
  556.     ShowOpponentArray('player');
  557.   end;
  558.  
  559.   if (Counter mod 2 = 0) and (ComputerPlay = true) then
  560.   begin
  561.     //Computer shot
  562.     ComputerShot();
  563.     CheckWin();
  564.     Form1.Label1.Caption := 'Ruch gracza pierwszego.';
  565.     inc(Counter);
  566.   end;
  567.  
  568.   if (ComputerPlay = false) then
  569.   begin
  570.     DisplayGrids(false);
  571.   end;
  572.  
  573.   CheckWin();
  574. end;
  575.  
  576. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement