this is for battleship, checking to see if the player can place their ship in the spot. the 'l' and 'r' work find, but not the 'u' and 'd'. those are for left, right, up, down. what it does wrong is, for the up and down, if there is a ship that would block the path for putting another ship down, it will overwrite whatever is in it's way. i put a comment on the spot that *i think* has the logical error in it.
Code:
do
{
if(test==0) cout<<"Invalid direction, please re-enter the info."<<endl<<endl;
test=1;
cout<<"Now choose which direction to finish the ship in (l,r,u,d): ";
cin>>dir;
switch(dir)
{
case 'l':
if(col-size>=1)
{
for(int i=0;i<=size-1;i++)
{
if(board[row][col-i]!='~')
{
test=0;
continue;
}
}
}
else
{
test=0;
continue;
}
if(test=1)
{
for(int x=0;x<=size-1;x++)
{
board[row][col-x]=ship[0];
}
}
break;
case 'r':
if(col+size<=10)
{
for(int i=0;i<=size-1;i++)
{
if(board[row][col+i]!='~')
{
test=0;
continue;
}
}
}
else
{
test=0;
continue;
}
if(test=1)
{
for(int x=0;x<=size-1;x++)
{
board[row][col+x]=ship[0];
}
}
break;
case 'u':
if(row-size<=1)
{
for(int i=0;i<=size-1;i++) //error spot is here, i think
{
if(board[row-i][col]!='~')
{
test=0;
continue;
}
}
}
else
{
test=0;
continue;
}
if(test=1)
{
for(int x=0;x<=size-1;x++)
{
board[row-x][col]=ship[0];
}
}
break;
case 'd':
if(row+size<=10)
{
for(int i=0;i<=size-1;i++)
{
if(board[row+i][col]!='~')
{
test=0;
continue;
}
}
}
else
{
test=0;
continue;
}
if(test=1)
{
for(int x=0;x<=size-1;x++)
{
board[row+x][col]=ship[0];
}
}
}
}while(test==0);