shootlaser.cpp
Published in DevCode 2001. Original code and wording are preserved.
//2-3-03
Laser::Laser(int l, int size)
{
boardsize = size;
if((l >= 0) && ( l < boardsize))
{
mydirection = 'd';
myrow = 0;
mycol = l;
}
else if (l<=19)
{
mydirection = 'l';
myrow = l - size;
mycol = 9;
}
else if (l <=29)
{
mydirection = 'u';
myrow = 9;
mycol = 29-l;
}
else if (l <=39)
{
mydirection = 'r';
myrow = 39 - l;
mycol = 0;
}
}
int Laser::row()
{
return myrow;
}
int Laser::col()
{
return mycol;
}
void Laser::Move()
{
if(mydirection =='d')
myrow = myrow + 1;
if(mydirection == 'u')
myrow = myrow - 1;
if(mydirection == 'r')
mycol = mycol + 1;
if(mydirection == 'l')
mycol = mycol - 1;
}
void Laser::HitSlash()
{
if(mydirection =='u')
{
mycol = mycol + 1;
mydirection = 'r';
}
if(mydirection == 'd')
{
mycol = mycol - 1;
mydirection = 'l';
}
if(mydirection == 'r')
{
myrow = myrow - 1;
mydirection = 'u';
}
if(mydirection == 'l')
{
myrow = myrow + 1;
mydirection = 'd';
}
}
void Laser::HitBack()
{
if(mydirection =='u')
{
mycol = mycol -1;
mydirection = 'l';
}
if(mydirection == 'd')
{
mycol = mycol + 1;
mydirection = 'r';
}
if(mydirection = 'r')
{
myrow = myrow + 1;
mydirection = 'd';
}
if(mydirection = 'l')
{
myrow = myrow - 1;
mydirection = 'l';
}
}
void Board::ShootLaser()
{
int shot = 0;
cout <<" Where do you want to shoot your laser from?"<<endl;
cin >> shot;
Laser lazer(shot, size);
while(( lazer.row() >=0) && ( lazer.col()>= 0) && (lazer.row() <=9) && (lazer.col() <=9))
{
lazer.Move();
if((board[lazer.row()][lazer.col()] =='/')||(board[lazer.row()][lazer.col()] =='s'))
lazer.HitSlash();
if((board[lazer.row()][lazer.col()] =='\\')||(board[lazer.row()][lazer.col()] =='b'))
lazer.HitBack();
if(lazer.row() < 0)
cout<<"The Laser exited at number " << lazer.col() << endl;
if(lazer.col() >= 9)
cout<<"The Laser exited at number " << lazer.row() + 10 << endl;
if(lazer.row() >= 9)
cout<<"The Laser exited at number " << 29 -lazer.col() << endl;
if(lazer.col() < 0)
cout<<"The Laser exited at number " << 39 - lazer.row() << endl;
}
}