Tin học Game Cờ Vua Trí Tuệ Nhân Tạo viết bằng PASCAL

trà nguyễn hữu nghĩa

Cựu Mod Vật Lí |Cây bút Thơ|Thần tượng VH
Thành viên
14 Tháng năm 2017
3,974
7,623
744
22
Phú Yên
Trường THPT Lương Văn Chánh
Anh ơi chưa có tính năng nhập thành à anh :>, chúc anh update hơn nha
Chức năng nhập thành làm cực dễ luôn......nhưng mà viết lại dài xíu nên mình hơi lười.....giờ đọc lại code cũng nản :D
 
  • Like
Reactions: phidaotram

Bạch Khả Ái

Học sinh mới
Thành viên
14 Tháng bảy 2019
1
1
1
Khánh Hòa
THCS Âu Cơ
if i <= 5 then BanCo[0].Oco[i,1].Quan := i else BanCo[0].Oco[i,1].Quan := 9-i; BanCo[0].OCo[i,1].NChoi := May; if i <= 5 then BanCo[0].Oco[i,8].Quan := i else BanCo[0].Oco[i,8].Quan := 9-i; BanCo[0].OCo[i,8].NChoi := Nguoi;
Sao e thấy đoạn này đều có If i<=5 then ... mà s a để thành hai câu riêng vậy ạ?
 

Trung Kiên ĐTTAK

Học sinh mới
Thành viên
17 Tháng mười một 2019
66
22
11
19
Nghệ An
THCS Nghĩa Lộc
View attachment 103442
Game này mình phải bỏ cả tuần mới viết được đó nha
Code này hơi rắc rối nên mình chia ra thành các file nhỏ, các bạn lưu theo hướng dẫn nha, nếu không thì Code không chạy được đâu

Code này các bạn lưu dưới tên CHESSAI.PAS
Mã:
 Program ChessAI;
 uses crt,dos;
 const
      Size = 7;
      Level = 2;

      Nguoi = -1;
      May = 1;

      Chon = 1;
      ChuanBi = 2;
      XuatPhat = 3;
      DiDen = 4;
      KeyIn = 5;

      Trong = 0;
      Xe = 1;
      Ma = 2;
      Tuong = 3;
      Vua = 4;
      Hau = 5;
      Tot = 6;

 type TOCo = object
      Quan: integer;
      NChoi: integer;
      Status: integer;
             end;

 type TBanCo = object
      OCo: array[1..8,1..8] of TOCo;
      Procedure InBanCo;
      Procedure Reset(stt: integer);
      Function DiVua(x,y,NC: integer): integer;
      Function DiHau(x,y,NC: integer): integer;
      Function DiXe(x,y,NC: integer): integer;
      Function DiMa(x,y,NC: integer): integer;
      Function DiTuong(x,y,NC: integer): integer;
      Function DiTot(x,y,NC: integer): integer;
      Function DiChuyen(x,y: integer): integer;
      Function DiCo(x1,y1,x2,y2: integer): integer;
      Procedure VeVua(x,y: integer);
      Procedure VeHau(x,y: integer);
      Procedure VeXe(x,y: integer);
      Procedure VeMa(x,y: integer);
      Procedure VeTuong(x,y: integer);
      Procedure VeTot(x,y: integer);
      Procedure VeCo(x,y: integer);
      Procedure PhongCap(x,y: integer);
               end;
 var
    BanCo: array[0..Level] of TBanCo;
    Dachon: Boolean;
    cx,cy,kx,ky: integer;
    ls,winer: integer;

 procedure setcusor(bot,top: byte);
 var regs : registers;
 begin
 regs.ah :=1;
 regs.ch:=bot;
 regs.cl:=top;
 intr($10,regs);
 end;

 {$I VEQUAN.INC}

 Procedure TBanCo.InBanCo;
 var i,j,x,y: integer;
 Begin
      Textbackground(Black);
      Setcusor(32,0);
      for x := 1 to 8 do
          for y := 1 to 8 do
          begin
               if (x+y) mod 2 = 0 then textBackground(White)
               else textBackground(Red);
               if (OCo[x,y].Status = Chon) or (OCo[x,y].Status = XuatPhat)
               then textBackground(Cyan)
               else
               if (OCo[x,y].Status = Chuanbi) or (OCo[x,y].Status = DiDen)
               then textBackground(13)
               else
               if (OCo[x,y].Status = KeyIn) then TextBackground(Blue);
               for i := 1 to Size do
                   for j := 1 to size div 2 do
                   begin
                   gotoxy((x-1)*size+i+2,(y-1)*(size div 2) +j);
                   write(' ');
                   end;
          if OCo[x,y].NChoi = Nguoi then
          Textcolor(Green) else TextColor(Black);
          VeCo(x,y);
          end;
 End;

 Procedure TBanCo.Reset(stt: integer);
 var i,j: integer;
 Begin
      for i := 1 to 8 do
          for j := 1 to 8 do
          if OCo[i,j].Status = stt then
             OCo[i,j].Status := Trong;
 End;

 Procedure TBanCo.PhongCap(x,y: integer);
 var q: integer;
 Begin
      if OCo[x,y].Quan = Tot then
      begin
           if (OCo[x,y].NChoi = May) and (y = 8) then
           begin
                OCo[x,y].Quan := Hau;
                clrscr;
                InBanCo;
           end
           else
           if (OCo[x,y].NChoi = Nguoi) and (y = 1) then
           begin
                TextBackGround(Black);
                clrscr;
                TextColor(LightRed);
                Gotoxy(2,2);Write('1. Xe');
                Gotoxy(2,3);Write('2. Ma');
                Gotoxy(2,4);Write('3. Tuong');
                Gotoxy(2,5);Write('4. Hau');
                Gotoxy(2,6);
                repeat readln(q);until q in [1..4];
                if q = 4 then q := Hau;
                OCo[x,y].Quan := q;
                clrscr;
                InBanCo;
           end;
      end;
 End;
 {$I AICHESS.INC}
 {$I DCCHESS.INC}

 Procedure KhoiTao;
 var i,j: integer;
 Begin
      for i := 1 to 8 do
          for j := 1 to 8 do
          begin
              BanCo[0].OCo[i,j].Quan := Trong;
              BanCo[0].OCo[i,j].Status := Trong;
              BanCo[0].Oco[i,j].NChoi := Trong;
          end;
      for i := 1 to 8 do
      begin
           if i <= 5 then
           BanCo[0].Oco[i,1].Quan := i
           else BanCo[0].Oco[i,1].Quan := 9-i;
           BanCo[0].OCo[i,1].NChoi := May;
           if i <= 5 then
           BanCo[0].Oco[i,8].Quan := i
           else BanCo[0].Oco[i,8].Quan := 9-i;
           BanCo[0].OCo[i,8].NChoi := Nguoi;
           for j := 1 to 8 do
           begin
                BanCo[0].OCo[i,2].Quan := Tot;
                BanCo[0].OCo[i,2].Nchoi := May;
                BanCo[0].OCo[i,7].Quan := Tot;
                BanCo[0].OCo[i,7].NChoi := Nguoi;
           end;
      end;
 End;

 Procedure KeyEvent;
 var k: char;
     x,y,d: integer;
 Begin
      repeat
      k := #0;
      if keypressed then k := readkey;
      case k of
      #72: if ky > 1 then
      begin
      BanCo[0].OCo[kx,ky].Status := Ls;Dec(ky);
      Ls := BanCo[0].OCo[kx,ky].Status;
      BanCo[0].OCo[kx,ky].Status := KeyIn;
      end;
      #80: if ky < 8 then
      begin
      BanCo[0].OCo[kx,ky].Status := Ls;Inc(ky);
      Ls := BanCo[0].OCo[kx,ky].Status;
      BanCo[0].OCo[kx,ky].Status := KeyIn;
      end;
      #75: if kx > 1 then
      begin
      BanCo[0].OCo[kx,ky].Status := Ls;Dec(kx);
      Ls := BanCo[0].OCo[kx,ky].Status;
      BanCo[0].OCo[kx,ky].Status := KeyIn;
      end;
      #77: if kx < 8 then
      begin
      BanCo[0].OCo[kx,ky].Status := Ls;Inc(kx);
      Ls := BanCo[0].OCo[kx,ky].Status;
      BanCo[0].OCo[kx,ky].Status := KeyIn;
      end;
             end;
      if k <> #0 then BanCo[0].InBanCo;
      x := kx;y := ky;
      if k = #13 then
      begin
           if (BanCo[0].OCo[x,y].Quan <> Trong) and
           (LS <> Chon)
           and (BanCo[0].OCo[x,y].NChoi = Nguoi) and not DaChon then
           begin
                BanCo[0].OCo[x,y].Status := Chon;
                LS := Chon;
                BanCo[0].ReSet(ChuanBi);
                BanCo[0].ReSet(XuatPhat);
                BanCo[0].ReSet(DiDen);
                DaChon := True;
                cx := x;
                cy := y;
                BanCo[0].DiChuyen(x,y);
                BanCo[0].InBanCo;
           end
           else
           if LS = Chon then
           begin
                BanCo[0].OCo[x,y].Status := Trong;
                LS := Trong;
                DaChon := False;
                BanCo[0].ReSet(ChuanBi);
                BanCo[0].InBanCo;
           end
           else if LS = ChuanBi then
           begin
                d := BanCo[0].DiCo(cx,cy,x,y);
                BanCo[0].Reset(ChuanBi);
                BanCo[0].Reset(XuatPhat);
                BanCo[0].Reset(DiDen);
                Dachon := False;
                BanCo[0].InBanCo;
                BanCo[0].PhongCap(x,y);
                BanCo[0].OCo[kx,ky].Status := KeyIn;
                if d <> diem[vua] then
                begin
                     TextColor(Red+Blink);TextBackground(Black);
                     Gotoxy(Size*8+4,5);write('Computer Thinking...');
                     d := AI.DiChuyenAI;
                     BanCo[0].InBanCo;
                     if d = Diem[Vua] then winer := May;
                     Gotoxy(Size*8+4,5);
                     textBackground(Black);
                     clreol;
                end
                else winer := Nguoi;
           end;
      end;
      until (k = #27) or (winer <> 0);
      if k <> #27 then
      begin
           Delay(2000);clrscr;Gotoxy(20,10);TextColor(Red);
           if winer = May then write('Y O U  L O S E')
           else write('Y O U   W I N');
           readln;
      end;
 End;

 BEGIN
      TextBackground(Black);
      Dachon := False;
      clrscr;
      KhoiTao;
      kx := 1;ky := 8;
      Ls := Trong;
      winer := 0;
      BanCo[0].OCo[kx,ky].Status :=  KeyIn;
      BanCo[0].InBanCo;
      KeyEvent;
 END.
-----------------------------------------------------------------------
Code này các bạn lưu dưới tên AICHESS.INC
Mã:
const
    Diem: array[Xe..Tot] of integer = (60,35,35,900,120,10);
    MVua: array[1..8,1..8] of real =
    ((-3,-3,-3,-3,-2,-1,2,2),(-4,-4,-4,-4,-3,-2,2,3),
     (-4,-4,-4,-4,-3,-2,0,1),(-5,-5,-5,-5,-4,-2,0,0),
     (-5,-5,-5,-5,-4,-2,0,0),(-4,-4,-4,-4,-3,-2,0,1),
     (-4,-4,-4,-4,-3,-2,2,3),(-3,-3,-3,-3,-2,-1,2,2));
   
    MHau: array[1..8,1..8] of real =
    ((-2,-1,-1,-0.5,0,-1,-1,-2),(-1,0,0,0,0,0.5,0,-1),
     (-1,0,0.5,0.5,0.5,0.5,0.5,-1),(-0.5,0,0.5,0.5,0.5,0.5,0,-0.5),
     (-0.5,0,0.5,0.5,0.5,0.5,0,-0.5),(-1,0,0.5,0.5,0.5,0.5,0,-1),
     (-1,0,0,0,0,0.5,0,-1),(-2,-1,-1,-0.5,0,-1,-1,-2));

    MXe: array[1..8,1..8] of real =
    ((0,0.5,-0.5,-0.5,-0.5,-0.5,-0.5,0),(0,1,0,0,0,0,0,0),
     (0,1,0,0,0,0,0,0),(0,1,0,0,0,0,0,0.5),
     (0,1,0,0,0,0,0,0.5),(0,1,0,0,0,0,0,0),
     (0,1,0,0,0,0,0,0),(0,0.5,-0.5,-0.5,-0.5,-0.5,-0.5,0));

    MTuong: array[1..8,1..8] of real =
    ((-2,-1,-1,-1,-1,-1,-1,-2),(-1,0,0,0.5,0,1,0.5,-1),
     (-1,0,0.5,0.5,1,1,0,-1),(-1,0,1,1,1,1,0,-1),
     (-1,0,1,1,1,1,0,-1),(-1,0,0.5,0.5,1,1,0,-1),
     (-1,0,0,0.5,0,1,0.5,-1),(-2,-1,-1,-1,-1,-1,-1,-2));

    MMa: array[1..8,1..8] of real =
    ((-5,-4,-3,-3,-3,-3,-4,-5),(-4,-2,0,0.5,0,0.5,-2,-4),
     (-3,0,1,1.5,1.5,1,0,-3),(-3,0,1.5,2,2,1.5,0.5,-3),
     (-3,0,1.5,2,2,1.5,0.5,-3),(-3,0,1,1.5,1.5,1,0,-3),
     (-4,-2,0,0.5,0,0.5,-2,-4),(-5,-4,-3,-3,-3,-3,-4,-5));

    MTot: array[1..8,1..8] of real =
    ((0,5,1,0.5,0,0.5,0.5,90),(0,5,1,0.5,0,0.5,0.5,90),
     (0,5,2,1,0,-1,1,90),(0,5,3,2.5,2,0,-2,90),
     (0,5,3,2.5,2,0,-2,90),(0,5,2,1,0,-1,1,90),
     (0,5,1,0.5,0,0.5,0.5,90),(0,5,1,0.5,0,0.5,0.5,90));
type TAI = object
    Function TinhDiem(x,y,quan: integer): real;
    Function DiCoAI(x,y,bc,soco,nc: integer;mx: real): real;
    Function ChuanBiAI(bc,nc: integer): real;
    Function DiChuyenAI: integer;
        end;
var dx1,dy1,dx0,dy0: longint;
    AI: TAI;
   
Function TAI.TinhDiem(x,y,quan: integer): real;
Begin
    case quan of
    Vua: Tinhdiem := MVua[x,y];
    Hau: Tinhdiem := MHau[x,y];
    Xe: Tinhdiem := MXe[x,y];
    Tuong: Tinhdiem := MTuong[x,y];
    Ma: Tinhdiem := MMa[x,y];
    Tot: Tinhdiem := MTot[x,y];
            end;
End;

Function TAI.DiCoAI(x,y,bc,soco,nc: integer;mx: real): real;
var b: TBanCo;
    i,j,k,quan: integer;
    d,max: real;
Begin
    max := -9999;
    k := 0;
    b := BanCo[bc];
    quan := b.OCo[x,y].Quan;
    for i := 1 to 8 do
        for j := 1 to 8 do
        if (BanCo[bc].OCo[i,j].Status = ChuanBi) then
        begin
            if nc = May then
            d := TinhDiem(i,j,quan)
            else d := TinhDiem(i,9-j,quan);
            d := d + BanCo[bc].DiCo(x,y,i,j);
            BanCo[bc].Reset(ChuanBi);
            if bc + 1 <= level then
            d := d - ChuanBiAI(bc+1,-nc);
            if d > max then
            begin
            max := d;
            if (bc = 1) and (max > mx) then
            begin
                dx1 := i;dy1 := j;
            end;
            end;
            BanCo[bc] := b;
            inc(k); if k >= soco then begin i:=8;j:=8;end;
        end;
    DiCoAI := max;
End;

Function TAI.ChuanBiAI(bc,nc: integer): real;
var i,j,cb: integer;
    d,max: real;
Begin
    BanCo[bc] := BanCo[bc-1];
    BanCo[bc].Reset(ChuanBi);
    max := -9999;
    for i := 1 to 8 do
        for j := 1 to 8 do
        begin
            if BanCo[bc].OCo[i,j].NChoi = nc then
            begin
            cb := BanCo[bc].DiChuyen(i,j);
            if cb > 0 then
                        begin
            d := DiCoAI(i,j,bc,cb,nc,max);
            if d > max then
            begin
                max := d;
                if bc = 1 then
                begin
                    dx0 := i;dy0 := j;
                end;
            end;
            end;
            BanCo[bc].Reset(ChuanBi);
                        end;
        end;
    ChuanBiAI := max;
End;

Function TAI.DiChuyenAI: integer;
var dc: integer;
Begin
    ChuanBiAI(1,May);
    dc := BanCo[0].DiCo(dx0,dy0,dx1,dy1);
    BanCo[0].PhongCap(dx1,dy1);
    DiChuyenAI := dc;
End;
-----------------------------------------------------------------------
Code này các bạn lưu dưới tên DCCHESS.INC
Mã:
 Function TBanCo.DiVua(x,y,NC: integer): integer;
 var dem: integer;
 Begin
     dem := 0;
      if ((x > 1) and (y > 1)) and (OCo[x-1,y-1].NChoi <> NC)
      then begin OCo[x-1,y-1].Status := ChuanBi;inc(dem);end;
      if ((y > 1)) and (OCo[x,y-1].NChoi <> NC)
      then begin OCo[x,y-1].Status := ChuanBi;inc(dem);end;
      if ((x < 8) and (y > 1)) and (OCo[x+1,y-1].NChoi <> NC)
      then begin OCo[x+1,y-1].Status := ChuanBi;inc(dem);end;
      if ((x > 1) and (y < 8)) and (OCo[x-1,y+1].NChoi <> NC)
      then begin OCo[x-1,y+1].Status := ChuanBi;inc(dem);end;
      if ((y < 8)) and (OCo[x,y+1].NChoi <> NC)
      then begin OCo[x,y+1].Status := ChuanBi;inc(dem);end;
      if ((x < 8) and (y < 8)) and (OCo[x+1,y+1].NChoi <> NC)
      then begin OCo[x+1,y+1].Status := ChuanBi;inc(dem);end;
      if ((x > 1)) and (OCo[x-1,y].NChoi <> NC)
      then begin OCo[x-1,y].Status := ChuanBi;inc(dem);end;
      if ((x < 8)) and (OCo[x+1,y].NChoi <> NC)
      then begin OCo[x+1,y].Status := ChuanBi;inc(dem);end;
    DiVua := Dem;
 End;

 Function TBanCo.DiHau(x,y,NC: integer): integer;
 var i,j,dem: integer;
 Begin
    dem:=0;
      i:=x;j:=y;
      while (i > 1) and (j > 1) do
      begin
           Dec(i);Dec(j);
           if OCo[i,j].NChoi = Trong then
        begin
                   OCo[i,j].Status := ChuanBi;
            inc(dem);
        end
           else
           begin
                if OCo[i,j].NChoi <> NC then OCo[i,j].Status := ChuanBi;
        inc(dem);
                i := 1;
           end;
      end;
      i:=x;j:=y;
      while (i < 8) and (j > 1) do
      begin
           Inc(i);Dec(j);
           if OCo[i,j].NChoi = Trong then
        begin
                   OCo[i,j].Status := ChuanBi;
            inc(dem);
        end
           else
           begin
                if OCo[i,j].NChoi <> NC then OCo[i,j].Status := ChuanBi;
        inc(dem);
                i := 8;
           end;
      end;
      i:=x;j:=y;
      while (i > 1) and (j < 8) do
      begin
           Dec(i);Inc(j);
           if OCo[i,j].NChoi = Trong then
        begin
                   OCo[i,j].Status := ChuanBi;
            inc(dem);
        end
           else
           begin
                if OCo[i,j].NChoi <> NC then OCo[i,j].Status := ChuanBi;
        inc(dem);
                i := 1;
           end;
      end;
      i:=x;j:=y;
      while (i < 8) and (j < 8) do
      begin
           Inc(i);Inc(j);
           if OCo[i,j].NChoi = Trong then
        begin
                   OCo[i,j].Status := ChuanBi;inc(dem);
        end
           else
           begin
                if OCo[i,j].NChoi <> NC then OCo[i,j].Status := ChuanBi;
        inc(dem);
                i := 8;
           end;
      end;

      for i := x-1 downto 1 do
          if OCo[i,y].Quan = Trong then
        begin OCo[i,y].Status := ChuanBi;inc(dem);end
          else if OCo[i,y].NChoi <> NC then
               begin
                    OCo[i,y].Status := ChuanBi;
            inc(dem);
                    break;
                    end
               else if OCo[i,y].NChoi = NC then break;
      for i := x+1 to 8 do
          if OCo[i,y].Quan = Trong then
        begin OCo[i,y].Status := ChuanBi;inc(dem);end
          else if OCo[i,y].NChoi <> NC then
               begin
                    OCo[i,y].Status := ChuanBi;
            inc(dem);
                    break;
                    end
               else if OCo[i,y].NChoi = NC then break;
      for i := y-1 downto 1 do
          if OCo[x,i].Quan = Trong then
        begin OCo[x,i].Status := ChuanBi;inc(dem);end
          else if OCo[x,i].NChoi <> NC then
               begin
                    OCo[x,i].Status := ChuanBi;
            inc(dem);
                    break;
                    end
               else if OCo[x,i].NChoi = NC then break;
      for i := y+1 to 8 do
          if OCo[x,i].Quan = Trong then
        begin OCo[x,i].Status := ChuanBi;inc(dem);end
          else if OCo[x,i].NChoi <> NC then
               begin
                    OCo[x,i].Status := ChuanBi;
            inc(dem);
                    break;
                    end
               else if OCo[x,i].NChoi = NC then break;
    DiHau := Dem;
 End;

 Function TBanCo.DiXe(x,y,NC: integer): integer;
 var i,dem: integer;
 Begin
    dem := 0;
      for i := x-1 downto 1 do
          if OCo[i,y].Quan = Trong then
        begin OCo[i,y].Status := ChuanBi;inc(dem);end
          else if OCo[i,y].NChoi <> NC then
               begin
                    OCo[i,y].Status := ChuanBi;
            inc(dem);
                    break;
                    end
               else if OCo[i,y].NChoi = NC then break;
      for i := x+1 to 8 do
          if OCo[i,y].Quan = Trong then
        begin OCo[i,y].Status := ChuanBi;inc(dem);end
          else if OCo[i,y].NChoi <> NC then
               begin
                    OCo[i,y].Status := ChuanBi;
            inc(dem);
                    break;
                    end
               else if OCo[i,y].NChoi = NC then break;
      for i := y-1 downto 1 do
          if OCo[x,i].Quan = Trong then
        begin OCo[x,i].Status := ChuanBi;inc(dem);end
          else if OCo[x,i].NChoi <> NC then
               begin
                    OCo[x,i].Status := ChuanBi;
            inc(dem);
                    break;
                    end
               else if OCo[x,i].NChoi = NC then break;
      for i := y+1 to 8 do
          if OCo[x,i].Quan = Trong then
        begin OCo[x,i].Status := ChuanBi;inc(dem);end
          else if OCo[x,i].NChoi <> NC then
               begin
                    OCo[x,i].Status := ChuanBi;
            inc(dem);
                    break;
                    end
               else if OCo[x,i].NChoi = NC then break;
    DiXe := Dem;
 End;

 Function TBanCo.DiMa(x,y,NC: integer): integer;
 var dem: integer;
 Begin
      dem := 0;
      if (x > 2) and (y > 1) then
      if (OCo[x-2,y-1].NChoi <> NC) then
    begin OCo[x-2,y-1].Status := ChuanBi;inc(dem);end;
      if (x > 2) and (y < 8) then
      if (OCo[x-2,y+1].NChoi <> NC) then
    begin OCo[x-2,y+1].Status := ChuanBi;inc(dem);end;
      if (x < 7) and (y > 1) then
      if (OCo[x+2,y-1].NChoi <> NC) then
    begin OCo[x+2,y-1].Status := ChuanBi;inc(dem);end;
      if (x < 7) and (y < 8) then
      if (OCo[x+2,y+1].NChoi <> NC) then
    begin OCo[x+2,y+1].Status := ChuanBi;inc(dem);end;

      if (x > 1) and (y > 2) then
      if (OCo[x-1,y-2].NChoi <> NC) then
    begin OCo[x-1,y-2].Status := ChuanBi;inc(dem);end;
      if (x > 1) and (y < 7) then
      if (OCo[x-1,y+2].NChoi <> NC) then
    begin OCo[x-1,y+2].Status := ChuanBi;inc(dem);end;
      if (x < 8) and (y > 2) then
      if (OCo[x+1,y-2].NChoi <> NC) then
    begin OCo[x+1,y-2].Status := ChuanBi;inc(dem);end;
      if (x < 8) and (y < 7) then
      if (OCo[x+1,y+2].NChoi <> NC) then
    begin OCo[x+1,y+2].Status := ChuanBi;inc(dem);end;
    DiMa := Dem;
 End;

 Function TBanCo.DiTuong(x,y,NC: integer): integer;
 var i,j,dem: integer;
 Begin
    dem := 0;
      i:=x;j:=y;
      while (i > 1) and (j > 1) do
      begin
           Dec(i);Dec(j);
           if OCo[i,j].NChoi = Trong then
        begin
                   OCo[i,j].Status := ChuanBi;
            inc(dem);
        end
           else
           begin
                if OCo[i,j].NChoi <> NC then OCo[i,j].Status := ChuanBi;
        inc(dem);
                i := 1;
           end;
      end;
      i:=x;j:=y;
      while (i < 8) and (j > 1) do
      begin
           Inc(i);Dec(j);
           if OCo[i,j].NChoi = Trong then
        begin
                   OCo[i,j].Status := ChuanBi;
            inc(dem);
        end
           else
           begin
                if OCo[i,j].NChoi <> NC then OCo[i,j].Status := ChuanBi;
        inc(dem);
                i := 8;
           end;
      end;
      i:=x;j:=y;
      while (i > 1) and (j < 8) do
      begin
           Dec(i);Inc(j);
           if OCo[i,j].NChoi = Trong then
        begin
                   OCo[i,j].Status := ChuanBi;
            inc(dem);
        end
           else
           begin
                if OCo[i,j].NChoi <> NC then OCo[i,j].Status := ChuanBi;
        inc(dem);
                i := 1;
           end;
      end;
      i:=x;j:=y;
      while (i < 8) and (j < 8) do
      begin
           Inc(i);Inc(j);
           if OCo[i,j].NChoi = Trong then
        begin
                   OCo[i,j].Status := ChuanBi;inc(dem);
        end
           else
           begin
                if OCo[i,j].NChoi <> NC then OCo[i,j].Status := ChuanBi;
        inc(dem);
                i := 8;
           end;
      end;
    DiTuong := Dem;
 End;

 Function TBanCo.DiTot(x,y,NC: integer): integer;
 var dem: integer;
 Begin
    dem := 0;
      if OCo[x,y+NC].NChoi = Trong then
      begin
           OCo[x,y+NC].Status := ChuanBi;inc(dem);
           if ((y = 2) or (y = 7)) and (OCo[x,y+NC*2].NChoi = Trong) then
           begin OCo[x,y+NC*2].Status := ChuanBi;inc(dem);end;
      end;
      if (OCo[x-1,y+NC].NChoi <> NC) and
      (OCo[x-1,y+NC].NChoi <> Trong) then
    begin OCo[x-1,y+NC].Status := ChuanBi;inc(dem);end;
      if (OCo[x+1,y+NC].NChoi <> NC) and
      (OCo[x+1,y+NC].NChoi <> Trong) then
    begin OCo[x+1,y+NC].Status := ChuanBi;inc(dem);end;
    DiTot := Dem;
 End;

 Function TBanCo.DiChuyen(x,y: integer): integer;
 var NC: integer;
 Begin
      NC := OCo[x,y].NChoi;
      case OCo[x,y].quan of
      Vua: DiChuyen := DiVua(x,y,NC);
      Hau: DiChuyen := DiHau(x,y,NC);
      Xe: DiChuyen := DiXe(x,y,NC);
      Ma: DiChuyen := DiMa(x,y,NC);
      Tuong: DiChuyen := DiTuong(x,y,NC);
      Tot: DiChuyen := DiTot(x,y,NC);
                end;
 End;

Function TBanCo.DiCo(x1,y1,x2,y2: integer): integer;
var d: integer;
Begin
    d := 0;
    if OCo[x2,y2].Quan <> Trong then d := Diem[OCo[x2,y2].Quan];
    OCo[x2,y2].Quan := OCo[x1,y1].Quan;
    OCo[x2,y2].NChoi := OCo[x1,y1].Nchoi;
    OCo[x2,y2].Status := DiDen;
    OCo[x1,y1].Quan := Trong;
    OCo[x1,y1].NChoi := Trong;
    OCo[x1,y1].Status := XuatPhat;
    DiCo := d;
End;
-----------------------------------------------------------------------
Code này các bạn lưu dưới tên VEQUAN.INC
Mã:
Procedure TBanCo.VeVua(x,y: integer);
Begin
    gotoxy((x-1)*Size+4,(y-1)*(Size div 2)+1);
    write(#95#22#254#22#95);
    gotoxy((x-1)*Size+4,(y-1)*(Size div 2)+2);
    write(#92#30#30#30#47);
    gotoxy((x-1)*Size+4,(y-1)*(Size div 2)+3);
    write(#223#223#223#223#223);
End;

Procedure TBanCo.VeHau(x,y: integer);
Begin
    gotoxy((x-1)*Size+4,(y-1)*(Size div 2)+1);
    write(#92#30#179#30#47);
    gotoxy((x-1)*Size+4,(y-1)*(Size div 2)+2);
    write(#32#219#219#219);
    gotoxy((x-1)*Size+4,(y-1)*(Size div 2)+3);
    write(#223#223#223#223#223);
End;

Procedure TBanCo.VeXe(x,y: integer);
Begin
    gotoxy((x-1)*Size+4,(y-1)*(Size div 2)+1);
    write(#219#32#219#32#219);
    gotoxy((x-1)*Size+4,(y-1)*(Size div 2)+2);
    write(#32#219#219#219);
    gotoxy((x-1)*Size+4,(y-1)*(Size div 2)+3);
    write(#223#223#223#223#223);
End;

Procedure TBanCo.VeMa(x,y: integer);
Begin
    gotoxy((x-1)*Size+4,(y-1)*(Size div 2)+1);
    write(#222#223#219#219);
    gotoxy((x-1)*Size+4,(y-1)*(Size div 2)+2);
    write(#32#222#219#219);
    gotoxy((x-1)*Size+4,(y-1)*(Size div 2)+3);
    write(#223#223#223#223#223);
End;

Procedure TBanCo.VeTuong(x,y: integer);
Begin
    gotoxy((x-1)*Size+4,(y-1)*(Size div 2)+1);
    write(#32#32#234);
    gotoxy((x-1)*Size+4,(y-1)*(Size div 2)+2);
    write(#32#219#254#219);
    gotoxy((x-1)*Size+4,(y-1)*(Size div 2)+3);
    write(#223#223#223#223#223);
End;

Procedure TBanCo.VeTot(x,y: integer);
Begin
    gotoxy((x-1)*Size+4,(y-1)*(Size div 2)+1);
    write(#32+#32+#219);
    gotoxy((x-1)*Size+4,(y-1)*(Size div 2)+2);
    write(#32#219#219#219);
    gotoxy((x-1)*Size+4,(y-1)*(Size div 2)+3);
    write(#223#223#223#223#223);
End;

Procedure TBanCo.VeCo(x,y: integer);
var quan: integer;
Begin
    quan := OCo[x,y].Quan;
    case Quan of
    Vua: VeVua(x,y);
    Hau: VeHau(x,y);
    Xe: VeXe(x,y);
    tuong: VeTuong(x,y);
    Ma: VeMa(x,y);
    Tot: VeTot(x,y);
            end;
End;

Các bạn có thể thay đổi level để tăng độ thông minh cho máy, nhưng level càng cáo nó chạy càng lâu nha.
Máy mình chạy level 2 là ok, còn hơn thì chạy không nổi.
View attachment 103444
Đỉnh quá đi anh ơi............!:MIM19:Rabbit27:Rabbit25
 

Deathheart

Cựu TMod Vật Lí
Thành viên
18 Tháng năm 2018
1,535
2,868
411
Quảng Trị
THPT Đông Hà
dùng lệnh nếu....thì....làm thêm 10 tỉ năm nữa nó cũng chơi k hay đc :D
Nếu theo cái trò chơi chess offline người ta làm theo kiểu duyệt (đoán trước) số bước đi của mình và địch từ đó chọn phương án tốt nhất mà đi. Vậy nên độ khó càng cao máy tốn nhiều thời gian hơn. Mà để xây dựng cái này trong pascal thì...
 
  • Like
Reactions: ~ Su Nấm ~

lâm tùng apollo

Cựu Mod Địa | PCN CLB Địa Lí
Thành viên
26 Tháng ba 2018
1,170
3,213
371
19
Shurima
Thái Nguyên
THPT LƯƠNG NGỌC QUYẾN
View attachment 103442
Game này mình phải bỏ cả tuần mới viết được đó nha
Code này hơi rắc rối nên mình chia ra thành các file nhỏ, các bạn lưu theo hướng dẫn nha, nếu không thì Code không chạy được đâu

Code này các bạn lưu dưới tên CHESSAI.PAS
Mã:
 Program ChessAI;
 uses crt,dos;
 const
      Size = 7;
      Level = 2;

      Nguoi = -1;
      May = 1;

      Chon = 1;
      ChuanBi = 2;
      XuatPhat = 3;
      DiDen = 4;
      KeyIn = 5;

      Trong = 0;
      Xe = 1;
      Ma = 2;
      Tuong = 3;
      Vua = 4;
      Hau = 5;
      Tot = 6;

 type TOCo = object
      Quan: integer;
      NChoi: integer;
      Status: integer;
             end;

 type TBanCo = object
      OCo: array[1..8,1..8] of TOCo;
      Procedure InBanCo;
      Procedure Reset(stt: integer);
      Function DiVua(x,y,NC: integer): integer;
      Function DiHau(x,y,NC: integer): integer;
      Function DiXe(x,y,NC: integer): integer;
      Function DiMa(x,y,NC: integer): integer;
      Function DiTuong(x,y,NC: integer): integer;
      Function DiTot(x,y,NC: integer): integer;
      Function DiChuyen(x,y: integer): integer;
      Function DiCo(x1,y1,x2,y2: integer): integer;
      Procedure VeVua(x,y: integer);
      Procedure VeHau(x,y: integer);
      Procedure VeXe(x,y: integer);
      Procedure VeMa(x,y: integer);
      Procedure VeTuong(x,y: integer);
      Procedure VeTot(x,y: integer);
      Procedure VeCo(x,y: integer);
      Procedure PhongCap(x,y: integer);
               end;
 var
    BanCo: array[0..Level] of TBanCo;
    Dachon: Boolean;
    cx,cy,kx,ky: integer;
    ls,winer: integer;

 procedure setcusor(bot,top: byte);
 var regs : registers;
 begin
 regs.ah :=1;
 regs.ch:=bot;
 regs.cl:=top;
 intr($10,regs);
 end;

 {$I VEQUAN.INC}

 Procedure TBanCo.InBanCo;
 var i,j,x,y: integer;
 Begin
      Textbackground(Black);
      Setcusor(32,0);
      for x := 1 to 8 do
          for y := 1 to 8 do
          begin
               if (x+y) mod 2 = 0 then textBackground(White)
               else textBackground(Red);
               if (OCo[x,y].Status = Chon) or (OCo[x,y].Status = XuatPhat)
               then textBackground(Cyan)
               else
               if (OCo[x,y].Status = Chuanbi) or (OCo[x,y].Status = DiDen)
               then textBackground(13)
               else
               if (OCo[x,y].Status = KeyIn) then TextBackground(Blue);
               for i := 1 to Size do
                   for j := 1 to size div 2 do
                   begin
                   gotoxy((x-1)*size+i+2,(y-1)*(size div 2) +j);
                   write(' ');
                   end;
          if OCo[x,y].NChoi = Nguoi then
          Textcolor(Green) else TextColor(Black);
          VeCo(x,y);
          end;
 End;

 Procedure TBanCo.Reset(stt: integer);
 var i,j: integer;
 Begin
      for i := 1 to 8 do
          for j := 1 to 8 do
          if OCo[i,j].Status = stt then
             OCo[i,j].Status := Trong;
 End;

 Procedure TBanCo.PhongCap(x,y: integer);
 var q: integer;
 Begin
      if OCo[x,y].Quan = Tot then
      begin
           if (OCo[x,y].NChoi = May) and (y = 8) then
           begin
                OCo[x,y].Quan := Hau;
                clrscr;
                InBanCo;
           end
           else
           if (OCo[x,y].NChoi = Nguoi) and (y = 1) then
           begin
                TextBackGround(Black);
                clrscr;
                TextColor(LightRed);
                Gotoxy(2,2);Write('1. Xe');
                Gotoxy(2,3);Write('2. Ma');
                Gotoxy(2,4);Write('3. Tuong');
                Gotoxy(2,5);Write('4. Hau');
                Gotoxy(2,6);
                repeat readln(q);until q in [1..4];
                if q = 4 then q := Hau;
                OCo[x,y].Quan := q;
                clrscr;
                InBanCo;
           end;
      end;
 End;
 {$I AICHESS.INC}
 {$I DCCHESS.INC}

 Procedure KhoiTao;
 var i,j: integer;
 Begin
      for i := 1 to 8 do
          for j := 1 to 8 do
          begin
              BanCo[0].OCo[i,j].Quan := Trong;
              BanCo[0].OCo[i,j].Status := Trong;
              BanCo[0].Oco[i,j].NChoi := Trong;
          end;
      for i := 1 to 8 do
      begin
           if i <= 5 then
           BanCo[0].Oco[i,1].Quan := i
           else BanCo[0].Oco[i,1].Quan := 9-i;
           BanCo[0].OCo[i,1].NChoi := May;
           if i <= 5 then
           BanCo[0].Oco[i,8].Quan := i
           else BanCo[0].Oco[i,8].Quan := 9-i;
           BanCo[0].OCo[i,8].NChoi := Nguoi;
           for j := 1 to 8 do
           begin
                BanCo[0].OCo[i,2].Quan := Tot;
                BanCo[0].OCo[i,2].Nchoi := May;
                BanCo[0].OCo[i,7].Quan := Tot;
                BanCo[0].OCo[i,7].NChoi := Nguoi;
           end;
      end;
 End;

 Procedure KeyEvent;
 var k: char;
     x,y,d: integer;
 Begin
      repeat
      k := #0;
      if keypressed then k := readkey;
      case k of
      #72: if ky > 1 then
      begin
      BanCo[0].OCo[kx,ky].Status := Ls;Dec(ky);
      Ls := BanCo[0].OCo[kx,ky].Status;
      BanCo[0].OCo[kx,ky].Status := KeyIn;
      end;
      #80: if ky < 8 then
      begin
      BanCo[0].OCo[kx,ky].Status := Ls;Inc(ky);
      Ls := BanCo[0].OCo[kx,ky].Status;
      BanCo[0].OCo[kx,ky].Status := KeyIn;
      end;
      #75: if kx > 1 then
      begin
      BanCo[0].OCo[kx,ky].Status := Ls;Dec(kx);
      Ls := BanCo[0].OCo[kx,ky].Status;
      BanCo[0].OCo[kx,ky].Status := KeyIn;
      end;
      #77: if kx < 8 then
      begin
      BanCo[0].OCo[kx,ky].Status := Ls;Inc(kx);
      Ls := BanCo[0].OCo[kx,ky].Status;
      BanCo[0].OCo[kx,ky].Status := KeyIn;
      end;
             end;
      if k <> #0 then BanCo[0].InBanCo;
      x := kx;y := ky;
      if k = #13 then
      begin
           if (BanCo[0].OCo[x,y].Quan <> Trong) and
           (LS <> Chon)
           and (BanCo[0].OCo[x,y].NChoi = Nguoi) and not DaChon then
           begin
                BanCo[0].OCo[x,y].Status := Chon;
                LS := Chon;
                BanCo[0].ReSet(ChuanBi);
                BanCo[0].ReSet(XuatPhat);
                BanCo[0].ReSet(DiDen);
                DaChon := True;
                cx := x;
                cy := y;
                BanCo[0].DiChuyen(x,y);
                BanCo[0].InBanCo;
           end
           else
           if LS = Chon then
           begin
                BanCo[0].OCo[x,y].Status := Trong;
                LS := Trong;
                DaChon := False;
                BanCo[0].ReSet(ChuanBi);
                BanCo[0].InBanCo;
           end
           else if LS = ChuanBi then
           begin
                d := BanCo[0].DiCo(cx,cy,x,y);
                BanCo[0].Reset(ChuanBi);
                BanCo[0].Reset(XuatPhat);
                BanCo[0].Reset(DiDen);
                Dachon := False;
                BanCo[0].InBanCo;
                BanCo[0].PhongCap(x,y);
                BanCo[0].OCo[kx,ky].Status := KeyIn;
                if d <> diem[vua] then
                begin
                     TextColor(Red+Blink);TextBackground(Black);
                     Gotoxy(Size*8+4,5);write('Computer Thinking...');
                     d := AI.DiChuyenAI;
                     BanCo[0].InBanCo;
                     if d = Diem[Vua] then winer := May;
                     Gotoxy(Size*8+4,5);
                     textBackground(Black);
                     clreol;
                end
                else winer := Nguoi;
           end;
      end;
      until (k = #27) or (winer <> 0);
      if k <> #27 then
      begin
           Delay(2000);clrscr;Gotoxy(20,10);TextColor(Red);
           if winer = May then write('Y O U  L O S E')
           else write('Y O U   W I N');
           readln;
      end;
 End;

 BEGIN
      TextBackground(Black);
      Dachon := False;
      clrscr;
      KhoiTao;
      kx := 1;ky := 8;
      Ls := Trong;
      winer := 0;
      BanCo[0].OCo[kx,ky].Status :=  KeyIn;
      BanCo[0].InBanCo;
      KeyEvent;
 END.
-----------------------------------------------------------------------
Code này các bạn lưu dưới tên AICHESS.INC
Mã:
const
    Diem: array[Xe..Tot] of integer = (60,35,35,900,120,10);
    MVua: array[1..8,1..8] of real =
    ((-3,-3,-3,-3,-2,-1,2,2),(-4,-4,-4,-4,-3,-2,2,3),
     (-4,-4,-4,-4,-3,-2,0,1),(-5,-5,-5,-5,-4,-2,0,0),
     (-5,-5,-5,-5,-4,-2,0,0),(-4,-4,-4,-4,-3,-2,0,1),
     (-4,-4,-4,-4,-3,-2,2,3),(-3,-3,-3,-3,-2,-1,2,2));
   
    MHau: array[1..8,1..8] of real =
    ((-2,-1,-1,-0.5,0,-1,-1,-2),(-1,0,0,0,0,0.5,0,-1),
     (-1,0,0.5,0.5,0.5,0.5,0.5,-1),(-0.5,0,0.5,0.5,0.5,0.5,0,-0.5),
     (-0.5,0,0.5,0.5,0.5,0.5,0,-0.5),(-1,0,0.5,0.5,0.5,0.5,0,-1),
     (-1,0,0,0,0,0.5,0,-1),(-2,-1,-1,-0.5,0,-1,-1,-2));

    MXe: array[1..8,1..8] of real =
    ((0,0.5,-0.5,-0.5,-0.5,-0.5,-0.5,0),(0,1,0,0,0,0,0,0),
     (0,1,0,0,0,0,0,0),(0,1,0,0,0,0,0,0.5),
     (0,1,0,0,0,0,0,0.5),(0,1,0,0,0,0,0,0),
     (0,1,0,0,0,0,0,0),(0,0.5,-0.5,-0.5,-0.5,-0.5,-0.5,0));

    MTuong: array[1..8,1..8] of real =
    ((-2,-1,-1,-1,-1,-1,-1,-2),(-1,0,0,0.5,0,1,0.5,-1),
     (-1,0,0.5,0.5,1,1,0,-1),(-1,0,1,1,1,1,0,-1),
     (-1,0,1,1,1,1,0,-1),(-1,0,0.5,0.5,1,1,0,-1),
     (-1,0,0,0.5,0,1,0.5,-1),(-2,-1,-1,-1,-1,-1,-1,-2));

    MMa: array[1..8,1..8] of real =
    ((-5,-4,-3,-3,-3,-3,-4,-5),(-4,-2,0,0.5,0,0.5,-2,-4),
     (-3,0,1,1.5,1.5,1,0,-3),(-3,0,1.5,2,2,1.5,0.5,-3),
     (-3,0,1.5,2,2,1.5,0.5,-3),(-3,0,1,1.5,1.5,1,0,-3),
     (-4,-2,0,0.5,0,0.5,-2,-4),(-5,-4,-3,-3,-3,-3,-4,-5));

    MTot: array[1..8,1..8] of real =
    ((0,5,1,0.5,0,0.5,0.5,90),(0,5,1,0.5,0,0.5,0.5,90),
     (0,5,2,1,0,-1,1,90),(0,5,3,2.5,2,0,-2,90),
     (0,5,3,2.5,2,0,-2,90),(0,5,2,1,0,-1,1,90),
     (0,5,1,0.5,0,0.5,0.5,90),(0,5,1,0.5,0,0.5,0.5,90));
type TAI = object
    Function TinhDiem(x,y,quan: integer): real;
    Function DiCoAI(x,y,bc,soco,nc: integer;mx: real): real;
    Function ChuanBiAI(bc,nc: integer): real;
    Function DiChuyenAI: integer;
        end;
var dx1,dy1,dx0,dy0: longint;
    AI: TAI;
   
Function TAI.TinhDiem(x,y,quan: integer): real;
Begin
    case quan of
    Vua: Tinhdiem := MVua[x,y];
    Hau: Tinhdiem := MHau[x,y];
    Xe: Tinhdiem := MXe[x,y];
    Tuong: Tinhdiem := MTuong[x,y];
    Ma: Tinhdiem := MMa[x,y];
    Tot: Tinhdiem := MTot[x,y];
            end;
End;

Function TAI.DiCoAI(x,y,bc,soco,nc: integer;mx: real): real;
var b: TBanCo;
    i,j,k,quan: integer;
    d,max: real;
Begin
    max := -9999;
    k := 0;
    b := BanCo[bc];
    quan := b.OCo[x,y].Quan;
    for i := 1 to 8 do
        for j := 1 to 8 do
        if (BanCo[bc].OCo[i,j].Status = ChuanBi) then
        begin
            if nc = May then
            d := TinhDiem(i,j,quan)
            else d := TinhDiem(i,9-j,quan);
            d := d + BanCo[bc].DiCo(x,y,i,j);
            BanCo[bc].Reset(ChuanBi);
            if bc + 1 <= level then
            d := d - ChuanBiAI(bc+1,-nc);
            if d > max then
            begin
            max := d;
            if (bc = 1) and (max > mx) then
            begin
                dx1 := i;dy1 := j;
            end;
            end;
            BanCo[bc] := b;
            inc(k); if k >= soco then begin i:=8;j:=8;end;
        end;
    DiCoAI := max;
End;

Function TAI.ChuanBiAI(bc,nc: integer): real;
var i,j,cb: integer;
    d,max: real;
Begin
    BanCo[bc] := BanCo[bc-1];
    BanCo[bc].Reset(ChuanBi);
    max := -9999;
    for i := 1 to 8 do
        for j := 1 to 8 do
        begin
            if BanCo[bc].OCo[i,j].NChoi = nc then
            begin
            cb := BanCo[bc].DiChuyen(i,j);
            if cb > 0 then
                        begin
            d := DiCoAI(i,j,bc,cb,nc,max);
            if d > max then
            begin
                max := d;
                if bc = 1 then
                begin
                    dx0 := i;dy0 := j;
                end;
            end;
            end;
            BanCo[bc].Reset(ChuanBi);
                        end;
        end;
    ChuanBiAI := max;
End;

Function TAI.DiChuyenAI: integer;
var dc: integer;
Begin
    ChuanBiAI(1,May);
    dc := BanCo[0].DiCo(dx0,dy0,dx1,dy1);
    BanCo[0].PhongCap(dx1,dy1);
    DiChuyenAI := dc;
End;
-----------------------------------------------------------------------
Code này các bạn lưu dưới tên DCCHESS.INC
Mã:
 Function TBanCo.DiVua(x,y,NC: integer): integer;
 var dem: integer;
 Begin
     dem := 0;
      if ((x > 1) and (y > 1)) and (OCo[x-1,y-1].NChoi <> NC)
      then begin OCo[x-1,y-1].Status := ChuanBi;inc(dem);end;
      if ((y > 1)) and (OCo[x,y-1].NChoi <> NC)
      then begin OCo[x,y-1].Status := ChuanBi;inc(dem);end;
      if ((x < 8) and (y > 1)) and (OCo[x+1,y-1].NChoi <> NC)
      then begin OCo[x+1,y-1].Status := ChuanBi;inc(dem);end;
      if ((x > 1) and (y < 8)) and (OCo[x-1,y+1].NChoi <> NC)
      then begin OCo[x-1,y+1].Status := ChuanBi;inc(dem);end;
      if ((y < 8)) and (OCo[x,y+1].NChoi <> NC)
      then begin OCo[x,y+1].Status := ChuanBi;inc(dem);end;
      if ((x < 8) and (y < 8)) and (OCo[x+1,y+1].NChoi <> NC)
      then begin OCo[x+1,y+1].Status := ChuanBi;inc(dem);end;
      if ((x > 1)) and (OCo[x-1,y].NChoi <> NC)
      then begin OCo[x-1,y].Status := ChuanBi;inc(dem);end;
      if ((x < 8)) and (OCo[x+1,y].NChoi <> NC)
      then begin OCo[x+1,y].Status := ChuanBi;inc(dem);end;
    DiVua := Dem;
 End;

 Function TBanCo.DiHau(x,y,NC: integer): integer;
 var i,j,dem: integer;
 Begin
    dem:=0;
      i:=x;j:=y;
      while (i > 1) and (j > 1) do
      begin
           Dec(i);Dec(j);
           if OCo[i,j].NChoi = Trong then
        begin
                   OCo[i,j].Status := ChuanBi;
            inc(dem);
        end
           else
           begin
                if OCo[i,j].NChoi <> NC then OCo[i,j].Status := ChuanBi;
        inc(dem);
                i := 1;
           end;
      end;
      i:=x;j:=y;
      while (i < 8) and (j > 1) do
      begin
           Inc(i);Dec(j);
           if OCo[i,j].NChoi = Trong then
        begin
                   OCo[i,j].Status := ChuanBi;
            inc(dem);
        end
           else
           begin
                if OCo[i,j].NChoi <> NC then OCo[i,j].Status := ChuanBi;
        inc(dem);
                i := 8;
           end;
      end;
      i:=x;j:=y;
      while (i > 1) and (j < 8) do
      begin
           Dec(i);Inc(j);
           if OCo[i,j].NChoi = Trong then
        begin
                   OCo[i,j].Status := ChuanBi;
            inc(dem);
        end
           else
           begin
                if OCo[i,j].NChoi <> NC then OCo[i,j].Status := ChuanBi;
        inc(dem);
                i := 1;
           end;
      end;
      i:=x;j:=y;
      while (i < 8) and (j < 8) do
      begin
           Inc(i);Inc(j);
           if OCo[i,j].NChoi = Trong then
        begin
                   OCo[i,j].Status := ChuanBi;inc(dem);
        end
           else
           begin
                if OCo[i,j].NChoi <> NC then OCo[i,j].Status := ChuanBi;
        inc(dem);
                i := 8;
           end;
      end;

      for i := x-1 downto 1 do
          if OCo[i,y].Quan = Trong then
        begin OCo[i,y].Status := ChuanBi;inc(dem);end
          else if OCo[i,y].NChoi <> NC then
               begin
                    OCo[i,y].Status := ChuanBi;
            inc(dem);
                    break;
                    end
               else if OCo[i,y].NChoi = NC then break;
      for i := x+1 to 8 do
          if OCo[i,y].Quan = Trong then
        begin OCo[i,y].Status := ChuanBi;inc(dem);end
          else if OCo[i,y].NChoi <> NC then
               begin
                    OCo[i,y].Status := ChuanBi;
            inc(dem);
                    break;
                    end
               else if OCo[i,y].NChoi = NC then break;
      for i := y-1 downto 1 do
          if OCo[x,i].Quan = Trong then
        begin OCo[x,i].Status := ChuanBi;inc(dem);end
          else if OCo[x,i].NChoi <> NC then
               begin
                    OCo[x,i].Status := ChuanBi;
            inc(dem);
                    break;
                    end
               else if OCo[x,i].NChoi = NC then break;
      for i := y+1 to 8 do
          if OCo[x,i].Quan = Trong then
        begin OCo[x,i].Status := ChuanBi;inc(dem);end
          else if OCo[x,i].NChoi <> NC then
               begin
                    OCo[x,i].Status := ChuanBi;
            inc(dem);
                    break;
                    end
               else if OCo[x,i].NChoi = NC then break;
    DiHau := Dem;
 End;

 Function TBanCo.DiXe(x,y,NC: integer): integer;
 var i,dem: integer;
 Begin
    dem := 0;
      for i := x-1 downto 1 do
          if OCo[i,y].Quan = Trong then
        begin OCo[i,y].Status := ChuanBi;inc(dem);end
          else if OCo[i,y].NChoi <> NC then
               begin
                    OCo[i,y].Status := ChuanBi;
            inc(dem);
                    break;
                    end
               else if OCo[i,y].NChoi = NC then break;
      for i := x+1 to 8 do
          if OCo[i,y].Quan = Trong then
        begin OCo[i,y].Status := ChuanBi;inc(dem);end
          else if OCo[i,y].NChoi <> NC then
               begin
                    OCo[i,y].Status := ChuanBi;
            inc(dem);
                    break;
                    end
               else if OCo[i,y].NChoi = NC then break;
      for i := y-1 downto 1 do
          if OCo[x,i].Quan = Trong then
        begin OCo[x,i].Status := ChuanBi;inc(dem);end
          else if OCo[x,i].NChoi <> NC then
               begin
                    OCo[x,i].Status := ChuanBi;
            inc(dem);
                    break;
                    end
               else if OCo[x,i].NChoi = NC then break;
      for i := y+1 to 8 do
          if OCo[x,i].Quan = Trong then
        begin OCo[x,i].Status := ChuanBi;inc(dem);end
          else if OCo[x,i].NChoi <> NC then
               begin
                    OCo[x,i].Status := ChuanBi;
            inc(dem);
                    break;
                    end
               else if OCo[x,i].NChoi = NC then break;
    DiXe := Dem;
 End;

 Function TBanCo.DiMa(x,y,NC: integer): integer;
 var dem: integer;
 Begin
      dem := 0;
      if (x > 2) and (y > 1) then
      if (OCo[x-2,y-1].NChoi <> NC) then
    begin OCo[x-2,y-1].Status := ChuanBi;inc(dem);end;
      if (x > 2) and (y < 8) then
      if (OCo[x-2,y+1].NChoi <> NC) then
    begin OCo[x-2,y+1].Status := ChuanBi;inc(dem);end;
      if (x < 7) and (y > 1) then
      if (OCo[x+2,y-1].NChoi <> NC) then
    begin OCo[x+2,y-1].Status := ChuanBi;inc(dem);end;
      if (x < 7) and (y < 8) then
      if (OCo[x+2,y+1].NChoi <> NC) then
    begin OCo[x+2,y+1].Status := ChuanBi;inc(dem);end;

      if (x > 1) and (y > 2) then
      if (OCo[x-1,y-2].NChoi <> NC) then
    begin OCo[x-1,y-2].Status := ChuanBi;inc(dem);end;
      if (x > 1) and (y < 7) then
      if (OCo[x-1,y+2].NChoi <> NC) then
    begin OCo[x-1,y+2].Status := ChuanBi;inc(dem);end;
      if (x < 8) and (y > 2) then
      if (OCo[x+1,y-2].NChoi <> NC) then
    begin OCo[x+1,y-2].Status := ChuanBi;inc(dem);end;
      if (x < 8) and (y < 7) then
      if (OCo[x+1,y+2].NChoi <> NC) then
    begin OCo[x+1,y+2].Status := ChuanBi;inc(dem);end;
    DiMa := Dem;
 End;

 Function TBanCo.DiTuong(x,y,NC: integer): integer;
 var i,j,dem: integer;
 Begin
    dem := 0;
      i:=x;j:=y;
      while (i > 1) and (j > 1) do
      begin
           Dec(i);Dec(j);
           if OCo[i,j].NChoi = Trong then
        begin
                   OCo[i,j].Status := ChuanBi;
            inc(dem);
        end
           else
           begin
                if OCo[i,j].NChoi <> NC then OCo[i,j].Status := ChuanBi;
        inc(dem);
                i := 1;
           end;
      end;
      i:=x;j:=y;
      while (i < 8) and (j > 1) do
      begin
           Inc(i);Dec(j);
           if OCo[i,j].NChoi = Trong then
        begin
                   OCo[i,j].Status := ChuanBi;
            inc(dem);
        end
           else
           begin
                if OCo[i,j].NChoi <> NC then OCo[i,j].Status := ChuanBi;
        inc(dem);
                i := 8;
           end;
      end;
      i:=x;j:=y;
      while (i > 1) and (j < 8) do
      begin
           Dec(i);Inc(j);
           if OCo[i,j].NChoi = Trong then
        begin
                   OCo[i,j].Status := ChuanBi;
            inc(dem);
        end
           else
           begin
                if OCo[i,j].NChoi <> NC then OCo[i,j].Status := ChuanBi;
        inc(dem);
                i := 1;
           end;
      end;
      i:=x;j:=y;
      while (i < 8) and (j < 8) do
      begin
           Inc(i);Inc(j);
           if OCo[i,j].NChoi = Trong then
        begin
                   OCo[i,j].Status := ChuanBi;inc(dem);
        end
           else
           begin
                if OCo[i,j].NChoi <> NC then OCo[i,j].Status := ChuanBi;
        inc(dem);
                i := 8;
           end;
      end;
    DiTuong := Dem;
 End;

 Function TBanCo.DiTot(x,y,NC: integer): integer;
 var dem: integer;
 Begin
    dem := 0;
      if OCo[x,y+NC].NChoi = Trong then
      begin
           OCo[x,y+NC].Status := ChuanBi;inc(dem);
           if ((y = 2) or (y = 7)) and (OCo[x,y+NC*2].NChoi = Trong) then
           begin OCo[x,y+NC*2].Status := ChuanBi;inc(dem);end;
      end;
      if (OCo[x-1,y+NC].NChoi <> NC) and
      (OCo[x-1,y+NC].NChoi <> Trong) then
    begin OCo[x-1,y+NC].Status := ChuanBi;inc(dem);end;
      if (OCo[x+1,y+NC].NChoi <> NC) and
      (OCo[x+1,y+NC].NChoi <> Trong) then
    begin OCo[x+1,y+NC].Status := ChuanBi;inc(dem);end;
    DiTot := Dem;
 End;

 Function TBanCo.DiChuyen(x,y: integer): integer;
 var NC: integer;
 Begin
      NC := OCo[x,y].NChoi;
      case OCo[x,y].quan of
      Vua: DiChuyen := DiVua(x,y,NC);
      Hau: DiChuyen := DiHau(x,y,NC);
      Xe: DiChuyen := DiXe(x,y,NC);
      Ma: DiChuyen := DiMa(x,y,NC);
      Tuong: DiChuyen := DiTuong(x,y,NC);
      Tot: DiChuyen := DiTot(x,y,NC);
                end;
 End;

Function TBanCo.DiCo(x1,y1,x2,y2: integer): integer;
var d: integer;
Begin
    d := 0;
    if OCo[x2,y2].Quan <> Trong then d := Diem[OCo[x2,y2].Quan];
    OCo[x2,y2].Quan := OCo[x1,y1].Quan;
    OCo[x2,y2].NChoi := OCo[x1,y1].Nchoi;
    OCo[x2,y2].Status := DiDen;
    OCo[x1,y1].Quan := Trong;
    OCo[x1,y1].NChoi := Trong;
    OCo[x1,y1].Status := XuatPhat;
    DiCo := d;
End;
-----------------------------------------------------------------------
Code này các bạn lưu dưới tên VEQUAN.INC
Mã:
Procedure TBanCo.VeVua(x,y: integer);
Begin
    gotoxy((x-1)*Size+4,(y-1)*(Size div 2)+1);
    write(#95#22#254#22#95);
    gotoxy((x-1)*Size+4,(y-1)*(Size div 2)+2);
    write(#92#30#30#30#47);
    gotoxy((x-1)*Size+4,(y-1)*(Size div 2)+3);
    write(#223#223#223#223#223);
End;

Procedure TBanCo.VeHau(x,y: integer);
Begin
    gotoxy((x-1)*Size+4,(y-1)*(Size div 2)+1);
    write(#92#30#179#30#47);
    gotoxy((x-1)*Size+4,(y-1)*(Size div 2)+2);
    write(#32#219#219#219);
    gotoxy((x-1)*Size+4,(y-1)*(Size div 2)+3);
    write(#223#223#223#223#223);
End;

Procedure TBanCo.VeXe(x,y: integer);
Begin
    gotoxy((x-1)*Size+4,(y-1)*(Size div 2)+1);
    write(#219#32#219#32#219);
    gotoxy((x-1)*Size+4,(y-1)*(Size div 2)+2);
    write(#32#219#219#219);
    gotoxy((x-1)*Size+4,(y-1)*(Size div 2)+3);
    write(#223#223#223#223#223);
End;

Procedure TBanCo.VeMa(x,y: integer);
Begin
    gotoxy((x-1)*Size+4,(y-1)*(Size div 2)+1);
    write(#222#223#219#219);
    gotoxy((x-1)*Size+4,(y-1)*(Size div 2)+2);
    write(#32#222#219#219);
    gotoxy((x-1)*Size+4,(y-1)*(Size div 2)+3);
    write(#223#223#223#223#223);
End;

Procedure TBanCo.VeTuong(x,y: integer);
Begin
    gotoxy((x-1)*Size+4,(y-1)*(Size div 2)+1);
    write(#32#32#234);
    gotoxy((x-1)*Size+4,(y-1)*(Size div 2)+2);
    write(#32#219#254#219);
    gotoxy((x-1)*Size+4,(y-1)*(Size div 2)+3);
    write(#223#223#223#223#223);
End;

Procedure TBanCo.VeTot(x,y: integer);
Begin
    gotoxy((x-1)*Size+4,(y-1)*(Size div 2)+1);
    write(#32+#32+#219);
    gotoxy((x-1)*Size+4,(y-1)*(Size div 2)+2);
    write(#32#219#219#219);
    gotoxy((x-1)*Size+4,(y-1)*(Size div 2)+3);
    write(#223#223#223#223#223);
End;

Procedure TBanCo.VeCo(x,y: integer);
var quan: integer;
Begin
    quan := OCo[x,y].Quan;
    case Quan of
    Vua: VeVua(x,y);
    Hau: VeHau(x,y);
    Xe: VeXe(x,y);
    tuong: VeTuong(x,y);
    Ma: VeMa(x,y);
    Tot: VeTot(x,y);
            end;
End;

Các bạn có thể thay đổi level để tăng độ thông minh cho máy, nhưng level càng cáo nó chạy càng lâu nha.
Máy mình chạy level 2 là ok, còn hơn thì chạy không nổi.
View attachment 103444
Cấp III có được học lập trình game kiểu này không anh Nghĩa....?
 
  • Like
Reactions: God of dragon

trà nguyễn hữu nghĩa

Cựu Mod Vật Lí |Cây bút Thơ|Thần tượng VH
Thành viên
14 Tháng năm 2017
3,974
7,623
744
22
Phú Yên
Trường THPT Lương Văn Chánh
Top Bottom