Программа "Морской бой"
Курсовой проект - Компьютеры, программирование
Другие курсовые по предмету Компьютеры, программирование
||
CPU -> ship[i].desp_of_part[j].x + 1 == x1 + k)&&
(CPU -> ship[i].desp_of_part[j].y 1 == y1 ||
CPU -> ship[i].desp_of_part[j].y + 1 == y1))
{
vertical = true;
regen = true;
continue;
}
}
if(vertical)
{
if((CPU -> ship[i].desp_of_part[j].x == x1&&
CPU -> ship[i].desp_of_part[j].y == y1 + k) ||
(y1 + k >= 10))
{
vertical = false;
regen = true;
continue;
}
if((CPU -> ship[i].desp_of_part[j].x 1 == x1 ||
CPU -> ship[i].desp_of_part[j].x + 1 == x1)&&
(CPU -> ship[i].desp_of_part[j].y 1 == y1 + k ||
CPU -> ship[i].desp_of_part[j].y + 1 == y1 + k))
{
vertical = true;
regen = true;
continue;
}
}
}
} while(regen);
if(! vertical)
for (i = 0; i < col_deck; i++)
{
CPU -> ship [col_ship 1].desp_of_part[i].x = x1 + i;
CPU -> ship [col_ship 1].desp_of_part[i].y = y1;
}
else
for (i = 0; i < col_deck; i++)
{
CPU -> ship [col_ship 1].desp_of_part[i].x = x1;
CPU -> ship [col_ship 1].desp_of_part[i].y = y1 + i;
}
return 0;
}
//
int Referee: GoChecking (int x, int y, int n)
{
if (n == 1)
if (! Check(net1, x, y))
return 0;
if (n == 2)
if (! Check(net2, x, y))
return 0;
return 1;
}
//
int Referee: Check (bool net[10] [10], int x, int y)
{
x /= 20; y /= 20;
if (! net[x] [y]) return 0;
return 1;
}
//
int Referee: GoScaning (int x, int y, int n)
{
if (n == 1)
if (! Scan(player, x, y))
return 0;
if (n == 2)
if (! Scan(CPU, x, y))
return 0;
return 1;
}
//
int Referee: Scan (Flot *fl, int x, int y)
{
x /= 20; y /= 20;
for (i = 0; i < 10; i++)
for (j = 0; j ship[i].col_deck; j++)
if (fl -> ship[i].desp_of_part[j].x == x&&
fl -> ship[i].desp_of_part[j].y == y)
{
fl -> ship[i].decks[j] = false;
return 0;
}
return 1;
}
//
int Referee: Miss (int x, int y, int n)
{
x /= 20; y /= 20;
if (n == 1) net1 [x] [y] = false;
if (n == 2) net2 [x] [y] = false;
return 1;
}
//
int Referee: EndRaund (int n)
{
if (n == 2)
Form1 -> Panel2 -> Caption = Вы победили!;
if (n == 1)
Form1 -> Panel2 -> Caption = Вы проиграли!;
game_over = true;
return 0;
}
//
int HandlersOfEvents: Play (TImage *Im1, TImage *Im2)
{
Im1 -> Enabled = true;
Im2 -> Enabled = true;
return 0;
}
//
int HandlersOfEvents: Desposition (int x, int y, TMouseButton Button)
{
if(play) return 0;
x /= 20; y /= 20;
for (k = 0; k < col_deck; k++)
for (i = 0; i < col_ship; i++)
for (j = 0; j ship[i].col_deck; j++)
{
if (Button == mbLeft)
{
if (player -> ship[i].desp_of_part[j].x == x + k&&
player -> ship[i].desp_of_part[j].y == y)
{
ShowMessage (Невозможно выполнить действие!);
return 0;
}
if (x + k >= 10)
{
ShowMessage (Невозможно выполнить действие!);
return 0;
}
if((player -> ship[i].desp_of_part[j].x 1 == x + k ||
player -> ship[i].desp_of_part[j].x + 1 == x + k)&&
(player -> ship[i].desp_of_part[j].y 1 == y ||
player -> ship[i].desp_of_part[j].y + 1 == y))
{
ShowMessage (Невозможно выполнить действие!);
return 0;
}
}
if (Button == mbRight)
{
if (player -> ship[i].desp_of_part[j].x == x&&
player -> ship[i].desp_of_part[j].y == y + k)
{
ShowMessage (Невозможно выполнить действие!);
return 0;
}
if (y + k >= 10)
{
ShowMessage (Невозможно выполнить действие!);
return 0;
}
if((player -> ship[i].desp_of_part[j].x 1 == x ||
player -> ship[i].desp_of_part[j].x + 1 == x)&&
(player -> ship[i].desp_of_part[j].y 1 == y + k ||
player -> ship[i].desp_of_part[j].y + 1 == y + k))
{
ShowMessage (Невозможно выполнить действие!);
return 0;
}
}
}
if (Button == mbLeft)
Draw (position_h, x*20, y*20, 1);
else
Draw (position_v, x*20, y*20, 1);
Generation (CPU, col_ship, col_deck);
if (col_ship == 1) col_deck ;
if (col_ship == 3) col_deck ;
if (col_ship == 6) col_deck ;
if (col_ship == 10) play = true;
if(play) Form1 -> Panel2 -> Caption = Поехали;
col_ship++;
return 0;
};
//
int HandlersOfEvents: Shoot (int x, int y)
{
if (game_over) return 0;
int shoot_player = ShootPlayer (x, y, 2);
if (shoot_player > 0)
do {
x = rand()% 200;
y = rand()% 200;
int shoot_cpu = ShootPlayer (x, y, 1);
if (shoot_cpu > 0)
break;
if (shoot_cpu < 0)
player -> GetStatus();
if (player -> destroy || target_CPU > 19)
{
EndRaund(1);
return 0;
}
} while(true);
else
{
if (shoot_player < 0)
CPU -> GetStatus();
if (CPU -> destroy || target_player > 19)
{
EndRaund(2);
return 0;
}
}
return 0;
}
//
int HandlersOfEvents: ShootPlayer (int x, int y, int n)
{
if(! play) return 0;
if (! GoChecking(x, y, n))
{
if (n == 2) Form1 -> Panel2 -> Caption = Ещё раз! Туда уже стреляли!;
return 0;
}
else if (! GoScaning(x, y, n))
{
Draw (target, x, y, n);
Miss (x, y, n);
if (n == 2)
{
Form1 -> Panel2 -> Caption = Попал! Ещё раз!;
target_player++;
}
else target_CPU++;
return -1;
}
Miss (x, y, n);
if (n == 2) Form1 -> Panel2 -> Caption = Мимо! Ход опонента;
Draw (miss, x, y, n);
return 1;
}
//
int HandlersOfEvents: Draw (String key, int x, int y, int n)
{
TImage *Im;
x /= 20; y /= 20;
if (n == 1) Im = Form1 -> Fild1;
if (n == 2) Im = Form1 -> Fild2;
if (key == target)
{
Im -> Canvas -> Rectangle (x*20, y*20, x*20 + 20, y*20 + 20);
Im -> Canvas -> Brush -> Color = clYellow;
Im -> Canvas -> Rectangle (x*20, y*20 + 20, x*20 + 20, y*20);
Im -> Canvas -> Brush -> Color = clWhite;
}
if (key == miss)
{
Im -> Canvas -> Ellipse (x*20, y*20, x*20 + 20, y*20 + 20);
Im -> Canvas -> Ellipse (x*20 + 5, y*20 + 5, x*20 + 15, y*20 + 15);
}
if (key == position_h)
{
for (i = 0; i < col_deck; i++)
{
Im -> Canvas -> Brush -> Color = clBlue;
Im -> Canvas -> Rectangle (x*20 + i*20, y*20, x*20 + 20 + i*20, y*20 + 20);
player -> ship [col_ship 1].desp_of_part[i].x = x + i;
player -> ship [col_ship 1].desp_of_part[i].y = y;
Im -> Canvas -> Brush -> Color = clWhite;
}
}
if (key == position_v)
{
for (i = 0; i < col_deck; i++)
{
Im -> Canvas -> Brush -> Color = clBlue;
Im -> Canvas -> Rectangle (x*20, y*20 + i*20, x*20 + 20, y*20 + 20 + i*20);
player -> ship [col_ship 1].desp_of_part[i].x = x;
player -> ship [col_ship 1].desp_of_part[i].y = y + i;
Im -> Canvas -> Brush -> Color = clWhite;
}
}
return 0;
}
//
void __fastcall TForm1: Fild2MouseDown (TObject *Sender,
TMouseButton Button, TShiftState Shift, int X, int Y)
{
HE -> Shoot (X, Y);
}
//
void __fastcall TForm1: BitBtn2Click (TObject *Sender)
{
HE -> Play (Fild1, Fild2);
BitBtn2 -> Visible = false;
BitBtn3 -> Visible = true;
Panel2 -> Caption = Расставьте корабли;
}
//
void __fastcall TForm1: Fild1MouseDown (TObject *Sender,
TMouseButton Button, TShiftState Shift, int X, int Y)
{
HE -> Desposition (X, Y, Button);
}
//
void __fastcall TForm1: BitBtn3Click (TObject *Sender)
{
Panel2 -> Caption = Расставьте корабли;
Picture->LoadFromFile(net.bmp);">Fild1 -> Picture -> LoadFromFile (net.bmp);
Picture->LoadFromFile(net.bm