Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- unit Unit1;
- interface
- uses
- Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
- Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.Menus, Vcl.StdCtrls, Vcl.ExtCtrls,
- Vcl.Grids;
- type
- TForm1 = class(TForm)
- MainMenu1: TMainMenu;
- Game1: TMenuItem;
- NewGame: TMenuItem;
- Label1: TLabel;
- PlayerGrid: TStringGrid;
- OpponentGrid: TStringGrid;
- Button1: TButton;
- Button2: TButton;
- N1: TMenuItem;
- ExitApplication: TMenuItem;
- Options1: TMenuItem;
- SecondPlayer1: TMenuItem;
- Computer1: TMenuItem;
- procedure NewGameClick(Sender: TObject);
- procedure PlayerGridClick(Sender: TObject);
- procedure OpponentGridClick(Sender: TObject);
- procedure PlayerDrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect;
- State: TGridDrawState);
- procedure OpponentDrawCell(Sender: TObject; ACol, ARow: Integer;
- Rect: TRect; State: TGridDrawState);
- procedure Button1Click(Sender: TObject);
- procedure Button2Click(Sender: TObject);
- procedure ExitApplicationClick(Sender: TObject);
- procedure SecondPlayer1Click(Sender: TObject);
- procedure Computer1Click(Sender: TObject);
- procedure FormCreate(Sender: TObject);
- private
- { Private declarations }
- public
- { Public declarations }
- end;
- const n = 9;
- var
- Form1: TForm1;
- PlayerArray, OpponentArray : array[0..n,0..n] of string;
- GameState, EndGame, Counter : integer;
- Hidden : boolean;
- ComputerPlay : boolean;
- i,j : integer;
- implementation
- {$R *.dfm}
- //Form Create
- procedure TForm1.FormCreate(Sender: TObject);
- begin
- ComputerPlay := false;
- end;
- procedure ClearGrid();
- begin
- for i := 0 to n do
- begin
- for j := 0 to n do
- begin
- Form1.PlayerGrid.Cells[i,j] := '';
- Form1.OpponentGrid.Cells[i,j] := '';
- end;
- end;
- end;
- procedure ClearArray();
- begin
- for i := 0 to n do
- begin
- for j := 0 to n do
- begin
- PlayerArray[i,j] := '';
- OpponentArray[i,j] := '';
- end;
- end;
- end;
- procedure ShowArray( ArrayName : string );
- begin
- for i := 0 to n do
- begin
- for j := 0 to n do
- begin
- if ArrayName = 'player' then
- begin
- Form1.PlayerGrid.Cells[i,j] := PlayerArray[i,j];
- end else if ArrayName = 'opponent' then
- begin
- Form1.PlayerGrid.Cells[i,j] := OpponentArray[i,j];
- end;
- end;
- end;
- end;
- procedure ShowOpponentArray( ArrayName : string );
- begin
- for i := 0 to n do
- begin
- for j := 0 to n do
- begin
- if ArrayName = 'player' then
- begin
- Form1.OpponentGrid.Cells[i,j] := PlayerArray[i,j];
- end else if ArrayName = 'opponent' then
- begin
- Form1.OpponentGrid.Cells[i,j] := OpponentArray[i,j];
- end;
- end;
- end;
- end;
- procedure DisplayGrids ( variable : boolean );
- begin
- if variable = true then
- begin
- Form1.PlayerGrid.Visible := true;
- Form1.OpponentGrid.Visible := true;
- end else
- begin
- Form1.PlayerGrid.Visible := false;
- Form1.OpponentGrid.Visible := false;
- end;
- end;
- procedure CheckWin ();
- var
- IsWinA, IsWinB : boolean;
- begin
- IsWinA := true;
- IsWinB := true;
- for i := 0 to n do
- begin
- for j := 0 to n do
- begin
- if PlayerArray[i,j] = '1' then
- IsWinA := false;
- if OpponentArray[i,j] = '1' then
- IsWinB := false;
- end;
- end;
- if (IsWinA = true) then
- begin
- if (ComputerPlay = false) then
- begin
- Form1.Label1.Caption := 'Koniec gry, wygrał gracz 2.';
- end else
- begin
- Form1.Label1.Caption := 'Koniec gry, wygrał komputer.';
- end;
- end else if (IsWinB = true) then
- begin
- Form1.Label1.Caption := 'Koniec gry, wygrał gracz 1.';
- end;
- if (IsWinA = true) or (IsWinB = true) then
- begin
- Form1.Button2.Visible := false;
- Form1.OpponentGrid.Enabled := false;
- DisplayGrids(true);
- GameState := 99;
- end;
- end;
- procedure ComputerShot();
- var
- x,y : byte;
- begin
- Form1.Label1.Caption := 'Ruch komputera.';
- DisplayGrids(true);
- randomize;
- repeat
- x := random(n+1);
- y := random(n+1);
- until ( PlayerArray[x,y] = '' ) or ( PlayerArray[x,y] = '1' );
- if (PlayerArray[x,y] = '1') then
- begin
- PlayerArray[x,y] := '3';
- end else
- begin
- PlayerArray[x,y] := '2';
- end;
- ShowArray('player');
- ShowOpponentArray('opponent');
- end;
- function isCollision ( x : integer; y : integer; orientation : integer; size : integer ) : boolean;
- var
- i : integer;
- check : boolean;
- begin
- check := false;
- if (orientation = 0) then
- begin
- for i := y-1 to y+size do
- begin
- if (OpponentArray[x,i] <> '') then
- begin
- check := true;
- end;
- end;
- end else
- begin
- for i:= x-1 to x+size do
- begin
- if (OpponentArray[i,y] <> '') then
- begin
- check := true;
- end;
- end;
- end;
- if check = true then
- begin
- result := true;
- end else
- begin
- result := false;
- end;
- end;
- procedure placeHolder ( x : integer; y : integer );
- begin
- if OpponentArray[x,y] = '' then
- begin
- OpponentArray[x,y] := '5';
- end;
- end;
- procedure placeShipInArray ( x : integer; y : integer; orientation : integer; size : integer );
- var
- i : byte;
- begin
- if (orientation = 0) then
- begin
- if ( y-1 >= 0 ) then
- begin
- // OpponentArray[x,y-1] := '5';
- placeHolder(x,y-1);
- if ( x-1 >= 0 ) then
- //OpponentArray[x-1,y-1] := '5';
- placeHolder(x-1,y-1);
- if ( x+1 <= n ) then
- //OpponentArray[x+1,y-1] := '5';
- placeHolder(x+1, y-1);
- end;
- for i := y to y+size-1 do
- begin
- OpponentArray[x,i] := '1';
- placeHolder(x,i);
- if ( x-1 >= 0 ) then
- //OpponentArray[x-1,i] := '5';
- placeHolder(x-1,i);
- if ( x+1 <= n ) then
- //OpponentArray[x+1,i] := '5';
- placeHolder(x+1,i);
- end;
- if ( y+size <= n ) then
- begin
- //OpponentArray[x,y+size] := '5';
- placeHolder(x, y+size);
- if ( x-1 >= 0 ) then
- //OpponentArray[x-1,y+size] := '5';
- placeHolder(x-1, y+size);
- if ( x+1 <= n ) then
- //OpponentArray[x+1,y+size] := '5';
- placeHolder(x+1, y+size);
- end;
- end
- else
- begin
- if ( x-1 >= 0 ) then
- begin
- //OpponentArray[x-1,y] := '5';
- placeHolder(x-1, y);
- if ( y-1 >= 0 ) then
- //OpponentArray[x-1,y-1] := '5';
- placeHolder(x-1, y-1);
- if ( y+1 <= n ) then
- //OpponentArray[x-1,y+1] := '5';
- placeHolder(x-1, y+1);
- end;
- for i := x to x+size-1 do
- begin
- OpponentArray[i,y] := '1';
- placeHolder(i,y);
- if ( y-1 >= 0 ) then
- //OpponentArray[i,y-1] := '5';
- placeHolder(i, y-1);
- if ( y+1 <= n ) then
- //OpponentArray[i,y+1] := '5';
- placeHolder(i, y+1);
- end;
- if ( x+size <= n ) then
- begin
- //OpponentArray[x+size,y] := '5';
- placeHolder(x+size, y);
- if ( y-1 >= 0 ) then
- //OpponentArray[x+size,y-1] := '5';
- placeHolder(x+size, y-1);
- if ( y+1 <= n ) then
- //OpponentArray[x+size,y+1] := '5';
- placeHolder(x+size, y+1);
- end;
- end;
- end;
- procedure addShip ( size : integer );
- var
- x,y,orientation : integer;
- begin
- repeat
- orientation := random(2);
- if (orientation = 0) then
- begin
- x := random(n+1);
- y := random( (n+1) - size + 1 );
- end else
- begin
- x := random( (n+1) - size + 1 );
- y := random(n+1);
- end;
- until isCollision(x,y, orientation, size);
- placeShipInArray(x,y, orientation,size);
- end;
- procedure TForm1.Button1Click(Sender: TObject);
- begin
- inc(GameState);
- ClearGrid();
- if (GameState = 2) and (ComputerPlay = true) then
- begin
- addShip(4);
- addShip(3);
- // addShip(3);
- // addShip(2);
- // addShip(2);
- // addShip(2);
- ClearGrid();
- inc(GameState);
- end;
- if (GameState = 3) then
- begin
- Form1.Label1.Caption := 'Statki ustawione, aby rozpocząć grę naciśnij przycisk start.';
- Form1.PlayerGrid.Left := 112;
- DisplayGrids(false);
- Form1.Button1.Visible := false;
- Form1.Button2.Caption := 'Start';
- Form1.Button2.Visible := true;
- end;
- end;
- procedure TForm1.Button2Click(Sender: TObject);
- var
- x,y : byte;
- begin
- Form1.Button2.Caption := 'Kontynuuj';
- if (Counter mod 2 = 1) then
- begin
- Form1.Label1.Caption := 'Ruch gracza pierwszego.';
- DisplayGrids(true);
- ShowArray('player');
- ShowOpponentArray('opponent');
- end else
- begin
- if (ComputerPlay = false) then
- begin
- Form1.Label1.Caption := 'Ruch gracza drugiego.';
- DisplayGrids(true);
- ShowArray('opponent');
- ShowOpponentArray('player');
- end;
- end;
- CheckWin();
- end;
- //Menu: Game
- procedure TForm1.NewGameClick(Sender: TObject);
- begin
- GameState := 1;
- Counter := 1;
- ClearArray();
- ClearGrid();
- Form1.Button1.Visible := true;
- Form1.PlayerGrid.Left := 112 + 140;
- Form1.PlayerGrid.Visible := true;
- Form1.Button2.Visible := false;
- Form1.Label1.Caption := 'Ustaw statki na planszy i naciśnij kontynuuj';
- end;
- procedure TForm1.ExitApplicationClick(Sender: TObject);
- begin
- Application.ShowMainForm := False;
- Application.Terminate;
- end;
- //Menu: Options
- procedure TForm1.SecondPlayer1Click(Sender: TObject);
- begin
- ComputerPlay := false;
- Form1.SecondPlayer1.Checked := true;
- Form1.Computer1.Checked := false;
- end;
- procedure TForm1.Computer1Click(Sender: TObject);
- begin
- ComputerPlay := true;
- Form1.SecondPlayer1.Checked := false;
- Form1.Computer1.Checked := true;
- end;
- procedure TForm1.PlayerDrawCell(Sender: TObject; ACol, ARow: Integer;
- Rect: TRect; State: TGridDrawState);
- begin
- with PlayerGrid, Canvas do
- begin
- if Cells[ACol, ARow] = '1' then
- begin
- Brush.Color := clBlack
- end
- else if Cells[ACol, ARow] = '2' then
- begin
- Brush.Color := clGreen;
- end
- else if Cells[ACol, ARow] = '3' then
- begin
- Brush.Color := clRed;
- end
- else
- Brush.Color := clWhite;
- FillRect(Rect);
- end;
- end;
- procedure TForm1.PlayerGridClick(Sender: TObject);
- begin
- if (GameState = 1) then
- begin
- with PlayerGrid do
- begin
- if Cells[Col, Row] = '' then
- begin
- Cells[Col, Row] := '1';
- PlayerArray[Col, Row] := '1';
- end
- else
- begin
- Cells[Col, Row] := '';
- PlayerArray[Col, Row] := '';
- end;
- end;
- end else if (GameState = 2) then
- begin
- with PlayerGrid do
- begin
- if Cells[Col, Row] = '' then
- begin
- Cells[Col, Row] := '1';
- OpponentArray[Col, Row] := '1';
- end
- else
- begin
- Cells[Col, Row] := '';
- OpponentArray[Col, Row] := '';
- end;
- end;
- end;
- end;
- procedure TForm1.OpponentDrawCell(Sender: TObject; ACol, ARow: Integer;
- Rect: TRect; State: TGridDrawState);
- begin
- with OpponentGrid, Canvas do
- begin
- if Cells[ACol, ARow] = '1' then
- begin
- Brush.Color := clBlack;
- end
- else if Cells[ACol, ARow] = '2' then
- begin
- Brush.Color := clGreen;
- end
- else if Cells[ACol, ARow] = '3' then
- begin
- Brush.Color := clRed;
- end
- else if Cells[ACol, ARow] = '5' then
- begin
- Brush.Color := clYellow;
- end
- else
- Brush.Color := clWhite;
- FillRect(Rect);
- end;
- end;
- procedure TForm1.OpponentGridClick(Sender: TObject);
- var
- x,y : byte;
- begin
- if (Counter mod 2 = 1) then
- begin
- // Player 1 shot, write on Player 2 array
- with OpponentGrid do
- begin
- if ( OpponentArray[Col, Row] = '1' ) or ( OpponentArray[Col, Row] = '3' ) then
- begin
- OpponentArray[Col, Row] := '3';
- Form1.Label1.Caption := 'Trafiony!';
- end else
- begin
- OpponentArray[Col, Row] := '2';
- Form1.Label1.Caption := 'Pudło!';
- end;
- end;
- inc(Counter);
- ShowOpponentArray('opponent');
- end else
- begin
- // Player 2 shot, write on Player 1 array
- with OpponentGrid do
- begin
- if ( PlayerArray[Col, Row] = '1' ) or ( PlayerArray[Col, Row] = '3' ) then
- begin
- PlayerArray[Col, Row] := '3';
- Form1.Label1.Caption := 'Trafiony!';
- end else
- begin
- PlayerArray[Col, Row] := '2';
- Form1.Label1.Caption := 'Pudło!';
- end;
- end;
- inc(Counter);
- ShowOpponentArray('player');
- end;
- if (Counter mod 2 = 0) and (ComputerPlay = true) then
- begin
- //Computer shot
- ComputerShot();
- CheckWin();
- Form1.Label1.Caption := 'Ruch gracza pierwszego.';
- inc(Counter);
- end;
- if (ComputerPlay = false) then
- begin
- DisplayGrids(false);
- end;
- CheckWin();
- end;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement