#include <iostream>
#include <stdio.h>
#include <string.h>
#include <cstring>
#include <windows.h>
usingnamespace std;
int main()
{
HANDLE hConsole;
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
//--------------------------- local variables ------------------------------
int ball[80][24];
void gotoxy(int, int);
int noOfCoords = 0;
int origX, origY;
char mov1, mov2;
int x, y;
int W = 80, H = 24;
//----------------------------- user prompt --------------------------------
SetConsoleTextAttribute(hConsole,249);
cout << "Enter the Width of the window for the ball bounce in (between 0 and " << W << ": ";
cin >> W;
cout << endl;
cout << "Enter the Height of the window for the ball to bounce in (between 0 and " << H << ": ";
cin >> H;
cout << endl;
cout << "Enter the X coordinate for the ball (between 0 and " << W << ": ";
cin >> origX;
cout << endl;
cout << "Enter the Y coordinate for the ball (between 0 and " << H << ": ";
cin >> origY;
cout << endl;
cout << "Would you like the ball to move [u]p or [d]own? ";
cin >> mov1;
cout << endl;
cout << "would you like the ball to move [l]eft or [r]ight? ";
cin >> mov2;
cout << endl;
system("CLS"); //clear the screen
x = origX; //make x and y equal the original entries
y = origY;
//initialize the ball array's elements to 0
for(int row = 0; row < W; row++)
{ for(int col = 0; col < H; col++)
ball[row][col] = 0;
}
gotoxy(x, y);
SetConsoleTextAttribute(hConsole,252); //go to the coordinate
cout<<"O"; //print O at the coordinate
//change the ball element to 1 if not 0 and increase the counter
if (ball[x][y] == 0)
{ ball[x][y] = 1;
noOfCoords++;
}//endif
//------------------------- move the ball once -----------------------------
if (mov1 == 'u')
{ if (y > 0) { y--; }//endif
else { mov1 = 'd'; }//endif
}//endif
if (mov1 == 'd')
{ if (y < H) { y++; }//endif
else { mov1 = 'u'; }//endif
}//endif
if (mov2 == 'r')
{ if (x < W) { x++; }//endif
else { mov2 = 'l'; }//endif
}//endif
if (mov2 == 'l')
{ if (x > 0) { x--; }//endif
else { mov2 = 'r'; }//endif
}//endif
gotoxy(x, y); //go to coordinate
cout << "O"; //print O at the coordinate
//change the ball element to 1 if not 0 and increase the counter
if (ball[x][y] == 0)
{
ball[x][y] = 1;
noOfCoords++;
}//endif
//------------------------ ALGORITHM to move ball ---------------------------
//while the coordinates are not the same as entered
while ((x != origX) || (y != origY))
{ //move the ball in the correct direction
if (mov1 == 'u')
{ if (y > 0) { y--; }//endif
else { mov1 = 'd'; }//endif
}//endif
if (mov1 == 'd')
{ if (y < H) { y++; }//endif
else { mov1 = 'u'; }//endif
}//endif
if (mov2 == 'r')
{ if (x < W) { x++; }//endif
else { mov2 = 'l'; }//endif
}//endif
if (mov2 == 'l')
{ if (x > 0) { x--; }//endif
else { mov2 = 'r'; }//endif
}//endif
gotoxy(x, y);
cout << "O"; //print O at the coordinate
if (ball[x][y] == 0)
{ ball[x][y] = 1;
noOfCoords++;
}//endif
system("CLS");
}//endwhile
//output the number of coordinates the O passed through
cout << endl;
cout << "The ball crossed " << noOfCoords << " unique coordinates.";
cout << endl;
system("PAUSE");
return 0;
}//endmain
//gotoxy definition
void gotoxy(int eex, int eey)
{
COORD coord;
coord.X = eex;
coord.Y = eey;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}//endgotoxy
I want, for example, the introduction of a second ball moving horizontal direction random movement and I want to also make the first movement of the ball moving vertically random movement and collided if the first ball second ball game stop working and prints on the screen that the game is over