Autor Beitrag
Camulus
Hält's aus hier
Beiträge: 11

Win 2000, Win XP
Borland Delphi 6
BeitragVerfasst: Mi 28.02.07 11:01 
Hi ihrs,

nochmal ne Anfängerfrage. Ich steh wieder mal total auf dem Schlauch. Ich habe ein String Grid mit 3 Zeilen und 3 Spalten.
Also das ganze soll ein Kaesekaestchen werden. Die X und O hab ich auch schon drin;-)
Jetzt muss ich aber überprüfen wann jeweils drei in einer Reihe sind. Kann mir mal einer einen Denkanstoss geben wie man das am besten macht?

Macht man das nicht mit einer verschachtelten if- Abfrage? Wenn ja, wie krieg ich da mehrere Bedingungen rein?


Bis jetzt sieht es so aus, ist bestimmt nicht der beste Ansatz aber ist auch mein erster Versuch in Delphi.



ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    StringGrid1: TStringGrid;
    Label1: TLabel;
    Button1: TButton;
    Button2: TButton;
    procedure FormShow(Sender: TObject);
    procedure StringGrid1DblClick(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;
  x,y, zaehler :integer;

implementation

{$R *.dfm}

procedure TForm1.FormShow(Sender: TObject);
begin
zaehler :=0;
end;
//---------------------------------------------------------
procedure TForm1.StringGrid1DblClick(Sender: TObject);
begin
  x := StringGrid1.Col;
  y := StringGrid1.Row;

  if zaehler = 0 then
  begin
  stringGrid1.Cells [x,y] := 'X';
  zaehler := 1
  end
  else begin
  stringGrid1.Cells [x,y] := 'O';
  zaehler := 0;
  end;
stringGrid1.Repaint;


nd;
end.


Moderiert von user profile iconjasocul: Delphi-Tags hinzugefügt
JoelH
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 806
Erhaltene Danke: 17

Win10
Delphi Alexandria 11.2 Patch 1
BeitragVerfasst: Mo 05.03.07 15:31 
Also grundsätzlich geht sowas mit einer rekursiven Funtkion, aber das würde in diesem Fall zu weit führen, da es nur sehr wenige Möglichkeiten gibt.

Darum braucht man hier wohl nur ein paar Schleifen. Ungefähr so

ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
procedure TForm1.Button2Click(Sender: TObject);
var i : Integer;
    f : boolean;
begin
  f := false;
  For i := 0 to 2 do
  begin
    if (stringgrid.cells[0,i] = 'X') AND (stringgrid.cells[1,i] = 'X') AND (stringgrid.cells[2,i] = 'X') then
      f := true;
  end;
  For i := 0 to 2 do
  begin
    if (stringgrid.cells[i,0] = 'X') AND (stringgrid.cells[i,1] = 'X') AND (stringgrid.cells[i,2] = 'X') then
      f := true;
  end;
  if  (stringgrid.cells[0,0] = 'X') AND (stringgrid.cells[1,1] = 'X') AND (stringgrid.cells[2,2] = 'X') then
    f := true;
  if  (stringgrid.cells[0,2] = 'X') AND (stringgrid.cells[1,1] = 'X') AND (stringgrid.cells[2,0] = 'X') then
    f := true;
  if NOT(f) then
  begin
    For i := 0 to 2 do
    begin
      if (stringgrid.cells[0,i] = 'O') AND (stringgrid.cells[1,i] = 'O') AND (stringgrid.cells[2,i] = 'O') then
        f := true;
    end;
    For i := 0 to 2 do
    begin
      if (stringgrid.cells[i,0] = 'O') AND (stringgrid.cells[i,1] = 'O') AND (stringgrid.cells[i,2] = 'O') then
        f := true;
    end;
    if  (stringgrid.cells[0,0] = 'O') AND (stringgrid.cells[1,1] = 'O') AND (stringgrid.cells[2,2] = 'O') then
      f := true;
    if  (stringgrid.cells[0,2] = 'O') AND (stringgrid.cells[1,1] = 'O') AND (stringgrid.cells[2,0] = 'O') then
      f := true;
    if f then
      showmessage('O hat gewonnen.');
  end
  else
    showmessage('X hat gewonnen');
end;

_________________
mfg. Joel