Thread: help me please, here is the code

  1. #1
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753

    help me please, here is the code

    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);

  2. #2
    Rambling Man
    Join Date
    Jan 2002
    Posts
    1,050
    Basically what I would do is check to see what spot the player is at then check to see if the spot where the player wants to move to is open. If it is not open do not allow the move and if it is open allow the move. If the move was accepted then redisplay the array with the changed postion of the player. Repeat process.

    Code:
    do
     {
      cin >> move;
        if(move == bad() )
          {
             num = 0;
           }
        else if(move == good() )
           {
              num = 1;
           }
        if(num == 1)
         {
             display();
          }
      }while(game != end);
    That would be some basic psuedo code for you. Hopefully that will help you out.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extended ASCII Characters in an RTF Control
    By JustMax in forum C Programming
    Replies: 18
    Last Post: 04-03-2009, 08:20 PM
  2. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM