⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gameboard.pas

📁 描述了根据五子规则做出相应的分步预测和胜负判断!也对DELPHI的面向对象的程序开发系统做了相应的阐述!
💻 PAS
📖 第 1 页 / 共 4 页
字号:
        end;
      end; // of JudgeImage;

    begin
      GetDirDelta(dir, dirx, diry);
      for i := -TypeArrayMax to TypeArrayMax do
        TypeImage[i] := Edge;
      for i := 0 to TypeArrayMax do
        if Board[x + i * dirx, y + i * diry] <> Edge then
          TypeImage[i] := Board[x + i * dirx, y + i * diry]
        else
          break;
      for i := -1 downto -TypeArrayMax do
        if Board[x + i * dirx, y + i * diry] <> Edge then
          TypeImage[i] := Board[x + i * dirx, y + i * diry]
        else
          break;

      // Update Differ
      ChessType := ReverseType(Board[x, y]);
      begin
        for i := 0 to 5 do
          if TypeImage[i] = Edge then
            break
          else
          begin
            EdgeBoard[ChessType][x + i * dirx, y + i * diry].TypeNum[dir].Edge := No;
            if i <> 0 then
              UpdateList(x + i * dirx, y + i * diry, ChessType, False);
          end;
        for i := -1 downto -5 do
          if TypeImage[i] = Edge then
            break
          else
          begin
            EdgeBoard[ChessType][x + i * dirx, y + i * diry].TypeNum[dir].Edge := No;
            if i <> 0 then
              UpdateList(x + i * dirx, y + i * diry, ChessType, False);
          end;
      end;

      TheOtherType := ReverseType(ChessType);
      i := -9;
      while (i < -4) do
      begin
        JudgeImage(-5, -1);
        inc(i);
      end; // for i:= -9 to -5

      i := 1;
      while (i < 6) do
      begin
        JudgeImage(1, 5);
        inc(i);
      end; // for i:= 1 to 5

      //  Update Same
      ChessType := Board[x, y];
      TheOtherType := ReverseType(ChessType);
      i := -4;
      while i < 1 do
      begin
        JudgeImage(-5, 5);
        inc(i);
      end; // for i:= -4 to 0

      for ChessType := Black to White do
      begin
        for i := -5 to 5 do
        begin
          if NeedToAdd[ChessType][i] then
          begin
            ux := x + i * dirx;
            uy := y + i * diry;

            UpdateList(ux, uy, ChessType, False);
          end;
        end;
      end;

    end; // UpdateDir

    procedure UpdateBoard;
    var dir: TDirection;
    begin
      for dir := Left to UpperRight do
        UpdateDir(dir, x, y);
    end;

  begin
    Result := False;
    if Depth <= 0 then exit;

    NextDepth := depth - 1;
    OtherType := ReverseType(TypeTurn);


    Recover := False;
    if x > 0 then
    begin
      Recover := True;
      Oldx := x;
      Oldy := y;
      Board[x, y] := OtherType;

      LoopChessType := OtherType;
      for i := 1 to NumPlaces do
      begin
        BestPlaces[LoopChessType][i].int := 0;
        BestPlaces[LoopChessType][i].level := 0;
      end;

      LoopChessType := TypeTurn;
      for i := 1 to NumPlaces do
      begin
        if ((BestPLaces[LoopChessType][i].dx = x) and (BestPlaces[LoopChessType][i].dy = y)) then
        begin
          for j := i to NumPlaces - 1 do
            BestPlaces[LoopChessType][j] := BestPlaces[LoopChessType][j + 1];
          BestPlaces[LoopChessType][NumPlaces].int := 0;
          BestPlaces[LoopChessType][NumPlaces].level := 0;
          break;
        end;
      end;

      UpdateBoard;
    end;

    OtherType := ReverseType(TypeOfChess);
    if TypeOfChess = TypeTurn then
    begin

      if BestPlaces[TypeOfChess][1].level = Five then
      begin
        x := BestPlaces[TypeOfChess][1].dx;
        y := BestPlaces[TypeOfChess][1].dy;
        Result := True;
        goto RecoverPoint;
      end;

      if BestPlaces[OtherType][1].level = Five then
      begin
        j := BestPlaces[OtherType][1].dx;
        k := BestPlaces[OtherType][1].dy;
        x := j;
        y := k;
        Result := WinPlace(TypeOfChess, OtherType, j, k, NextDepth, Child, EdgeBoard, BestPlaces, CutLevel);
        goto RecoverPoint;
      end;

      if BestPlaces[TypeOfChess][1].level = Live4 then
      begin
        x := BestPlaces[TypeOfChess][1].dx;
        y := BestPlaces[TypeOfChess][1].dy;
        Result := True;
        goto RecoverPoint;
      end;

      if BestPlaces[TypeOfChess][1].level = Dead4 then
      begin
        for i := 1 to NumPlaces do
          if BestPlaces[TypeOfChess][i].level >= Dead4 then
          begin
            j := BestPlaces[TypeOfChess][i].dx;
            k := BestPlaces[TypeOfChess][i].dy;
            x := j;
            y := k;
            Result := WinPlace(TypeOfChess, OtherType, j, k, NextDepth, Child, EdgeBoard, BestPlaces, Dead4);
            if Result then
              goto RecoverPoint;
          end else
            break;
      end;

      if CutLevel >= Dead4 then
      begin
        Result := False;
        goto RecoverPoint;
      end;

      if BestPlaces[OtherType][1].level = Live4 then
      begin
        for i := 1 to NumPlaces do
          //  It is Dead4 For the situation of  011010 !!!!!!!  2004.5.25
          if BestPlaces[OtherType][i].level >= Dead4 then
          begin
            j := BestPlaces[OtherType][i].dx;
            k := BestPlaces[OtherType][i].dy;
            x := j;
            y := k;
            Result := WinPlace(TypeOfChess, OtherType, j, k, NextDepth, Child, EdgeBoard, BestPlaces, CutLevel);
            if Result then
              goto RecoverPoint;
          end else
          begin
            Result := False;
            goto RecoverPoint;
          end;
      end;

      if BestPlaces[OtherType][1].level = Dead4 then
      begin
        for i := 1 to NumPlaces do
          if BestPlaces[OtherType][i].level = Dead4 then
          begin
            j := BestPlaces[OtherType][i].dx;
            k := BestPlaces[OtherType][i].dy;
            x := j;
            y := k;
            Result := WinPlace(OtherType, TypeOfChess, j, k, NextDepth, Child, EdgeBoard, BestPlaces, Dead4);
            if Result then
            begin
              Result := False;
              goto RecoverPoint;
            end;
          end else
            break;
      end;

      if BestPlaces[TypeOfChess][1].level >= Live3Loose then
      begin
        for i := 1 to NumPlaces do
        begin
          if BestPlaces[TypeOfChess][i].level >= Live3Loose then
          begin
            j := BestPlaces[TypeOfChess][i].dx;
            k := BestPlaces[TypeOfChess][i].dy;
            x := j;
            y := k;
            Result := WinPlace(TypeOfChess, OtherType, j, k, NextDepth, Child, EdgeBoard, BestPlaces, Live3Loose);
            if Result then
              goto RecoverPoint;
          end else
            break;
        end;
      end;

      if CutLevel >= Live3Loose then
      begin
        Result := False;
        goto RecoverPoint;
      end;

      Result := False;
      goto RecoverPoint;

    end // TypeOfChess = TypeTurn
    else begin

      if BestPlaces[OtherType][1].level = Five then
      begin
        Result := False;
        goto RecoverPoint;
      end;

      if BestPlaces[TypeOfChess][1].level = Five then
      begin
        j := BestPlaces[TypeOfChess][1].dx;
        k := BestPlaces[TypeOfChess][1].dy;
        Result := WinPlace(TypeOfChess, TypeOfChess, j, k, NextDepth, Child, EdgeBoard, BestPlaces, CutLevel);
        goto RecoverPoint;
      end;

      if BestPlaces[OtherType][1].level = Live4 then
      begin
        Result := False;
        goto RecoverPoint;
      end;

      //  No need because it has been tested on upper depth level
{      if BestPlaces[OtherType][1].level = Dead4 then
      begin
        for i:= 1 to NumPlaces do
        begin
          if BestPlaces[OtherType][i].level = Dead4 then
          begin
            j := BestPlaces[OtherType][i].dx;
            k := BestPlaces[OtherType][i].dy;
            Child := Form1.TreeView1.Items.AddChild( Tree, inttostr(j) + inttostr(k) );
            Result := WinPlace(OtherType, TypeOfChess, j, k, NextDepth, Child, EdgeBoard, BestPlaces);
            if Result then
            begin
              Result := False;
              goto RecoverPoint;
            end;
          end else
            break;
        end;
      end;
 }
      if BestPlaces[TypeOfChess][1].level = Live4 then
      begin
        for i := 1 to NumPlaces do
        begin
          //  It is Dead4 for the situation of 011010 !!!!!!!!  2004.5.25
          if BestPlaces[TypeOfChess][i].level >= Dead4 then
          begin
            j := BestPlaces[TypeOfChess][i].dx;
            k := BestPlaces[TypeOfChess][i].dy;
            Result := WinPlace(TypeOfChess, TypeOfChess, j, k, NextDepth, Child, EdgeBoard, BestPlaces, CutLevel);
            if not Result then
            begin
              Result := False;
              goto RecoverPoint;
            end;
          end else
          begin
            Result := True;
            goto RecoverPoint;
          end;
        end;
      end;

      Result := False;
      goto RecoverPoint;

    end; // TypeOfChess <> TypeTurn


    RecoverPoint:
    if Recover then
    begin
      Board[OldX, OldY] := None;
    end;

    //    if Result then
    //      Form1.Memo1.Lines.Add(GetTypeString(TypeOfChess) + ' Win Path : ' + inttostr(x) + ', '+ inttostr(y));
  end; //  WinPlace

  function FindMayWinPlace(TypeOfChess, TypeTurn: TChessType; var x, y: integer; depth: integer; Tree: TTreeNode;
    EdgeBoard: TEdgeBoard; BestPlaces: TBestList; CutLevel: TEdgeEnum): boolean;
  var i, j, k: integer;
    NextDepth: integer;
    DeeperLevel: integer;
    Oldx, Oldy: integer;
    Recover, Win1: boolean;
    Score, LevelScore, BestScore: real;
    LoopChessType, OtherType: TChessType;
    Child: TTreeNode;

    procedure UpdateList(x, y: SmallInt; TypeOfChess: TChessType; Delete: Boolean);
    var i, j: integer;
      l: TDirection;
      BestType: TEdgeEnum;
      Score: integer;
    begin
      if Delete then
      begin
        for i := 1 to NumPlaces do
        begin
          if ((BestPlaces[TypeOfChess][i].dx = x) and (BestPlaces[TypeOfChess][i].dy = y)) then
          begin
            for j := i to NumPlaces - 1 do
              BestPlaces[TypeOfChess][j] := BestPlaces[TypeOfChess][j + 1];
            BestPlaces[TypeOfChess][NumPlaces].level := No;
            BestPlaces[TypeOfChess][NumPlaces].int := 0;
            BestPlaces[TypeOfChess][NumPlaces].Score := 0;
            exit;
          end;
        end;
      end else
      begin
        if Board[x, y] <> None then exit;
        BestType := No;
        for l := Left to UpperRight do
        begin
          Score := Score + EdgeBoard[TypeOfChess][x, y].TypeNum[l].Edge;
          if EdgeBoard[TypeOfChess][x, y].TypeNum[l].Edge > BestType then
            BestType := EdgeBoard[TypeOfChess][x, y].TypeNum[l].Edge;
        end;

        // 若已经在列表中则去掉
        for i := 1 to NumPlaces do
          if ((BestPlaces[TypeOfChess][i].dx = x) and (BestPlaces[TypeOfChess][i].dy = y)) then
          begin
            k := i;
            if ((i > 1) and (BestType > BestPlaces[TypeOfChess][i - 1].level)) then
            begin
              k := 1;
              for j := i - 1 downto 1 do
              begin
                if BestType > BestPlaces[TypeOfChess][j].level then
                  BestPlaces[TypeOfChess][j + 1] := BestPlaces[TypeOfChess][j]
                else
                begin
                  k := j + 1;
                  break;
                end;
              end;
            end else
              if ((i < NumPlaces) and (BestType < BestPlaces[TypeOfChess][i + 1].level)) then

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -