  unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    Label1: TLabel;
    Edit1: TEdit;
    Edit2: TEdit;
    Button1: TButton;
    Button2: TButton;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Button3: TButton;
    Shape1: TShape;
    Shape2: TShape;
    Shape3: TShape;
    Shape4: TShape;
    Shape5: TShape;
    Shape6: TShape;
    Shape7: TShape;
    Shape8: TShape;
    Shape9: TShape;
    Shape10: TShape;
    Shape11: TShape;
    Shape12: TShape;
    Shape13: TShape;
    Shape14: TShape;
    Shape15: TShape;
    Shape16: TShape;
    MediaPlayer1: TMediaPlayer;
    Button4: TButton;
    Shape17: TShape;
    Shape18: TShape;
    Shape19: TShape;
    Shape20: TShape;
    Shape21: TShape;
    Shape22: TShape;
    Shape23: TShape;
    Shape24: TShape;
    Shape25: TShape;
    Shape26: TShape;
    Shape27: TShape;
    Shape28: TShape;
    Shape29: TShape;
    Shape30: TShape;
    Shape31: TShape;
    Shape32: TShape;
    Shape33: TShape;
    Shape34: TShape;
    Shape35: TShape;
    Shape36: TShape;
    Shape37: TShape;
    Shape38: TShape;
    Shape39: TShape;
    Shape40: TShape;
    Shape41: TShape;
    Shape42: TShape;
    Label6: TLabel;
    Label7: TLabel;
    Label8: TLabel;
    MediaPlayer2: TMediaPlayer;
    Button5: TButton;
    Shape43: TShape;
    Shape44: TShape;
    Label9: TLabel;
    Label10: TLabel;
   procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
   procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

type
  TSpeelstuk = 0..3;
  TReihe = array [1..7] of TSpeelstuk;
  TSpalte = array [1..7] of TReihe;
  TBort = array[1..6,1..7] of TShape;

var
  reihe : TReihe;
  matrix : TSpalte;
  Bort: TBort;

function IsGetal(invoer: string): boolean;
var
  temp: real;
  code: integer;
begin
  isgetal:= true;
  val(invoer, temp, code);  // code=0, dann nur zahlen. wenn code andere getal, dan letters
    If code <> 0 then
      begin
        isgetal := false;
      end;
end;

function IsKl4(Zahl1 : integer): boolean;
begin
  IsKL4 := true;
  If Zahl1 > 7 then
    begin
     IsKl4 := false;
    end;
end;


function Voll(quadrat: TSpalte) : boolean;
var
  c: integer;
  s: integer;
begin
  result:= true;
  for c:= 1 to 7 do
    for s:= 1 to 7 do
    begin
      if quadrat[c,s]= 0 then
      result := false
    end;
end;

procedure TForm1.FormCreate(Sender: TObject);
var
  bestand : TextFile;
  leesvar : string;
  i,j : integer;
begin
  AssignFile(bestand, 'File1.txt');
  Reset(bestand);
  while not (eof(bestand))do
  begin
      Readln(bestand, leesvar);
      Memo1.Lines.Add(leesvar);
  end;
    CloseFile(bestand);
    matrix[7,1] := 3;
    matrix[7,2] := 3;
    matrix[7,3] := 3;
    matrix[7,4] := 3;
    matrix[7,5] := 3;
    matrix[7,6] := 3;
    matrix[7,7] := 3;

    Bort[1,1] := Shape1 ;
    Bort[2,1] := Shape2 ;
    Bort[3,1] := Shape3 ;
    Bort[4,1] := Shape4 ;
    Bort[5,1] := Shape5 ;
    Bort[6,1] := Shape6 ;
    Bort[1,2] := Shape7 ;
    Bort[2,2] := Shape8 ;
    Bort[3,2] := Shape9 ;
    Bort[4,2] := Shape10 ;
    Bort[5,2] := Shape11 ;
    Bort[6,2] := Shape12 ;
    Bort[1,3] := Shape13 ;
    Bort[2,3] := Shape14 ;
    Bort[3,3] := Shape15 ;
    Bort[4,3] := Shape16 ;
    Bort[5,3] := Shape17 ;
    Bort[6,3] := Shape18 ;
    Bort[1,4] := Shape19 ;
    Bort[2,4] := Shape20 ;
    Bort[3,4] := Shape21 ;
    Bort[4,4] := Shape22 ;
    Bort[5,4] := Shape23 ;
    Bort[6,4] := Shape24 ;
    Bort[1,5] := Shape25 ;
    Bort[2,5] := Shape26 ;
    Bort[3,5] := Shape27 ;
    Bort[4,5] := Shape28 ;
    Bort[5,5] := Shape29 ;
    Bort[6,5] := Shape30 ;
    Bort[1,6] := Shape31 ;
    Bort[2,6] := Shape32 ;
    Bort[3,6] := Shape33 ;
    Bort[4,6] := Shape34 ;
    Bort[5,6] := Shape35 ;
    Bort[6,6] := Shape36 ;
    Bort[1,7] := Shape37 ;
    Bort[2,7] := Shape38 ;
    Bort[3,7] := Shape39 ;
    Bort[4,7] := Shape40 ;
    Bort[5,7] := Shape41 ;
    Bort[6,7] := Shape42 ;
    button2.Enabled := false;
    edit2.enabled := false;
end;

procedure showWinner(beurt : integer);
var
  kolom:      integer;
  rij:        integer;
  winteller:  integer;
  kolom1:     integer;
  rij1:       integer;
  kolom2:     integer;
  winteller2: integer;
  kolom3:     integer;

begin //verticaal
  for kolom := 1 to 7 do
  begin
    winteller:= 0;
    for rij:= 1 to 6 do
    begin
      if matrix[rij,kolom]= beurt then
      begin
        inc(winteller);
      end
      else
      begin
        winteller := 0;
      end;
      if winteller = 4 then
      begin
        if beurt = 1 then
        begin
          Form1.Mediaplayer2.FileName := 'champion.mid';
          Form1.Mediaplayer2.open;
          Form1.Mediaplayer2.Play;
          ShowMessage('Speler rood heeft gewonnen!!');
        end
        else
        begin
          Form1.Mediaplayer2.FileName := 'champion.mid';
          Form1.Mediaplayer2.open;
          Form1.Mediaplayer2.Play;
          showmessage('speler geel heeft gewonnen!');
        end;
        exit;
      end;
    end;
  end;
  for kolom1 := 1 to 7 do // wagerecht
  begin
    winteller := 0;
    for rij1 := 1 to 6 do
    begin
      if matrix[kolom1,rij1] = beurt then
      begin
        inc(winteller);
      end
      else
      begin
        winteller := 0;
      end;
      if winteller = 4 then
      begin
        if beurt = 1 then
        begin
          Form1.Mediaplayer2.FileName := 'champion.mid';
          Form1.Mediaplayer2.open;
          Form1.Mediaplayer2.Play;
          ShowMessage('Speler rood heeft gewonnen!!');
        end
        else
        begin
          Form1.Mediaplayer2.FileName := 'champion.mid';
          Form1.Mediaplayer2.open;
          Form1.Mediaplayer2.Play;
          showmessage('speler geel heeft gewonnen!');
        end;
        exit;
      end;
    end;
  end;
  winteller2 := 0; //diagonal von linksunten nach rechts
  for kolom := 5 to 7 do
  begin
    for rij := 0 to 3 do
    begin
      for kolom2 := 1 to 6 do
      begin
        if matrix[kolom-kolom2,kolom2+rij]= beurt then
        begin
          inc(winteller2);
        end
        else
        begin
          winteller2 :=0;
        end;
        if winteller2 = 4 then
        begin
          if beurt = 1 then
          begin
            Form1.Mediaplayer2.FileName := 'champion.mid';
            Form1.Mediaplayer2.open;
            Form1.Mediaplayer2.Play;
            ShowMessage('Speler rood heeft gewonnen!!');
          end
          else
          begin
            Form1.Mediaplayer2.FileName := 'champion.mid';
            Form1.Mediaplayer2.open;
            Form1.Mediaplayer2.Play;
            showmessage('speler geel heeft gewonnen!');
          end;
          exit;
        end;
      end;
    end;
  end;
  winteller := 0;  // diagonal von linksoben nach rechts
  for kolom1 := 0 to 2 do
  begin
    for kolom2 := 0 to 3 do
    begin
      for kolom3 := 1 to 7 do
      begin
        if matrix[kolom3+kolom1,kolom3+kolom2]= beurt then
        begin
          inc(winteller);
        end
        else
        begin
          winteller := 0;
        end;
        if winteller = 4 then
        begin
          if beurt = 1 then
          begin
            Form1.Mediaplayer2.FileName := 'champion.mid';
            Form1.Mediaplayer2.open;
            Form1.Mediaplayer2.Play;
            ShowMessage('Speler rood heeft gewonnen!!');
          end
          else
          begin
            Form1.Mediaplayer2.FileName := 'champion.mid';
            Form1.Mediaplayer2.open;
            Form1.Mediaplayer2.Play;
            showmessage('speler geel heeft gewonnen!');
          end;
          exit;
        end;
      end;
    end;
  end;
  if Voll(matrix)= true then
  begin
    Beep;
    ShowMessage('Niemand heeft gewonnen!!'+ #13+
                'Het spel begint op nieuw!!');
    Form1.Button3.Click;
  end;
end;



procedure TForm1.Button1Click(Sender: TObject);
var
  keuze1:     integer;
  teller1:    integer;
  text1:      string;

begin
  text1 := Edit1.Text;
  if IsGetal(text1) = false then
  begin
    Beep;
    Showmessage('Geef een getal in!');
  end
  else
  begin
    keuze1 := StrToInt(Edit1.Text);
    if IsKl4(keuze1) = false then
    begin
      Beep;
      ShowMessage('Geef een getal in die kleiner is dan 7');
    end
    else
    begin
      teller1 := 0;
      while matrix[teller1 +1][keuze1] = 0 do
      begin
        teller1 := teller1 +1;
      end;
      if teller1 < 1 then
      begin
        ShowMessage('Rij vol');
        exit;
      end;
      Matrix[teller1 ][keuze1]:= 1;
      Bort[teller1,keuze1].Brush.Color := clred;
      showwinner(1);
    end;
  end;
  if (IsKl4(keuze1) = true) and (IsGetal(text1) = true) then
  begin
  button1.Enabled := false;
  edit1.Enabled := false;
  button2.Enabled := true;
  edit2.enabled:= true;
  end;
  edit1.clear;
end;




procedure TForm1.Button2Click(Sender: TObject);
var
  keuze2:     integer;
  teller2:    integer;
  text2:      string;

begin
  text2 := Edit2.Text;
  if IsGetal(text2) = false then
  begin
    Beep;
    Showmessage('geef een getal in!')
  end
  else
  begin
    keuze2 := StrToInt(Edit2.Text);
    if IsKl4(keuze2) = false then
    begin
      Beep;
      ShowMessage('Geef een getal in die kleiner is dan 7');
    end
    else
    begin
      teller2 := 0;
      while matrix[teller2 + 1][keuze2] = 0 do
      begin
        teller2 := teller2 +1;
      end;
      if teller2 < 1 then
      begin
        ShowMessage('Rij voll');
        exit;
      end
      else
      begin
        Matrix[teller2 ][keuze2]:= 2;
        Bort[teller2,keuze2].Brush.Color := clyellow;
        showwinner(2);
      end;
    end;
  end;
  if (IsKl4(keuze2) = true) and (IsGetal(text2) = true) then
  begin
  button2.Enabled := false;
  edit2.Enabled := false;
  button1.Enabled := true;
  edit1.enabled:= true;
  end;
  edit2.clear;
end;



procedure TForm1.Button3Click(Sender: TObject);
var
  x: integer;
  y: integer;
begin
  for x:= 1 to 6 do
  begin
    for y:= 1 to 7 do
    begin
      matrix[x,y] := 0;
      Bort[x,y].Brush.Color := clwhite;
      Mediaplayer2.Close;
    end;
  end;
end;

procedure TForm1.Button4Click(Sender: TObject);
begin
  Mediaplayer1.FileName := 'vrolijk.mp3';
  Mediaplayer1.open;
  Mediaplayer1.Play;
end;

procedure TForm1.Button5Click(Sender: TObject);
begin
  Mediaplayer1.Close;
end;

end.
