BEREZUG.INC: Pascal move engine

This is the original Pascal source for my contribution to the REVERSI high-school project. The file was called BEREZUG.INC and contains the material and positional evaluation routines, move generation, depth-limited negamax search, and the Berechne_Zug entry point used by the main program.

The include file relies on the surrounding REVERSI sources. It is presented here unchanged as a record of the original implementation.

  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
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
CONST MaxZuege=60;

TYPE TZug=RECORD
            x,y:Byte
          END;
     TZugFeld=ARRAY[1..MaxZuege] OF TZug;
     TStellung=TSpielFeld;

PROCEDURE Materiell(Stellung:TStellung;CFarbe:TFarbe;VAR Wert:Integer;
                    VAR Ende:Boolean);
 { Führt eine mat. Bewertung der Stellung durch;
   Gegnerische Steine zählen -100, eigene +100 }
VAR Hilf:Integer;
    Laufx,Laufy:TKoord;
    GegSteine,EigSteine:Byte;
BEGIN
  Hilf:=0; GegSteine:=0; EigSteine:=0; Ende:=TRUE;
  FOR Laufx:=1 TO 8 DO
    FOR Laufy:=1 TO 8 DO
    BEGIN
      IF Stellung[Laufx,Laufy]=Frei THEN Ende:=FALSE
      ELSE
        IF Stellung[Laufx,Laufy]=CFarbe THEN
        BEGIN
          Hilf:=Hilf+5;
          Inc(EigSteine)
        END
          ELSE
          BEGIN
            Hilf:=Hilf-5;
            Inc(GegSteine)
          END
    END;
  IF Ende THEN
    IF Hilf>0 THEN Hilf:=MaxInt ELSE Hilf:=-32767;
  IF GegSteine=0 THEN Hilf:=MaxInt;
  IF EigSteine=0 THEN Hilf:=-32767;
  Wert:=Hilf
END;

PROCEDURE Positionell(Stellung:TStellung;CFarbe:TFarbe;VAR Wert:Integer);
VAR Laufx,Laufy:Byte;
    Hilf:Integer;
  FUNCTION Qualle(x,y:TKoord;CFarbe:TFarbe):Integer;
  VAR Hilf:Integer;
  BEGIN
    Hilf:=0;
    IF (x=1) OR (x=8) THEN Hilf:=Hilf+300;
    IF (y=1) OR (y=8) THEN Hilf:=Hilf+300;
    IF (x=3) OR (x=6) THEN Hilf:=Hilf+15;
    IF (y=3) OR (y=6) THEN Hilf:=Hilf+15;
    IF x=2 THEN IF Stellung[1,y]<>CFarbe THEN Hilf:=Hilf-25;
    IF x=7 THEN IF Stellung[8,y]<>CFarbe THEN Hilf:=Hilf-25;
    IF y=2 THEN IF Stellung[x,1]<>CFarbe THEN Hilf:=Hilf-25;
    IF y=7 THEN IF Stellung[x,8]<>CFarbe THEN Hilf:=Hilf-25;
    IF (x=2) AND (y=2) AND (Stellung[1,1]<>CFarbe) THEN Hilf:=Hilf-50;
    IF (x=7) AND (y=7) AND (Stellung[8,8]<>CFarbe) THEN Hilf:=Hilf-50;
    IF (x=2) AND (y=7) AND (Stellung[1,8]<>CFarbe) THEN Hilf:=Hilf-50;
    IF (x=7) AND (y=2) AND (Stellung[8,1]<>CFarbe) THEN Hilf:=Hilf-50;
    IF (x=1) AND (y=1) THEN Hilf:=Hilf+3000;
    IF (x=1) AND (y=8) THEN Hilf:=Hilf+3000;
    IF (x=8) AND (y=1) THEN Hilf:=Hilf+3000;
    IF (x=8) AND (y=8) THEN Hilf:=Hilf+3000;
    IF (x=1) OR (x=8) THEN
      IF (y=2) THEN IF Stellung[x,1]=CFarbe THEN Hilf:=Hilf+1000
                                            ELSE Hilf:=Hilf-1000
               ELSE IF (y=7) THEN IF Stellung[x,8]=CFarbe THEN Hilf:=Hilf+1000
                                                          ELSE Hilf:=Hilf-1000;
    IF (y=1) OR (y=8) THEN
      IF (x=2) THEN IF Stellung[1,y]=CFarbe THEN Hilf:=Hilf+1000
                                            ELSE Hilf:=Hilf-1000
               ELSE IF (x=7) THEN IF Stellung[8,y]=CFarbe THEN Hilf:=Hilf+1000
                                                          ELSE Hilf:=Hilf-1000;
    IF (x=2) AND (y=2) THEN IF Stellung[1,1]<>CFarbe THEN Hilf:=Hilf-1000;
    IF (x=7) AND (y=2) THEN IF Stellung[8,1]<>CFarbe THEN Hilf:=Hilf-1000;
    IF (x=2) AND (y=7) THEN IF Stellung[1,8]<>CFarbe THEN Hilf:=Hilf-1000;
    IF (x=7) AND (y=7) THEN IF Stellung[8,8]<>CFarbe THEN Hilf:=Hilf-1000;
    Qualle:=Hilf
  END;
BEGIN
  Wert:=0;
  FOR Laufx:=1 TO 8 DO
    FOR Laufy:=1 TO 8 DO
    BEGIN
      IF Stellung[Laufx,Laufy]<>Frei THEN
      BEGIN
        Hilf:=Qualle(Laufx,Laufy,Stellung[Laufx,Laufy]);
        IF Stellung[Laufx,Laufy]=CFarbe THEN Wert:=Wert+Hilf
          ELSE Wert:=Wert-Hilf
      END
    END
END;

FUNCTION Bewertung(Stellung:TStellung;CFarbe:TFarbe):Integer;
VAR Wert:Integer;
    Hilf:Integer;
    Ende:Boolean;
BEGIN
  Materiell(Stellung,CFarbe,Hilf,Ende);
  IF NOT Ende THEN Positionell(Stellung,CFarbe,Wert);
  Bewertung:=Hilf+Wert
END;

FUNCTION Gegner(Farbe:TFarbe):TFarbe;
BEGIN

  IF Farbe=Weiss THEN Gegner:=Schwarz ELSE Gegner:=Weiss
END;

PROCEDURE Finde_Zuege(Stellung:TStellung;Farbe:TFarbe;
                      VAR Zuege:TZugFeld;VAR ZugZahl:Byte);
VAR Laufx,Laufy:TKoord;
    moeglich:Boolean;
BEGIN
  ZugZahl:=0;
  FOR Laufy:=1 TO 8 DO
    FOR Laufx:=1 TO 8 DO
      IF Eingabe_richtig(Stellung,Laufx,Laufy,Farbe) THEN
      BEGIN
        Inc(ZugZahl);
        Zuege[ZugZahl].x:=Laufx;
        Zuege[ZugZahl].y:=Laufy
      END
END;

PROCEDURE Zug_Suche(Stellung:TStellung;Farbe:TFarbe;VAR Zug:TZug;
                    VAR Wert:Integer;Level:TLevel;Start:Boolean);
VAR MWert:Integer;
    ZStellung:TStellung;
    Lauf,Zahl:Byte;
    Zuege:TZugFeld;
BEGIN
  IF Level=0 THEN Wert:=Bewertung(Stellung,Farbe)
  ELSE
  BEGIN
    Finde_Zuege(Stellung,Farbe,Zuege,Zahl);
    MWert:=-32767;
    Lauf:=0;
    REPEAT
      Inc(Lauf);
      ZStellung:=Stellung;
      IF Zahl>0 THEN
        Zug_ausfuehren(ZStellung,Zuege[Lauf].x,Zuege[Lauf].y,Farbe);
      Zug_Suche(ZStellung,Gegner(Farbe),Zug,Wert,Level-1,FALSE);
      Wert:=-Wert;
      IF Wert>MWert THEN
      BEGIN
        MWert:=Wert;
        IF Start THEN Zug:=Zuege[Lauf]
      END;
    UNTIL Lauf>=Zahl;
    Wert:=MWert
  END
END;

PROCEDURE Berechne_Zug(Spielfeld:TStellung;VAR x,y:TKoord;Spielerfarbe:TFarbe;
                       Spielstaerke:TLevel);
VAR Zug:TZug;
    Wert:Integer;
    Satz:TString;
BEGIN
  Zug_Suche(Spielfeld,Spielerfarbe,Zug,Wert,Spielstaerke,TRUE);
  x:=Zug.x; y:=Zug.y;
  Str(Wert,Satz);
  Satz:='Bewertung '+Satz;
  Gib_Info_aus(Satz)
END;