/**************************************************                 
  InitBoard                 
  Randomly position a single-cell ship on each board line.                      
  When done, set the global variable "gShipLocations" to a                 
  comma-delimited list of the ship positions, and                 
  return to the Firing card to the user can fire some torpedos.                 
**************************************************/                 
                 
extern function InitBoard () {                    

  var shiploc, shiplocs, x, y;
 
  var width = WMLBrowser.getVar("iwidth");
  var height = WMLBrowser.getVar("iheight");
  width = Lang.parseInt(width);
  height = Lang.parseInt(height);
// Check for a valid board size.   

//if ((height >= 1)&&(height <= 5)&&(width >= 5)&&(width <= 10)) {       

// Initialize the global variables

    WMLBrowser.setVar ( "gNumMoves", 0 );  
    WMLBrowser.setVar ( "gBoardWidth", width );
    WMLBrowser.setVar ( "gBoardHeight", height );

// Initialize one ship per board line, map zero-origin x & y            
// coordinates into a single zero-origin integer index.             

    for ( y = 0; y < height; y++ ) {        
      x = Lang.random ( width - 1 );               
      shiploc  = MapCoords ( width, x, y );                 
      shiplocs = shiplocs + shiploc + ",";         
    }
                
    WMLBrowser.setVar ( "gShipLocations", shiplocs );          
        
// Set the length of each line of the playing board      
          
    WMLBrowser.setVar ( "gLine1", SetLineLength ( 0 < height, width ) );        
    WMLBrowser.setVar ( "gLine2", SetLineLength ( 1 < height, width ) );        
    WMLBrowser.setVar ( "gLine3", SetLineLength ( 2 < height, width ) );      
    WMLBrowser.setVar ( "gLine4", SetLineLength ( 3 < height, width ) );      
    WMLBrowser.setVar ( "gLine5", SetLineLength ( 4 < height, width ) );      
    WMLBrowser.setVar ( "gLine6", SetLineLength ( 5 < height, width ) ); 
         
// Go display the game board.     
         
    WMLBrowser.go ( "bships.wml#display" );
    WMLBrowser.refresh();
//}
  
// Invalid board size. Display error message and return to start card.

//  Dialogs.alert ("Invalid values" );  
//  WMLBrowser.go ( "bships.wml#start" );
                         
}                    
   
/**************************************************                 
  MapCoords ( width, x, y )                 
  Convert a pair of zero-based coordinates in a space width x height             
  into a single, zero-based index into a single string.
**************************************************/                 
            
function MapCoords ( width, x, y ) {            
              
  var index = ( y * width ) + x;             
  return index;                
} 
           
// SetLineLength initializes each display line to its proper length. 
     
/**************************************************                 
  SetLineLength   
  Initialize a game board line to lineWidth "^"s   
  if it's not a NULL line.   
**************************************************/         
        
function SetLineLength ( notEmptyLine, lineWidth ) {        
        
  var defaultLine = "^^^^^^^^^^^^^^^";         
        
  if ( notEmptyLine ) {              
    return ( String.subString ( defaultLine, 0, lineWidth ) );        
  } else        
    return "";          
}        
        

/**************************************************                 
  FireTorpedo                 
  Figure out if the user hit a ship.                
  Alter the game board according to the result                
  and return for another move.                 
**************************************************/                 
                 
extern function FireTorpedo ( row, col ) {                      
                          
  var resultChar = "_", move="", moves;       
            
  var shiplocs = WMLBrowser.getVar ( "gShipLocations" ); 
  var width    = WMLBrowser.getVar ( "iwidth" );
  width = Lang.parseInt(width);
  var height   = WMLBrowser.getVar ( "iheight" );        
  height = Lang.parseInt(height);

// Check for a valid firing location.

  if (( col < 1 ) || (col > width)) {
      Dialogs.alert ( "Your shot went wide!");
  }
  if ( row > height ) {  
      Dialogs.alert ( "Your shot went long!");
  }
  if ( row < 1 ) {  
      Dialogs.alert ( "Your shot was short!");
  }


  if (( col >= 1 ) && ( col <= width ) &&
      ( row >= 1 ) && ( row <= height )) {
// Valid coordinates, increment the moves counter.
  
    moves = Lang.parseInt ( WMLBrowser.getVar ( "gNumMoves" )) + 1;  
    WMLBrowser.setVar ( "gNumMoves", moves );            
             
// Convert the location to a single zero-based 
// coordinate and check for a ship there.                 
                 
    var torpedoLoc = MapCoords ( width, col - 1, row - 1 );            
               
    for ( var i = 0; i < String.elements ( shiplocs, "," ); i++ ) {            
      if ( torpedoLoc == String.elementAt ( shiplocs, i, ",")) {            
        resultChar = "=";  
        shiplocs = String.removeAt ( shiplocs, i, "," );  
        WMLBrowser.setVar ( "gShipLocations", shiplocs );   
        break;            
    } }         
            
// Check to see if all ships have been obliterated. If so,  
// tell the user they've won. Update the playing screen.  
  
    if ( String.length ( shiplocs ) == 0 ) {  
      Dialogs.alert ( "Congratulations! You've won after only " +   
        WMLBrowser.getVar ( "gNumMoves" ) + " moves.");  
    }
    UpdateDisplay ( col, row, resultChar );  
  } 

  WMLBrowser.go ( "bships.wml#display" );                           
}
     
/**************************************************                 
  UpdateDisplay ( x, y, char )                 
  Insert char in location x, y (one-based coordinates) and            
  redipsplay the playing board.             
**************************************************/                 
            
function UpdateDisplay ( x, y, char ) {            
              
  var part1, part2, len1, len2, line;           
           
// Get the correct line to update         
         
  if ( y == 1 ) {            
    line = WMLBrowser.getVar ( "gLine1" );           
  } else if ( y == 2 ) {            
    line = WMLBrowser.getVar ( "gLine2" );           
  } else if ( y == 3 ) {            
    line = WMLBrowser.getVar ( "gLine3" );           
  } else if ( y == 4 ) {            
    line = WMLBrowser.getVar ( "gLine4" );           
  } else if ( y == 5 ) {            
    line = WMLBrowser.getVar ( "gLine5" );           
  } else if ( y == 6 ) { 
    line = WMLBrowser.getVar ( "gLine6" ); 
  }         
   
// Check to see if the line has already been cleared. If so, do nothing. 
 
  if ( String.find( line, "^" ) < 0 ) return; 
       
// Split out its two parts--before and after the location to be marked--    
// and insert resultchar at the firing location     
          
  part1 = String.subString ( line, 0, x - 1 ) + char;         
  part2 = String.subString ( line, x, String.length ( line ) - x );           
  line  = part1 + part2;         
         
// If it's a hit, clear the whole line except the ship location        
         
  if ( char == "=" ) {         
    line = String.replace ( line, "^", "_" );         
  }         
           
// Send the updated line back to the browser for redisplay         
          
  if ( y == 1 ) {            
    line = WMLBrowser.setVar ( "gLine1", line );           
  } else if ( y == 2 ) {            
    line = WMLBrowser.setVar ( "gLine2", line );           
  } else if ( y == 3 ) {            
    line = WMLBrowser.setVar ( "gLine3", line );           
  } else if ( y == 4 ) {            
    line = WMLBrowser.setVar ( "gLine4", line );           
  } else if ( y == 5 ) {            
    line = WMLBrowser.setVar ( "gLine5", line );           
  } else if ( y == 6 ) {            
    line = WMLBrowser.setVar ( "gLine6", line );      
  }  
}          


