Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Курсовая_Программа «Морской бой».doc
Скачиваний:
24
Добавлен:
23.06.2014
Размер:
153.6 Кб
Скачать
  1. Описание входных и выходных данных

Ввод исходных данных (координаты расположения точки хода) осуществляется в формате A1 с клавиатуры по запросу (тип переменных, соответствующих этим значениям,Char,Integer).

Вывод данных осуществляется стандартными командами TurboPascal.

Программное и аппаратное обеспечение для нормальной работы программы стандартное – TurboPascal– при отсутствии исполнимого файла. Операционная система любая из семействаWindowsс режимом эмуляцииMS-DOS.

  1. Руководство пользователя

Ввод исходных данных осуществляется с клавиатуры по запросу программы.

Данные необходимо вводить строго придерживаясь указанного формата: A1 – координата по оси у(A,B,C,D,E,F,G,H,I,J),координата по осиx(1-10), разделять значения ненужно. Окончание ввода строки данных подтвердить стандартно с помощью клавишиENTER.

  1. Заключение

Создание алгоритма для решения задач какого-либо типа, его представление исполнителю в удобной для него форме – это творческий акт. Алгоритм может быть представлен различными способами: на разговорном естественном язык; на языке блок-схем; на языке программирования. Выбор и разработка алгоритма и численного метода решения задачи имеют важнейшее значение для успешной работы над программой. Тщательно проработанный алгоритм решения задачи – необходимое условие эффективной работы по составлению алгоритму. Данная программа реализует алгоритм и визуализации игры “Морской бой” в наиболее понятной форме.

  1. Список литературы

1. Рапаков Г.Г., Ржеуцкая С.Ю.TurboPascalдля студентов и школьников. – Спб.: БХВ-Петербург, 2004. – 352 с.

2. Епанешников А., Епанешников В. Программирование в среде TurboPascal7.0. – М.: Диалог-мифи, 1995. – 288 с.

3. Николаев А.Б., Акатнова Л.А., Алексахин С.В. Турбо-Паскаль в примерах. – М.: Просвещение, 2002. – 111 с.

4. Аляев. Практикум по алгоритмизации и программированию на языке Паскаль. – М.: Финансы и статистика, 2004. - 528 с.

  1. Приложение 1. Листинг программы

uses CRT;

type TPole = array[1..10,1..10] of ShortInt;

var Pole: TPole;

type PPole = array[1..10,1..10] of ShortInt;

var Pale: PPole;

type IPole = array[1..10,1..10] of ShortInt;

var Pule: IPole;

procedure InitP (var Pule: IPole);

var X, Y: Integer;

begin

Randomize;

for X := 1 to 10 do

for Y := 1 to 10 do

Pule[X,Y] := -1;

end; {proc Inits}

function Freedoms (x, y: Integer; Pule: IPole): Boolean;

const d: array[1..8,1..2] of Integer =

((0,1),(1,0),(0,-1),(-1,0),(1,1),(-1,1),(1,-1),(-1,-1));

var i: Integer;

dx, dy: Integer;

begin

if (x > 0) and (x < 11) and (y > 0) and (y < 11) and (Pule[x,y] = -1) then

begin

for i := 1 to 8 do

begin

dx := x + d[i,1];

dy := y + d[i,2];

if (dx > 0) and (dx < 11) and (dy > 0) and (dy < 11) and (Pule[dx,dy] > -1) then

begin

Freedoms := False;

Exit;

end; {if}

end; {for}

Freedoms := True;

end else Freedoms := False;

end; {func Freedoms}

procedure Ship (var Pule: IPole);

var N, M, i: Integer;

x, y, kx, ky: Integer;

a, c: Integer;

B: Boolean;

begin

InitP (Pule);

for N := 3 downto 0 do

for M := 0 to 3 - N do

repeat

x := Random (10) + 1;

y := Random (10) + 1;

kx := Random (2);

if kx = 0 then ky := 1

else ky := 0;

B := True;

for i := 0 to N do

if not Freedoms (x + kx * i, y + ky * i, Pule) then B := False;

if B then

for i := 0 to N do

Pule[x+kx*i,y+ky*i] := 0;

until B;

end; {proc Ship}

procedure Init (var Pole: TPole);

var X, Y: Integer;

begin

Randomize;

for X := 1 to 10 do

for Y := 1 to 10 do

Pole[X,Y] := -1;

end; {proc Init}

function Freedom (x, y: Integer; Pole: TPole): Boolean;

const d: array[1..8,1..2] of Integer =

((0,1),(1,0),(0,-1),(-1,0),(1,1),(-1,1),(1,-1),(-1,-1));

var i: Integer;

dx, dy: Integer;

begin

if (x > 0) and (x < 11) and (y > 0) and (y < 11) and (Pole[x,y] = -1) then

begin

for i := 1 to 8 do

begin

dx := x + d[i,1];

dy := y + d[i,2];

if (dx > 0) and (dx < 11) and (dy > 0) and (dy < 11) and (Pole[dx,dy] > -1) then

begin

Freedom := False;

Exit;

end; {if}

end; {for}

Freedom := True;

end else Freedom := False;

end; {func Freedom}

procedure Ships (var Pole: TPole);

var N, M, i: Integer;

x, y, kx, ky: Integer;

B: Boolean;

begin

Init (Pole);

for N := 3 downto 0 do

for M := 0 to 3 - N do

repeat

x := Random (10) + 1;

y := Random (10) + 1;

kx := Random (2);

if kx = 0 then ky := 1

else ky := 0;

B := True;

for i := 0 to N do

if not Freedom (x + kx * i, y + ky * i, Pole) then B := False;

if B then

for i := 0 to N do

Pole[x+kx*i,y+ky*i] := 0;

until B;

end; {proc Ships}

var Play: TPole;

Kor: array[1..4] of Integer;

State: Integer;

Len: Integer;

Pkx, Pky: Integer;

Px, Py: Integer;

procedure Start;

var I: Integer;

begin

Init (Play);

Ships (Pole);

State := 1;

for I := 1 to 4 do

Kor[I] := 5 - I;

end; {proc Start}

function Killed (x, y: Integer): Boolean;

var K: Char;

begin

Write (Char (y + 64), x, ' (y/n)? ');

repeat

K := ReadKey;

if K = #27 then Halt (0);

until K in ['y','Y','n','N'];

WriteLn (K);

if (K = 'y') or (K = 'Y') then

begin

Killed := True;

Pule[x,y] := 1;

end;

if (K = 'n') or (K = 'N') then

begin

Killed := False;

Pule[x,y] := -2;

end;

end; {func Killed}

function MaxShip: Integer;

var i: Integer;

begin

for i := 1 to 4 do

if Kor[i] > 0 then MaxShip := i;

end; {func MaxShip}

function GameOver: Boolean;

var I: Integer;

begin

for I := 1 to 4 do

if Kor[I] > 0 then

begin

GameOver := False;

Exit;

end; {if}

GameOver := True;

end; {func GameOver}

function State1 (var x, y: Integer): Boolean;

var k, i, n, m: Integer;

B, St: Boolean;

tmp: Integer;

begin

repeat

repeat

x := Random (10) + 1;

y := Random (10) + 1;

until Freedom (x, y, Play);

Pkx := Random (2);

Pky := Pkx - 1;

for m := 1 to 2 do

begin

i := 0;

k := 0;

for n := 1 to 2 do

begin

while Freedom (x + Pkx * i, y + Pky * i, Play) do

begin

Inc (k);

Inc (i);

end; {while}

Pkx := -Pkx;

Pky := -Pky;

i := 1;

end; {for}

B := k >= MaxShip;

if B then Break;

tmp := Pkx;

Pkx := Pky;

Pky := tmp;

end; {for}

until B;

St := Killed (x, y);

State1 := St;

if St then

begin

Px := x;

Py := y;

Len := 1;

if MaxShip > 1

then State := 2

else Dec (Kor[Len]);

end; {if}

end; {func State1}

function State2 (var x, y: Integer): Boolean;

var Old: ShortInt;

tmp: Integer;

k: Integer;

St: Boolean;

begin

Old := Play[Px,Py];

Play[Px,Py] := -1;

repeat

if not Freedom (Px + Pkx, Py + Pky, Play) and not Freedom (Px - Pkx, Py - Pky, Play) then

begin

tmp := Pkx;

Pkx := Pky;

Pky := tmp;

end; {if}

if Random (2) = 0 then

begin

x := Px + Pkx;

y := Py + Pky;

end else

begin

x := Px - Pkx;

y := Py - Pky;

end; {if}

until Freedom (x, y, Play);

St := Killed (x, y);

State2 := St;

if St then

begin

Len := 2;

State := 1;

if MaxShip > 2

then State := 3

else Dec (Kor[Len]);

end else

begin

k := 4;

if not Freedom (Px + 1, Py, Play) then Dec (k);

if not Freedom (Px - 1, Py, Play) then Dec (k);

if not Freedom (Px, Py + 1, Play) then Dec (k);

if not Freedom (Px, Py - 1, Play) then Dec (k);

if k < 2 then State := 1;

end; {if}

Play[Px,Py] := Old;

end; {func State2}

function State3 (var x, y: Integer): Boolean;

var Old: ShortInt;

i: Integer;

B, St: Boolean;

begin

for i := 1 to 2 do

begin

x := Px;

y := Py;

while Play[x,y] = 1 do

begin

Inc (x, Pkx);

Inc (y, Pky);

end; {while}

Old := Play[x-Pkx,y-Pky];

Play[x-Pkx,y-Pky] := -1;

B := Freedom (x, y, Play);

Play[x-Pkx,y-Pky] := Old;

if B then Break;

Pkx := -Pkx;

Pky := -Pky;

end; {for}

if B then

begin

St := Killed (x, y);

State3 := St;

if St then

begin

Inc (Len);

if Len = MaxShip then

begin

Dec (Kor[Len]);

State := 1;

end; {if}

end;

end else

begin

Dec (Kor[Len]);

State := 1;

State3 := State1 (x, y);

end; {if}

end; {func State3}

function Comput: Boolean;

var x, y: Integer;

Kill: Boolean;

begin

repeat

case State of

1: Kill := State1 (x, y);

2: Kill := State2 (x, y);

3: Kill := State3 (x, y);

end; {case}

if Kill then Play[x,y] := 1

else Play[x,y] := -2;

until not Kill or GameOver;

Comput := GameOver;

end; {func Comput}

function Life (var Res: Boolean): Boolean;

var K1, K2: Char;

d,n,k,m,c,a,x, y: Integer;

B: Boolean;

begin

repeat

Write ('Your course: ');

repeat

K1 := ReadKey;

if K1 = #27 then Halt (0);

until K1 in ['a'..'j','A'..'J'];

Write (K1);

if K1 < 'a' then y := Ord (K1) - 64

else y := Ord (k1) - 96;

repeat

K2 := ReadKey;

if K2 = #27 then Halt (0);

until K2 in ['0'..'9'];

x := Ord (K2) - 48;

if x = 0 then x := 10;

WriteLn (x);

B := True;

if (Pole[x,y] = -2) or (Pole[x,y] = 1) then

begin

WriteLn ('Erroneous course...');

B := False;

end; {if}

until B;

if Pole[x,y] = 0 then

begin

WriteLn ('Shot down!');

Pole[x,y] := 1;

Pale[x,y] := 1;

{Pule[x,y] := 1;}

Res := True;

end else

begin

WriteLn ('Miss.');

Pole[x,y] := -2;

Pale[x,y] := -2;

{ Pule[x,y] := -2;}

Res := False;

end; {if}

Life := False;

for x := 1 to 10 do

for y := 1 to 10 do

if Pole[x,y] = 0 then Life := True;

Write (' 1234567890');

WriteLn (' 1234567890');

Write (' ЪДДДДДДДДДДї');

WriteLn (' ЪДДДДДДДДДДї');

for c := 1 to 10 do

begin

Write (Char (c + 64), 'і');

for a := 1 to 10 do

begin

case Pale[a,c] of

-2: Write ('.');

1: write ('*');

else Write (' ');

end; {case}

end;

Write ('і');

Write (Char (c + 64), 'і');

for a := 1 to 10 do

begin

case Pule[a,c] of

-2: Write ('.');

0: Write ('ю');

1: write ('*');

else Write (' ');

end; {case}

end;

WriteLn ('і');

end; {for}

Write (' АДДДДДДДДДДЩ');

WriteLn (' АДДДДДДДДДДЩ');

end; {func Life}

procedure Print;

var x, y: Integer;

begin

WriteLn (' 1234567890');

WriteLn (' ЪДДДДДДДДДДї');

for y := 1 to 10 do

begin

Write (Char (y + 64), 'і');

for x := 1 to 10 do

begin

case Pole[x,y] of

-2: Write ('.');

0: Write ('ю');

1: write ('*');

else Write (' ');

end; {case}

end; {for}

WriteLn ('і');

end; {for}

WriteLn (' АДДДДДДДДДДЩ');

end; {proc Print}

{=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-}

var K: Char;

GM, R: Boolean;

c,a:integer;

N, M, i: Integer;

begin

ClrScr;

repeat

WriteLn;

Write ('Playing (y/n)? ');

K := ReadKey;

if K = #27 then Halt (0);

WriteLn (K);

if (K = 'Y') or (K = 'y') then

begin

Ship(Pule);

Start;

WriteLn ('I am ready!');

repeat

repeat

GM := Life (R);

until not GM or not R;

if GM then

begin

GM := not Comput;

if not GM then

begin

WriteLn ('You have lost.');

WriteLn;

Print;

end; {if}

end else

begin

WriteLn ('You winner!');

end; {if}

until not GM;

end; {if}

until (K = 'N') or (K = 'n');

end.