public class TriangleBug extends Bug
{
	private int steps;
	private int sideLength;

	// The variable state makes life much simpler by keeping track
	// of what stage the TriangleBug is in drawing a triangle
	//
	// 0: base, 1: first leg, 2: second leg
	
	private int state;

	/**
	 * Constructs a Triangle bug that traces a triangle of a given side length
	 * @param length the side length
	 */

	public TriangleBug(int length)
	{
		steps = 0;
		sideLength = length;
		state = 0;
	}

	public void turn(int turns) {
		for (int i = 0; i < turns; i++) {
			// Delegates turn with no inputs to Bug class
			turn();
		}
	}

	/**
	 * Moves to the next location of the triangle.
	 */
	public void act()
	{
		// If the bug is on the base
		if (state == 0) {
			if (steps < sideLength && canMove())
			{
				move();
				steps++;
			}
			else
			{
				turn(3);
				steps = 0;
				state = 1;
			}
		} else {  // the bug is on a leg
			if (steps < sideLength/2 && canMove())
			{
				move();
				steps++;
			} else {
				steps = 0;
				// The number of turns and the next state
				// depend on which leg the bug is on
				turn(state == 1 ? 2 : 3);
				state = (state == 1 ? 2 : 0);
			}
		}
	}
}