import java.awt.*;
import java.applet.*;

public class Robot extends Applet
{
    char direction; //NEWS or X for dead
    int x; //x co-ordinate on the screen
    int y; //y co-ordinate on the screen
    Color clr; //colour to fill the robot
    long delayTime; //change to 0 if you are using buttons to move the robot

    public Robot ()
    { //Default constructor
	x = 100;
	y = 100;
	clr = Color.gray;
	direction = 'S';
	delayTime = 10000000;
    }//Robot constructor


    public Robot (Color colour, int xpos, int ypos)
    { //Constructor that allows the user to set the xpos, ypos and colour
	clr = colour;
	x = xpos;
	y = ypos;
	direction = 'S';
	delayTime = 10000000;
    }//Robot constructor


    public void drawRobot (Graphics g)
    { //Facilitator that draw the robot

	//head and body
	g.setColor (Color.gray);
	g.fillRect (x + 11, y + 7, 8, 6);
	g.fillRect (x + 12, y + 13, 6, 5);
	//left arm
	g.drawLine (x + 11, y + 14, x + 8, y + 14);
	g.drawLine (x + 8, y + 14, x + 6, y + 15);
	g.drawLine (x + 8, y + 14, x + 6, y + 12);
	//right arm
	g.drawLine (x + 18, y + 14, x + 20, y + 14);
	g.drawLine (x + 20, y + 14, x + 22, y + 15);
	g.drawLine (x + 20, y + 14, x + 22, y + 12);
	//left leg
	g.drawLine (x + 13, y + 18, x + 13, y + 21);
	g.drawLine (x + 13, y + 21, x + 10, y + 21);
	//right leg
	g.drawLine (x + 16, y + 18, x + 16, y + 21);
	g.drawLine (x + 16, y + 21, x + 18, y + 21);
	//antenna
	g.drawLine (x + 14, y + 7, x + 14, y + 3);
	g.setColor (Color.red);
	g.drawLine (x + 14, y + 3, x + 14, y + 3);
	//face
	g.drawLine (x + 13, y + 11, x + 16, y + 11);
	g.setColor (Color.black);
	g.drawLine (x + 13, y + 8, x + 13, y + 8);
	g.drawLine (x + 16, y + 8, x + 16, y + 8);

	//draw the direction arrow
	g.setColor (Color.green);
	if (direction == 'N')
	{
	    g.drawLine (x + 1, y + 15, x + 15, y + 1);
	    g.drawLine (x + 15, y + 1, x + 29, y + 15);
	}
	else if (direction == 'E')
	{
	    g.drawLine (x + 15, y + 1, x + 29, y + 15);
	    g.drawLine (x + 29, y + 15, x + 15, y + 29);
	}
	else if (direction == 'W')
	{
	    g.drawLine (x + 15, y + 1, x + 1, y + 15);
	    g.drawLine (x + 1, y + 15, x + 15, y + 29);
	}
	else if (direction == 'S')
	{
	    g.drawLine (x + 1, y + 15, x + 15, y + 29);
	    g.drawLine (x + 15, y + 29, x + 29, y + 15);
	}
	else //robot is dead
	{
	    g.setColor (Color.red);
	    g.drawLine (x + 1, y + 1, x + 29, y + 29);
	    g.drawLine (x + 29, y + 1, x + 1, y + 29);
	}
    } //drawRobot


    public void drawBox (Graphics g, RobotWorld dirt)
    {
	g.setColor (dirt.getBackground ());
	g.fillRect (x, y, 30, 30);
	g.setColor (dirt.getOutline ());
	g.drawRect (x, y, 30, 30);

    }//drawBox


    public void turnNorth (Graphics g, RobotWorld dirt)
    { //Mutator that turns the robot north
	if (direction == 'X')
	{
	    //if dead, do nothing
	    drawRobot (g);
	}
	else
	{
	    drawBox (g, dirt);
	    direction = 'N';
	    drawRobot (g);
	}
    }


    public void turnWest (Graphics g, RobotWorld dirt)
    { //Mutator that turns the robot West
	if (direction == 'X')
	{ //if dead, do nothing
	    drawRobot (g);
	}
	else
	{
	    drawBox (g, dirt);
	    direction = 'W';
	    drawRobot (g);
	}
    }


    public void turnSouth (Graphics g, RobotWorld dirt)
    { //Mutator that turns the Robot south
	if (direction == 'X')
	{
	    //if dead, do nothing
	    drawRobot (g);
	}
	else
	{
	    drawBox (g, dirt);
	    direction = 'S';
	    drawRobot (g);
	}
    }


    public void turnEast (Graphics g, RobotWorld dirt)
    { //Mutator that turns the Robot East
	if (direction == 'X')
	{
	    //if dead, do nothing
	    drawRobot (g);
	}
	else
	{
	    drawBox (g, dirt);
	    direction = 'E';
	    drawRobot (g);
	}
    }


    public void move (Graphics g, RobotWorld dirt)
    { //Mutator & Facilitator that moves the robot forward

	//Fill in the old position
	drawBox (g, dirt);

	if (direction == 'X')
	{
	    //if dead, do nothing
	    drawRobot (g);
	}
	else if (direction == 'E')
	{ //move east if there is no wall
	    if (dirt.isOK (x + 30, y))
	    {
		x += 30;
		drawRobot (g);
	    }
	    else
	    {
		direction = 'X';
		drawRobot (g);
	    }
	}
	else if (direction == 'W')
	{ //move west if there is no wall
	    if (dirt.isOK (x - 30, y))
	    {
		x -= 30;
		drawRobot (g);
	    }
	    else
	    {
		direction = 'X';
		drawRobot (g);
	    }
	}
	else if (direction == 'S')
	{ //move south if there is no wall
	    if (dirt.isOK (x, y + 30))
	    {
		y += 30;
		drawRobot (g);
	    }
	    else
	    {
		direction = 'X';
		drawRobot (g);
	    }
	}
	else if (direction == 'N')
	{ //move north if there is no wall
	    if (dirt.isOK (x, y - 30))
	    {
		y -= 30;
		drawRobot (g);
	    }
	    else
	    {
		direction = 'X';
		drawRobot (g);
	    }
	}
	for (int i=0;i<delayTime;i++);
    }
}

