void gotoxy(int, int); //declare a function called gotoxy
void clrscr(void); //declare clear screen function
void textcolor(int); //declare text colour function
void hline(int, int, int, int);
void vline(int, int, int, int);
void invader1(int, int, int); //draw invader(x,y,colour)
void invader2(int, int, int); //draw invader(x,y,colour)
char z = 219;
int a[80][25];
int b[80][25];
int x, y, c = 0, i;
void main(void) //main program start
{
int choice;
do
{
do
{ // menu
textcolor(15);
clrscr();
gotoxy(30, 18);
cout << "Press (1) Flying Ironman";
gotoxy(30, 20);
cout << "Press (2) Game of Life";
gotoxy(30, 22);
cout << "Press (3) Random blocks";
gotoxy(30, 24);
cout << "Press (4) Exit";
gotoxy(30, 26);
cout << "Enter 1, 2, 3, or 4 - ";
choice = _getch();
} while (choice != '1' && choice != '2' && choice != '3' && choice != '4');
//------------------------------------------------------------------1-----------------------------------------------------------
if (choice == '1') {
int x = 10, y = 10, vx = 1, vt = 0;
DWORD t1, t2;
t1 = GetTickCount();
do
{
t2 = GetTickCount();//check time
if (t2 - t1>100)
{
t1 = t2;
if (KEY_DOWN(VK_LEFT))
{
x--;
if (x<1) x = 1;
}
if (KEY_DOWN(VK_RIGHT))
{
x++;
if (x>49) x = 49;
}
if (KEY_DOWN(VK_UP))
{
y--;
if (y<1) y = 1;
}
if (KEY_DOWN(VK_DOWN))
{
y++;
if (y>23) y = 23;
}
//now draw
clrscr();
if (vt == 0)//vt is a flip flop switch to draw vader1 then vader2
{
invader1(x, y, 10);
vt = 1;
}
else
{
invader2(x, y, 12);
vt = 0;
}
}
} while (!KEY_DOWN(VK_ESCAPE));
}
//------------------------------------------------------------------2-----------------------------------------------------------
if (choice == '2') {
//randomise the random number generator
srand((unsigned)time(NULL));
//randomly set array a
for (x = 0; x<80; x++)
{
for (y = 0; y<25; y++)
{
a[x][y] = (int)((rand() / (float)RAND_MAX)*2.0);
//cout<<a[x][y]<<" ";
}
}
{
do {
clrscr();
//view array a
for (x = 0; x<80; x++)
{
for (y = 0; y<25; y++)
{
gotoxy(x, y);
if (a[x][y] == 1) cout << z;
textcolor(3);
}
}
//calculate next generation
for (x = 1; x<79; x++)
{
for (y = 1; y<24; y++)
{
c = 0;
if (a[x - 1][y - 1] == 1) c++;//top left
if (a[x][y - 1] == 1) c++;//top middle
if (a[x + 1][y - 1] == 1) c++;//top right
if (a[x - 1][y] == 1) c++;//middle left
if (a[x + 1][y] == 1) c++;//middle right
if (a[x - 1][y + 1] == 1) c++;//bottom left
if (a[x][y + 1] == 1) c++;//bottom middle
if (a[x + 1][y + 1] == 1) c++;//bottom right
//game of life rules
if (a[x][y] == 1 && c<2) b[x][y] = 0;
if (a[x][y] == 1 && (c == 2 || c == 3)) b[x][y] = 1;
if (a[x][y] == 1 && c>3) b[x][y] = 0;
if (a[x][y] == 0 && c == 3) b[x][y] = 1;
}
}
//copy b to a
for (x = 0; x<80; x++)
{
for (y = 0; y<25; y++)
{
a[x][y] = b[x][y];
}
}
Sleep(500);
if (KEY_DOWN(VK_SPACE))
{
for (x = 0; x<80; x++)
{
for (y = 0; y<25; y++)
{
a[x][y] = rand() % 2;
//cout<<a[x][y]<<" ";
}
}
}
} while (!KEY_DOWN(VK_ESCAPE));
}
}
//------------------------------------------------------------------3-----------------------------------------------------------
if (choice == '3')
{
//randomise the random number generator
srand((unsigned)time(NULL));
//randomly set array a
for (x = 0; x < 80; x++)
{
for (y = 0; y < 25; y++)
{
a[x][y] = (int)((rand() / (float)RAND_MAX)*2.0);
//cout<<a[x][y]<<" ";
}
}
{
for (i = 0; i < 1000; i++) {
clrscr();
//view array a
for (x = 0; x < 80; x++)
{
for (y = 0; y < 25; y++)
{
gotoxy(x, y);
if (a[x][y] == 1) cout << z;
textcolor(rand() % 16);
}
}
Sleep(500);
}
}
}
while (!KEY_DOWN(VK_ESCAPE));} //this ESC is the problem
while (choice != '4');
}//end of the main code
//put functions here
//Alter the code below at your peril!!!!
//It defines the textcolor,gotoxy and clrscr functions
void textcolor(int c)
{
HANDLE hConsole;
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsole, c);
}
void gotoxy(int x, int y) //define gotoxy function
{
static HANDLE hStdout = NULL;
COORD coord;