//Monty Hall Game in JavaScript Code
//Programmed by Shawn Olson | http://www.shawnolson.net
//Copyright (c) 2004 by Shawn Olson
//Thanks to Andrew Penry ( http://www.thepenry.net ) for showing me the Monty Hall Game and adding constructive criticism.

	var board = '<table><tr><td id="cell1"><img id="img1" src="media/montyHallCard.gif" onclick="pickCard(1)"/></td><td id="cell2"><img id="img2" src="media/montyHallCard.gif" onclick="pickCard(2)"/></td><td id="cell3"><img id="img3" src="media/montyHallCard.gif" onclick="pickCard(3)"/></td></tr></table><hr/><table class="winChart"><tr><td>Wins</td><td>Losses</td><td>Win Percentage</td></tr><tr><td><h1 id="wins"></h1></td><td><h1 id="losses"></h1></td><td><h1 id="rate"></h1></td></tr></table>';
	
	var selectedCard;
	var prizeCard;
	var wins = 0;
	var losses = 0;
	var games = 0;
	var turn;
	
	
	
	function randomCard(){
	  prizeCard = Math.floor(Math.random()*3+1);
	  if(prizeCard==0){randomCard();}
	}
	
	function showTurn(){
	  if(turn==1){document.getElementById("turn").innerHTML = 'Pick a card!';}
      if(turn==2){document.getElementById("turn").innerHTML = 'Do you want to keep your previous choice or switch to the other card?';}
	}
	
	
	function startGame(){
	  turn = 1;
	  document.getElementById("gameBoard").innerHTML = board;
	  document.getElementById("start").innerHTML = 'New Game';
	  showTurn();
	  randomCard();
	}
	
	
	function showWins(){
	  document.getElementById("wins").innerHTML = wins;
	  document.getElementById("losses").innerHTML = losses;
	  rates = Math.round((wins/games)*100);
	  document.getElementById("rate").innerHTML = rates+'%';
	}
	
	
	function showCard(id){
	 
	  if(id==prizeCard){winLose='Win';}
	  if(id!=prizeCard){winLose='Lose';}
	  string = 'document.getElementById("img'+id+'").src = "media/Hall'+winLose+'.gif";';
	  eval(string);
	}
	
	
	function randomSwitch(){
	  
	  num = Math.floor(Math.random()*2+1);
	  if (num == 0){randomSwitch();}
	  return num;
	}
	
	function highlightCard(id){
	   string = 'document.getElementById("img'+id+'").className = "selected";';
	   eval(string);
	}
	
	function pickCard(card){
	  selectedCard = card;
	  highlightCard(card);
	  if(turn == 1){
	     switchCard = randomSwitch();
	     if(selectedCard == prizeCard){
		   switch(card){
		     case 1:
			   if(switchCard ==2){
			     showCard(2);
			   }
			   if(switchCard ==1){
			     showCard(3);
			   }
			 break;
		     case 2:
			   if(switchCard ==1){
			     showCard(1);
			   }
			   if(switchCard ==2){
			     showCard(3);
			   }
			 break;
			 case 3:
			   if(switchCard ==1){
			     showCard(2);
			   }
			   if(switchCard ==2){
			     showCard(1);
			   }
			 break;
		   }
		 }
	     if(selectedCard != prizeCard){
	       if(selectedCard==1 && prizeCard==3){showCard(2);}
		   if(selectedCard==1 && prizeCard==2){showCard(3);}
		   if(selectedCard==2 && prizeCard==1){showCard(3);}
		   if(selectedCard==2 && prizeCard==3){showCard(1);}
		   if(selectedCard==3 && prizeCard==1){showCard(2);}
		   if(selectedCard==3 && prizeCard==2){showCard(1);}
		 }
	  }
	  if(turn==2){
	    showCard(1);
		showCard(2);
		showCard(3);
		games++;
	    if(selectedCard==prizeCard){wins++; alert('You Win');}
	    if(selectedCard!=prizeCard){losses++; alert('You Lose');}
		startGame();
		showWins();
		return;
	  }
	 turn++;
	 showTurn();
	}