Tin học Game "Caro 2 máy" 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,623
744
22
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.

Alooooo!
Đột nhiên mình lại nghĩ ra một ý tưởng tuyệt với, cho 2 chương trình Pascal chạy song song với nhau mà vẫn kết nối với nhau được.
Mình sẽ Show từng ảnh nha.
upload_2018-7-14_14-46-23.png
upload_2018-7-14_14-46-58.png
upload_2018-7-14_14-47-29.png
upload_2018-7-14_14-48-3.png

Đây là Code Game nha
Thử giải thích xem tại sao lại có thể chơi được như vậy nha

Mã:
 Program Caro_2;
 uses crt;
 const
      width = 15;
      height = 15;

 var f: text;
     Player, Turn, Count: integer;
     Board: array[1..width,1..height] of integer;
     ht: array[1..2] of string[3];
     Hited: array[1..width*height] of string[3];
     rv: string[1];


 Procedure LoadGame;
 var txt: string;
 Begin
      reset(f);
      Readln(f,txt);
      close(f);
      if txt <> 'Connected' then
      begin
           Player := 1;
           Rewrite(f);
           Write(f,'Connected');
           Close(f);
           Gotoxy(1,1);Write('Waiting....');
           repeat
                 reset(f);
                 readln(f,txt);
                 Close(f);
           until txt = 'True';
           ReWrite(f);
           clrscr;
           Writeln('Connected!');
           Delay(1000);
           Write('You are x');
           Delay(1000);
      end
      else
      begin
           Player := 2;
           ReWrite(f);
           Write(f,'True');
           Close(f);
           Writeln('Connected!');
           Delay(1000);
           Write('You are o');
           Delay(1000);
      end;
      clrscr;
 End;

 Procedure CreateBoard;
 var i,j: integer;
 Begin
      for i := 1 to width do
          for j := 1 to height do
          Board[i,j] := 0;
 End;

 Procedure DrawBoard;
 var i,j: integer;
 Begin
      for i := 1 to width do
          for j := 1 to height do
          begin
               Gotoxy(3+i*2,1+j);
               if Board[i,j] = 1 then write('x')
               else if Board[i,j] = 2 then write('o')
               else Write('.');
          end;
      for i := 1 to width do
      begin
           Gotoxy(3+i*2,1);Write(chr(i+96));
      end;
      for j := 1 to height do
      begin
           Gotoxy(2,1+j);Write(j);
      end;
 End;

 Procedure DrawLastHit;
 Begin
      if Player = 1 then
      begin
           Gotoxy(3,Height + 3);
           clreol;
           Write('Your last hit: ',ht[1]);
           Gotoxy(3,Height + 4);
           clreol;
           Write('Your opponent last hit: ',ht[2]);
      end
      else
      begin
           Gotoxy(3,Height + 3);
           clreol;
           Write('Your last hit: ',ht[2]);
           Gotoxy(3,Height + 4);
           clreol;
           Write('Your opponent last hit: ',ht[1]);
      end
 End;

 Function Hit(h: string): Boolean;
 var x,y,c,i: integer;
 Begin
      x := 0;
      y := 0;
      Hit := True;
      for i := 1 to width do
      if chr(i+96) = h[1] then
      begin
           x := i;
           break;
      end;
      Val(Copy(h,2,2),y,c);
      if (Board[x,y] = 0) and (x <> 0) and (y in[1..Height]) and (c = 0) then
      begin
      Ht[Turn] := h;
      Board[x,y] := Turn;
      DrawBoard;
      DrawLastHit;
      end
      else Hit := False;
 End;

 Function WinGame: Integer;
 var x,y,t,dr: integer;
 Begin
      WinGame := 0;
      Dr := 0;
      if Turn = 1 then t := 2 else t := 1;
      for x := 1 to width do
          for y := 1 to height do
          if Board[x,y] in [1..2] then
          begin
               if x <= width - 4 then
               if (Board[x+1,y] and Board[x+2,y] and Board[x+3,y]
               and Board[x+4,y]) = Board[x,y] then WinGame := T;

               if y <= height - 4 then
               if (Board[x,y+1] and Board[x,y+2] and Board[x,y+3]
               and Board[x,y+4]) = Board[x,y] then WinGame := T;

               if (x <= width - 4) and (y <= height - 4) then
               if (Board[x+1,y+1] and Board[x+2,y+2] and Board[x+3,y+3]
               and Board[x+4,y+4]) = Board[x,y] then WinGame := T;

               if (x >= 5) and (y <= height - 4) then
               if (Board[x-1,y+1] and Board[x-2,y+2] and Board[x-3,y+3]
               and Board[x-4,y+4]) = Board[x,y] then WinGame := T;

               inc(Dr);
          end;
      if Dr = width*height then WinGame := 3;
 End;

 Procedure ReadHit;
 var h: string[3];
 Begin
      repeat
      Gotoxy(3,height+6);
      clreol;
      Write('Where do you move? ');
      readln(h);
      until Hit(h);
      inc(Count);
      Hited[Count] := h;
      Rewrite(f);
      Write(f,h);
      Close(f);
      Gotoxy(3,height+6);
      clreol;
      if Turn = 1 then Turn := 2 else if Turn = 2 then Turn := 1;
      Delay(500);
 End;

 Procedure ReadFile;
 var h: string;
 Begin
      reset(f);
      Readln(f,h);
      Close(f);
      if h <> '' then
      begin
           Hit(h);
           inc(Count);
           Hited[Count] := h;
           ReWrite(f);
           if Turn = 1 then Turn := 2 else if Turn = 2 then Turn := 1;
      end;
 End;

 Procedure ReView;
 var i: integer;
 Begin
      clrscr;
      Turn := 1;
      CreateBoard;
      for i := 1 to Count do
      begin
           Hit(Hited[i]);
           Delay(750);
           if Turn = 1 then Turn := 2 else Turn := 1;
      end;
 End;

 BEGIN
      clrscr;
      Assign(f,'Caro2.txt');
      {Rewrite(f);}
      Turn := 1;
      Count := 0;
      LoadGame;
      CreateBoard;
      DrawBoard;
      repeat
      if Turn <> Player then Readfile
      else
      ReadHit;
      until WinGame <> 0;
      Gotoxy((3+width) - 6,(height+1) div 2);
      if Player = WinGame then Write('Y O U  W I N')
      else if WinGame <> 3 then write('Y O U  L O S E')
      else Write('D R A W  G A M E');
      Gotoxy(3,Height + 7);Write('Do you want to review (y = yes)? ');
      readln(rv);
      if rv = 'y' then ReView;
 END.
 
Top Bottom