Lập trình trò chơi tính điểm quân đôminô trong Pascal

Chủ nhật - 09/08/2020 10:05
Cho 1 quân đôminô trên bàn và một số quân đôminô. Một người chơi một trò chơi sau đây:
- Người đó lần lượt gắn các quân đôminô vào quân đã có sẵn ban đầu.
- Người đó được gọi là thắng cuộc nếu tổng các số điểm của các quân còn lại là nhỏ nhất trong tất cả cách lắp.
- Hãy lập trình để máy chơi được trò chơi trên và luôn thắng cuộc.
■ Hướng dẫn:
- Đây là trò chơi một người, thuật toán chủ yếu là vét cạn các cách đi có thể để tổng số điểm còn lại đạt giá trị nhỏ nhất.
- Ta dùng thủ tục đệ qui Find (trai, phai: byte) để có thể quay lui khi vét cạn.
Thủ tục này thể hiện bằng PASCAL như sau:
{$M65520,0,0}
PROGRAM BAILAM3;
USES CRT;
TYPE Conco=Record
           x,y: Byte;
           co: set of byte;
           End;
VAR i,j,m,1m,1maxphai,1maxtrai,1minphai,lmintrai: Integer;
sigma,max,min: longint;
domino: array[1..100 of conco;
luutrai,luuphai,kqmaxtrai,kqmaxphai,kqmintrai,kqminphai: array[1.. 100] og byte;
          flag: set of byte;
          ch:char;
          tarn: set of byte;
          trai,phai,demtrai,demphai: Byte;
          oknhap: boolean;
PROCEDURE KEU;
BEGIN
      Sound(1305);
      Delay(300);
      Nosound;
END;
PROCEDURE FIND (trai,phai: Byte);
VAR j : integer;
         found: boolean;
BEGIN
         found : = false;
         For j : = 1 to 1m do
               If j in flag then
                  With domino[j] do
                       Begin
                            If (trai in co) then
                                Begin
                                    found : = true;
                                    sigma : = sigma-x-y;
                                    flag: = flag-[j]
                                    demtrai : = demtrai+1;
                                    luutrai[demtrai] : = j;
                                    If (trai=x) then Find(y,phai)
                                    Else Find(x,phai);
                                    demtrai: = demtrai-1;
                                    sigma: = sigma+x+y;
                                    flag: = flag+[j];
                               End;
                               If (phai in co) then
                               Begin
                                     found: = true;
                                     flag : = flag-[j];
                                     sigma: = sigma-x-y;
                                     demphai: = demphai+1;
                                     luuphai[demphai] : = j;
                                     If (phai=x) then find (trai,y);
                                     Else find(trai,x);
                                     demphai : = demphai-1;
                                     sigma : = sigma+x+y;
                                     flag : = flag+[j];
                                End;
                        End;
                  If (not found) then
                  Begin
                       If (sigma>max) then
                       begin
                            max: = sigma;
                            1maxphai : = demphai;
                            1maxtrai : = demtrai
                            kqmaxtrai : = luutrai;
                            kqmaxphai : = luuphai;
                       end;
                       If (sigmacmin) then
                       begin
                           min : = sigma;
                           1minphai : = demphai;
                           1mintrai : = demtrai;
                           kqmintrai : = luutrai
                           kqminphai : = luuphai;
                      end;
                 End;
END;
PROCEDURE XUAT;
BEGIN
      Repeat
           clrscr;
           writeln(’Menu’);
           Writeln(‘1. Xem dap so cau 2');
           Writeln('2. Xem dap so cau 3');
           Writeln('3. quit');
           Repeat
               ch : = readkey;
           Until ch in; [‘1’..’3’]
           Case ch of
               '1' : begin
                    For i : = 1mintrai down to 1 do
                   With domino[kqmintrai[i] do
                    begin
                       write(x',’,y,'->');
                    end;
                    For i : = 1 to 1minphai do
                    With domino[kqminphai[i]] do
                     begin
                         write(x,',',y,'->');
                    end;
                    Gotoxy(where-2,wherey);Writeln(' ');
                    Writeln(‘So diem con lai la',min) ;
                    readln;
               end;
               '2' : begin
                    For i: = 1maxtrai down to 1 do
                    With domino[kqmaxphai[i]] do
                    begin
                        write(x,',',y,’->');
                    end;
                    Gotoxy(where-2,wherey);Writeln(' ');
                    Writeln(‘So diem con lai la',max);
                    readln;
                    end;
               end;
          Until ch=’3';
END;
PROCEDURE DOING;
BEGIN
      clrscr;
      Writeln(Nhap vao cap i0 j0');
      Readln(trai phai);
      If (((trai-m)*(trai)>0) or ((phai-m)*(phai)>0)) then
      begin
          keu;
              Writeln(‘Nhap bay ba');
              Readin;
              exit;
       end;
       tam : = [trai,phai];
       i: = 0;
       Repeat
            i : = i + 1;
       Until (domino[i].co=tam);
       demphai : = 1;
       demtrai : = 0;
       flag : = flag-[i];
       luuphai[1]: = i;
       sigma: = sigma-domino[i].x-domino[i].y;
       Writeln(‘Wait me a few minutes long’);
       max : = 0;
      min : = 2147483647;
      Find(trai,phai);
      xuat;
      readln
END;
PROCEDURE NHAP;
VAR s : string;
         error : integer;
BEGIN
        Writeln(‘Nhap m');
        Readln(s);
       Val(s,m,error);
        If (error< >0) then
        begin
             Writeln(‘Kieu so sai.Nhap lai');
             Readin;
             exit;
         end;
         Writeln(‘Cong thuc tinh so hinh chu nhat la To hop C(m+1), 2+m+1=(m+1)*(m+2)/2');
         1m : = (m+1)*(m+2) div 2;
         Writeln(‘Va so do bang',1m);
         oknhap : = true;
         flag : = [1..1m];
         1m : = 0;
         sigma : = 0;
         For i : = 0 to m do
             For j : = i to m do
                 begin
                     1m: = lin+1;
                     sigma: = sigma+i+j;
                     domino[1m].x : = i;
                     domino[1m].y: = j;
                     domino[1m].co: = [i,j];
                 end;
          Oknhap : = true;
          readln;
END;
BEGIN
      oknhap : = false;
      Repeat
      clrscr;
      writeln(’Menu’);
      Writeln(1. Cau 1');
      Writeln('2. Can 2 & 3');
      Writeln('3. quit');
      Repeat
          ch: = readkey;
      Until ch in ['1'..'3'];
      Case ch of
           '1' : Nhap;
           '2' : If oknhap then Doing
                Else.
                keu;
           end;
      Until ch='3';
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í ⇔ u888
Kênh 90Phut TV full HD ⇔ https://okvip.training/
ABC8 ⇔ Jun88 ⇔ BET88 ⇔ pg88
kuwin ⇔ link xem truc tiep bong da xoilac tv ⇔ https://104.248.99.177/
8kbet ⇔ xem bóng đá cà khịa tv trực tuyến hôm nay
78win ⇔ ABC8 ⇔ 88clb ⇔ df999
23win ⇔ 789BET ⇔ kuwin ⇔ 79king
New88 ⇔ 789BET ⇔ BJ88 ⇔ 23win
BET88 ⇔ qq88 ⇔ BJ88 ⇔  SHBET
https://789betcom0.com/ ⇔ https://hi88.baby/
OK365 ⇔ https://98win.care/ ⇔ 88clb
nhà cái shbet ⇔ Nhà cái 789bet
bet88 ⇔ F168 ⇔ 23win
hi88 ⇔ GO99 ⇔ BJ88 ⇔ KUBET
https://789club64.com/ ⇔ 99ok ⇔ 33WIN
 ⇔ nhà cái ok365 ⇔ https://fun88.social/
J88 ⇔ 8kbet ⇔ ww88 ⇔ QQ88
33win ⇔ hi88 ⇔ hi88 ⇔ i9bet
ok365 ⇔ https://okvipno1.com/
8K BET ⇔ 8kbet ⇔ https://789club63.com/
https://23win.school/ ⇔ hi88 ⇔ 33win
 ⇔ https://0fun88.com/ ⇔ New88
 ⇔ https://vicky-brown.co.uk/ ⇔ i9bet
18win ⇔ THABET ⇔ 33win ⇔ bk8
HUBETu888 ⇔ https://kubetvn88.com/
https://8kbet25.com/ ⇔ https://uk88.rocks
https://8xbet68.net/ ⇔ https://789bet188.com/
cwin ⇔ KU3933 ⇔ https://hello8880.net/
sunwin ⇔ 33win ⇔ hi88 ⇔ https://sunwin53.com/
SH BET ⇔ 23WIN ⇔ https://ww88.cruises/
F168 ⇔ V9bet ⇔ LUCK8
trang chủ 789bet ⇔ iWin ⇔ https://kuwin.education/
12bet ⇔ https://789club24.com/
https://33win103.com/ ⇔ U888
 ⇔ 88CLB ⇔ https://u888rabit.com/
https://33win102.com/ ⇔ https://hi88.garden/
https://33win100.com/ ⇔ hcm66
https://betvisa8.net/ ⇔ https://hi88.report/
https://hi88.tours/ ⇔ Trang cá cược bóng đá
 ⇔ U888 ⇔ https://789win1.black
https://789bet188.us/ ⇔ https://hello880.net/
https://789club60.com/ ⇔ https://betvisacom2.com/
https://789club60.com/ ⇔ 68gamebai
qh88 đăng nhập ⇔ https://789club24.com/ ⇔ good88
SHBETSHBET ⇔ qh 88 ⇔ 8xbet
sunwin ⇔ 789win ⇔ https://sunwin70.com
https://bet88.football/ ⇔ ABC8
https://go88club13.com/https://789bet188.today/
https://bk8link2.com/ ⇔ https://bk8link3.com/
https://bk8link4.com/ ⇔ https://bk8link5.com/
https://bk8link6.com/ ⇔ https://789bet188.xyz/
https://33win101.com/ ⇔ FABET ⇔ SHBET
https://tp88.finance/ ⇔ https://hi88.gives/
33win ⇔ f168 ⇔ win55 ⇔ J88
ok365 ⇔ https://789bet188.pro/
88CLB ⇔ https://789bet188.cloud/
https://ww88.supply/ ⇔ https://fb88.voyage/
Link vào NEW88 ⇔ https://f8betlv.com/
https://abc8.education/ ⇔  ⇔ 789Bet
https://i9bet.supply/ ⇔ https://bj88.gen.in/
https://88clb.lawyer/ ⇔ https://j8884.com/ ⇔ i9bet
Kubet ⇔ kubet ⇔ j88 ⇔ abc8
Nhà cái SHBET ⇔ https://shbet.law/
https://polodemocratico.info/ ⇔ https://ok365.tours/
https://j88.photography/ ⇔ f168
https://23win.cruises/ ⇔ https://kuwin.support/
https://f168.loans/ ⇔ 8kbet
https://w88okvip.com/ ⇔ ok88
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://new8818.net/ ⇔ 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://new8818.club/
https://new8818.vip/ ⇔ https://new8818.site/
https://new8818.ink/ ⇔ https://ahihi88.host/
https://hi8818.xyz/ ⇔ https://hi8818.us/
https://hi8818.live/ ⇔ https://hi8818.online/
https://hi8818.site/ ⇔ https://hi8818.ink/
https://hi8818.org/ ⇔ 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://j88.shoes/ ⇔ https://f168.party/
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://win55.sh/ ⇔ KING88
https://kubet.graphics/ ⇔ https://79king.gdn/
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