1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
|
#include <ctime>
#include <cstdlib>
#include<iostream>
#include<cmath>
#include<iomanip>
#include<fstream>
using namespace std;
int stepX( int randx, int randy, int l, int r);
int stepY( int randx, int randy, int l, int r);
int main() {
int randxA=0, randyA=0, randxB=0, randyB=0; // random value of x and y
// the first object is of type
// A and second object is of type B
int xA=0, xB=0; // x and y postition
int yA=0, yB=0;
int n,N,uA=0,dA=0,lA=0,rA=0,uB=0,dB=0,lB=0,rB=0;
int z;
int xstep;
int ystep;
srand(time(0));
for (int i=1 ; i<=100 ; i++){
int ran= rand() %2;
if (ran==1){
xstep = stepX( randxA, randyA, lA, rA);
ystep=0;
}
else if (ran==1){
xstep=0;
ystep=stepY( randxA, randyA, dA, uA);
}
xA=xA + xstep;
yA=yA + xstep;
cout << setw(11) << i << " " << randxA << " " << setw(4) << xA << " " << yA << endl;
}
}
int stepX( int randx, int randy, int l, int r){
randx = rand() % 2;
randy = 0;
if ( randx == 0 ){
randx=randx-1; // moves left
l=l+1;
}else if ( randx==1){
r=r+1; // moves right
}
}
int stepY( int randx, int randy, int d, int u){
randx=0;
randy = rand() % 2;
if ( randy == 0 ){
randy=randy-1; // moves left
d=d+1;
}else if ( randy==1){
u=u+1; // moves right
}
}
|