function randInt(mn,mx) {
return mn + (Math.floor(Math.random()*(mx+1-mn)));
}

var game;
var MAX_COINS=12;
function ForgedCoin() {
}
ForgedCoin.prototype.initHtml = function() {
var s = "<table><tr><th>Coin #</th><th>Left</th><th>Aside</th><th>Right</th></tr>";
for (var i = 0; i < MAX_COINS; i++) {
s += "<tr>";
s += "<td>"+(i+1)+"</td>";
s += '<td><input type="radio" class="leftradio" id="coinl'+i+'" name="coin'+i+'"/></td>';
s += '<td><input type="radio" class="asideradio" id="coinx'+i+'" checked="checked" name="coin'+i+'"/></td>';
s += '<td><input type="radio" class="rightradio" id="coinr'+i+'" name="coin'+i+'"/></td>';
s += "</tr>";
}
s += "</table>";
$("#canvas").html(s);
}
ForgedCoin.prototype.start = function() {
    this.setAttempt(1);
this.coin = randInt(0, MAX_COINS-1);
this.coinWeight = randInt(0, 1) ? 1 : -1;
    this.clearLog();
this.log("You start a new game");
this.allAside();
}
ForgedCoin.prototype.setAttempt = function(a) {
this.attempt = a;
$("#attempt").text(a);
}
ForgedCoin.prototype.allAside = function() {
$(".asideradio").attr("checked", "checked");
}

ForgedCoin.prototype.clearLog = function() {
$("#log").html("");
var elt = $("#log").get(0);
elt.scrollTop = elt.scrollHeight;
}

ForgedCoin.prototype.log = function(s) {
$("#log").append(s+'\n');
var elt = $("#log").get(0);
elt.scrollTop = elt.scrollHeight;
}
ForgedCoin.prototype.containsForgedCoin = function(coins) {
var ret = false;
for (var i = 0; i < coins.length; i++) {
if (coins[i] == this.coin) { ret = true; break; }
}
return ret;
}

ForgedCoin.prototype.coinsToStr = function(coins)  {
var s;
if (coins.length == 0) {
s = "no coins";
}
else if (coins.length == 1) {
s = "coin " + (coins[0]+1);
}
else {
s = "coins ";
for (var i = 0; i < coins.length; i++) {
if (i > 0) { s+=", "; }
s += (coins[i]+1);
}
}
return s;
}

ForgedCoin.prototype.weigh = function() {
var left = []; var right = []; var aside = [];
for (var i = 0; i < MAX_COINS; i++) {
if ($("#coinl"+i).attr('checked')) {
left.push(i);
}
else if ($("#coinr"+i).attr('checked')) {
right.push(i);
}
else {
aside.push(i);
}
}
this.log('You place '+this.coinsToStr(left)+' on the left, '
	 +'and '+this.coinsToStr(right)+' to the right. ');

var toleft = 'The balance tends to the left';
var toright = 'The balance tends to the right';
var m;
if (left.length == right.length) {
if (this.containsForgedCoin(left)) {
m =(this.coinWeight > 0) ? toleft : toright;
}
else if (this.containsForgedCoin(right)) {
m =(this.coinWeight > 0) ? toright : toleft;
}
else {
m= "The load on both sides is equal.";
}
}
else if (left.length > right.length) {
m=toleft;
}
else {
m=toright;
}
this.log(m);
this.allAside();
this.setAttempt(this.attempt+1);
if (this.attempt == 4) {
    this.log("You should know the solution by now...");
}
}
ForgedCoin.prototype.validate = function() {
var c = $("#coin").attr('value');
var cw = $("#coinweight").attr('value');
if (""+this.coin == c && ""+this.coinWeight==cw) {
    if (this.attempt <= 4) {    alert("Perfect!");
    }
    else {
	alert("Okay, but you used too many attempts!");
    }
    this.start();
}
else {
this.setAttempt(this.attempt+1);
this.log("You made a bad guess...");
    alert("No, that is not correct");
}
}
ForgedCoin.prototype.giveUp = function() {
alert("Coin " + (this.coin+1) + " is the forged one. It is "+(this.coinWeight > 0?"heavier":"lighter"));
this.start();
}
$(function(){
game = new ForgedCoin();
game.initHtml();
game.start();
});

