package WheelOfFortune.game.player;


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

import WheelOfFortune.game.GameModel;

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


public class NormalCPUPlayerStrategy implements CPUPlayerStrategy, Serializable{
   
 private String popular = "SETARINOLCPDMFBGKUYJHWVQXZ";
 /**
  * @roseuid 418931A30222
  */
 public NormalCPUPlayerStrategy() {}
   
 /**
  * @param gm
  * @roseuid 41892EF50193
  */
 public NormalCPUPlayerStrategy(GameModel gm) {}
   
 /**
  * @return int
  * @roseuid 41892F000121
  */
 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 < 3)
  	result = GameModel.SOLVE;
  else if(gm.getBoard().getConsonantsLeft().equalsIgnoreCase(""))
  	result = GameModel.SOLVE;
  else if(gm.getBoard().getVowelsLeft().equalsIgnoreCase(""))
  	result = Math.random() > .30 ? 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.85)
  	result = GameModel.BUY_VOWEL;
  else
  	result = GameModel.SOLVE;
  return result;
 }
   
 /**
  * @return char
  * @roseuid 41892F050308
  */
 public char getConsonant(GameModel gm) {
  String cleft = gm.getBoard().getConsonantsLeft(); 
  for(int i = 0; i < popular.length(); i++)
  	if(cleft.indexOf(popular.charAt(i)+"") >=0)
  		return popular.charAt(i);
  return cleft.charAt((int)(Math.random()*cleft.length()));
 }
   
 /**
  * @return char
  * @roseuid 41892F0C029A
  */
 public char getVowel(GameModel gm) {
 	  String vleft = gm.getBoard().getVowelsLeft(); 
 	  for(int i = 0; i < popular.length(); i++)
 	  	if(vleft.indexOf(popular.charAt(i)+"") >=0)
 	  		return popular.charAt(i);
 	  return vleft.charAt((int)(Math.random()*vleft.length()));	  
 }
   
 /**
  * @return String
  * @roseuid 41892F120195
  */
 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 > .8)
		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 41892F19033A
  */
 public int getUseToken(GameModel gm) {
  return 0;
 }
}
