Форум программистов
 

Восстановите пароль или Зарегистрируйтесь на форуме, о проблемах и с заказом рекламы пишите сюда - alarforum@yandex.ru, проверяйте папку спам!

Вернуться   Форум программистов > C/C++ программирование > Общие вопросы C/C++
Регистрация

Восстановить пароль
Повторная активизация e-mail

Купить рекламу на форуме - 42 тыс руб за месяц

Ответ
 
Опции темы Поиск в этой теме
Старый 19.12.2012, 18:02   #1
Gog
Новичок
Джуниор
 
Регистрация: 12.12.2012
Сообщений: 2
По умолчанию Задача про Рыбок в Озере

Имеется код программы при запуске которого в озере плавают рыбки двух типов: карась и щука. Необходима сделать так, что бы при сближении щуки к карасю карась удалялся с экрана. Помогите пожалуйста...в программировании вообще не шарю!

#include <stdio.h>
#include <conio.h>
#include <graphics.h>
#include <dos.h>
#include <stdlib.h>

typedef struct
{
int code;
int key;
} message;

class sreda
{
protected:
int x, y, color;
public:
sreda (int ix=0, int iy=0, int icolor=WHITE)
{
x=ix;
y=iy;
color=icolor;
}
int getobjx ();
int getobjy ();
int getobjcolor ();
};

class fish : public sreda
{
protected:
int dx, dy;
int takt, kol_takt;
public:
sreda *container;
fish (int ix=0, int iy=0, int icolor=0, sreda *icont, int idx=0, int idy=0):
sreda (ix, iy, icolor)
{
container=icont;
dx=idx;
dy=idy;
kol_takt=random(50)+10;
takt=0;
}
virtual void show ();
virtual void hide ();
void move ();
void proverka ();
};

class karp : public fish
{
protected:
int xr, yr;
public:
karp (int ix=0, int iy=0, int icolor=WHITE, sreda *icont, int idx=0, int idy=0, int ixr=0, int iyr=0):
fish (ix, iy, icolor, icont, idx, idy)
{
xr=ixr;
yr=iyr;
}
virtual void show ();
virtual void hide ();
};

class predator : public fish
{
protected:
int xr1, yr1;
public:
predator (int ix=0, int iy=0, int icolor=WHITE, sreda *icont, int idx=0, int idy=0, int ixr1=0, int iyr1=0):
fish (ix, iy, icolor, icont, idx, idy)
{
xr1=ixr1;
yr1=iyr1;
}
virtual void show ();
virtual void hide ();
};

void fish :: proverka ()
{
if (getobjx () < 5) {dx=-dx; takt=0; kol_takt=random(20)+10;}
if (getobjy () < 5) {dy=-dy; takt=0; kol_takt=random(20)+10;}
if (getobjx () > 620) {dx=-dx; takt=0; kol_takt=random(20)+10;}
if (getobjy () > 400) {dy=-dy; takt=0; kol_takt=random(20)+10;}
}

int sreda :: getobjx ()
{
return x;
}

int sreda :: getobjy ()
{
return y;
}

int sreda :: getobjcolor ()
{
return color;
}

void fish :: show () {}

void fish :: hide () {}


void fish :: move ()
{
proverka ();
takt++;
if (takt==kol_takt)
{
dx=random (7)-3;
dy=random (7)-3;
takt=0;
kol_takt=random(50)+10;
}
hide ();
x+=dx;
y+=dy;
show ();
}

void karp :: show ()
{
setcolor (color);
setfillstyle (SOLID_FILL, color);
fillellipse (x, y, xr, yr);
}

void karp :: hide ()
{
setcolor (container -> getobjcolor ());
setfillstyle (SOLID_FILL, container ->getobjcolor ());
fillellipse (x, y, xr, yr);
}

void predator :: show ()
{
setcolor (color);
setfillstyle (SOLID_FILL, color);
fillellipse (x, y, xr1, yr1);
}

void predator :: hide ()
{
setcolor (container -> getobjcolor ());
setfillstyle (SOLID_FILL, container -> getobjcolor ());
fillellipse (x, y, xr1, yr1);
}
Gog вне форума Ответить с цитированием
Старый 19.12.2012, 18:03   #2
Gog
Новичок
Джуниор
 
Регистрация: 12.12.2012
Сообщений: 2
По умолчанию

class lake : public sreda
{
protected:
int sizex, sizey;
int kol;
int fillstyle;
fish **comp;
public:
lake ();
lake (int ix, int iy, int icolor, int isizex, int isizey, int ikol, int ifillstyle);
~lake ();
void show ();
void hide ();
void run (message mes);
};

lake :: lake (int ix=0, int iy=0, int icolor=WHITE, int isizex=0, int isizey=0, int ikol=0, int ifillstyle=0):
sreda (ix, iy, icolor)
{
sizex=isizex;
sizey=isizey;
kol=ikol;
fillstyle=ifillstyle;
comp=new fish *[kol];
for (int i=0; i<kol;i++)
{
switch (random (2))
{
case 0: comp [i]=new karp (random (400), random (400), YELLOW, this, random (6)-3, random (6)-3, 10, 10);
break;
case 1: comp [i]=new predator (random (400), random (400), RED, this, random (9)-3, random (9)-3, 30, 20);
break;
}
}
}

lake :: ~lake ()
{
hide ();
for (int i=0; i<kol; i++)
{
if (comp [i])
comp [i] -> ~fish ();
}
delete [] comp;
}

void lake :: show ()
{
setcolor (color);
setfillstyle (fillstyle, color);
bar (x, y, sizex, sizey);
setcolor (BROWN);
setfillstyle (fillstyle, BROWN);
bar (0, 430, sizex, sizey);
for (int i=0; i<kol; i++)
{
if (comp [i])
comp [i] -> show ();
}
}

void lake :: hide ()
{
setcolor (BLACK);
setfillstyle (EMPTY_FILL, BLACK);
bar (x, y, sizex, sizey);
}

void lake :: run (message mes)
{
switch (mes. code)
{
case 0:
{
for (int i=0; i<kol;i++)
{
if (comp [i])
{
comp [i] -> move ();
/* int xcomp=comp [i] -> getobjx ();
if (xcomp > x+sizex-10)
{
comp [i] -> hide ();
comp [i] -> ~fish ();
comp [i] = NULL;
} */
}
else
{
switch (random (2))
{
case 0: comp [i]=new karp (0, random (480), YELLOW, this, random (6)-3, random (6)-3, 10, 10);
break;
case 1: comp [i]=new predator (0, random (480), RED, this, random (9)-3, random (9)-3, 30, 20);
break;
}
}
}
delay (100);
break;
}
case 1:
{
if (mes.key == 'D') color=BLUE;
else if (mes.key == 'N') color=BLACK;
show ();
break;
}
case 2:
{
int i; // ???
if (mes.key == 'K') comp [i] = new karp (50, 50, YELLOW, this, random (6)-3, random (6)-3, 10, 10);
break;
}
case 3: {break;}
}
}

void main ()
{
message mes;
int gdriver=DETECT, gmode, errorcode;
char s;
initgraph (&gdriver, &gmode, " ");
errorcode=graphresult ();
if (errorcode!=grOk)
{
printf ("Graphics error: %s\n", grapherrormsg (errorcode));
printf ("Press any key to halt: ");
getch ();
exit (1);
}
randomize ();
lake l (0, 0, BLUE, 640, 480, 3, SOLID_FILL);
l.show ();
do
{
mes.code=0;
if (kbhit ())
{
s=getch ();
switch (s)
{
case 'D': case 'N':
{
mes.code=1;
mes.key=s;
break;
}
case 'K':
{
mes.code=2;
mes.key=s;
break;
}
case 27:
{
mes.code=3;
break;
}
}
}
l.run (mes);
}
while (mes.code!=3);
l.~lake ();
}
Gog вне форума Ответить с цитированием
Старый 19.12.2012, 18:12   #3
Smitt&Wesson
Старожил
 
Аватар для Smitt&Wesson
 
Регистрация: 31.05.2010
Сообщений: 13,543
По умолчанию

Вы в серьёз думаете, что в этом монстре кто-нибудь будет разбираться "за здорово живёшь"?
Пиши пьяным, редактируй трезвым.
Справочник по алгоритмам С++ Builder
Smitt&Wesson вне форума Ответить с цитированием
Старый 08.05.2013, 01:22   #4
spawn969
 
Регистрация: 09.04.2011
Сообщений: 7
По умолчанию

Получилось реализовать???? Просто у меня подобная задача
spawn969 вне форума Ответить с цитированием
Ответ


Купить рекламу на форуме - 42 тыс руб за месяц



Похожие темы
Тема Автор Раздел Ответов Последнее сообщение
Задача про хорды Assasin92 Помощь студентам 11 03.04.2012 21:58
Задача про мандарины Thunder Dragon Паскаль, Turbo Pascal, PascalABC.NET 6 26.03.2012 21:48
Задача про списки Алекс12345 Паскаль, Turbo Pascal, PascalABC.NET 2 20.08.2011 19:33
Задача про треугольник YO$YA Помощь студентам 10 15.11.2008 20:29