You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

308 lines
9.6 KiB

/* This code was originally based on code by
Husain Limdiyawala(MSc IT DA-IICT) */
9 years ago
// Global Variables.
9 years ago
var totblocks = 0;
var data = "";
var currentblock = 0;
var lastposition = new Array();
var randomno = 0;
var tots = new Array();
var l = 0;
var srcsnake = new Array(4);
var destsnake = new Array(4);
var ladsrc = new Array(3);
var laddest = new Array(3);
var quest = new Array(); // Available questions along with multiple answers.
var coranswered = new Array(); // Record all questions (along with answers) the user responded CORRECTLY.
var wroanswered = new Array(); // Record all questions (along with answers) the user responded WRONGLY.
9 years ago
var user = new Array();
// Constract table with questions and answers and pick question to display.
9 years ago
quest[0] = "Spell 1";
quest[1] = "one";
quest[2] = "two";
quest[3] = "three";
quest[4] = "Spell 2";
quest[5] = "two";
quest[6] = "three";
quest[7] = "four";
quest[8] = "Spell 3";
quest[9] = "three";
quest[10] = "two";
quest[11] = "four";
quest[12] = "Spell 4";
quest[13] = "four";
quest[14] = "three";
quest[15] = "one";
var z = 0;
var allQuest;
for () {
if (quest[z] === null) {
break;
}
9 years ago
}
allQuest = z / 4;
function selectQuest(all) {
9 years ago
pickOne = Math.floor((Math.random() * all));
return pickOne;
}
// The Below Function will hide all the snakes.
9 years ago
function hideAll() {
document.getElementById("img1").style.display = "none";
document.getElementById("img2").style.display = "none";
document.getElementById("img3").style.display = "none";
document.getElementById("img4").style.display = "none";
9 years ago
document.getElementById("lad1").style.display = "none";
document.getElementById("lad2").style.display = "none";
9 years ago
document.getElementById("lad3").style.display = "none";
}
// The Below Function will Render The Main Board.
9 years ago
function paintBoard(a) {
var j;
9 years ago
totblocks = (a * a);
if ((a * a) % 2 == 0) {
currentblock = (a * a) - a + 1;
for (j = 0; j < (a / 2); j++) {
for (i = 0; i < a; i++) {
data += "<div class='blocks' id='" + currentblock + "'>" + currentblock + "</div>";
currentblock++;
}
currentblock -= (a + 1);
for (i = 0; i < a; i++) {
data += "<div id='" + currentblock + "' class ='blocks'>" + currentblock + "</div>";
currentblock--;
}
currentblock -= (a - 1);
}
} else {
currentblock = (a * a);
for (j = 0; j < (a / 2); j++) {
for (i = 0; i < a; i++) {
data += "<div class='blocks' id='" + currentblock + "'>" + currentblock + "</div>";
currentblock--;
}
currentblock -= (a - 1);
if (currentblock < 2) {
break;
}
for (i = 0; i < a; i++) {
data += "<div id='" + currentblock + "' class ='blocks'>" + currentblock + "</div>";
currentblock++;
}
currentblock -= (a + 1);
}
}
9 years ago
document.getElementById("cont").style.width = (a * 52 + 52) + "px"
document.getElementById("cont").innerHTML = data;
$("#cont").slideDown( "slow");
$("#cont").effect( "shake",3000);
$("img:hidden").fadeIn( 5000);
9 years ago
if (a == 6) {
registerSnake(158, 196, "img1", 14, 3, 0);
registerSnake(62, 183, "img2", 27, 24, 1);
registerSnake(175, 18, "img3", 18, 4, 2);
registerSnake(10, 45, "img4", 32, 23, 3);
9 years ago
registerLadder(27, 132, "lad1", 28, 34, 0);
registerLadder(90, 22, "lad2", 19, 30, 1);
registerLadder(179, 137, "lad3", 2, 16, 2);
9 years ago
} else if (a == 8) {
registerSnake(300, 380, "img1", 44, 29, 0);
registerSnake(180, 550, "img2", 51, 46, 1);
registerSnake(290, 50, "img3", 41, 40, 2);
registerSnake(500, 280, "img4", 27, 22, 3);
registerLadder(350, 515, "lad1", 19, 35, 0);
registerLadder(180, 230, "lad2", 43, 54, 1);
registerLadder(80, 350, "lad3", 53, 60, 2);
}
}
// The below Function will simulate throwing of a dice.
9 years ago
function throwDice(i) {
randomno = Math.floor((Math.random() * 6)) + 1;
document.getElementById("diceimg").src = "images/dice_" + randomno + ".PNG";
9 years ago
document.getElementById("diceimg").style.display = "block";
if (lastposition[i] > 0) {
9 years ago
document.getElementById(lastposition[i]).style.background = "url(images/square52.png)";
}
tots[i] += randomno;
9 years ago
if (totblocks - tots[i] >= 0) {
9 years ago
lastposition[i] = tots[i];
document.getElementById(tots[i]).style.background = "url(images/pawn1.png)";
} else {
9 years ago
tots[i] -= randomno;
document.getElementById(tots[i]).style.background = "url(images/pawn1.png)";
}
9 years ago
}
// The below Function Checks The Snake Biting for a user.
function snakescheck(k) {
var i;
for(i = 0; i <= srcsnake.length; i++) {
if (srcsnake[i] == tots[k]) {
alert("Ωχ! Σε τσίμπησε φίδι στο τετράγωνο " + srcsnake[i] + " και θα πρέπει να γυρίσεις στο τετράγωνο " + destsnake[i] + ", εκτός κι αν απαντήσεις σωστά στην ερώτηση που ακολουθεί.");
document.getElementById(destsnake[i]).style.background = "url(images/pawn1.png)";
document.getElementById(tots[k]).style.background = "url(images/square52.png)";
lastposition[k] = destsnake[i];
tots[k] = destsnake[i];
break;
}
}
9 years ago
if (!checkWin(k)) {
alert("???d?se?!S???a??t???a!");
9 years ago
}
}
// The below function checks the ladders for a user.
9 years ago
function laddercheck(k) {
var i;
9 years ago
for(i = 0; i <= ladsrc.length; i++) {
if (ladsrc[i] == tots[k]) {
9 years ago
alert("Υπάρχει μια σκάλα στο τετράγωνο " + ladsrc[i] + " και θα σας οδηγήσει κατευθείαν στο τετράγωνο " + laddest[i] + "αν απαντήσεις σωστά στην ερώτηση που ακολουθεί.");
document.getElementById(laddest[i]).style.background = "url(images/pawn1.png)";
document.getElementById(tots[k]).style.background = "url(images/square52.png)";
lastposition[k] = laddest[i];
tots[k] = laddest[i];
break;
}
}
if(!checkWin(k)) {
alert("You have won!");
9 years ago
}
}
// The below function will register a snake.
function registerSnake(tp, lft, dv, src, dest, i) {
document.getElementById(dv).style.top = tp + "px";
document.getElementById(dv).style.left = lft + "px";
srcsnake[i] = src;
destsnake[i] = dest;
9 years ago
}
// The below function will register a ladder.
9 years ago
function registerLadder(tp, lft, dv, src, dest, i) {
document.getElementById(dv).style.top = tp + "px";
document.getElementById(dv).style.left = lft + "px";
ladsrc[i] = src;
laddest[i] = dest;
9 years ago
}
// The below function checks the change in the boardtype selection combobox.
9 years ago
function selectBoard() {
totblocks = 0;
data = "";
currentblock = 0;
hideAll();
if (document.getElementById("boardtype").value != null) {
paintBoard(parseInt(document.getElementById("boardtype").value));
}
}
// The below function checks the change in the player selection combobox.
function selectPlayer() {
if (document.getElementById("players").value != null) {
user[document.getElementById("players").value - 1] = 0;
tots[document.getElementById("players").value - 1] = 0;
lastposition[document.getElementById("players").value - 1] = 0;
for(var j = 0; j < lastposition.length; j++) {
9 years ago
lastposition[j] = 0;
tots[j] = 0;
}
}
9 years ago
}
// The below function starts the play.
function play() {
if (tots[l] != null) {
var s;
s = "<ul class='nodis'><li>O Paiktis " + (l + 1);
s = s + " </li><li>brisketai sto tetragwno " + tots[l] + "</li><li></li></ul>";
9 years ago
disableField();
document.getElementById("status").innerHTML = s;
9 years ago
question();
s = "<ul class='nodis'><li>O Paiktis " + (l + 1);
s = s + " </li><li>vrisketai sto tetragwno " + tots[l] + "</li></ul>";
document.getElementById("status").innerHTML = s;
} else {
9 years ago
document.getElementById("status").innerHTML = "<ul class='nodis'><li>Molis exases...</li></ul>";
}
if( l == lastposition.length - 1) {
l = 0;
} else {
l++;
}
}
// The below function regulates the play.
function doit(i) {
throwDice(i);
if (checkWin(i)) {
snakescheck(i);
laddercheck(i);
9 years ago
} else {
alert("Congatulations!!!");
9 years ago
}
}
// The below function checks whether the player has won or not.
function checkWin(i) {
if (tots[i] == totblocks) {
9 years ago
return false;
} else {
return true;
}
}
// The below function will disable both the combobox .
function disableField() {
document.getElementById("players").disabled = "disabled";
document.getElementById("boardtype").disabled = "disabled";
9 years ago
}
function question() {
var picked, q1;
9 years ago
picked = selectQuest(allQuest);
alert("Randomly selected number:" + picked);
q1 = prompt(quest[picked * 4], "Απάντηση");
if (q1 == quest[picked * 4 + 1]) {
9 years ago
alert("Σωστά!")
doit(l);
coranswered.concat(quest.splice(picked * 4, 4));
9 years ago
} else {
alert("Η απάντηση δεν ήταν σωστή. Χάνεις τη σειρά σου για αυτό το γύρο!")
wroanswered.concat(quest.splice(picked * 4, 4));
9 years ago
}
// Remove question and answers from available questions - (thus not allowing to have a Repeated question).
9 years ago
allQuest--;
}