Lập trình trò chơi bốc sỏi trong Pascal

Chủ nhật - 09/08/2020 09:55
Cho n đống sỏi với số lượng là a1, an. Có 2 đối thủ tham dự 1 trò chơi như sau:
- Mỗi người lần lượt bốc ra từ 1 đống tùy ý một số sỏi tùy ý.
- Ai bốc hết sỏi trước thì thắng.
Hãy xây dựng chương trình cho phép thể hiện trò chơi trên với một đối thủ là máy tính và chương trình thể hiện chiến thuật thắng của máy tính.
■ Hướng dẫn:
• Thuật toán của máy tính như sau:
- Đổi các số a1, a2, an ra số nhị phân.
- Cộng không nhớ các số nhị phân đó vào biến T.
- T chính là số sỏi cần phải bốc (từ một đống tùy ý).
• Chiến lược chơi của máy tính là khi bốc:
- Nếu T = 0 (tình huống xấu) thì bốc thăm dò (tùy ý).
- T < > 0 (tình huống tốt) thì bốc như đã nói ở trên.
Chú ý rằng đây là một trò chơi căn bản; rất nhiều trò chơi khác được đưa về nó.
Sau đây là các procedure cần thiết để thể hiện chơi trên bằng PASCAL với n = 4.
CONST d=10;
VAR
       x,y,z,t,l,i,j,k,w,e,g,s : byte;
       a: array[1..4,1..10] of byte;
       b: array[1..10] of byte;
       c: array[1..10] ofbyte;
       u1,u2,ch : string[10];
PROCEDURE DOIHE(n: bety);
VAR p: type;
BEGIN
          p : = 0
          for i : = 1 to 10 do
              c[i] : = 0;
           Repeat
                inc(p);
                c[p] : = n mod 2;
                n : = n div 2;
          Until n=0;
END;
FUNCTION TAOLAPMANG(m,n,p,q: byte): byte;
BEGIN
          If(m+n+p+q) mod 2 = 0 then taolapmang : = 0
          Else taolapmang: = 1;
END;
PROCEDURE CONGMANG(x,y,z,t: byte);
VAR p: byte;
BEGIN
          p: = 10;
          doihe(x);
          For k : = 1 to p do
               a[1,110-k+1] : = c[k];
          doihe(y);
          For k : = 1 to p do
               a[2,10-k+l] : = c[k];
          doihe(z);
          For k : = 1 to p do
               a[3,10-k+1] : = c[k];
          doihe(t);
          For k : = 1 to p do
               a[4,10-k+1] : = c[k];
                        {da thanh lap 4 mang}
           For k : = 1 to 10 do
                 b[10-k+1]: = Taolapmang(a[1,10-k+1J), a[2,10-k+l|, a[3,10-k+l], a[4,10-k+l];
END;
PROCEDURE MACHINEPLAY;
BEGIN
          Congmang(x,y,z,t);
          w: = 0;
          Repeat
               inc(w);
          Until (b[w]=1) or (w=10);
          If b[w]=1 then
          Begin
               e : = 0;
               e : = 0;
          Repeat
              inc(e);
          Until a[e,w]=l
          {đa phat hien ra mang can giam}
          f : = 0;
          Repeat
              inc(f);
              If b[f]=1 then
              begin
                  If a[e,f]=0 then a[e,f] : = 1
                  Esle s[e,f] : = 0;
              end;
         Until f=10;
        {da co mang noi}
        {doi tro lai}
         u2 : = “ ;
         For s: = 1 to 10 do
         begin
             str(a[e,s],ch];
             u2: = u2+ch;
         end;
         g : = 0;
         Repeat
             inc(g);
             Doihe(g);
             u1 : =“ ;
             For s: - 1 to 10 do
             begin
                 str(c[10-s+1],ch);
                 u1 : = u1+ch;
             end;
        Until u1 = u2
   end;
END;

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
leo88 ⇔ hb 88 ⇔ go88 ⇔ BET88

https://go88live.net/ ⇔ https://69vn.living/
Link MB66 ⇔ U888 ⇔ ABC88
link xem truc tiep bong da xoilac tv ⇔ https://104.248.99.177/
xem bóng đá cà khịa tv trực tuyến hôm nay
 ⇔ hi88 ⇔ qq88 ⇔ TK88
https://hi88.tours/ ⇔ 789BET ⇔ 
789BET ⇔ BJ88 ⇔ 789bet ⇔ hitclub
ok9 ⇔ QQ88 ⇔ qq 88 ⇔ OK9
https://789betcom0.com/ ⇔ https://hi88.baby/
https://f168.blue/ ⇔ sunwin
QQ88 ⇔  ⇔ 
789club ⇔ 78WIN
8kbet ⇔ sunwin
https://789club64.com/ ⇔ 
18win ⇔ https://fun88.social/
  ⇔  ⇔ QQ88
Rikvip ⇔ hi88 ⇔ 
https://go88so.net/ ⇔ https://f168.law/
 ⇔ https://789club63.com/
 ⇔ hi88 ⇔ 
 ⇔ 
https://f168.dad/
https://qq88.fun/ ⇔ 
 ⇔  ⇔ 
nhà cái net88
s666 ⇔ https://kubetvn88.com/
https://f168.guide/ ⇔ https://uk88.rocks
https://8xbet68.net/ ⇔ https://789bet188.com/
https://shbetb0.com/ ⇔ https://hello8880.net/
sunwin ⇔  ⇔ hi88 ⇔ qh88 com
 ⇔ f168 ⇔ https://qq88.marketing/
F168 ⇔ new88 ⇔ 
https://mb66az.com/ ⇔ 
Link vào NEW88 ⇔ https://789club24.com/
https://33win103.com/ ⇔ 
https://888bz.vip ⇔ 
https://33win102.com/ ⇔ 
https://33win100.com/ ⇔ 
https://ww88.supply/ ⇔ https://hi88.report/
https://58win1.info/ ⇔ Hi88
https://789bet188.us/ ⇔ https://hello880.net/
https://789club60.com/ ⇔ 
shbet ⇔ cwin ⇔ 68gamebai
qh88 đăng nhập ⇔ https://789club24.com/
78win ⇔ 8xbet com
 ⇔  ⇔ BAY789
https://go88club13.com/https://789bet188.today/
https://bk8link2.com/ ⇔ https://bk8link3.com/
https://789bet188.xyz/ ⇔ https://jun88pro.club/
https://33win101.com/ ⇔ SHBET
 ⇔ https://hi88.gives/
 ⇔ https://789bet188.pro/
 ⇔ https://789bet188.cloud/
 ⇔ https://nohu90m.net/
 ⇔ https://bj8884.com/
https://789bet188.info/ ⇔ https://789bet188.live/
https://789bet188.online/ ⇔ https://789bet188.co/
https://789bet188.tech/ ⇔ https://789bet188.biz/
https://789bet188.club/ ⇔ https://789bet188.vip/
https://789bet188.site/ ⇔ https://789bet.asia/
https://new8822.com/ ⇔ https://new8818.org/
https://new8818.me/ ⇔ https://new8818.xyz/
https://new8818.pro/ ⇔ https://new8818.cloud/
https://new8818.info/ ⇔ https://new8818.us/
https://new8818.live/ ⇔ https://new8818.online/
https://new8818.co/ ⇔ https://new8818.today/
https://new8818.biz/ ⇔ https://new88820.com/
https://new8818.vip/ ⇔ https://new8818.site/
https://new8818.ink/ ⇔ https://ahihi88.host/
https://hi8818.xyz/ ⇔ https://hi8818.us/
https://hi8818.blog/ ⇔ https://hi8818.online/
https://hi8818.site/ ⇔ https://hi8818.ink/
https://hi8818.cloud/ ⇔ https://hi8818.me/
https://shbet188.org/ ⇔ https://shbet188.pro/
https://shbet188.cloud/ ⇔ https://shbet188.ink/
https://shbet288.store/ ⇔ https://shbet288.today/
https://shbet288.tech/ ⇔ https://shbet188.xyz/
https://shbet188.us/ ⇔ https://shbet188.shop/
https://kubet288.com/ ⇔ https://kubet188.mobi/
https://kubet188.dev/ ⇔ https://kubet288.xyz/
https://kubet288.pro/ ⇔ https://kubet288.cloud/
https://jun8818.org/ ⇔ https://jun8818.net/
https://jun8818.me/ ⇔ https://jun8818.xyz/
https://jun8818.pro/ ⇔ https://jun8818.cloud/
https://jun8818.info/ ⇔ https://jun8818.us/
https://jun8818.live/ ⇔ https://jun8818.shop/
https://loteriadeboyaca.com.co/
https://f8bet288.com/ ⇔ https://f8bet288.org/
https://f8bet288.net/ ⇔ https://f8bet288.me/
https://f8bet288.xyz/ ⇔ https://f8bet288.pro/
https://f8bet288.cloud/ ⇔ https://f8bet288.info/
https://f8bet288.us/ ⇔ https://f8bet288.live/
https://f8bet288.online/ ⇔ https://f8bet288.co/
https://f8bet288.today/ ⇔ https://f8bet288.biz/
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