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
RR88 ⇔ 79king ⇔ bk8 ⇔ 123b

78win ⇔ SV388 ⇔ cakhiatv ⇔ f168
789f ⇔ Cakhia TV ⇔ rikvip ⇔ 8x bet
kết quả bóng đá ⇔ keonhacai ⇔ okvip
b52club ⇔ Kkwin ⇔ ko66 ⇔ Link MB66
https://88betcom.pro/ ⇔ 8x bet ⇔ 
789BET ⇔ shbet ⇔ 32 win ⇔ Daga
j88 ⇔ 789win link ⇔ hi88 ⇔ b52 club
https://789betcom0.com/ ⇔ https://hi88.baby/
TK88 ⇔ rwin ⇔ luongsontv ⇔ SHBET
QQ88 ⇔ qq88 ⇔ i9 Bet ⇔ go88
88bet ⇔ 78win ⇔ j88 ⇔  ⇔ 32win
Bet88 ⇔ 789Win ⇔ HUBET ⇔ bj88
https://789bett1.blog/ ⇔ bj88 ⇔ uu88
F168 ⇔ bet88 ⇔ QQ88 ⇔ bk8 ⇔ bk8
MB66 ⇔ iwinclub ⇔ MB66 ⇔ net88
 ⇔ soc88 ⇔ https://j88t3.com/
https://hi88.gives/ ⇔ 23win ⇔ 8kbet
789F ⇔ hi88 ⇔ https://fun88.social/
https://qq88z.net/ ⇔ https://hubet3d.com/
https://qq88.fun/ ⇔ f168 ⇔ HUBET
rik vip ⇔ v9 bet ⇔ keo nha cai ⇔ u88
https://bshbet.com/ ⇔ https://uk88.rocks
MM88 ⇔ Vuabet88 ⇔ 88AA ⇔ 98win
https://luongson117.tv ⇔ https://hello8880.net/
u888 ⇔ betvisa ⇔ 789p ⇔ https://king88aff.com
red88.com ⇔ choáng club ⇔ bong 88
https://u888lm.com/ ⇔ sv388 ⇔ 23win
https://ww88.supply ⇔ https://f168.com.co/
sin88.com ⇔ 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://new88c.co/ ⇔ https://hello880.net/
https://789club60.com/ ⇔ 789WIN
https://f168.dad/ ⇔  ⇔ f168 ⇔ f168
fun 88 ⇔ https://789club24.com/ ⇔ Kuwin
King 88 ⇔ 8xbet ⇔ 7m ⇔ j88 ⇔ EV88
78 win ⇔ KUBET ⇔ 99OK ⇔ 68WIN
88i ⇔ 8 day ⇔ 33win ⇔ HUBET
https://33win101.com/ ⇔ SHBET ⇔ BJ88
five88.com ⇔ https://shbet.gg/ ⇔ SHBET
https://33winpro.me/ ⇔ https://23win.build
alo789 ⇔ hubet ⇔ UU88
https://23win.men/ ⇔ nhà cái 8Kbet
 ⇔ https://789win.voyage/
https://u888.prof/ ⇔ https://ww88i.club/
https://hi88.voyage/ ⇔ https://bk8co.net/
cakhiatv ⇔ https://23wincom.info
https://hi88o.com/ ⇔ https://f168.law/
https://88bett.vip/ ⇔ https://j88.ventures/
https://rcc.eu.com/ ⇔ https://j88com.limited/
New88 ⇔ https://j88.now/ ⇔ hi88
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
789win ⇔ https://nhacaiuytin.garden/
https://xx88.ink/ ⇔ https://bk88vn.net/
https://23win.kim/ ⇔ https://69vn.co.in/
tỷ lệ kèo nhà cái hôm nay ⇔ King88 link mới
nhà cái UU88 ⇔ https://qq88.markets/
https://j88ss.com ⇔ https://qq88.studio/
888B ⇔ GK88 ⇔ nổ hũ đổi thưởng
https://mm88.blue/ ⇔ trực tiếp bóng đá
Hay 88 ⇔ https://rr882.net/ ⇔ King88
https://789winlem.com/ ⇔ https://cakhiatv88.net/
https://shbet.is/ ⇔ https://13win.london/
https://789win.fund/ ⇔ https://nhacaiuytinso1.net/
nohu ⇔ https://abcvip.ru.com/ ⇔ RR88
https://king88.international/ ⇔ 33win ⇔ 98WIN
https://qq88.racing/ ⇔ https://j88uk.com
https://hubest.co/ ⇔ https://23winvn.co/
https://muranoglass-shop.cn.com/ ⇔ 
soi kèo nhà cái ⇔ https://king88.giving/
https://bet88.ventures/ ⇔
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