Autor Beitrag
F34r0fTh3D4rk
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 5284
Erhaltene Danke: 27

Win Vista (32), Win 7 (64)
Eclipse, SciTE, Lazarus
BeitragVerfasst: Di 25.04.06 19:22 
Hallo, folgende funktion soll eine Situation bei dem Spiel TicTacToe bewerten und zwar werden wenn zwei felder in einer reihe oder spalte von einer farbe sind, jedoch noch eines der 3 möglichen felder frei ist, 2 punkte addiert oder subtrahiert, je nach spieler, sollte nur ein feld vom spieler belegt sein, wird ein punkt berechnet (ich weiß noch nicht ob das sinnvoll ist, werde ich wohl weglassen), wie kann man das optimieren ?
ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
function TForm1.bewertung: integer;
  function bewertefarbe(farbe: TFeld): integer;
    function addsub(var a: integer; const b: integer = 2): integer;
    begin
      result := 0;
      if farbe = rot then
        result := a + b;
      if farbe = blau then
        result := a - b;
    end;
  var
    i, count: integer;
  begin
    count := 0;
    //Reihen OO
    result := 0;
    if (((felder[1] = farbe) and (felder[3] = nix)) or ((felder[3] = farbe) and (felder[1] = nix))) and (felder[2] = farbe) then
      result := addsub(result);
    if (((felder[4] = farbe) and (felder[6] = nix)) or ((felder[6] = farbe) and (felder[4] = nix))) and (felder[5] = farbe) then
      result := addsub(result);
    if (((felder[7] = farbe) and (felder[9] = nix)) or ((felder[9] = farbe) and (felder[7] = nix))) and (felder[8] = farbe) then
      result := addsub(result);
    //Reihen O O
    if ((felder[1] = farbe) and (felder[3] = farbe) and (felder[2] = nix)) then
      result := addsub(result);
    if ((felder[4] = farbe) and (felder[6] = farbe) and (felder[5] = nix)) then
      result := addsub(result);
    if ((felder[7] = farbe) and (felder[9] = farbe) and (felder[8] = nix)) then
      result := addsub(result);
    //Spalten OO
    if (((felder[1] = farbe) and (felder[7] = nix)) or ((felder[7] = farbe) and (felder[1] = nix))) and (felder[4] = farbe) then
      result := addsub(result);
    if (((felder[2] = farbe) and (felder[8] = nix)) or ((felder[8] = farbe) and (felder[2] = nix))) and (felder[5] = farbe) then
      result := addsub(result);
    if (((felder[3] = farbe) and (felder[9] = nix)) or ((felder[9] = farbe) and (felder[3] = nix))) and (felder[6] = farbe) then
      result := addsub(result);
    //Spalten O O
    if ((felder[1] = farbe) and (felder[7] = farbe) and (felder[4] = nix)) then
      result := addsub(result);
    if ((felder[2] = farbe) and (felder[8] = farbe) and (felder[5] = nix)) then
      result := addsub(result);
    if ((felder[3] = farbe) and (felder[9] = farbe) and (felder[6] = nix)) then
      result := addsub(result);
    //Diagonalen OO
    if (((felder[1] = farbe) and (felder[5] = nix)) or ((felder[5] = farbe) and (felder[1] = nix))) and (felder[9] = farbe) then
      result := addsub(result);
    if (((felder[3] = farbe) and (felder[5] = nix)) or ((felder[5] = farbe) and (felder[3] = nix))) and (felder[7] = farbe) then
      result := addsub(result);
    //Diagonalen O O
    if ((felder[1] = farbe) and (felder[9] = farbe) and (felder[5] = nix)) then
      result := addsub(result);
    if ((felder[3] = farbe) and (felder[7] = farbe) and (felder[5] = nix)) then
      result := addsub(result);
    //Else
    for i := 1 to 9 do
      if felder[i] = farbe then
        inc(count);
    if count = 1 then
      result := addsub(result, 1);    
  end;
begin
  result := 0;
  if gwinner = maschine then
  begin
    result := - maxint;
    exit;
  end;
  if gwinner = mensch then
  begin
    result :=   maxint;
    exit;
  end;
  result := bewertefarbe(rot) + bewertefarbe(blau);
end;


im moment sind das ziemlich viele abfragen, aufgebaut ist das feld als ein array [1..9of TFeld wobei TFeld nix, rot oder blau sein kann.


danke schonmal ;)
starsurfer
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 334

Win 95, Win 98, Win XP, Win Vista, Linux
D5 Enterprise ,D2005, D6 Personal, Visual C++ Express 2005, C++ Builder 6 E, Dev-C++
BeitragVerfasst: Di 25.04.06 19:36 
ich versteh zwar den sinn nich aba es geht auch kürzer zu schreiben^^

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
if (((felder[1] = farbe) and (felder[3] = nix)) or ((felder[3] = farbe) and (felder[1] = nix))) and (felder[2] = farbe) then  
      result := addsub(result);  
    if (((felder[4] = farbe) and (felder[6] = nix)) or ((felder[6] = farbe) and (felder[4] = nix))) and (felder[5] = farbe) then  
      result := addsub(result);  
    if (((felder[7] = farbe) and (felder[9] = nix)) or ((felder[9] = farbe) and (felder[7] = nix))) and (felder[8] = farbe) then  
      result := addsub(result);  

//...wird zu...

for i:=0 to 2 do
    begin
    if (((felder[1+i*3] = farbe) and (felder[3+i*3] = nix)) or ((felder[3+i*3] = farbe) and (felder[1+i*3] = nix))) and (felder[2+i*3] = farbe) then
    result := addsub(result);
    end;


die weiteren abfragen lassen sich mit der methode natürlich auch kürzer schreiben...
//EDIT: hoffen wir mal das die Delphi Code optimierung da kein Strich durch macht^^

_________________
GEIZ IST GEIL! - Ihr Sozialamt
F34r0fTh3D4rk Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 5284
Erhaltene Danke: 27

Win Vista (32), Win 7 (64)
Eclipse, SciTE, Lazarus
BeitragVerfasst: Di 25.04.06 19:48 
jo wäre vielleicht ne möglichkeit, mal weiter sehen ;)