игра морской бой.(играть должны два компьютера друг с другом через программу-сервер)есть исходный код(ниже) и рандомная стрельба, но нужно переделать стрельбу из рандомной в стрельбу по какому либо правилу. (но не по порядку(слева направо-сверху вниз). помогите!!!
procedure TForm1.FormActivate(Sender: TObject); Var i,j:integer; begin ClientSocket1.Active:=true; for i:=0 to 9 do for j:=0 to 9 do begin StringGrid1.Cells[j,i]:=''; StringGrid3.Cells[j,i]:=''; matr_hod_1[i,j]:=0; matr_hod_2[i,j]:=0; ship_2[i,j]:=0; ship_1[i,j]:=0; end; end;
procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState); begin //процедура прорисовки if StringGrid1.Cells[ACol,ARow]='1' then begin with StringGrid1.Canvas do begin Draw(Rect.Left, Rect.Top, Image1.Picture.Graphic); end; end; if StringGrid1.Cells[ACol,ARow]='5' then begin with StringGrid1.Canvas do begin Draw(Rect.Left, Rect.Top, Image2.Picture.Graphic); end; end; end;
procedure TForm1.Data11Click(Sender: TObject); Var f:TextFile; s:string; i,k,p,j:byte; begin OpenDialog1.Execute; //считываем координаты кораблей if OpenDialog1.FileName<>'' then begin AssignFile(f,OpenDialog1.FileName); Reset(f); k:=0;p:=0; while not eof(f) do begin Readln(f,s); inc(p); for i:=1 to 20 do if (s[i]='0') or (s[i]='1') then begin inc(k);ship_1[p,k]:=StrToInt(s[i]);end; k:=0; end; CloseFile(f); for i:=1 to 10 do for j:=1 to 10 do if IntToStr(ship_1[i,j])='1' then StringGrid1.Cells[j-1,i-1]:=IntToStr(ship_1[i,j]); end; // MainMenu1.Items.Items[0].Items[1].Items[0].Enabled:=false; end;
procedure TForm1.New1Click(Sender: TObject); Var i,j:integer; begin //очистка экрана for i:=0 to 9 do for j:=0 to 9 do begin StringGrid1.Cells[j,i]:=''; StringGrid3.Cells[j,i]:=''; end; i_hod:=0;j_hod:=0; MainMenu1.Items.Items[0].Items[1].Enabled:=true; MainMenu1.Items.Items[0].Items[1].Enabled:=true; end;
procedure TForm1.Close1Click(Sender: TObject); begin Close; end;
procedure TForm1.ClientSocket1Connect(Sender: TObject; Socket: TCustomWinSocket); begin Form1.Caption:='Connect is successfull!!!'; end;
procedure TForm1.ClientSocket1Read(Sender: TObject; Socket: TCustomWinSocket); label 1; Var s,f:string; zn:array [1..5] of string; i,k:integer; begin //mes_1 - возвращение координат хода,подбит не подбит s:=ClientSocket1.Socket.ReceiveText; if s='value_1' then begin mode:='auto';BitBtn1.Click;end; k:=0; s:=s+','; f:=s; if (Copy(s,length(s)-5,5)='hands') or (Copy(s,length(s)-4,4)='auto') and (s<>'') then begin 1: for i:=1 to length(s) do if s[i]=',' then begin inc(k);zn[k]:=Copy(s,1,i-1);Delete(s,1,i);goto 1;end; if (zn[1]='1') and (zn[2]='1') then begin StringGrid3.Cells[StrToInt(zn[4])-1,StrToInt(zn[3])-1]:='5'; end; //мы сходили и попали if (zn[1]='0') and (zn[2]='0') then begin StringGrid1.Cells[StrToInt(zn[4])-1,StrToInt(zn[3])-1]:='5'; end;
//мы сходили и промахнулись if (zn[1]='0') and (zn[2]='1') then begin StringGrid3.Cells[StrToInt(zn[4])-1,StrToInt(zn[3])-1]:='0'; end; if (zn[1]='Win 1') then begin ShowMessage('Игрок №1 победил!'); end; end; if (Copy(f,length(f)-4,4)='auto') and (zn[1]='auto') and (zn[2]='') then ClientSocket1.Socket.SendText('Hod_1,'+IntToStr(1)+','+IntToStr(1)+','+'auto'); if (Copy(f,length(f)-4,4)='auto') and (zn[1]='1') and (zn[2]='1') then begin BitBtn1.Enabled:=false;mode:='auto';BitBtn1.Click;Delete(f,length(f)-4,4); end; if Copy(f,length(f)-5,5)='hands' then begin mode:='hands';BitBtn1.Enabled:=true;Delete(f,length(f)-5,5); end; end;
procedure TForm1.StringGrid3DrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState); //процедура прорисовки begin if StringGrid3.Cells[ACol,ARow]='1' then begin with StringGrid3.Canvas do begin Draw(Rect.Left, Rect.Top, Image1.Picture.Graphic); end; end; if StringGrid3.Cells[ACol,ARow]='5' then begin with StringGrid3.Canvas do begin Draw(Rect.Left, Rect.Top, Image2.Picture.Graphic); end; end; if StringGrid3.Cells[ACol,ARow]='0' then begin with StringGrid3.Canvas do begin Image3.Stretch:=true; Draw(Rect.Left, Rect.Top, Image3.Picture.Graphic); end; end; end;
procedure TForm1.BitBtn1Click(Sender: TObject); begin repeat i_hod:=RandomRange(1,12); //получаем координаты кораблей j_hod:=RandomRange(1,12); until (matr_hod_1[i_hod,j_hod]<>1) and (i_hod<>11) and (i_hod<>12) and (j_hod<>11) and (j_hod<>12); ClientSocket1.Socket.SendText('Hod_1,'+IntToStr(i_hod)+','+IntToStr(j_hod)+','+mode); //кто ходил,отсылаем координаты,сигнал режима end;
procedure TForm1.ClientSocket1Error(Sender: TObject; Socket: TCustomWinSocket; ErrorEvent: TErrorEvent; var ErrorCode: Integer); begin ErrorCode:=0; end;
end.
и провертте плз на ошибки.одна выходит постоянно а в чём она не пойму.
andriano
27.07.2008 0:32
Сформулируй правило, по которому следует осуществлять стрельбу.
Гость
19.09.2008 5:09
Думаю с этим как раз у него и проблема, ему нужен алгоритм стрельбы если я не ошибаюсь. Как вариант сделай так - простреливать по линии, параллельной побочной диагонали, от края 3 клетки, от этой линии еще три клетки и так пока не закончится поле или не попадешь по кораблю. Потом аналогично, только не по 3 клетки, а по 2 отступаешь, и так далее. Суть в "сканировании" поля на 4х-палубники, потом 3х, 2х и так далее.
Это текстовая версия — только основной контент. Для просмотра полной версии этой страницы, пожалуйста, нажмите сюда.