Thread: help me please, im stuck

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

    help me please, im stuck

    please look at this code for battleship, look at the 'u' and 'd' part. something goes wrong there, if a ship would, under correct circumstances, block you from putting a ship in a certain direction it will just overwrite whats there. the 'l' and 'r' work fine, but not the 'u' and 'd'. left, right, up, down, you get 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 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++) //error 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];
    				}
    			}
    		}
    		
    	}while(test==0);

  2. #2
    Aran
    Guest
    you might want to consider putting the row+size<=10 like this: (row + size) <= 10


    order of operations might be the problem.

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    14
    Hi!
    I would not even consided reading that code line by line.. but I accidentaly stumbled over a line that says

    if(test=1)

    and I'll bet my right leg that's, at least one of the things that might mess up your program.

    the equalilty operator is == and not =, = is the assignement operator.

    /Laos

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 10-23-2006, 07:22 PM
  2. string array stuck:(
    By mass in forum C Programming
    Replies: 18
    Last Post: 05-22-2006, 04:44 PM
  3. Program stuck in infinite loop-->PLEASE HELP
    By Jedijacob in forum C Programming
    Replies: 5
    Last Post: 03-26-2005, 12:40 PM
  4. Stuck on random generating
    By Vegtro in forum C++ Programming
    Replies: 3
    Last Post: 10-01-2003, 07:37 PM
  5. stuck ky
    By JaWiB in forum Tech Board
    Replies: 2
    Last Post: 06-15-2003, 08:28 PM