Einzelnen Beitrag anzeigen

FlyorCry

Registriert seit: 15. Dez 2011
6 Beiträge
 
#10

AW: Schiffe versenken - verhindern von überlappen der schiffe

  Alt 19. Dez 2011, 09:43
Sorry,-> Anfänger.
Ich stell mal unseren ( bitte nicht Lachen ) Code rauf.
Delphi-Quellcode:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Grids, ExtCtrls, Menus;

type
  TForm1 = class(TForm)
    Label1: TLabel;
    StringGrid1: TStringGrid;
    Button1: TButton;
    Button2: TButton;
    GroupBox1: TGroupBox;
    CheckBox1: TCheckBox;
    CheckBox2: TCheckBox;
    CheckBox3: TCheckBox;
    CheckBox4: TCheckBox;
    CheckBox5: TCheckBox;
    StringGrid2: TStringGrid;
    RadioGroup1: TRadioGroup;
    RadioGroup2: TRadioGroup;
    Label2: TLabel;
    Label3: TLabel;
    MainMenu1: TMainMenu;
    Settings1: TMenuItem;
    Help1: TMenuItem;
    Highscores1: TMenuItem;
    NewGame1: TMenuItem;
    SaveGame1: TMenuItem;
    ExitGame1: TMenuItem;
    GlobalSettings1: TMenuItem;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure StringGrid1SelectCell(Sender: TObject; ACol, ARow: Integer;
    var CanSelect: Boolean);
    procedure StringGrid2SelectCell(Sender: TObject; ACol, ARow: Integer;
    var CanSelect: Boolean);
    procedure Ausgabe;
    Procedure Bufferzone;
    function checkplace(_richtung, _start_x, _start_y, _schifflaenge : integer) : boolean;
    procedure ExitGame1Click(Sender: TObject);
    procedure NewGame1Click(Sender: TObject);
    procedure GlobalSettings1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  gamefield, playerfield: array [0..11,0..11] of string;
  richtung : integer;

implementation

uses Unit2;

{$R *.dfm}
function TForm1.checkplace(_richtung, _start_x, _start_y, _schifflaenge : integer) : boolean;
var
  i : integer;
begin
  result := true;
  if _richtung = 0 then
    begin
      for i := _start_x to (_start_x + _schifflaenge) do
        if gamefield[i,_start_y] <> 'then result := false;
    end;
  if _richtung = 1 then
    begin
      for i := _start_y to (_start_y + _schifflaenge) do
        if gamefield[_start_x,i] <> 'then result := false;
    end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  richtung, i , start_x, start_y: integer;
begin
  //------ 5er --------------------------------------------
  if checkbox1.Checked then
  begin
  richtung := random(2) + 1;
  if richtung = 1 then
    begin
      repeat
        start_x := random(6) + 1;
        start_y := random(10) + 1;
      until checkplace(richtung,start_x,start_y,4) = true;
      for i := start_x to (start_x + 4) do
        gamefield[i,start_y] := 'X';
    end
  else
    begin
      repeat
        start_x := random(10) + 1;
        start_y := random(6) + 1;
      until checkplace(richtung,start_x,start_y,4) = true;
      for i := start_y to (start_y + 4) do
        gamefield[start_x,i] := 'X';
   end;
  Bufferzone();
  end;
  //------ 4er --------------------------------------------
  if checkbox2.Checked then
  begin
  richtung := random(2)+1;
  if richtung = 1 then
    begin
      repeat
        start_x := random(7)+1;
        start_y := random(10)+1;
      until checkplace(richtung,start_x,start_y,3) = true;
      for i := start_x to (start_x + 3) do
          gamefield[i,start_y] := 'X';
    end
  else
    begin
      repeat
        start_x := random(10)+1;
        start_y := random(7)+1;
      until checkplace(richtung,start_x,start_y,3) = true;
      for i := start_y to (start_y + 3) do
          gamefield[start_x,i] := 'X';
    end;
    Bufferzone();
  end;
  //------ 3er --------------------------------------------
  if checkbox3.Checked then
  begin
  richtung := random(2)+1;
  if richtung = 1 then
    begin
      repeat
        start_x := random(8)+1;
        start_y := random(10)+1;
      until checkplace(richtung,start_x,start_y,2) = true;
      for i := start_x to (start_x + 2) do
          gamefield[i,start_y] := 'X';
    end
  else
    begin
      repeat
        start_x := random(10)+1;
        start_y := random(8)+1;
      until checkplace(richtung,start_x,start_y,2) = true;
      for i := start_y to (start_y + 2) do
          gamefield[start_x,i] := 'X';
    end;
    Bufferzone();
  end;
  //------ 2er --------------------------------------------
  if checkbox4.Checked then
  begin
  richtung := random(2)+1;
  if richtung = 1 then
    begin
      repeat
        start_x := random(9) + 1;
        start_y := random(10) + 1;
      until checkplace(richtung,start_x,start_y,1) = true;
      for i := start_x to (start_x + 1) do
          gamefield[i,start_y] := 'X';
    end
  else
    begin
      repeat
        start_x := random(10) + 1;
        start_y := random(9) + 1;
      until checkplace(richtung,start_x,start_y,1) = true;
      for i := start_y to (start_y + 1) do
          gamefield[start_x,i] := 'X';
    end;
  Bufferzone()
  end;
  //------ 1er --------------------------------------------
  if checkbox5.Checked then
  begin
  richtung := random(2) + 1;
  if richtung = 1 then
    begin
      repeat
        start_x := random(10) + 1;
        start_y := random(10) + 1;
      until checkplace(richtung,start_x,start_y,0) = true;
      for i := start_x to (start_x) do
          gamefield[i,start_y] := 'X';
    end
  else
    begin
      repeat
        start_x := random(10) + 1;
        start_y := random(10) + 1;
      until checkplace(richtung,start_x,start_y,0) = true;
      for i := start_y to (start_y) do
          gamefield[start_x,i] := 'X';
    end;
  Bufferzone();
  end;

  Ausgabe();
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
randomize;
end;

procedure TForm1.Ausgabe();
var i, j :integer;
begin
  for j := 0 to 11 do
   begin
     for i := 0 to 11 do
       begin
         stringgrid2.Cells[i,j] := gamefield[i,j];
         stringgrid1.Cells[i,j] := playerfield[i,j];
       end;
   end;
end;

procedure TForm1.Button2Click(Sender: TObject);
var i, j :integer;
begin
  for j := 0 to 11 do
   begin
     for i := 0 to 11 do
       begin
         gamefield[i,j] := '';
       end;
   end;
   for j := 0 to 11 do
   begin
     for i := 0 to 11 do
       begin
         playerfield[i,j] := '';
       end;
   end;
end;

procedure TForm1.StringGrid1SelectCell(Sender: TObject; ACol,
  ARow: Integer; var CanSelect: Boolean );
  var i, j , richtung, schifflaenge, start_x, start_y : integer;

begin
  start_x := ACol;
  start_y := ARow;

  richtung := radiogroup2.ItemIndex;
  schifflaenge := (radiogroup1.itemindex );
  checkplace(richtung,start_x, start_y, schifflaenge);

  if checkplace(richtung,start_x, start_y, schifflaenge) = false then ShowMessage('Unmöglicher Startpunkt')
  else
    if richtung = 0 then
      begin
        for i := ACol to (ACol + schifflaenge) do
          begin
            for j := ARow to ARow do
              begin
                playerfield[i,j] := 'x';
              end;
          end;

      end;
      if richtung = 1 then
      begin
        for i := ACol to ACol do
          begin
            for j := ARow to (ARow + schifflaenge) do
              begin
                playerfield[i,j] := 'x';
              end;
          end;

      end;
  Ausgabe();
end;

procedure TForm1.StringGrid2SelectCell(Sender: TObject; ACol,
  ARow: Integer; var CanSelect: Boolean);
begin
 if gamefield[Acol,arow] = 'then showmessage('Kein Treffer')
 else showmessage('Treffer');
end;

procedure TForm1.Bufferzone();
var i,j : integer;
begin
  for i := 0 to 11 do
    begin
      for j :=0 to 11 do
        begin
          if gamefield[i,j] = 'Xthen
            begin
              if gamefield[i - 1,j - 1] = 'Then gamefield[i - 1,j - 1] := 'o';
              if gamefield[i - 1,j] = 'Then gamefield[i - 1,j] := 'o';
              if gamefield[i - 1,j + 1] = 'Then gamefield[i - 1,j + 1] := 'o';
              if gamefield[i,j - 1] = 'Then gamefield[i,j - 1] := 'o';
              if gamefield[i + 1,j - 1]='Then gamefield[i + 1,j - 1] := 'o';
              if gamefield[i + 1 ,j + 1] = 'Then gamefield[i + 1,j +1] := 'o';
              if gamefield[i + 1,j] = 'Then gamefield[i + 1,j] := 'o';
              if gamefield[i,j + 1] = 'Then gamefield[i,j + 1] := 'o';
            end;
        end;
    end;
end;

procedure TForm1.ExitGame1Click(Sender: TObject);
begin
  close;
end;

procedure TForm1.NewGame1Click(Sender: TObject);
var i, j :integer;
begin
  for j := 0 to 11 do
   begin
     for i := 0 to 11 do
       begin
         gamefield[i,j] := '';
       end;
   end;
   for j := 0 to 11 do
   begin
     for i := 0 to 11 do
       begin
         playerfield[i,j] := '';
       end;
   end;
end;

procedure TForm1.GlobalSettings1Click(Sender: TObject);
begin
  Form2.Visible := true;
end;

end.
  Mit Zitat antworten Zitat