// Student: C. H.
// Hangman Project
// Computer Science 1

// Implementation of the Hangman game. Make a guess by tapping on a letter.

package edu.compsci;

import cs1.android.*;

public class HangmanApp extends AndroidApp
{
	final double MIDDLEX=240;   // for 320x480 device
	final double MIDDLEY=160;

	//determine if two strings are the same
	boolean equalStrings(String str1,String str2)
	{
		if(str1.length()!=str2.length())
		{
			return false;
		}
		else 
		{
			for(int index=0;index<str1.length();index=index+1)
			{
				char currLetter1=str1.charAt(index);
				char currLetter2=str2.charAt(index);

				if(currLetter1!=currLetter2)
				{
					return false;
				}
			}

			return true;
		}
	}

	//determine does the string include the letter
	boolean hasCharacter(String word, char letter)
	{
		for(int index=0;index<word.length();index=index+1)
		{
			char currLetter=word.charAt(index);
			if(currLetter==letter)
			{
				return true;
			}
		}

		return false;
	}

	//create a string of n blank symbols
	String blankString(int n)
	{
		String result="";

		for(int count=1;count<=n;count=count+1)
		{
			result=result+"-";
		}

		return result;
	}

	//creat a brand new string that is a modification of str2
	//such that locations in str2 that correspond to locations
	//in str1 that contain the given character letter is replaced by letter
	String revealCharacter(String str1,String str2,char letter)
	{
		String result="";

		for(int index=0;index<str2.length();index=index+1)
		{
			char currLetter1=str1.charAt(index);
			char currLetter2=str2.charAt(index);

			if(letter==currLetter1)
			{
				result=result+currLetter1;
			}
			else
			{
				result=result+currLetter2;
			}
		}

		return result;
	}

	//creat a brand new string that is a modification of the string str such 
	//that all occurences of the characters oldCh are replaced by newCh
	String replaceCharacter(String word,char oldCh,char newCh)
	{
		String result="";
		for(int index=0;index<word.length();index=index+1)
		{
			char currLetter=word.charAt(index);
			if(currLetter==oldCh)
			{
				result=result+newCh;
			}
			else
			{
				result=result+currLetter;
			}
		}

		return result;
	}

	//draw the alphabet as a row of circles
	void drawAlphabet(String alphabet)
	{

		double y=canvas.getHeight()/4;
		double radius=canvas.getWidth()/alphabet.length()/2;
		double x=radius;

		for(int index=0;index<alphabet.length();index=index+1)
		{ 
			char currAlpha=alphabet.charAt(index);
			canvas.drawCircle(x,y,radius,"tomato");

			if(currAlpha=='*')
			{
				canvas.drawCircle(x,y,radius,"white");   
			}
			else
			{    
				canvas.drawText(x,y,currAlpha);   
			}

			x=x+2*radius;
		}
	}

	//draw a figure that represents the errors graphically
	void drawFigure(int errors)
	{
		double x=canvas.getWidth()/2;
		double y=(canvas.getHeight()/3)*2;

		//draw the head
		if(errors==1)
		{
			drawSmiley(x,y-80,30);
		}

		//draw the body and the head
		else if(errors==2)
		{
			drawSmiley(x,y-80,30);
			canvas.drawRectangle(x,y,60,100,"yellow");
		}

		//draw the body,head,and the right arm
		else if(errors==3)
		{
			drawSmiley(x,y-80,30);
			canvas.drawRectangle(x,y,60,100,"yellow");
			canvas.drawRectangle(x-60,y-15,60,20,"blue");
		}

		//draw the body,head,and two arms
		else if(errors==4)
		{
			drawSmiley(x,y-80,30);
			canvas.drawRectangle(x,y,60,100,"yellow");
			canvas.drawRectangle(x-60,y-15,60,20,"blue");
			canvas.drawRectangle(x+60,y-15,60,20,"blue");
		}

		//draw the body,head,two arms,and the right leg
		else if(errors==5)
		{
			drawSmiley(x,y-80,30);
			canvas.drawRectangle(x,y,60,100,"yellow");
			canvas.drawRectangle(x-60,y-15,60,20,"blue");
			canvas.drawRectangle(x+60,y-15,60,20,"blue");
			canvas.drawRectangle(x-20,y+80,20,55,"blue");
		}

		//draw all the elements
		else if(errors==6)
		{            
			canvas.drawCircle(x,y-80,30,"blue");
			canvas.drawRectangle(x,y,60,100,"yellow");
			canvas.drawRectangle(x-60,y-15,60,20,"blue");
			canvas.drawRectangle(x+60,y-15,60,20,"blue");
			canvas.drawRectangle(x-20,y+80,20,55,"blue");
			canvas.drawRectangle(x+20,y+80,20,55,"blue");
		}
	}

	//draw a Smiley face
	void drawSmiley(double x, double y, double radius)
	{
		// draw the face
		canvas.drawCircle(x, y, radius, "blue");

		// draw the left eye
		double eyeX = x + 0.3*radius;
		double eyeY = y - 0.3*radius;
		double eyeRadius = 0.2*radius;
		canvas.drawCircle(eyeX, eyeY, eyeRadius);

		// draw the right eye -- only the x coordinate needs to be updated
		eyeX = x - 0.3*radius;
		canvas.drawCircle(eyeX, eyeY, eyeRadius);

		// draw the mouth
		double mouthX = x;
		double mouthY = y + 0.3*radius;
		double mouthRadius = 0.4*radius;
		canvas.drawCircle(mouthX, mouthY, mouthRadius);

		// draw the mouth cover -- only the y coordinate is updated
		mouthY = y + 0.2*radius;
		canvas.drawCircle(mouthX, mouthY, mouthRadius, "blue");
	}

	void playHangman()
	{
		//String secretWord=canvas.getRandomColor().toUpperCase();
		String secretWord="HANGMAN";
		String guessWord=blankString(secretWord.length());
		String alphabet="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
		String prompt="Guess a character";
		String usedGuess="";

		drawAlphabet(alphabet);

		canvas.drawText(MIDDLEX,canvas.getHeight()/7,guessWord);
		char guess;

		int errors=0;


		while(equalStrings(secretWord,guessWord)==false && errors<6)
		{   
			/*
			 * Old code for desktop app
			 * 
			 * guess=canvas.readChar(prompt);
			 *
			 */

			/*
			 * New code for Android App
			 */
			Touch touch = canvas.waitForTouch();
			int diam = canvas.getWidth()/alphabet.length();
			int i = touch.getX() / diam;
			guess = alphabet.charAt(i);
			/*
			 * End New code for Android App
			 */


			canvas.clear();
			alphabet=replaceCharacter(alphabet,guess,'*');
			drawAlphabet(alphabet);

			if(hasCharacter(usedGuess,guess)==true)
			{
				prompt="Already Used!Guess again";
			}
			else 
			{   
				if(hasCharacter(secretWord,guess)==true)
				{
					guessWord=revealCharacter(secretWord,guessWord,guess);
					prompt="Correct!Guess again";
				}
				else 
				{
					prompt="Incorrect.Guess again";
					errors=errors+1;
				}
			}
			canvas.drawText(MIDDLEX,canvas.getHeight()/7,guessWord);
			drawFigure(errors);
			usedGuess=usedGuess+guess;  
		}

		if(equalStrings(secretWord,guessWord)==true)
		{
			canvas.drawText(MIDDLEX,canvas.getHeight()/3,"Congratulations, you survived",40,"red");
		}
		else
		{
			canvas.drawText(MIDDLEX,canvas.getHeight()/3,"Sorry,you are hanged",40);

		}        
	}

	public void run()
	{
		playHangman();
	}
}
