我编了个两人对战的五子棋程序,但判断胜负的子程序除了些问题,请大家帮忙看看。注:此程序必需用FreePascal编译。
uses crt,dos;
type ty=array[1..30,1..79]of char;
var i,j,k,x,y:integer;
ch,c:char;
a:ty;
f:boolean;
t:text;
procedure go(var ch:char;a:ty);
var a2,b,c,k,i,j:shortint;
begin
ch:='+';
for i:=1 to 30 do
for j:=1 to 79 do
begin
a2:=0;
for k:=1 to 4 do
begin
if (a[i,j+k*2]=a[i,j])and not (a[i,j] in ['+','-']) then inc(a2);
if (a[i+k,j]=a[i,j])and not(a[i,j] in ['+','-']) then inc(b);
if (a[i+k,j+k*2]=a[i,j])and not(a[i,j] in ['+','-']) then inc©;
end;
if a2=4 then begin ch:=a[i,j];{write(t,' ',ch,' ');}exit;end;
if b=4 then begin ch:=a[i,j];{write(t,' ',ch,' ');}exit;end;
if c=4 then begin ch:=a[i,j];{write(t,' ',ch,' ');}exit;end;
end;
end;
procedure b1; {*,B}
begin
textcolor(black);
ch:=readkey;
while not((ch='h')and(a[x,y]='+')) do
begin
if ch='q' then begin f:=false;exit;end;
if ch='l' then inc(y,2)
else if ch='k' then inc(x)
else if ch='j' then dec(y,2)
else if ch='i' then dec(x);
gotoxy(y,x);
ch:=readkey;
end;
write('*');
write(t,'B ',x,' ',y,' ');
a[x,y]:='B';
gotoxy(y,x);
end;
procedure b2; {o,W}
begin
textcolor(white);
ch:=readkey;
while not((ch='0')and(a[x,y]='+')) do
begin
if ch='q' then begin f:=false;exit;end;
if ch='6' then inc(y,2)
else if ch='5' then inc(x)
else if ch='4' then dec(y,2)
else if ch='8' then dec(x);
gotoxy(y,x);
ch:=readkey;
end;
write('o');
writeln(t,'W ',x,' ',y,';');
a[x,y]:='W';
gotoxy(y,x);
end;
begin
fillchar(a,sizeof(a),' ');
textbackground(lightgreen);
textcolor(lightred);
assign(t,'five.txt');
rewrite(t);
for i:=1 to 30 do
begin
write('+');
for j:=1 to 39 do write('-+');
writeln;
end;
for i:=1 to 30 do
for j:=1 to 79 do
if odd(j) then a[i,j]:='+'
else a[i,j]:='-';
gotoxy(1,1);
x:=1;
y:=1;
f:=true;
while f do
begin
b1;
go(c,a);
if c='*' then
begin
writeln('Black win!!!');
readln;
writeln(t);
writeln(t,'Black win');
break
end
else if c='o' then
begin
writeln('White win!!!');
readln;
writeln(t);
writeln(t,'White win');
break;
end;
if not f then break;
b2;
go(c,a);
if c='*' then
begin
writeln('Black win!!!');
readln;
writeln(t,'Black win');
break;
end
else if c='o' then
begin
writeln('White win!!!');
readln;
writeln(t,'White win');
break;
end;
end;
close(t);
close(output);
assign(output,'five.txt');
append(output);
writeln;
writeln;
for i:=1 to 30 do
begin
for j:=1 to 79 do
write(a[i,j],' ');
writeln;
end;
close(output);
end.
uses crt,dos;
type ty=array[1..30,1..79]of char;
var i,j,k,x,y:integer;
ch,c:char;
a:ty;
f:boolean;
t:text;
procedure go(var ch:char;a:ty);
var a2,b,c,k,i,j:shortint;
begin
ch:='+';
for i:=1 to 30 do
for j:=1 to 79 do
begin
a2:=0;
for k:=1 to 4 do
begin
if (a[i,j+k*2]=a[i,j])and not (a[i,j] in ['+','-']) then inc(a2);
if (a[i+k,j]=a[i,j])and not(a[i,j] in ['+','-']) then inc(b);
if (a[i+k,j+k*2]=a[i,j])and not(a[i,j] in ['+','-']) then inc©;
end;
if a2=4 then begin ch:=a[i,j];{write(t,' ',ch,' ');}exit;end;
if b=4 then begin ch:=a[i,j];{write(t,' ',ch,' ');}exit;end;
if c=4 then begin ch:=a[i,j];{write(t,' ',ch,' ');}exit;end;
end;
end;
procedure b1; {*,B}
begin
textcolor(black);
ch:=readkey;
while not((ch='h')and(a[x,y]='+')) do
begin
if ch='q' then begin f:=false;exit;end;
if ch='l' then inc(y,2)
else if ch='k' then inc(x)
else if ch='j' then dec(y,2)
else if ch='i' then dec(x);
gotoxy(y,x);
ch:=readkey;
end;
write('*');
write(t,'B ',x,' ',y,' ');
a[x,y]:='B';
gotoxy(y,x);
end;
procedure b2; {o,W}
begin
textcolor(white);
ch:=readkey;
while not((ch='0')and(a[x,y]='+')) do
begin
if ch='q' then begin f:=false;exit;end;
if ch='6' then inc(y,2)
else if ch='5' then inc(x)
else if ch='4' then dec(y,2)
else if ch='8' then dec(x);
gotoxy(y,x);
ch:=readkey;
end;
write('o');
writeln(t,'W ',x,' ',y,';');
a[x,y]:='W';
gotoxy(y,x);
end;
begin
fillchar(a,sizeof(a),' ');
textbackground(lightgreen);
textcolor(lightred);
assign(t,'five.txt');
rewrite(t);
for i:=1 to 30 do
begin
write('+');
for j:=1 to 39 do write('-+');
writeln;
end;
for i:=1 to 30 do
for j:=1 to 79 do
if odd(j) then a[i,j]:='+'
else a[i,j]:='-';
gotoxy(1,1);
x:=1;
y:=1;
f:=true;
while f do
begin
b1;
go(c,a);
if c='*' then
begin
writeln('Black win!!!');
readln;
writeln(t);
writeln(t,'Black win');
break
end
else if c='o' then
begin
writeln('White win!!!');
readln;
writeln(t);
writeln(t,'White win');
break;
end;
if not f then break;
b2;
go(c,a);
if c='*' then
begin
writeln('Black win!!!');
readln;
writeln(t,'Black win');
break;
end
else if c='o' then
begin
writeln('White win!!!');
readln;
writeln(t,'White win');
break;
end;
end;
close(t);
close(output);
assign(output,'five.txt');
append(output);
writeln;
writeln;
for i:=1 to 30 do
begin
for j:=1 to 79 do
write(a[i,j],' ');
writeln;
end;
close(output);
end.

