Kółko i krzyżyk problem z ruchami komputera

0

Witam,
Napisałem w Delphi grę kółko i krzyżyk z procedurą, która ma za zadanie wykonać ruch komputera.
Wszystko było ładnie, pięknie dopóki podczas testowania komputer dostawił sobie dwa krzyżyki a ja za cholerę nie mogę znaleźć błędu w kodzie. Szukam od trzech dni i nic, wszystko wydaje mi się ok, a komputer dalej stawia sobie dwa znaki (oczywiście nie zawsze). Będę baaardzo wdzięczny gdy ktoś poświęci trochę czasu na przejrzenie tego kodu i wytknie mi błąd. Komputer zaprogramowałem w ten sposób, aby możliwa była wygrana.
Dodam, że jako pierwszy ruch wykonuje gracz, a potem komputer.

Zmienne:

  • t[x] - zmienna t to tablica typu integer, która zapisuje co znajduje się na danym polu (nic=0, kółko(gracz)=1, krzyżyk(komputer)=10)
    x to numer pola:

t[1] t[2] t[3]
t[4] t[5] t[6]
t[7] t[8] t[9]

  • zn - zmienna typu boolean. nazwa od słowa "znalazł" czyli przyjmuje wartość true gdy komputer znalazł odpowiedni ruch. Jest resetowana przy każdorazowym wywołaniu procedury ruchu komputera.

  • gr - zmienna typu integer. nazwa od słowa "gracz". Przyjmuje wartość 1 gdy ruch wykonuje gracz, a 2 gdy ruch wykonuje komputer.

  • r - zmienna typu integer. nazwa od słowa "random". Gdy na planszy nie ma żadnego krzyżyka, komputer losowo go dostawia, na podstawie tej właśnie zmiennej.

Plansza to 9 pól tekstowych typu Edit.

Edit1 Edit2 Edit3
Edit4 Edit5 Edit6
Edit7 Edit8 Edit9

procedure RuchKomputera;
begin
 zn:=false;
 {czesc sprawdzajaca czy komputer moze wygrac}
 if (t[1]+t[2]+t[3]=20)AND(zn=false) then
 begin
  if t[1]=0 then begin Edit1.Text:='X'; gr:=1; t[1]:=10; zn:=true; end;
  if t[2]=0 then begin Edit2.Text:='X'; gr:=1; t[2]:=10; zn:=true; end;
  if t[3]=0 then begin Edit3.Text:='X'; gr:=1; t[3]:=10; zn:=true; end;
 end;
 if (t[4]+t[5]+t[6]=20)AND(zn=false) then
 begin
  if t[4]=0 then begin Edit4.Text:='X'; gr:=1; t[4]:=10; zn:=true; end;
  if t[5]=0 then begin Edit5.Text:='X'; gr:=1; t[5]:=10; zn:=true; end;
  if t[6]=0 then begin Edit6.Text:='X'; gr:=1; t[6]:=10; zn:=true; end;
 end;
 if (t[7]+t[8]+t[9]=20)AND(zn=false) then
 begin
  if t[7]=0 then begin Edit7.Text:='X'; gr:=1; t[7]:=10; zn:=true; end;
  if t[8]=0 then begin Edit8.Text:='X'; gr:=1; t[8]:=10; zn:=true; end;
  if t[9]=0 then begin Edit9.Text:='X'; gr:=1; t[9]:=10; zn:=true; end;
 end;
 if (t[1]+t[4]+t[7]=20)AND(zn=false) then
 begin
  if t[1]=0 then begin Edit1.Text:='X'; gr:=1; t[1]:=10; zn:=true; end;
  if t[4]=0 then begin Edit4.Text:='X'; gr:=1; t[4]:=10; zn:=true; end;
  if t[7]=0 then begin Edit7.Text:='X'; gr:=1; t[7]:=10; zn:=true; end;
 end;
 if (t[2]+t[5]+t[8]=20)AND(zn=false) then
 begin
  if t[2]=0 then begin Edit2.Text:='X'; gr:=1; t[2]:=10; zn:=true; end;
  if t[5]=0 then begin Edit5.Text:='X'; gr:=1; t[5]:=10; zn:=true; end;
  if t[8]=0 then begin Edit8.Text:='X'; gr:=1; t[8]:=10; zn:=true; end;
 end;
 if (t[3]+t[6]+t[9]=20)AND(zn=false) then
 begin
  if t[3]=0 then begin Edit3.Text:='X'; gr:=1; t[3]:=10; zn:=true; end;
  if t[6]=0 then begin Edit6.Text:='X'; gr:=1; t[6]:=10; zn:=true; end;
  if t[9]=0 then begin Edit9.Text:='X'; gr:=1; t[9]:=10; zn:=true; end;
 end;
 if (t[1]+t[5]+t[9]=20)AND(zn=false) then
 begin
  if t[1]=0 then begin Edit1.Text:='X'; gr:=1; t[1]:=10; zn:=true; end;
  if t[5]=0 then begin Edit5.Text:='X'; gr:=1; t[5]:=10; zn:=true; end;
  if t[9]=0 then begin Edit9.Text:='X'; gr:=1; t[9]:=10; zn:=true; end;
 end;
 if (t[3]+t[5]+t[7]=20)AND(zn=false) then
 begin
  if t[3]=0 then begin Edit3.Text:='X'; gr:=1; t[3]:=10; zn:=true; end;
  if t[5]=0 then begin Edit5.Text:='X'; gr:=1; t[5]:=10; zn:=true; end;
  if t[7]=0 then begin Edit7.Text:='X'; gr:=1; t[7]:=10; zn:=true; end;
 end;
 {koniec czesci sprawdzajacej czy komputer moze wygrac}
 {sprawdzanie czy gracz moze wygrac}
 if (t[1]+t[2]+t[3]=2)AND(zn=false) then
 begin
  if t[1]=0 then begin Edit1.Text:='X'; gr:=1; t[1]:=10; zn:=true; end;
  if t[2]=0 then begin Edit2.Text:='X'; gr:=1; t[2]:=10; zn:=true; end;
  if t[3]=0 then begin Edit3.Text:='X'; gr:=1; t[3]:=10; zn:=true; end;
 end;
 if (t[4]+t[5]+t[6]=2)AND(zn=false) then
 begin
  if t[4]=0 then begin Edit4.Text:='X'; gr:=1; t[4]:=10; zn:=true; end;
  if t[5]=0 then begin Edit5.Text:='X'; gr:=1; t[5]:=10; zn:=true; end;
  if t[6]=0 then begin Edit6.Text:='X'; gr:=1; t[6]:=10; zn:=true; end;
 end;
 if (t[7]+t[8]+t[9]=2)AND(zn=false) then
 begin
  if t[7]=0 then begin Edit7.Text:='X'; gr:=1; t[7]:=10; zn:=true; end;
  if t[8]=0 then begin Edit8.Text:='X'; gr:=1; t[8]:=10; zn:=true; end;
  if t[9]=0 then begin Edit9.Text:='X'; gr:=1; t[9]:=10; zn:=true; end;
 end;
 if (t[1]+t[4]+t[7]=2)AND(zn=false) then
 begin
  if t[1]=0 then begin Edit1.Text:='X'; gr:=1; t[1]:=10; zn:=true; end;
  if t[4]=0 then begin Edit4.Text:='X'; gr:=1; t[4]:=10; zn:=true; end;
  if t[7]=0 then begin Edit7.Text:='X'; gr:=1; t[7]:=10; zn:=true; end;
 end;
 if (t[2]+t[5]+t[8]=2)AND(zn=false) then
 begin
  if t[2]=0 then begin Edit2.Text:='X'; gr:=1; t[2]:=10; zn:=true; end;
  if t[5]=0 then begin Edit5.Text:='X'; gr:=1; t[5]:=10; zn:=true; end;
  if t[8]=0 then begin Edit8.Text:='X'; gr:=1; t[8]:=10; zn:=true; end;
 end;
 if (t[3]+t[6]+t[9]=2)AND(zn=false) then
 begin
  if t[3]=0 then begin Edit3.Text:='X'; gr:=1; t[3]:=10; zn:=true; end;
  if t[6]=0 then begin Edit6.Text:='X'; gr:=1; t[6]:=10; zn:=true; end;
  if t[9]=0 then begin Edit9.Text:='X'; gr:=1; t[9]:=10; zn:=true; end;
 end;
 if (t[1]+t[5]+t[9]=2)AND(zn=false) then
 begin
  if t[1]=0 then begin Edit1.Text:='X'; gr:=1; t[1]:=10; zn:=true; end;
  if t[5]=0 then begin Edit5.Text:='X'; gr:=1; t[5]:=10; zn:=true; end;
  if t[9]=0 then begin Edit9.Text:='X'; gr:=1; t[9]:=10; zn:=true; end;
 end;
 if (t[3]+t[5]+t[7]=2)AND(zn=false) then
 begin
  if t[3]=0 then begin Edit3.Text:='X'; gr:=1; t[3]:=10; zn:=true; end;
  if t[5]=0 then begin Edit5.Text:='X'; gr:=1; t[5]:=10; zn:=true; end;
  if t[7]=0 then begin Edit7.Text:='X'; gr:=1; t[7]:=10; zn:=true; end;
 end;
 {koniec czesci sprawdzajacaej czy gracz moze wygrac}
 {dostawianie znaku do juz istniejacego}
 if (t[1]+t[2]+t[3]=10)AND(zn=false) then
 begin
  if t[1]=10 then begin randomize; r:=random(2); case r of
                                                 0: begin Edit2.Text:='X'; gr:=1; t[2]:=10; zn:=true; end;
                                                 1: begin Edit3.Text:='X'; gr:=1; t[3]:=10; zn:=true; end;
                                                 end; end;
  if t[2]=10 then begin randomize; r:=random(2); case r of
                                                 0: begin Edit1.Text:='X'; gr:=1; t[1]:=10; zn:=true; end;
                                                 1: begin Edit3.Text:='X'; gr:=1; t[3]:=10; zn:=true; end;
                                                 end; end;
  if t[3]=10 then begin randomize; r:=random(2); case r of
                                                 0: begin Edit1.Text:='X'; gr:=1; t[1]:=10; zn:=true; end;
                                                 1: begin Edit2.Text:='X'; gr:=1; t[2]:=10; zn:=true; end;
                                                 end; end; end;
 if (t[4]+t[5]+t[6]=10)AND(zn=false) then
 begin
  if t[4]=10 then begin randomize; r:=random(2); case r of
                                                 0: begin Edit5.Text:='X'; gr:=1; t[5]:=10; zn:=true; end;
                                                 1: begin Edit6.Text:='X'; gr:=1; t[6]:=10; zn:=true; end;
                                                 end; end;
  if t[5]=10 then begin randomize; r:=random(2); case r of
                                                 0: begin Edit4.Text:='X'; gr:=1; t[4]:=10; zn:=true; end;
                                                 1: begin Edit6.Text:='X'; gr:=1; t[6]:=10; zn:=true; end;
                                                 end; end;
  if t[6]=10 then begin randomize; r:=random(2); case r of
                                                 0: begin Edit4.Text:='X'; gr:=1; t[4]:=10; zn:=true; end;
                                                 1: begin Edit5.Text:='X'; gr:=1; t[5]:=10; zn:=true; end;
                                                 end; end; end;
 if (t[7]+t[8]+t[9]=10)AND(zn=false) then
 begin
  if t[7]=10 then begin randomize; r:=random(2); case r of
                                                 0: begin Edit8.Text:='X'; gr:=1; t[8]:=10; zn:=true; end;
                                                 1: begin Edit9.Text:='X'; gr:=1; t[9]:=10; zn:=true; end;
                                                 end; end;
  if t[8]=10 then begin randomize; r:=random(2); case r of
                                                 0: begin Edit7.Text:='X'; gr:=1; t[7]:=10; zn:=true; end;
                                                 1: begin Edit9.Text:='X'; gr:=1; t[9]:=10; zn:=true; end;
                                                 end; end;
  if t[9]=10 then begin randomize; r:=random(2); case r of
                                                 0: begin Edit7.Text:='X'; gr:=1; t[7]:=10; zn:=true; end;
                                                 1: begin Edit8.Text:='X'; gr:=1; t[8]:=10; zn:=true; end;
                                                 end; end; end;
 if (t[1]+t[4]+t[7]=10)AND(zn=false) then
 begin
  if t[1]=10 then begin randomize; r:=random(2); case r of
                                                 0: begin Edit4.Text:='X'; gr:=1; t[4]:=10; zn:=true; end;
                                                 1: begin Edit7.Text:='X'; gr:=1; t[7]:=10; zn:=true; end;
                                                 end; end;
  if t[4]=10 then begin randomize; r:=random(2); case r of
                                                 0: begin Edit1.Text:='X'; gr:=1; t[1]:=10; zn:=true; end;
                                                 1: begin Edit7.Text:='X'; gr:=1; t[7]:=10; zn:=true; end;
                                                 end; end;
  if t[7]=10 then begin randomize; r:=random(2); case r of
                                                 0: begin Edit1.Text:='X'; gr:=1; t[1]:=10; zn:=true; end;
                                                 1: begin Edit4.Text:='X'; gr:=1; t[4]:=10; zn:=true; end;
                                                 end; end; end;
 if (t[2]+t[5]+t[8]=10)AND(zn=false) then
 begin
  if t[2]=10 then begin randomize; r:=random(2); case r of
                                                 0: begin Edit5.Text:='X'; gr:=1; t[5]:=10; zn:=true; end;
                                                 1: begin Edit8.Text:='X'; gr:=1; t[8]:=10; zn:=true; end;
                                                 end; end;
  if t[5]=10 then begin randomize; r:=random(2); case r of
                                                 0: begin Edit2.Text:='X'; gr:=1; t[2]:=10; zn:=true; end;
                                                 1: begin Edit8.Text:='X'; gr:=1; t[8]:=10; zn:=true; end;
                                                 end; end;
  if t[8]=10 then begin randomize; r:=random(2); case r of
                                                 0: begin Edit2.Text:='X'; gr:=1; t[2]:=10; zn:=true; end;
                                                 1: begin Edit5.Text:='X'; gr:=1; t[5]:=10; zn:=true; end;
                                                 end; end; end;
 if (t[3]+t[6]+t[9]=10)AND(zn=false) then
 begin
  if t[3]=10 then begin randomize; r:=random(2); case r of
                                                 0: begin Edit6.Text:='X'; gr:=1; t[3]:=10; zn:=true; end;
                                                 1: begin Edit9.Text:='X'; gr:=1; t[6]:=10; zn:=true; end;
                                                 end; end;
  if t[6]=10 then begin randomize; r:=random(2); case r of
                                                 0: begin Edit3.Text:='X'; gr:=1; t[3]:=10; zn:=true; end;
                                                 1: begin Edit9.Text:='X'; gr:=1; t[9]:=10; zn:=true; end;
                                                 end; end;
  if t[9]=10 then begin randomize; r:=random(2); case r of
                                                 0: begin Edit3.Text:='X'; gr:=1; t[3]:=10; zn:=true; end;
                                                 1: begin Edit6.Text:='X'; gr:=1; t[6]:=10; zn:=true; end;
                                                 end; end;  end;
 if (t[1]+t[5]+t[9]=10)AND(zn=false) then
 begin
  if t[1]=10 then begin randomize; r:=random(2); case r of
                                                 0: begin Edit5.Text:='X'; gr:=1; t[5]:=10; zn:=true; end;
                                                 1: begin Edit9.Text:='X'; gr:=1; t[9]:=10; zn:=true; end;
                                                 end; end;
  if t[5]=10 then begin randomize; r:=random(2); case r of
                                                 0: begin Edit1.Text:='X'; gr:=1; t[1]:=10; zn:=true; end;
                                                 1: begin Edit9.Text:='X'; gr:=1; t[9]:=10; zn:=true; end;
                                                 end; end;
  if t[9]=10 then begin randomize; r:=random(2); case r of
                                                 0: begin Edit1.Text:='X'; gr:=1; t[1]:=10; zn:=true; end;
                                                 1: begin Edit5.Text:='X'; gr:=1; t[5]:=10; zn:=true; end;
                                                 end; end;  end;
 if (t[3]+t[5]+t[7]=10)AND(zn=false) then
 begin
  if t[3]=10 then begin randomize; r:=random(2); case r of
                                                 0: begin Edit5.Text:='X'; gr:=1; t[5]:=10; zn:=true; end;
                                                 1: begin Edit7.Text:='X'; gr:=1; t[7]:=10; zn:=true; end;
                                                 end; end;
  if t[5]=10 then begin randomize; r:=random(2); case r of
                                                 0: begin Edit3.Text:='X'; gr:=1; t[3]:=10; zn:=true; end;
                                                 1: begin Edit7.Text:='X'; gr:=1; t[7]:=10; zn:=true; end;
                                                 end; end;
  if t[7]=10 then begin randomize; r:=random(2); case r of
                                                 0: begin Edit3.Text:='X'; gr:=1; t[3]:=10; zn:=true; end;
                                                 1: begin Edit5.Text:='X'; gr:=1; t[5]:=10; zn:=true; end;
                                                 end; end; end;

 {koniec czesci dostawiajacej znak do juz istniejacego}
 {czesc losujaca znak jezeli nie ma zadnego innego}

 if zn=false then
 begin
  repeat
   r:=0;
   randomize;
   r:=random(9);
   Inc(r);
  until t[r]=0;
  case r of
   1: begin t[1]:=10; Edit1.Text:='X'; gr:=1; zn:=true; end;
   2: begin t[2]:=10; Edit2.Text:='X'; gr:=1; zn:=true; end;
   3: begin t[3]:=10; Edit3.Text:='X'; gr:=1; zn:=true; end;
   4: begin t[4]:=10; Edit4.Text:='X'; gr:=1; zn:=true; end;
   5: begin t[5]:=10; Edit5.Text:='X'; gr:=1; zn:=true; end;
   6: begin t[6]:=10; Edit6.Text:='X'; gr:=1; zn:=true; end;
   7: begin t[7]:=10; Edit7.Text:='X'; gr:=1; zn:=true; end;
   8: begin t[8]:=10; Edit8.Text:='X'; gr:=1; zn:=true; end;
   9: begin t[9]:=10; Edit9.Text:='X'; gr:=1; zn:=true; end;
  end;
 end;
end;
0

Strasznie dużo tego kodu. Nie dałoby się użyć znacznika kolorowania składni?

if cos then
begin
   cosik
end;

Do tematu. Przeglądałem kod i nic nie wyłapałem. Rzuca się w oczy fakt, że nie używasz else if. Podejrzewam, że gdzieś spełniają Ci się dwa warunki if w jednym przejściu i dlatego. Pododawaj tam gdzie ify się wykluczają. W tej chwili nawet jak pierwszy się spełni, to wszystkie inne są sprawdzane, co nie ma sensu chociażby ze względu na zmienną zn.
PS. Wersję 2.0 zrób na drzewie decyzyjnym ;].

0

Ten kod:

case r of
   1: begin t[1]:=10; Edit1.Text:='X'; gr:=1; zn:=true; end;
   2: begin t[2]:=10; Edit2.Text:='X'; gr:=1; zn:=true; end;
   3: begin t[3]:=10; Edit3.Text:='X'; gr:=1; zn:=true; end;
   4: begin t[4]:=10; Edit4.Text:='X'; gr:=1; zn:=true; end;
   5: begin t[5]:=10; Edit5.Text:='X'; gr:=1; zn:=true; end;
   6: begin t[6]:=10; Edit6.Text:='X'; gr:=1; zn:=true; end;
   7: begin t[7]:=10; Edit7.Text:='X'; gr:=1; zn:=true; end;
   8: begin t[8]:=10; Edit8.Text:='X'; gr:=1; zn:=true; end;
   9: begin t[9]:=10; Edit9.Text:='X'; gr:=1; zn:=true; end;
  end;

można skrócić do takiej postaci:

t[r] := 10;
gr := 1;
zn := True;
TEdit(FindComponent('Edit' + IntToStr(r))).Text := 'X';

Strasznie długi i nieczytelny ten kod... Musisz popracować nad formatowaniem kodu oraz optymalizacją...

@ Jak dobrze pomyślisz, możesz napisać kilka funkcji, które znacznie skrócą kod; Pomyśl nad tym;
@ Wszędzie tam, gdzie masz instrukcje warunkowe if i robi się drzewko - zastąp je case .. of - będzie większa czytelność;

@ Zobacz na ten fragment:

procedure RuchKomputera;
begin
  zn:=false;

  {czesc sprawdzajaca czy komputer moze wygrac}
  if (t[1]+t[2]+t[3]=20)AND(zn=false) then
    begin
      if t[1]=0 then begin Edit1.Text:='X'; gr:=1; t[1]:=10; zn:=true; end;
      if t[2]=0 then begin Edit2.Text:='X'; gr:=1; t[2]:=10; zn:=true; end;
      if t[3]=0 then begin Edit3.Text:='X'; gr:=1; t[3]:=10; zn:=true; end;
    end;

  if (t[4]+t[5]+t[6]=20)AND(zn=false) then
    begin
      if t[4]=0 then begin Edit4.Text:='X'; gr:=1; t[4]:=10; zn:=true; end;
      if t[5]=0 then begin Edit5.Text:='X'; gr:=1; t[5]:=10; zn:=true; end;
      if t[6]=0 then begin Edit6.Text:='X'; gr:=1; t[6]:=10; zn:=true; end;
    end;

  {...}

Jak spełniony zostanie jakiś warunek, zmienna zn zostaje ustawiona na True, dzięki czemu następne warunki nigdy nie zostaną spełnione; Równie dobrze można by skorzystać z Exit, żeby program nie sprawdzał następnych warunków;

Można by każdy z tych warunków oprzeć na jednej funkcji, która jako argument będzie pobierać indeksy trzech pól w macierzy i wartość sumy, dla jakiej spełniony ma być warunek; Później odpowiednio utworzyć szereg warunków opartych na tej funkcji; Skrócisz znacznie kod, a działanie będzie takie same;

@ Te dwie instrukcje:

r:=random(9);
Inc(r);

możesz zamienić na jedną:

r := Random(9) + 1;

Poza tym, jeśli stosujesz losowanie liczby przez funkcję Random(), to nie uruchamiaj generatora Randomize() za każdym razem, tylko raz przed warunkami, które będą z niego korzystać;

0
  repeat
   r:=0;
   randomize;
   r:=random(9);
   Inc(r);
  until t[r]=0;

Tu też masz źle, bo uruchomienie generatora losowania za każdym razem wywołujesz w pętli. Wyrzuć randomize; przed pętlę. Tak samo wyżej, za każdym razem wywołujesz randomize; potem random(2) - to też popraw.

0

yyy... co to jest drzewo decyzyjne?
Sorry za n00bizm ale jestem w szóstej klasie i pisze program na (chyba) powiatowy konkurs no i nie moge im wyslac programu z takim bledem...

Aha dzieki za rade z procedurą Exit. Może to rozwiąże mój problem. Tego że gdzieś spełniają się dwa warunki już się domyśliłem ale nie mam bladozielonego pojęcia gdzie.

0

Zobacz tu: Drzewo decyzyjne

RadekPL napisał(a)

Aha dzieki za rade z procedurą Exit. Może to rozwiąże mój problem.

Twój problem rozwiąże Debuger, naucz się tylko nim posługiwać;

Poza tym można ten kod znacznie skrócić, dzięki zastosowaniu odpowiednio przygotowanej(ych) funkcji; Masz przecież macierz, czyli stałe indeksy, czyli można napisać jedną funkcję, która będzie przyjmować w argumentach sprawdzane indeksy, po czym je odpowiednio sprawdzać i edytować konkretne zmienne i właściwości komponentów; Pomyśl nad tym co się powtarza, a jak znajdziesz (co gołym okiem widać) to spróbuj wymyślić prostsze i krótsze rozwiązanie; Poczytaj o DRY i KISS a może coś wymyślisz;

@ Popracuj nad formatowaniem kodu, bo warunki prezentują się tragicznie...

0

Ja to bym zrobił tak, że gdy masz tam ten pierdyliard warunków, to nie przypisywałbym od razu X do pola, tylko przypisał jakiejś zmiennej numer tego pola, i dopiero na końcu procedury zmieniłbym wartość pola o nr zmiennej.

0
RadekPL napisał(a)

yyy... co to jest drzewo decyzyjne?
Sorry za n00bizm ale jestem w szóstej klasie i pisze program na (chyba) powiatowy konkurs no i nie moge im wyslac programu z takim bledem...

Drzewo decyzyjne wykorzystywane jest w algorytmie z wartościowaniem poszczególnych ścieżek w drzewie, dzięki czemu komputer może zawsze wybierać najlepszą z dostępnych opcji. Po prostu sprawdza wszystkie kombinacje. Jest to zagadnienie z teorii grafów, a więc etap studiów. Dlatego może to zostawmy ;)

0

Skoro autor nie miał styczności z drzewem decyzyjnym (fakt, to podstawówka), zrób sobie statyczną macierz przechowującą trójki indeksów pól, które chcesz sprawdzać:

const
  TCombinationsArr: array [0 .. 7, 0 .. 2] of 1 .. 9 = ((1, 2, 3),   {1   2   3}
                                                        (4, 5, 6),   {         }
                                                        (7, 8, 9),   {4   5   6}
                                                        (1, 4, 7),   {         }
                                                        (2, 5, 8),   {7   8   9}
                                                        (3, 6, 9),
                                                        (1, 5, 9),
                                                        (3, 5, 7));

Teraz napisz sobie funkcję, która będzie pobierać jako argument trójkę indeksów z ww macierzy i sumę, dla jakiej ma być spełniony warunek; Jak napiszesz taką to pozostaje tylko w pętli od 0 .. 7 (bo tak jest indeksowana macierz kombinacji) wywołać ją z argumentem równym trójce indeksów o numerze takim, jaki jest licznik pętli (czyli od 0 do 7); Dzięki temu kod będzie krótszy i łatwiejszy do zrozumienia;

Jeśli w nowej funkcji także wykorzystasz macierze, będziesz mógł oprzeć wszystko na pętlach co jeszcze bardziej skróci kod; Wszystko zależy od tego jak sobie to zaplanujesz;

@ Zamieszczam z załączniku kod z pierwszego postu, ale sformatowany, w którym coś widać...

0

Ok dzięki za radę z procedurą Exit (jeszcze raz). Działa jak należy. Postaram się skrócić ten kod jak mi polecił kolega wyżej.

1 użytkowników online, w tym zalogowanych: 0, gości: 1