AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Thema durchsuchen
Ansicht
Themen-Optionen

Sudoku Generator (Anfänge)

Ein Thema von xyss · begonnen am 23. Jan 2013 · letzter Beitrag vom 28. Jan 2013
 
xyss

Registriert seit: 9. Nov 2012
7 Beiträge
 
#1

Sudoku Generator (Anfänge)

  Alt 23. Jan 2013, 19:36
Delphi-Version: 6
Guten Abend

Auch wenn ich noch relativ neu bin, hab ich mir gedacht, ich Versuch mich mal an einem Sudoku Generator. (vor allem, da ich mich selbst sehr viel mit Sudokus beschäftige) Tragischerweise scheiter ich schon in der ersten Etappe des Programms, nämlich dem kompletten Befüllen eines Sudokufeldes.
Es erscheint jedes Mal ein StackOverflow.
Ob mein Programm jetzt funktionieren würde oder nicht, sei mal dahingestellt, aber was mich stört ist, dass ich nicht draufkomm, woher dieser Stack Overflow herkommt. Wäre also super, wenn mir jemand das erklären könnte

Vielen Dank im Vorraus

Die StringGridDrawCell und ButtonClick Prozeduren hab ich jetzt weggelassen, da diese eigendlich funktionieren Der Buttonclick aktiviert die Prozedur mit (StringGrid1,0,0).

Code:
unit Unit1;

interface

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

type
  TfrmMain = class(TForm)
    btnExe: TButton;
    btnExit: TButton;
    StringGrid1: TStringGrid;
    procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    procedure btnExitClick(Sender: TObject);
    function TryNumber(SG:TStringGrid; n,ColPos,RowPos:integer):boolean;
    procedure FillUp(SG:TStringGrid;Col,Row:integer);
    procedure btnExeClick(Sender: TObject);
    procedure TestValues(SG:TStringGrid;Col,Row:integer);
    function IsFilled(SG:TStringGrid):boolean;
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmMain: TfrmMain;
  Test:boolean;

implementation

{$R *.dfm}

{**************************************************************}

function TfrmMain.TryNumber(SG:TStringGrid; n,ColPos,RowPos:integer):boolean;
var
  i,j,ColPosCon,RowPosCon:integer;
  verif:boolean;
begin
  verif:=true;

  for i:=0 to SG.ColCount-1 do
    if SG.Cells[i,RowPos]=InttoStr(n) then
      verif:=false;

  for i:=0 to SG.RowCount-1 do
    if SG.Cells[ColPos,i]=InttoStr(n) then
      verif:=false;

  case ColPos of
    0: ColPosCon:=0;
    1: ColPosCon:=0;
    2: ColPosCon:=0;
    3: ColPosCon:=3;
    4: ColPosCon:=3;
    5: ColPosCon:=3;
    6: ColPosCon:=6;
    7: ColPosCon:=6;
    8: ColPosCon:=6;
  end;

  case RowPos of
    0: RowPosCon:=0;
    1: RowPosCon:=0;
    2: RowPosCon:=0;
    3: RowPosCon:=3;
    4: RowPosCon:=3;
    5: RowPosCon:=3;
    6: RowPosCon:=6;
    7: RowPosCon:=6;
    8: RowPosCon:=6;
  end;

  for i:=ColPosCon to ColPosCon+2 do
  begin
    for j:=RowPosCon to RowPosCon+2 do
      if SG.Cells[i,j]=InttoStr(n) then
        verif:=false;
  end;
  result:=verif;
end;

procedure TfrmMain.TestValues(SG:TStringGrid;Col,Row:integer);
var
  n:integer;
begin
  Test:=false;
  for n:=1 to 9 do
  begin
    if (TryNumber(SG,n,Col,Row)=true) and (SG.Cells[Col,Row]='') then
    begin
      Test:=true;
      SG.Cells[Col,Row]:=InttoStr(n);
    end;
  end;
end;


function TfrmMain.IsFilled(SG:TStringGrid):boolean;
var
  Row,Col:integer;
begin
  result:=true;
  for Row:=0 to SG.RowCount-1 do
  begin
    for Col:=0 to SG.ColCount-1 do
    begin
      if SG.Cells[Col,Row]='' then
        result:=false;
    end;
  end;
end;

procedure TfrmMain.FillUp(SG:TStringGrid;Col,Row:integer);
var
  n:integer;
begin
  n:=Random(9)+1;

  if (SG.Cells[Col,Row]<>'') then
  begin
    Col:=Col+1;
  end

  else if (TryNumber(SG,n,Col,Row)=true) and (SG.Cells[Col,Row]='') then
  begin
    SG.Cells[Col,Row]:=InttoStr(n);
    Col:=Col+1;
  end

  else if (TryNumber(SG,n,Col,Row)=false) and (SG.Cells[Col,Row]='') then
  begin
    TestValues(SG,Col,Row);
    if not Test then
      Col:=Col-1
    else if Test then
      Col:=Col+1;
  end;


  if Col<0 then
  begin
    Row:=Row-1;
    Col:=8;
    SG.Cells[Col,Row]:='';
  end;

  if Row<0 then
    Row:=0;

  if Col>8 then
  begin
    Row:=Row+1;
    Col:=0;
  end;


  if IsFilled(SG)=false then FillUp(SG,Col,Row);

end;
  Mit Zitat antworten Zitat
 


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 18:24 Uhr.
Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz