Lập trình trò chơi quân cờ trong Pascal

Chủ nhật - 09/08/2020 09:59
Trên một bàn cờ n x m có đặt n quân xe trắng và n quân xe đen sao cho trên mỗi cột đều có 1 xe trắng và 1 xe đen. Hai người A, B chơi với nhau một trò chơi như sau:
- A đi xe trắng, B đi xe đen.
- Các quân xe không ăn nhau, không được đi qua đầu nhau và chỉ được di chuyển trên một cột.
Hãy lập trình để máy tính chơi với người trò chơi trên với khả năng thắng cao nhất.

Hướng dẫn:

- Có thể đưa về bài toán bốc sỏi.
- Khoảng cách giữa 2 xe chính là số sỏi.
- Chú ý rằng số sỏi có thể tăng hoặc giảm.
Chương trình PASCAL chi tiết như sau:
uses crt;
const max=20;
          mu: array[0..4] of byte = (1,2,4,8,16);
type d=array[1..5] of 0..1;
var i,n,k,l,j,m, where1, where2, that, new1, new2: byte; may, người: 1..2; 
           a: array[l..max,l..maxj of 0..2;
           have: array[l..max] of byte;
           cot: set of byte;
           b : d;
           suly: array[1..max] of d;
           truoc,trang,error,stop: boolean;
procedure draw;
begin
          textbackground(11);
          textcolor(15);
          clrscr,
          for i : = 1 to n do
          begin
               textcolor(14);
                gotoxy(30-n,i+12-(n div 2));Write(i: 3);
                gotoxy(30+i * 3-n,12-(n div 2));Write(i: 3);
                For j : = 1 to n do
                begin
                    gotoxy(31+i * 3-n,J+12-(n div 2));
                  if a[i,j]=o then.
                  begin
                      textcolor(7);
                      Writer (‘   ’);
                  end;
                  if a[ij]=l then
                  begin
                      textcolor(15);
                      Writer (‘   ’);
                  end;
                  if a[ij]=2 then
                  begin
                       textcolor(0);
                       Writer (‘   ’);
                   end;
              end;
         end;
         procedure init;
         var ch: char;
         begin
              stop: = false;
              fillchar(a,sizeof(a),0);
              Writeln(‘Nhap kich thuoc n');
              repeat
                    clreol;
                    readln(n);
              until (n>0) and (n<21);
              Writeln ('Nhap gia tri k’);
              Repeat
                   clreol;
                   readln(m);
              until (m>0) and (m<= n);
              i : = 0;
              cot : = [];
              Repeat
                   j: = random(n)+1;
                  If (not (j in cot)) then
                      begin
                           i : = i+1;
                           cot : = cot+[j];
                           k: = random(n)+l;
                         repeat
                            1: = random(n)+l;
                         until 1< > k;
                         a[j,k] : = 1;
                         a[j,1] : = 2;
                    end;
       until i=m;
       draw;
       readln;
       textbackground(0);
       textcolor(15)
       clrscr;
       Writeln(‘Ban muon di quan trang hay den <1>: trang, <2>: den');
       repeat
            ch : = readkey;
            if ch = “1' then
            begin
                nguoi : = 1;
                may : = 2;
                trang : = true;
            end;
            if ch = '2' then
            begin
                nguoi : = 2;
                may : - 1;
                trang : = false;
            end;
      until (ch in ['1','2']);
      Writeln(‘Ban muon di truoc hay di sau <1 >: truoc, <2>: sau');
      repeat
           ch: = readkey;
           if ch='1' then truoc : = true;
           if ch='2' then truoc : = false;
      until (ch in ['1','2'1)1;
end;
procedure nguoidi;
var ch: char;
begin
     draw;
     stop : = true;
     for i : = 1 to n do
         if i in cot then
         begin
             for j : = 1 to n do
                  begin
                      if (a[i,j]=may) then where2 : = j;
                      if (a[i,j]=nguoi) then where1: - j;
                  end;
                  have[i]: = abs(where2-where1)-1;
                  if (have[i]< >0) or ((where1-n)*(where1-1)< >0)
                  then stop : = false;
          end;
          if stop = false then
          begin
              gotoxy(1,23);
              clreol;
              textcolor(4);
              write(‘Ban muon chon quan 0 cot thu may ?');
          repeat
              gotoxy(1,24);
              clreol;
              read(i);
          until i in cot;
          gotoxy(l,23);
          clreol;
          textcolor(4);
          writeln(‘Ban muon doi quan cua ban 0 cot, ',i,' sang hang nao ?');
          repeat
               gotoxy(l,24);
               clreol;
               read(j);
          until j in [1..n];
          For k : = 1 to n do
          begin
               if a[i,k]=nguoi then where 11: = k;
               if a[i,k]=may then where2 : = k;
          end;
          clreol;
          if ((wherel-where2)*(j-where2)<=0) or (where1=j) then
          begin
              error : = true;
              write(‘Ban da di sai luat. An phim ENTER de di lai...');
               readln;
          end;
         else
             begin a[i,wherel]: = 0;
             a[i,j] : = nguoi;
             error : = false;
             write(‘Ban da di dung luat. An phim ENTER de choi tiep...');
             readln;
         end;
    end;
    else
         begin
                 gotoxy(l,24);
                 clreol;
                 textcolor(4);
                 writeln(‘Ban da thua !');
                 clreol;
                 write(‘An ENTER de thoat...');
                 readln;
                 stop: = true;
         end;
    end;
procedure doihe( byte var c: d);
var p,tg,i: byte;
begin
    p : = 0;
    fillchar(c,sizeof(c),0);
    repeat
       inc(p);
       c[p] : = x mod 2;
       x : = x div 2;
       until x=0;
end;
procedure congmang;
var p: byte;
begin
     fillchar(b,sizeof(b),0);
     for i : = 1 to n do
     begin
         doihe(have[i],suly[i]);
         for j : = 1 to 5 do
         b[j]: = (b[j]+suly[i][j])mod 2;
     end;
end;
procedure maydi;
var ch : char;
begin
    draw;
    delay(random(500));
    that : = 21;
    for i : = 1 to n do
    if i in cot then
    begin .
        for j : = 1 to n do
        begin
            if (a[i,j]=may) then where2 : = j;
            if (a[i,j]=nguoi)then where1: = j;
        end;
        have[i] : = abs(wherel-where2)-l;
        if (have[i]< >0) or ((where2-n)* (where2-1)< >0) then that : = i;
        {dung cho khong tim ra duong di}
     end;
     else
          have[i] : = 0;
     congmang;
     i : = 6;
     repeat
          dec(i);
     until (i=0) or (b[i]=l);
     if b[i]=l then
         begin
             j : = 0;
             repeat
                  inc(j);
             until suly[j][i]=l;
             for k : = 1 to 5 do
                   if b[k]=1 then suly[j][k]: = abs(1-suly[j][k]);
             that: = 0;
             for 1: = 1 to 5 do
                  that : = that+suly|j][l]*mu[1-1];
             for k : = 1 to n do
             begin
                   if (a[j,k]=may) then where2 : = k;
                   if (a[j,k]=nguoi) then where 1: = k;
             end;
new1 : = where1+1+that;
new2 : = where 1-1-that;
if abs(new1-where2)>abs(new2-where2) then
    begin
        gotoxy(1,24);
        clreol;
        textcolor(4);
        writeln(‘May se doi quan cua may o cot ' j,' sang hang',new2);
         a[j,where2] : = 0;
         a[j,new2] : = may;
         clreol;
         Write(‘An phim ENTER de choi tiep...’);
         readln;
    end;
  else
    begin
        gotoxy(1,24);
        clreol;
        textcolor(4);
        writeln(’May se doi quan cua may o cot ’j,’ sang hang',new1);
        a[j,where2] : = 0;
        a[j,new1] : = may;
        cireol;
        write(An phim ENTER de choi tiep...');
        readln;
    end;
  end;
else
    if that < >21 then
        begin
            for k : = 1 to n do
            begin
                if (a[that,k|=may) then where2 : = k;
                if (a[that,k]=nguoi) then where1 : = k;
            end;
            new1 : = where2+1;
            new2 : = where2-l;
            if ((have[that]>0) and (abs(new1-where2) > abs(new2-where2))) or ((have[tha=0) and (abs(newl=wherel) < abs(new2-wherel))) then
            begin
                gotoxy(l,24)
                clreol;
                textcolor(4);
               writeln(May se doi quan cua may o cot', that,'sang hang',new2);
               a[that,where2]: = 0;
               a[that,new2]: = may;
               clreol;
               Write(‘An phim ENTER de choi tiep...’) readln;
               end;
         else
              begin
                  gotoxy(l,24);
                  clreol;
                  textcolor(4);
                  writeln(May se doi quan cua may o cot', that,'sang hang',new1);
                  a[that,where2] : = 0;
                  a[that,new]: = may;
                  clreol;
                  Write(‘An phim ENTER de choi tiep...’);
                   readln;
              end;
         end;
         else
             begin
                  gotoxy(1,24);
                  clreol;
                  textcolor(4);
                  writeln(‘Ban da thang !');
                  clreol;
                  Write(‘An phim ENTER de thoat...');
                   readln;
                   stop : = true;
             end;
       end;
            begin
                clrscr;
                stop : = false;
                randomize;
                init;
                repeat
                   if truoc then
                       begin
                           repeat
                              nguoidi;
                           until error = false;
                          if sotp=false then maydi;
                      end;
                 else
                      begin
                          maydi;
                          if stop=false then
                              repeat
                                   nguoidi ;
                              until error = false;
                        end;
                  until stop;
                  textbackground(0);
                  clrscr;
                      end.

  Ý kiến bạn đọc

THÀNH VIÊN

Hãy đăng nhập thành viên để trải nghiệm đầy đủ các tiện ích trên site
Kênh Bóng đá trực tiếp hôm nay miễn phí
Kênh
90Phut TV full HD ⇔ 32win
69VN ⇔ 78 WIN ⇔ F168 ⇔ 123b

Vmax ⇔ 78Win ⇔ https://xn88.bio/
GK88 ⇔ SHBET ⇔ nh88 ⇔ u888 ⇔ 789f
fo88vic88 ⇔ x88 ⇔ win678 ⇔ 69vn
BL555 ⇔ 99OK ⇔ KJC ⇔ Link MB66
mv66af88 ⇔ 8x bet ⇔ 88AA
789BET ⇔ LC88 ⇔ BL555 ⇔ 789f
OKFUN ⇔ 789F ⇔ MB66 ⇔ 8xbet
https://789betcom0.com/ ⇔ https://hi88.baby/
UU88 ⇔ au88 ⇔ win678 ⇔ https://xx88.ink/
zo88 ⇔ ev99 ⇔ 888b ⇔ go88 ⇔ kèo nhà cái
cm88 ⇔ 88aa ⇔ Kkwin ⇔ tv88 ⇔ 77win
mb66.racing ⇔ u888 ⇔ FO88 ⇔ 88xx
188v ⇔ f168 ⇔ QQ88 ⇔ bk8 ⇔ bk8
789club ⇔ iwinclub ⇔ Subnhanh ⇔ Bluphim
m88 ⇔ Nettruyenviet ⇔ P3 ⇔ 8s
vn23 ⇔ SHBET ⇔ kubet ⇔ 888new
NOHU ⇔ hi88 ⇔ https://fun88.social/
thabet ⇔ SV88 ⇔ https://vnalo789c.com/
https://qq88.fun/ ⇔ f168 ⇔ 888New ⇔ QQ88
daga ⇔ SHBET ⇔ keo nha cai ⇔ bl-555.site
https://bshbet.com/ ⇔ https://uk88.rocks
kkwin ⇔ KUWIN ⇔ 8xbet ⇔ RR88 ⇔ 188v
mb6678win ⇔ https://hello8880.net/
tv88UU88 ⇔ 5mb ⇔ MB66 ⇔ TV88
NOHU ⇔ hi88com ⇔ 32win ⇔ Kuwin
ek333 ⇔ xoso66 ⇔ nổ hũ ⇔ SUN WIN
Fun88 ⇔ https://f168.com.co/ ⇔ uu88
NOHU ⇔ KINGFUN ⇔ https://789club24.com/
https://98winlive.com/ ⇔ https://789win.voyage/
https://bet88yk.com/ ⇔ https://alo789apk.app/
https://33win100.com/ ⇔ https://u888ny.com/
69vn ⇔ vn88 ⇔ OK8386 ⇔ x88 ⇔ SHBET
https://j88usd.com/ ⇔ https://79kings.blue/
https://dom88.jp.net/ ⇔ https://hello880.net/
https://789club60.com/ ⇔ https://blc88.net/
23win ⇔ 6ff ⇔ f168 ⇔ xoso66 ⇔ bomwin
nhatvip ⇔ https://789club24.com/ ⇔ 789 bet
win68 ⇔ 8xbet ⇔ go99 ⇔ 78win ⇔ QQ88
XX88 ⇔ ev99 ⇔ 69vn ⇔ rr88 ⇔ rr88
Uu88 ⇔ win456 ⇔ Tài xỉu online ⇔ XN88
https://33win101.com/ ⇔ oxbet ⇔ Slot365
78win ⇔ https://shbet.gg/ ⇔ XX88
daga ⇔ SV388 ⇔ OKFUN ⇔ ee88 ⇔ 12bet
hitclub ⇔ Kkwin ⇔ VankhanhTV ⇔ Hay 88
3WIN ⇔ WIN678 ⇔ https://58winbank.com/
68 game bàiEE88 ⇔ 77BET ⇔ 12BET
vmaxVMAX ⇔ ok9 football ⇔ K8CC
https://hi88o.com/ ⇔ https://hi88.gives/
https://qq88.co.com/ ⇔ https://f168.law/
8xbet ⇔ https://shbetk.net/ ⇔ RIKVIP ⇔ rikvip
https://mm88login.com/ ⇔ 79king
ww88 ⇔ https://78win.bot/ ⇔ https://13win.vegas/
https://lc88o.com/ ⇔ https://hi88.uno/
https://58win1.info/ ⇔ j88 ⇔ j88 ⇔ 69vn
Slot365 ⇔ 78 win ⇔ UU88 ⇔ 32 win
888TO ⇔ wstar77 ⇔ 73bet ⇔ 888b
11uu ⇔ 123B ⇔ 78win ⇔ Slot365
789f ⇔ mv88 ⇔ YO88 ⇔ QQ88
888New ⇔ Bet88 ⇔ Vmax ⇔ Sun Win
tỷ lệ kèo nhà cái hôm nay ⇔ BIN88
https://j88.ventures/ ⇔ https://qq88.studio/
https://nh8811.com/ ⇔ Leo88 ⇔ 78win
88CLB ⇔ SV388 ⇔ https://cakhiatv88.net/
7m ⇔ i9 Bet ⇔ Tv88 ⇔ hi88 ⇔ 888To
socolive trực tiếp74bet ⇔ VN168
Bong88 ⇔ socolive ⇔ bet88 ⇔ 555WIN
https://au88.black/ ⇔ trực tiếp bóng đá
https://king88clb.com/ ⇔ https://f168hi.com/
sv66nhà cái thabet ⇔ 88clb ⇔ Nn88
https://32win.vc/ ⇔ 888new ⇔ PG88 ⇔ PG88
EE88 ⇔ SanClub ⇔ F168 ⇔ QQ88
hb88 ⇔ Vin777 ⇔ SV388 ⇔ MV66 ⇔ 32win
https://vankhanhtvv.com/ ⇔ Nhà Cái LC88
https://789fnew.com/ ⇔ https://u888lm.com/
https://sv66.coupons/ ⇔ https://f168.tech/
https://8kbetbh.com/ ⇔ https://ta88club.kim/
socolivemv88 ⇔ https://rr88.com.se/
https://bet168.com.co/ ⇔ https://8kbet.co/
https://888b-link.cc/ ⇔ https://sunwinn.deal/
88gokingfun ⇔ https://bong88vn.wiki/
https://mb66az.com/ ⇔ https://shbet.racing/
789f ⇔ 88i ⇔ https://new88blog.org/
Jun88 ⇔ Jun88 ⇔ jun88 ⇔ 67bet
MM88 ⇔ https://789betw.co/ ⇔ UU88
https://qq887p.com/ ⇔ https://w88link1.com/
xocdia88 ⇔ 77ball ⇔ vuabet88 ⇔ betvisa
https://789freal.com/ ⇔ https://2789f.com/
https://fun88link0.com/ ⇔ https://fun88link1.com/
https://188betlink0.com/ ⇔ https://188betlink1.com/
https://188betlink2.com/ ⇔ https://w88link0.com/
thabet ⇔ 32WIN ⇔ J88 ⇔ ax88 ⇔ 789F
QQ88 COM ⇔ https://xx88.sale/ ⇔ 78win
https://bet88.pictures/ ⇔ https://xo88.group/
https://86bet.tech/ ⇔ https://nohu90.consulting/
789BET ⇔ https://i9betz4.com/ ⇔ đá gà 88
w88link2.com ⇔ w88link3.com ⇔ luck8
https://8dayvip.mobi/ ⇔ https://8dayvip.com/
QQ88 Com ⇔ af88 ⇔ https://79kingsr.com/
https://pg99yk.com/ ⇔  hm88.com ⇔ J88
https://luongson161.tv/ ⇔ 78WIN ⇔ MB66
78win ⇔ U888 ⇔ nn88 ⇔ 99win ⇔ hi88
https://8kbettt.com/ ⇔ https://hi88.biz/
https://thabetcasino.us.com/ ⇔ 789Win ⇔ vn88
https://hello88net.com/ ⇔ https://pg88.jpn.com/
https://az8888.org/ ⇔ https://fly88.gifts/
lc88 ⇔ VN88 ⇔ 69vn ⇔ nh88 ⇔ vn88
GO8 ⇔ 888new ⇔ sv388 ⇔ KWIN
tỷ lệ kèo nhà cái 88 ⇔ PG66 ⇔ 68win
BL555 ⇔ 23win ⇔ SV388 ⇔ SHBET ⇔ hitclub
Game bài đổi thưởng ⇔ https://saobet.shop/
https://99ok99ok.com/ ⇔ UU88 ⇔ HM88
https://bet88.ventures/ ⇔ bongdalu22.com
zx88 ⇔ ev99 ⇔ 8s ⇔ bet8s ⇔ QQ88
https://f168.name/ ⇔ https://13wincom.io/
Nhà cái vn88 ⇔ Nhà Cái QQ88
https://fb888.in.net/ ⇔ https://kjc.ac/
https://onbet.in.net/ ⇔ link vn88 ⇔ HZ88
http://58win.trading/ ⇔ https://qq88.gives/
bong88 ⇔ sky88 ⇔ Cổng game rikvip ⇔ ev99
79KING ⇔ Bet88 ⇔ XN88 ⇔ S666 ⇔ VN23
https://kkwin.fan/ ⇔ https://555win.bike/
https://f8bet.courses/ ⇔ https://go8play.com/
https://lc88.bar/ ⇔ 789f ⇔ abc8 ⇔ good88
789win ⇔ Hitclub ⇔ https://f8bet80.one/
 ⇔
Bạn đã không sử dụng Site, Bấm vào đây để duy trì trạng thái đăng nhập. Thời gian chờ: 60 giây