Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Sonstige Fragen zu Delphi (https://www.delphipraxis.net/19-sonstige-fragen-zu-delphi/)
-   -   Delphi Unentschieden ! Wie definiere ich es (https://www.delphipraxis.net/121842-unentschieden-wie-definiere-ich-es.html)

harryi 5. Okt 2008 22:22


Unentschieden ! Wie definiere ich es
 
Liste der Anhänge anzeigen (Anzahl: 1)
Hallo,

ich habe hier mal ein Spiel geschrieben als Schulaufgabe.
Alle Proceure festgelegt.

Es ist das bekannte 3 gewinnt Spiel auch TicTaeToe genannt. :-D

-Wie definiere ich, dass wenn alle Felder( Buttons) angeklickt sind und keiner
Delphi-Quellcode:
procedure Gewinner
aktiviert? Dann ist nämlich unentschieden für Beiden.

(- Ich freue mich auch gerne über Verbesserungsvorschläge und Tipps für meinen Design usw.) :-D




Hier mal mein Gewinnprocedure:

Delphi-Quellcode:
procedure TForm1.Gewinner;
begin
// ___________ Gewinnerdefinition___________ \\
                 //  1-2-3  \\
                //   4-5-6   \\
               //    7-8-9    \\



//Player 1 = O gewinnt!

// [Player Win 1.1] 1-2-3
if (Button1.caption='O') and (Button2.caption='O') and (Button3.caption='O') then
Form2.Show;
if (Button1.caption='O') and (Button2.caption='O') and (Button3.caption='O') then
Panel2.Caption:='gewonnen';

// [Player Win 1.2] 4-5-6
if (Button4.caption='O') and (Button5.caption='O') and (Button6.caption='O') then
Form2.Show;
if (Button4.caption='O') and (Button5.caption='O') and (Button6.caption='O') then
Panel2.Caption:='gewonnen';

// [Player Win 1.3] 7-8-9
if (Button7.caption='O') and (Button8.caption='O') and (Button9.caption='O') then
Form2.Show;
if (Button7.caption='O') and (Button8.caption='O') and (Button9.caption='O') then
Panel2.Caption:='gewonnen';

// [Player Win 1.4] 1-4-7
if (Button1.caption='O') and (Button4.caption='O') and (Button7.caption='O') then
Form2.Show;
if (Button1.caption='O') and (Button4.caption='O') and (Button7.caption='O') then
Panel2.Caption:='gewonnen';

// [Player Win 1.5] 2-5-8
if (Button2.caption='O') and (Button5.caption='O') and (Button8.caption='O') then
Form2.Show;
if (Button2.caption='O') and (Button5.caption='O') and (Button8.caption='O') then
Panel2.Caption:='gewonnen';

// [Player Win 1.6] 3-6-9
if (Button3.caption='O') and (Button6.caption='O') and (Button9.caption='O') then
Form2.Show;
if (Button3.caption='O') and (Button6.caption='O') and (Button9.caption='O') then
Panel2.Caption:='gewonnen';

// [Player Win 1.7] 1-5-9
if (Button1.caption='O') and (Button5.caption='O') and (Button9.caption='O') then
Form2.Show;
if (Button1.caption='O') and (Button5.caption='O') and (Button9.caption='O') then
Panel2.Caption:='gewonnen';

// [Player Win 1.8] 3-5-7
if (Button3.caption='O') and (Button5.caption='O') and (Button7.caption='O') then
Form2.Show;
if (Button3.caption='O') and (Button5.caption='O') and (Button7.caption='O') then
Panel2.Caption:='gewonnen';






//Player 2 = X gewinnt!

// [Player Win 2.1] 1-2-3
if (Button1.caption='X') and (Button2.caption='X') and (Button3.caption='X') then
Form3.Show;
if (Button1.caption='X') and (Button2.caption='X') and (Button3.caption='X') then
Panel2.Caption:='gewonnen';

// [Player Win 2.2] 4-5-6
if (Button4.caption='X') and (Button5.caption='X') and (Button6.caption='X') then
Form3.Show;
if (Button4.caption='X') and (Button5.caption='X') and (Button6.caption='X') then
Panel2.Caption:='gewonnen';

// [Player Win 2.3] 7-8-9
if (Button7.caption='X') and (Button8.caption='X') and (Button9.caption='X') then
Form3.Show;
if (Button7.caption='X') and (Button8.caption='X') and (Button9.caption='X') then
Panel2.Caption:='gewonnen';

// [Player Win 2.4] 1-4-7
if (Button1.caption='X') and (Button4.caption='X') and (Button7.caption='X') then
Form3.Show;
if (Button1.caption='X') and (Button4.caption='X') and (Button7.caption='X') then
Panel2.Caption:='gewonnen';

// [Player Win 2.5] 2-5-8
if (Button2.caption='X') and (Button5.caption='X') and (Button8.caption='X') then
Form3.Show;
if (Button2.caption='X') and (Button5.caption='X') and (Button8.caption='X') then
Panel2.Caption:='gewonnen';

// [Player Win 2.6] 3-6-9
if (Button3.caption='X') and (Button6.caption='X') and (Button9.caption='X') then
Form3.Show;
if (Button3.caption='X') and (Button6.caption='X') and (Button9.caption='X') then
Panel2.Caption:='gewonnen';

// [Player Win 2.7] 1-5-9
if (Button1.caption='X') and (Button5.caption='X') and (Button9.caption='X') then
Form3.Show;
if (Button1.caption='X') and (Button5.caption='X') and (Button9.caption='X') then
Panel2.Caption:='gewonnen';

// [Player Win 2.8] 3-5-7
if (Button3.caption='X') and (Button5.caption='X') and (Button7.caption='X') then
Form3.Show;
if (Button3.caption='X') and (Button5.caption='X') and (Button7.caption='X') then
Panel2.Caption:='gewonnen'


Danke

Dax 5. Okt 2008 22:34

Re: Unentschieden ! Wie definiere ich es
 
Überleg mal. Unentschieden ist, wenn keine gewonnen hat, aber alle Felder gesetzt sind.. ;)

Hador 6. Okt 2008 01:25

Re: Unentschieden ! Wie definiere ich es
 
Erst einmal kannst du deinen Code ganz einfach um ein vielfaches verkürzen.
Aus
Delphi-Quellcode:
// [Player Win 2.1] 1-2-3 
if (Button1.caption='X') and (Button2.caption='X') and (Button3.caption='X') then
Form3.Show;
if (Button1.caption='X') and (Button2.caption='X') and (Button3.caption='X') then
Panel2.Caption:='gewonnen';

// [Player Win 2.2] 4-5-6 
if (Button4.caption='X') and (Button5.caption='X') and (Button6.caption='X') then
Form3.Show;
if (Button4.caption='X') and (Button5.caption='X') and (Button6.caption='X') then
Panel2.Caption:='gewonnen';
machst du zunächst einmal
Delphi-Quellcode:
// [Player Win 2.1] 1-2-3 
if (Button1.caption='X') and (Button2.caption='X') and (Button3.caption='X') then
begin
  Form3.Show;
  Panel2.Caption:='gewonnen';
end;

// [Player Win 2.2] 4-5-6 
if (Button4.caption='X') and (Button5.caption='X') and (Button6.caption='X') then
begin
  Form3.Show;
  Panel2.Caption:='gewonnen';
end;
und daraus
Delphi-Quellcode:
// [Player Win 2]
if ((Button1.caption='X') and (Button2.caption='X') and (Button3.caption='X')) // 1-2-3
  or ((Button4.caption='X') and (Button5.caption='X') and (Button6.caption='X') ) then // 4-5-6
begin
  Form3.Show;
  Panel2.Caption:='gewonnen';
end;
Dann solltest du unbedingt deinen Elementen vernünftige und aussagekräftige Namen geben. Bspw. statt "Panel2" "Ergebnis" oder wozu es auch immer gut ist :wink:

Wenn du den ersten Spieler schon überprüft hast und dieser gewonnen hat, so brauchst du ja nicht mehr den folgenden überprüfen. Daher kannst du noch folgendes Ändern:
Delphi-Quellcode:
if {Überprüfung ob Spieler 1 gewonnen hat} then
begin
  [...]
end else if {Überprüfung ob Spieler 2 gewonnen hat} then
begin
  [...]
end else if {Überprüfung ob unendschieden} then
begin
  [..]
end;
Dass sind jetzt erstmal einige Kleinigkeiten. Du kannst das ganze natürlich auch noch weiter optimieren, aber mit den von mir angesprochenen Änderungen sollte es schon um einiges kürzer werden.

Übrigends zur Überprüfung auf unentschieden: Wenn die Caption der Buttons vorher leer ist und die Länge 0 hat (also Caption := '') dann wäre eine mögliche Überprüfung:
Delphi-Quellcode:
if Length(Button1.Caption + Button2.Caption + ... + Button9.Caption) >= 9 then // Unentschieden
oder natürlich:
Delphi-Quellcode:
if (Button1.Caption <> '') and (Button2.Caption <> '') and ... and (Button9.Caption <> '') then // Unentschieden

alzaimar 6. Okt 2008 06:13

Re: Unentschieden ! Wie definiere ich es
 
Kurze Einführung in gute Programmierung:
[einführung]
1. Trenne Funktion und Darstellung
[/einführung]
Hier solltest Du dir überlegen, wie Du dein Spielfeld darstellst (Buttons wäre eine Idee) und wie Du die Funktion abbildest (wie wäre es mit einem Array).

In einen Array [0..2,0..2] Of Spielstein könntest Du viel einfacher prüfen, ob irgendwo eine Dreierreihe ist.

Ein weiterer Vorteil ist, das Du die Darstellung später austauschen kannst (da gibt es tolle Komponenten, z.B. ein TDrawGrid), ohne an der 'KI' an sich etwas zu verändern...

Wenn der Anwender bzw. der Computer einen Zug mmacht, dann steht das ja in dem Array. Nach jeden Zug stellst Du also den Inhalt des Arrays neu dar, indem Du z.B. die Buttons füllst oder im TDrawGrid für jedes Feld ein entsprechendes Bild (Leer, Kreuz, Kringel) hinmalst.

harryi 7. Okt 2008 21:44

Re: Unentschieden ! Wie definiere ich es
 
ok danke

ich habe schon gegooglet nach ungleich Zeichen <> umd unentschieden zu definieren.

Weil bei 9 Felder , wenn alle angeklickt sind ist es unenschieden.
Wenn jemand davor gewonnen hat kommt eh Gewinnbenachrichtigung.


Ich habe aber weiterversucht und es kam , wo ich beim belegen des neunten Feldes gewonnen habe zugleich ein unentschieden.

Hador 8. Okt 2008 00:11

Re: Unentschieden ! Wie definiere ich es
 
Zitat:

Zitat von Hador
Wenn du den ersten Spieler schon überprüft hast und dieser gewonnen hat, so brauchst du ja nicht mehr den folgenden überprüfen. Daher kannst du noch folgendes Ändern:
Delphi-Quellcode:
if {Überprüfung ob Spieler 1 gewonnen hat} then
begin
  [...]
end else if {Überprüfung ob Spieler 2 gewonnen hat} then
begin
  [...]
end else if {Überprüfung ob unendschieden} then
begin
  [..]
end;

Wenn du das so gemacht hättest, hättest du das Problem nicht, da dann ja nur dann weiter geprüft wird, wenn niemand vorher gewonnen hat :wink:

harryi 8. Okt 2008 11:30

Re: Unentschieden ! Wie definiere ich es
 
Hey, super Tipp^^

ich habe meine Tic Tac Toe Version jetzt eh verbessert.

Danke nochmal!

Delphi-Quellcode:
if {Überprüfung ob Spieler 1 gewonnen hat} then
begin
  [...]
end else if {Überprüfung ob Spieler 2 gewonnen hat} then
begin
  [...]
end else if {Überprüfung ob unendschieden} then
begin
  [..]
end;


Alle Zeitangaben in WEZ +1. Es ist jetzt 00:20 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