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;
- 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);
- private
- { Private declarations }
- public
- { Public declarations }
- end;
- const n = 8;
- var
- Form1: TForm1;
- PlayerArray, OpponentArray : array[1..n,1..n] of string;
- GameState, EndGame, Counter : integer;
- Hidden : boolean;
- ShotClicked : boolean;
- i,j : integer;
- implementation
- {$R *.dfm}
- procedure ClearGrid();
- begin
- for i := 1 to n do
- begin
- for j := 1 to n do
- begin
- Form1.PlayerGrid.Cells[i,j] := '';
- Form1.OpponentGrid.Cells[i,j] := '';
- end;
- end;
- end;
- procedure ClearArray();
- begin
- for i := 1 to n do
- begin
- for j := 1 to n do
- begin
- PlayerArray[i,j] := '';
- OpponentArray[i,j] := '';
- end;
- end;
- end;
- procedure ShowArray( ArrayName : string );
- begin
- for i := 1 to n do
- begin
- for j := 1 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 := 1 to n do
- begin
- for j := 1 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 := 1 to n do
- begin
- for j := 1 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
- Form1.Label1.Caption := 'Koniec gry, wygrał gracz 2.';
- Form1.Button2.Visible := false;
- Form1.OpponentGrid.Enabled := false;
- DisplayGrids(true);
- GameState := 99;
- end else if (IsWinB = true) then
- begin
- Form1.Label1.Caption := 'Koniec gry, wygrał gracz 1.';
- Form1.Button2.Visible := false;
- Form1.OpponentGrid.Enabled := false;
- DisplayGrids(true);
- GameState := 99;
- end;
- end;
- procedure TForm1.Button1Click(Sender: TObject);
- begin
- inc(GameState);
- ClearGrid();
- 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);
- 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
- Form1.Label1.Caption := 'Ruch gracza drugiego.';
- DisplayGrids(true);
- ShowArray('opponent');
- ShowOpponentArray('player');
- end;
- CheckWin();
- inc(Counter);
- end;
- 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.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';
- ShowMessage(inttostr(Col) + inttostr(Row));
- 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
- Brush.Color := clWhite;
- FillRect(Rect);
- end;
- end;
- procedure TForm1.OpponentGridClick(Sender: TObject);
- begin
- if (Counter mod 2 = 0) 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;
- 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;
- ShowOpponentArray('player');
- end;
- DisplayGrids(false);
- CheckWin();
- end;
- end.
Advertisement
Add Comment
Please, Sign In to add comment