/* jslint browser: true */ $(document).ready(function(){ var k = Karma({ image: [ {name: "ball", file: "ball37px.png"}, {name: "balloon", file: "balloon37px.png"}, {name: "banana", file: "banana37px.png"}, {name: "chilli", file: "chilli.png"}, {name: "fish" , file: "fish64px.png"}, {name: "flower", file: "flower37px.png"}, {name: "normalChimp", file: "normalChimp_120x125.png"}, {name: "happyChimp", file: "happyChimp_120x125.png"}, {name: "sadChimp", file: "sadChimp_120x125.png"}], audio: [ {name: "correct", file: "correct.ogg"}, {name: "incorrect", file: "incorrect.ogg"}, {name: "trigger", file: "trigger.ogg"} ] }); k.ready(function() { var imageNames = ["ball", "banana", "balloon","chilli", "fish", "flower"]; //game logic var cards; var totalCorrect = 0, n0 = 0, n1 = 0, correctCard = 0, level = 0, score = 0, numCorrectAnswers = 0; var DRAW_MAX_X = 170, DRAW_MAX_Y = 170; var choices=[0, 0, 0]; var timerSpeed = 12000; var START_TIMER_Y = 25, END_TIMER_Y = 125; var timerPaper, chimpPaper; var timerRect; var normalChimpImage, sadChimpImage, happyChimpImage; var overlayCard, topLeftCard, topRightCard, bottomLeftCard, bottomMiddleCard, bottomRightCard; var buttons=[]; var isTimerRunning = false; var isGameRunning = false; var createCard = function (paperName, width, height) { var set; var paper; if(!width || !height){ paper = Raphael(paperName+"Paper", 200, 200); } else { paper = Raphael(paperName+"Paper", width, height); } set = paper.set(); return { "paper": paper, "prefix": paperName, "set": set}; }; overlayCard = createCard("overlay", 800, 600); topLeftCard = createCard("topLeft"); topRightCard = createCard("topRight"); bottomLeftCard = createCard("bottomLeft"); bottomMiddleCard = createCard("bottomMiddle"); bottomRightCard = createCard("bottomRight"); cards = [ topLeftCard, topRightCard, bottomLeftCard, bottomMiddleCard, bottomRightCard]; sets = [topLeftCard.set, topRightCard.set, bottomLeftCard.set, bottomMiddleCard.set, bottomRightCard.set]; function drawCards () { var imageId = imageNames[ level ]; //reinitialize choices to zero choices = [0, 0, 0]; cards.forEach(function (box) { box.set.remove(); }); totalCorrect = Karma.rand( 2, 5 + level ); //the totalCorrect n0 = totalCorrect - Karma.rand(1, totalCorrect - 1 ); //first number n1 = totalCorrect - n0; //second number //chose one option (the correct option) //and then put the correct value into it correctCard = Karma.rand( 0, 2 ); choices[ correctCard ] = totalCorrect; var computeUniqueChoice = function(choice){ var newChoice = 0; if (choice === totalCorrect) { return choice; } else { newChoice = Karma.rand( 1, 10 ); if (newChoice === totalCorrect){ return computeUniqueChoice(choice); } else { return newChoice; } } }; choices = choices.map(computeUniqueChoice); var drawCard = function (card, n) { var positions = []; var x = 0, y = 0; var isOverlapping = false; var imageVarNames = {}; var varPrefix = card.prefix; imageVarNames[varPrefix] = []; card.set = card.paper.set(); for (var i=0; i