
package WheelOfFortune.game.player;


import java.io.Serializable;
import java.util.StringTokenizer;

import WheelOfFortune.game.GameModel;


// Source file: U:\\cse308\\asn8\\src\\game\\player\\HardCPUPlayerStrategy.java


public class HardCPUPlayerStrategy implements CPUPlayerStrategy, Serializable{
   
 /**
  * @roseuid 418930E30094
  */
 public HardCPUPlayerStrategy() {}

 public HardCPUPlayerStrategy(GameModel gm) {}
   
 /**
  * @return int
  * @roseuid 418930FB0020
  */
 public int getDecision(GameModel gm) {
 	 double answer = Math.random();
 	  int result;
 	  int unguessed = 0;
 	  for(int i = 0; i < gm.getBoard().display().length();i++)
 	  {
 	  	if(gm.getBoard().display().charAt(i) == '!')
 	  		unguessed++;
 	  }
 	  
 	  if(unguessed < 5)
 	  	result = GameModel.SOLVE;
 	  else if(gm.getBoard().getConsonantsLeft().equalsIgnoreCase(""))
 	  	result = GameModel.SOLVE;
 	  else if(gm.getBoard().getVowelsLeft().equalsIgnoreCase(""))
 	  	result = Math.random() > .50 ? GameModel.SOLVE : GameModel.SPIN;
 	  else if(gm.activePlayer().getRoundMoney()<GameModel.VOWEL_PRICE)
 	  	result = GameModel.SPIN;
 	  else if(answer < 0.5)
 	  	result = GameModel.SPIN;
 	  else if(answer < 0.75)
 	  	result = GameModel.BUY_VOWEL;
 	  else
 	  	result = GameModel.SOLVE;
 	  return result;
 }
   
 /**
  * @return char
  * @roseuid 4189310E00DC
  */
 public char getConsonant(GameModel gm) {
 	  String cleft = gm.getBoard().getConsonantsLeft(); 
 	  String puzleft = "";
 	  String notpuzleft = "";
 	  char answer;
 	  for(int i = 0; i < cleft.length(); i++)
 	  {
 	  	if(gm.getBoard().getPuzzleString().indexOf(cleft.charAt(i))>=0)
 	  		puzleft += cleft.charAt(i);
 	  	else
 	  		notpuzleft += cleft.charAt(i);
 	  }		 
 	  if(Math.random() > .2 || notpuzleft.length() == 0)
 	  	answer = puzleft.charAt((int)(puzleft.length() * Math.random()));
 	  else
 	  	answer = notpuzleft.charAt((int)(notpuzleft.length() * Math.random()));
 	  return answer;
 }
   
 /**
  * @return char
  * @roseuid 41893117011B
  */
 public char getVowel(GameModel gm) {
	  String vleft = gm.getBoard().getVowelsLeft(); 
 	  String puzleft = "";
 	  String notpuzleft = "";
 	  char answer;
 	  for(int i = 0; i < vleft.length(); i++)
 	  {
 	  	if(gm.getBoard().getPuzzleString().indexOf(vleft.charAt(i))>=0)
 	  		puzleft += vleft.charAt(i);
 	  	else
 	  		notpuzleft += vleft.charAt(i);
 	  }		 
 	  if(Math.random() > .2 || notpuzleft.length() == 0)
 	  	answer = puzleft.charAt((int)(puzleft.length() * Math.random()));
 	  else
 	  	answer = notpuzleft.charAt((int)(notpuzleft.length() * Math.random()));
 	  return answer;
 }
   
 /**
  * @return String
  * @roseuid 4189311C0122
  */
 public String getPuzzleAnswer(GameModel gm) {
 	String board = gm.getBoard().display();
	int unknown = 0, space = 0;
	for(int i = 0; i < board.length(); i++)
	{
	  	if(board.charAt(i) == ' ')
	  		space++;
	  	else if(board.charAt(i)=='!')
	  		unknown++;
	}
	int known = board.length() - unknown - space;
	double percentageKnown = (known*1.0)/(board.length()-space);
	StringBuffer wrong = new StringBuffer("");
	StringTokenizer st = new StringTokenizer(board, " ,", true);
	String temp;

	while(st.hasMoreTokens())
	{
		temp = st.nextToken();
		StringBuffer g = new StringBuffer("");
		for(int i = 0; i < temp.length(); i++)
		{
			if(temp.charAt(i) == '!')
				g.append((char)((int)(Math.random()*26)+65)+"");
			else
				g.append(temp.charAt(i));		
		}
		wrong.append(g);
	}
	
	if(percentageKnown > .75)
		return gm.getBoard().getPuzzleString();
	else if(percentageKnown > .40)
	{
		if(Math.random() > .5)
			return gm.getBoard().getPuzzleString();
		else
			return wrong.toString();
	}
	else
		return wrong.toString();
 }
   
 /**
  * @return int
  * @roseuid 4189312401C4
  */
 public int getUseToken(GameModel gm) {
  return 0;
 }
}
