Tin học Game Ô Ăn Quan viết bằng Pascal

trà nguyễn hữu nghĩa

Cựu Mod Vật Lí |Cây bút Thơ|Thần tượng VH
Thành viên
14 Tháng năm 2017
3,974
7,619
744
21
Phú Yên
Trường THPT Lương Văn Chánh
[TẶNG BẠN] TRỌN BỘ Bí kíp học tốt 08 môn
Chắc suất Đại học top - Giữ chỗ ngay!!

ĐĂNG BÀI NGAY để cùng trao đổi với các thành viên siêu nhiệt tình & dễ thương trên diễn đàn.

Game này viết lâu rồi mà giờ mới hoàn thành được mọi người ạ.
Mã:
 Program O_An_Quan;
 uses crt,dos;
 const
      Trai = -1;
      Phai = 1;
      Nguoi = 1;
      May = 0;
      X0 = 5;
      Y0 = 10;
      speed = 1000;
      Time = 20;
 var
    OCo, OCoAo: array[0..1,0..5] of integer;
    diem,diemao: array[0..1] of integer;
    nc,x,tg,demtg: integer;
    k: char;

 Procedure setcusor(bot,top: byte);
 var regs : registers;
 begin
 regs.ah :=1;
 regs.ch:=bot;
 regs.cl:=top;
 intr($10,regs);
 end;

 Procedure TaoBanCo(x,y: integer);
 Begin
      Textcolor(White);
      Gotoxy(x,y);
      Write('ÚÄÄÄÄÂÄÄÄÂÄÄÄÂÄÄÄÂÄÄÄÂÄÄÄÂÄÄÄÄ¿');
      Gotoxy(x,y+1);
      Write('³    ³   ³   ³   ³   ³   ³    ³');
      Gotoxy(x,y+2);
      Write('³    ÃÄÄÄÅÄÄÄÅÄÄÄÅÄÄÄÅÄÄÄ´    ³');
      Gotoxy(x,y+3);
      Write('³    ³   ³   ³   ³   ³   ³    ³');
      Gotoxy(x,y+4);
      Write('ÀÄÄÄÄÁÄÄÄÁÄÄÄÁÄÄÄÁÄÄÄÁÄÄÄÁÄÄÄÄÙ');
 End;

 Procedure InCo(x,y: integer);
 var i: integer;
 Begin
      clrscr;
      TextColor(Yellow);
      gotoxy(13,3);write('GAME O AN QUAN');
      TaoBanCo(X0,Y0);
      for i := 1 to 5 do
      begin
           Textcolor(Red);
           gotoxy(X0 + 3 + (6-i) * 4,Y0+1);
           if OCo[may,i] <> 0 then
           Write(OCo[May,i])
           else write('  ');
           Gotoxy(X0 + 27,Y0 + 2);
           if OCo[may,0] <> 0 then
           Write(OCo[May,0])
           else write('  ');
           textcolor(Blue);
           gotoxy(X0 + 3 + i * 4,Y0+3);
           if OCo[nguoi,i] <> 0 then
           Write(OCo[Nguoi,i])
           else write('  ');
           Gotoxy(X0 + 2,Y0 + 2);
           if OCo[nguoi,0] <> 0 then
           Write(OCo[Nguoi,0])
           else write('  ');
      end;
      if x > -1 then
      begin
           if nc = nguoi then
           textcolor(Cyan)
           else TextColor(LightRed);
           if x = nguoi then
           gotoxy(X0 + 3 + (y)*4,Y0 + 5)
           else gotoxy(X0 + 3 + (6-y)*4,Y0 -1 );
           write(Chr(31-x));
      end;
      TextColor(Brown);
      Gotoxy(X0 + 11,Y0-3);write('May: ',Diem[may]);
      Gotoxy(X0 + 11,Y0+7);write('Ban: ',Diem[nguoi]);
 End;

 Function Doiben(ben: integer): integer;
 Begin
      if ben = nguoi then ben := may else ben := nguoi;
      Doiben := ben;
 End;

 Procedure Chup_An(x,y,huong: integer);
 var i,j: integer;
 Begin
      i := x;
      j := y;
      if y <> 0 then
      begin
           if huong = phai then
           begin
                inc(j);
                if j > 5 then begin i := doiben(i);j := 0;end;
                if OCo[i,j] <> 0 then
                begin
                     Inc(diem[nc],Oco[i,j]);
                     OCo[i,j] := 0;
                     InCo(x,y);
                     Delay(speed);
                     inc(j);
                     if j > 5 then begin i := doiben(i);j := 0;end;
                     if (OCo[i,j] = 0) then Chup_An(i,j,huong);
                end;
           end
           else
           if huong = trai then
           begin
                Dec(j);
                if OCo[i,j] <> 0 then
                begin
                     inc(diem[nc], Oco[i,j]);
                     OCo[i,j] := 0;
                     InCo(x,y);
                     Delay(speed);
                     Dec(j);
                     if j < 0 then begin i := doiben(i);j := 5;end;
                     if (OCo[i,j] = 0) then Chup_An(i,j,huong);
                end;
           end;
      end;
 End;

 Procedure Gieo(x,y,huong: integer);
 var vt,nc,sco: integer;
 Begin
   if y <> 0 then
   begin
      nc := x;
      vt := y;
      sco := Oco[x,y];
      OCo[x,y] := 0;
      if huong = phai then
      begin
           inc(vt);
           if vt > 5 then begin nc := doiben(nc);vt := 0;end;
           while sco > 0 do
           begin
                inc(OCo[nc,vt]);
                Inco(nc,vt);
                delay(speed);
                inc(vt);
                if vt > 5 then begin nc := doiben(nc);vt := 0;end;
                dec(sco);
           end;
           if OCo[nc,vt] <> 0 then gieo(nc,vt,huong)
           else Chup_an(nc,vt,huong);
      end
      else
      if huong = trai then
      begin
           Dec(vt);
           if vt >= 0 then
           begin
           while sco > 0 do
           begin
                inc(OCo[nc,vt]);
                Inco(nc,vt);
                delay(speed);
                Dec(vt);
                if vt < 0 then begin nc := doiben(nc);vt := 5;end;
                dec(sco);
           end;
           if OCo[nc,vt] <> 0 then gieo(nc,vt,huong)
           else Chup_an(nc,vt,huong);
           end;
      end;
    end;
 End;

 Procedure SaoChep;
 var i,j: integer;
 Begin
      for i := 0 to 1 do
          for j := 0 to 5 do
          OCoAo[i,j] := OCo[i,j];
 End;

 Procedure Chup_AnAo(x,y,huong: integer);
 var i,j: integer;
 Begin
      i := x;
      j := y;
      if y <> 0 then
      begin
           if huong = phai then
           begin
                inc(j);
                if j > 5 then begin i := doiben(i);j := 0;end;
                if OCoAo[i,j] <> 0 then
                begin
                     diemao[nc] := OcoAo[i,j];
                     OCoAo[i,j] := 0;
                     inc(j);
                     if j > 5 then begin i := doiben(i);j := 0;end;
                     if (OCoAo[i,j] = 0) then Chup_AnAo(i,j,huong);
                end;
           end
           else
           if huong = trai then
           begin
                Dec(j);
                if OCoAo[i,j] <> 0 then
                begin
                     diemao[nc] := OcoAo[i,j];
                     OCoAo[i,j] := 0;
                     Dec(j);
                     if j < 0 then begin i := doiben(i);j := 5;end;
                     if (OCoAo[i,j] = 0) then Chup_AnAo(i,j,huong);
                end;
           end;
      end;
 End;

 Procedure GieoAo(x,y,huong: integer);
 var vt,nc,sco: integer;
 Begin
   if y <> 0 then
   begin
      nc := x;
      vt := y;
      sco := OcoAo[x,y];
      OCoAo[x,y] := 0;
      if huong = phai then
      begin
           inc(vt);
           if vt > 5 then begin nc := doiben(nc);vt := 0;end;
           while sco > 0 do
           begin
                inc(OCoAo[nc,vt]);
                inc(vt);
                if vt > 5 then begin nc := doiben(nc);vt := 0;end;
                dec(sco);
           end;
           if OCoAo[nc,vt] <> 0 then gieoAo(nc,vt,huong)
           else Chup_anAo(nc,vt,huong);
      end
      else
      if huong = trai then
      begin
           Dec(vt);
           if vt >= 0 then
           begin
           while sco > 0 do
           begin
                inc(OCoAo[nc,vt]);
                Dec(vt);
                if vt < 0 then begin nc := doiben(nc);vt := 5;end;
                dec(sco);
           end;
           if OCoAo[nc,vt] <> 0 then gieoAo(nc,vt,huong)
           else Chup_anAo(nc,vt,huong);
           end;
      end;
    end;
 End;

 Function NguoiGieoAo: Integer;
 var max,i,j,k: integer;
     OC: array[0..1,0..5] of integer;
 Begin
      nc := nguoi;
      max := -100;
      for k := 0 to 1 do
          for j := 0 to 5 do OC[k,j] := OCoAo[k,j];
      For i := 1 to 5 do
      begin
           for k := 0 to 1 do
               for j := 0 to 5 do OCoAo[k,j] := OC[k,j];
           if OCoAo[nguoi,i] <> 0 then
           begin
                diemao[nguoi] := 0;
                GieoAo(nguoi,i,phai);
                if diemao[nguoi] > max then
                max := diemao[nguoi];

                for k := 0 to 1 do
                    for j := 0 to 5 do OCoAo[k,j] := OC[k,j];

                diemAo[nguoi] := 0;
                GieoAo(nguoi,i,trai);
                if diemao[nguoi] > max then
                max := diemao[nguoi];
           end;
      end;
      for k := 0 to 1 do
          for j := 0 to 5 do OCoAo[k,j] := OC[k,j];
      NguoiGieoAo := max;
 End;

 Function TrongAo(nchoi: integer): Boolean;
 var i: integer;
 Begin
      TrongAo := True;
      for i := 1 to 5 do
          if OCoAo[nchoi,i] <> 0 then begin TrongAo := False;break;end;
 End;

 Procedure MayGieo;
 var max,vt,i,dm,dn,huong: integer;
 Begin
      max := -100;
      vt := 1;
      For i := 1 to 5 do
      begin
           SaoChep;
           if OCoAo[may,i] <> 0 then
           begin
                diemao[may] := 0;
                nc := may;
                GieoAo(may,i,phai);
                if TrongAo(may) then DiemAo[may] := diemAo[may] - 5;
                dm := diemao[may];
                dn := nguoigieoAo;
                if dm - dn > max then
                begin
                     max := dm - dn;
                     vt := i;
                     huong := phai
                end;

                SaoChep;
                diemao[may] := 0;
                nc := may;
                GieoAo(may,i,trai);
                if TrongAo(may) then DiemAo[may] := diemAo[may] - 5;
                dm := diemao[may];
                dn := nguoigieoAo;
                if dm - dn > max then
                begin
                     max := dm - dn;
                     vt := i;
                     huong := trai;
                end;
           end;
      end;
      nc := may;
      InCo(may,vt);
      Delay(speed);
      Gieo(may,vt,huong);
 End;

 Function Trong(nchoi: integer): Boolean;
 var i: integer;
 Begin
      Trong := True;
      for i := 1 to 5 do
          if OCo[nchoi,i] <> 0 then begin Trong := False;break;end;
 End;

 Procedure RaiHat;
 var i,j: integer;
 Begin
      for i := 0 to 1 do
      if Trong(i) then
      begin
           if diem[i] >= 5 then
           begin
                Diem[i] := diem[i] - 5;
                for j := 1 to 5 do
                begin OCo[i,j] := 1;InCo(i,j);Delay(speed);end;
           end
           else
           begin
                for j := 1 to diem[i] do
                begin OCo[i,j] := 1;InCo(i,j);Delay(speed);end;
                Diem[i] := 0;
           end;
           InCo(-1,0);
      end;
 End;

 Procedure TinhDiem;
 var i,j: integer;
 Begin
      for i := 0 to 1 do
          for j := 1 to 5 do
          Diem[i] := Diem[i] + OCo[i,j];
 End;

 Procedure TinhThoigian;
 var i: integer;
 Begin
      Gotoxy(5,20);
      TextColor(Cyan);
      write('Thoi gian: ');
      TextColor(LightGreen);
      for i := 1 to Time do
      if i <= tg then write(#219)
      else write(#32);
      inc(demtg);
      if demtg >= 250 then
      begin
           demtg := 0;
           Dec(tg);
      end;
 End;

 Procedure KhoiTao;
 var i: integer;
 Begin
      for i := 1 to 5 do
      begin
           OCo[may,i] := 5;
           OCo[Nguoi,i] := 5;
      end;
      OCo[May,0] := 10;
      OCo[Nguoi,0] := 10;
      diem[may] := 0;
      diem[nguoi] := 0;
      nc := nguoi;
      x := 1;
      tg := Time;
      demtg := 0;
 End;

 BEGIN
      TextMode(Co40);
      SetCusor(32,0);
      clrscr;
      KhoiTao;
      InCo(-1,0);
      repeat
            TinhThoiGian;
            k := #0;
            if keypressed then k := readkey;
            if (k = #75) and (x > 1) then
            begin
                 Dec(x);
                 gotoxy(X0,Y0+5);clreol;
            end;
            if (k = #77) and (x < 5) then
            begin
                 Inc(x);
                 gotoxy(X0,Y0+5);clreol;
            end;
            if (k = #13) and (Oco[nguoi,x] <> 0) then
            begin
                 repeat
                       if keypressed then k := readkey;
                       TinhThoiGian;
                 until (k = #75) or (k = #77) or (k = #27);
                 nc := nguoi;
                 if k = #75 then gieo(nguoi,x,trai)
                 else if k = #77 then gieo(nguoi,x,phai);
                 if k <> #27 then
                 if (OCo[may,0] <> 0) or (OCo[nguoi,0] <> 0) then
                 begin
                      RaiHat;
                      Maygieo;
                      Inco(-1,0);
                      RaiHat;
                      nc := nguoi;
                      tg := Time;
                 end;
            end;
            textcolor(Yellow);
            gotoxy(X0 + 3 + x*4,Y0 + 5);
            write(#30);
      until (k = #27) or ((OCo[may,0] = 0) and (OCo[nguoi,0] = 0)) or (tg = 0);
      if (k <> #27) and (tg <> 0) then
      begin
           TinhDiem;
           readln;
           clrscr;
           Gotoxy(15,12);
           TextColor(LightRed);
           if diem[nguoi] > diem[may] then write('Ban Thang Roi')
           else if Diem[nguoi] < diem[may] then Write('Ban Thua Roi')
           else Write('Hoa roi nha');
           readln;
      end
      else if k <> #27 then
      begin
           Delay(Speed);
           clrscr;
           Gotoxy(15,12);
           TextColor(LightRed);
           Write('Ban Thua Roi');
           readln;
      end;

 END.
Cách chơi: Di chuyển bằng phím mũi tên rồi nhấn Enter, sau đó bấm tiếp hướng gieo để gieo nha.
Demo cho thèm chơi nè :D
upload_2019-1-14_23-36-3.png
 
Last edited:
Top Bottom