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í
Kênh
90Phut TV full HD ⇔ 32win
Thabet ⇔  ⇔ bk8 ⇔ bet88

78win ⇔ Kubet ⇔ 88CLB ⇔ shbet
789f ⇔ ABC88 ⇔ rikvip ⇔ 8x bet
link xem truc tiep bong da xoilac tv ⇔ okvip
xem bóng đá cà khịa tv trực tuyến hôm nay
MB66 ⇔ Kkwin ⇔  ⇔ Link MB66
 ⇔  ⇔ 8x bet ⇔ hi88
789BET ⇔ tylekeo ⇔ 32 win ⇔ Daga
 ⇔ QQ88 ⇔ hi88 ⇔ b52 club
https://789betcom0.com/ ⇔ https://hi88.baby/
TK88 ⇔ rwin ⇔ b52club ⇔ SHBET
QQ88 ⇔ 32win ⇔ 8kbet ⇔ go88
88bet ⇔ 78win ⇔ j88 ⇔ GK88 ⇔ 32win
Bet88 ⇔ 789Win ⇔ J88 ⇔ bj88
 ⇔  ⇔ bj88 ⇔ uu88
F168 ⇔ bet88 ⇔ QQ88 ⇔ bk8 ⇔ bk8
MB66 ⇔ iwinclub ⇔ 789bet ⇔ net88
keonhacai ⇔ soc88 ⇔ https://j88t3.com/
https://hi88.gives/ ⇔ 23win ⇔ 98win
23WIN ⇔ hi88 ⇔ https://fun88.social/
https://iwinpro.live/ ⇔ https://23win.kim/
https://qq88.fun/ ⇔ https://j88ss.com
rik vip ⇔ v9 bet ⇔ keo nha cai ⇔ u88
 ⇔ https://uk88.rocks
32win ⇔ Vuabet88 ⇔ 88AA ⇔ 98win
https://luongson117.tv ⇔ https://hello8880.net/
u888 ⇔ betvisa ⇔ hi88 ⇔ https://king88aff.com
NOHU ⇔  ⇔ choáng club ⇔ bong 88
https://u888lm.com/ ⇔ https://dt68.cc/ ⇔ bj88
https://ww88.supply/ ⇔ https://f168.com.co/
 ⇔ https://789club24.com/
https://33win103.com/ ⇔ https://f168.group/
https://33win102.com/ ⇔ https://abc8255.com/
https://33win100.com/ ⇔ https://hi88.tours/
https://myeat.net/ ⇔ https://hi88.report/
https://58win1.info/ ⇔ https://hi88.garden/
https://debetso.com/ ⇔ https://hello880.net/
https://789club60.com/ ⇔ https:/nau888.com/
https://f168.dad/ ⇔ dt68 ⇔ 99WIN
fun 88 ⇔ https://789club24.com/ ⇔ Kuwin
33win ⇔ 8xbet ⇔  ⇔ New88
78 win ⇔ KUBET ⇔ 99OK ⇔ 68WIN
ww88 ⇔ 8 day ⇔ 33win ⇔ HUBET
https://33win101.com/ ⇔ SHBET ⇔ BJ88
 ⇔ https://98win.supply/
https://33winpro.me/ ⇔ https://23win.build
https://kuwinvef.me/ ⇔ https://bongvip.space/
https://23win.men/ ⇔ https://new88838.com/
https://nhacaiuytin88.me/ ⇔ https://hb88ai.com/
https://hb88top.com/ ⇔ https://8day111.com/
https://8day112.com/https://789win.voyage/
https://u888.prof/ ⇔ https://69win.me/
https://abc8.house/ ⇔ https://789p.partners/
https://goal123.directory/  ⇔ https://bk8co.net/
https://23wincom.info ⇔ https://j88com.limited
https://j88uk.com ⇔ https://f168.movie/
https://8kbetttt.com/ ⇔ https://f168.law/
https://88bett.vip/ ⇔ https://j88cem.com/
https://qq88pro.vip/ ⇔ https://ww88i.club/
New88 ⇔ https://8kbet25.com ⇔ HUBET
kubet ⇔ https://luongsontv72.com/
https://qq88.gives/ ⇔ 789BET ⇔ ww88
https://88vvcom.net/ ⇔ 789BET
https://32win.vc/ ⇔ 78win ⇔ vegas79
bong88 ⇔ j88 ⇔ j88 ⇔ sunwin ⇔ sunwin
hitclub ⇔ hitclub ⇔ 888b ⇔ 8 day ⇔ go 88
https://f168.giving/ ⇔ s 666 ⇔ QQ88
hi88 ⇔ 79King ⇔ kubet ⇔ 8kbet
7MCN ⇔ Keonhacai55.ws ⇔ RR88
http://79king.ac/ ⇔ https://nhacaiuytin.garden/
https://xx88.ink/ ⇔ https://bk88vn.net/
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