/// <summary> /// Builds Rover objects by parsing the line /// </summary> /// <param name="line"></param> /// <returns></returns> public static Rover buildRover(string line) { string[] partsOfline = line.Split(' '); if (partsOfline.Length != 3) { Console.WriteLine("Invalid Rover Input"); Environment.Exit(-1); } try { int x = Convert.ToInt32(partsOfline[0]); int y = Convert.ToInt32(partsOfline[1]); char ch = Convert.ToChar(partsOfline[2]); if ((x <= Plateau.getMaxx() && x >= 0) && (y <= Plateau.getMaxy() && y >= 0) && (ch == 'N' || ch == 'W' || ch == 'E' || ch == 'S')) { return(new Rover(x, y, ch)); } else { Console.WriteLine("Invalid Rover Input"); Environment.Exit(-1); return(null); } } catch (Exception ex) { Console.WriteLine("Error in Rover Parsing" + ex.Message); Environment.Exit(-1); return(null); } }
/// <summary> /// Advance method to change the co-ordinates based on controller input. /// Skips the action of controller if it finds another rover for the action, /// or crosses the boundary limits. /// </summary> public void advance() { char currentDirection = getDirection(); switch (currentDirection) { case 'N': if ((_ycoord + 1 <= Plateau.getMaxy()) && (_ycoord + 1 >= 0) && (!(MarsHelper.checkCollision(_xcoord, _ycoord + 1, _direction)))) { _ycoord = _ycoord + 1; } break; case 'W': if ((_xcoord - 1 <= Plateau.getMaxx()) && (_xcoord - 1 >= 0) && (!(MarsHelper.checkCollision(_xcoord - 1, _ycoord, _direction)))) { _xcoord = _xcoord - 1; } break; case 'E': if ((_xcoord + 1 <= Plateau.getMaxx()) && (_xcoord + 1 >= 0) && (!(MarsHelper.checkCollision(_xcoord + 1, _ycoord, _direction)))) { _xcoord = _xcoord + 1; } break; case 'S': if ((_ycoord - 1 <= Plateau.getMaxy()) && (_ycoord - 1 >= 0) && (!(MarsHelper.checkCollision(_xcoord, _ycoord - 1, _direction)))) { _ycoord = _ycoord - 1; } break; } }