AGB  ·  Datenschutz  ·  Impressum  







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

Unentschieden ! Wie definiere ich es

Ein Thema von harryi · begonnen am 5. Okt 2008 · letzter Beitrag vom 8. Okt 2008
Antwort Antwort
harryi

Registriert seit: 29. Sep 2008
14 Beiträge
 
#1

Unentschieden ! Wie definiere ich es

  Alt 5. Okt 2008, 22:22
Hallo,

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

Es ist das bekannte 3 gewinnt Spiel auch TicTaeToe genannt.

-Wie definiere ich, dass wenn alle Felder( Buttons) angeklickt sind und keiner
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.)




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
Angehängte Dateien
Dateityp: rar p9_154.rar (561,3 KB, 15x aufgerufen)
  Mit Zitat antworten Zitat
Dax
(Gast)

n/a Beiträge
 
#2

Re: Unentschieden ! Wie definiere ich es

  Alt 5. Okt 2008, 22:34
Überleg mal. Unentschieden ist, wenn keine gewonnen hat, aber alle Felder gesetzt sind..
  Mit Zitat antworten Zitat
Benutzerbild von Hador
Hador

Registriert seit: 11. Dez 2004
Ort: Recke
682 Beiträge
 
Turbo Delphi für Win32
 
#3

Re: Unentschieden ! Wie definiere ich es

  Alt 6. Okt 2008, 01:25
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

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:
if Length(Button1.Caption + Button2.Caption + ... + Button9.Caption) >= 9 then // Unentschieden oder natürlich:
if (Button1.Caption <> '') and (Button2.Caption <> '') and ... and (Button9.Caption <> '') then // Unentschieden
Lars Kiesow
http://www.larskiesow.de

Computer gehorchen deinen Befehlen, nicht deinen Absichten.
  Mit Zitat antworten Zitat
alzaimar
(Moderator)

Registriert seit: 6. Mai 2005
Ort: Berlin
4.956 Beiträge
 
Delphi 2007 Enterprise
 
#4

Re: Unentschieden ! Wie definiere ich es

  Alt 6. Okt 2008, 06:13
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.
"Wenn ist das Nunstruck git und Slotermeyer? Ja! Beiherhund das Oder die Flipperwaldt gersput!"
(Monty Python "Joke Warefare")
  Mit Zitat antworten Zitat
harryi

Registriert seit: 29. Sep 2008
14 Beiträge
 
#5

Re: Unentschieden ! Wie definiere ich es

  Alt 7. Okt 2008, 21:44
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.
  Mit Zitat antworten Zitat
Benutzerbild von Hador
Hador

Registriert seit: 11. Dez 2004
Ort: Recke
682 Beiträge
 
Turbo Delphi für Win32
 
#6

Re: Unentschieden ! Wie definiere ich es

  Alt 8. Okt 2008, 00:11
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
Lars Kiesow
http://www.larskiesow.de

Computer gehorchen deinen Befehlen, nicht deinen Absichten.
  Mit Zitat antworten Zitat
harryi

Registriert seit: 29. Sep 2008
14 Beiträge
 
#7

Re: Unentschieden ! Wie definiere ich es

  Alt 8. Okt 2008, 11:30
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;
  Mit Zitat antworten Zitat
Antwort Antwort


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 17:22 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