Difference between revisions of "Template:Tsinghua-A/JS/Game main"

Line 43: Line 43:
 
var imgData = new Array(
 
var imgData = new Array(
 
{name:"backImage", path:"https://static.igem.org/mediawiki/2017/7/70/TsinghuaA-fg_backimage.png"},
 
{name:"backImage", path:"https://static.igem.org/mediawiki/2017/7/70/TsinghuaA-fg_backimage.png"},
{name:"btnFluid", path:"https://static.igem.org/mediawiki/2017/4/44/TsinghuaA-fg_btnFluid.png"},
 
 
{name:"btnFluid", path:"https://static.igem.org/mediawiki/2017/4/44/TsinghuaA-fg_btnFluid.png"},
 
{name:"btnFluid", path:"https://static.igem.org/mediawiki/2017/4/44/TsinghuaA-fg_btnFluid.png"},
 
{name:"inputBG", path:"https://static.igem.org/mediawiki/2017/1/1a/TsinghuaA-fg_inputBG.png"},
 
{name:"inputBG", path:"https://static.igem.org/mediawiki/2017/1/1a/TsinghuaA-fg_inputBG.png"},

Revision as of 14:47, 24 October 2017

//++++++++++++++++++ Init Canvase ++++++++++++++++++++// init(40, "mylegend", 640, 480, main); // ,LEvent.INIT


//++++++++++++++++ Global Variables ++++++++++++++++++//

//--------------- BackLayer Things var backLayer, loadingLayer, fluidButton;

//--------------- InputLayer Things var inputLayer;

//--------------- Game Scene Things var clickLayer, lifeLayer, cellLayer, resultLayer;

//---------------- Game Statistics

var gameNum = 1;

var TOTALCELL = 60000, ONEGAMECELL = TOTALCELL / 10; var userCellTot=[0,0,0], aiCellTot=[0,0,0]; var userCellGame = [[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0]]; var aiCellGame = [[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0]]; var gameResult = new Array(5); // 1: User Win

var userMax=0, aiMax=0; var userWin=0, aiWin=0, drawNum=0; var USER = 0, AI = 1, DRAW = 2;

var started;

var nowPara;

/*var QUEUE_LEN = 200; var cellLog = new Array(QUEUE_LEN); var logTotal, logNow;*/

// ----------------- Timers // var timer1, timer2;

// ----------------- Images var imglist = {}; var imgData = new Array( {name:"backImage", path:"TsinghuaA-fg_backimage.png"}, {name:"btnFluid", path:"TsinghuaA-fg_btnFluid.png"}, {name:"inputBG", path:"TsinghuaA-fg_inputBG.png"},

   {name:"resultBG", path:"TsinghuaA-fg_resultBG.png"},

{name:"OKBtn", path:"TsinghuaA-fg_OKBtn.png"},

   {name:"endBtn", path:"TsinghuaA-fg_endBtn.png"},
   {name:"resultBtn", path:"TsinghuaA-fg_resultBtn.png"},

{name:"lWarrior", path:"TsinghuaA-fg_lWarrior.png"}, {name:"lFarmer", path:"TsinghuaA-fg_lFarmer.png"}, {name:"lBeggar", path:"TsinghuaA-fg_lBeggar.png"},

   {name:"rWarrior", path:"TsinghuaA-fg_rWarrior.png"},
   {name:"rFarmer", path:"TsinghuaA-fg_rFarmer.png"},
   {name:"rBeggar", path:"TsinghuaA-fg_rBeggar.png"},
   {name:"StartBtn", path:"TsinghuaA-fg_StartBtn.png"},
   {name:"BloodCap", path:"TsinghuaA-fg_BloodCap.png"},
   {name:"Blood", path:"TsinghuaA-fg_Blood.png"}

);


// +++++++++++++++ Functions +++++++++++++++//

function main(){ //背景层初始化 backLayer = new LSprite(); //在背景层上绘制黑色背景 backLayer.graphics.drawRect(1,"#000000",[0,0,640,480],true,"#000000"); //背景显示 addChild(backLayer); //进度条读取层初始化 loadingLayer = new LoadingSample1(); //进度条读取层显示 backLayer.addChild(loadingLayer); //利用LLoadManage类,读取所有图片,并显示进度条进程 LLoadManage.load( imgData, function(progress){ loadingLayer.setProgress(progress); }, gameInit ); }

//读取完所有图片,进行游戏标题画面的初始化工作 function gameInit(result){ //取得图片读取结果 imglist = result;

//移除进度条层 backLayer.removeChild(loadingLayer); loadingLayer = null;

initBackLayer(); }

// The first scene to be seen, click to start game. function initBackLayer() {

   backLayer.graphics.drawRect(1,"#0072E3",[0,0,640,480],true,"#0072E3");
   //显示游戏标题
   var title = new LTextField();
   title.x = 90;
   title.y = 150;
   title.size = 35;
   title.color = "#ffffff";
   title.text = "Game for Liquid Medium";
   title.weight = "bolder";
   title.textBaseline = "alphabetic";
   title.font = "Georgia";
   title.style = "italic";
   backLayer.addChild(title);
   fluidButton = getButton("btnFluid")
   fluidButton.x = 245;
   fluidButton.y = 300;
   backLayer.addChild(fluidButton);
   fluidButton.addEventListener(LMouseEvent.MOUSE_UP,startFluid);

}


function initTopData() {

   userWin = aiWin = drawNum = 0;
   gameNum = 1;
   nowPara = initPara.dup();

}

function initOneGameData() {

   nowPara = initPara.dup();
   userMax = aiMax = 0;

}

//游戏画面初始化 function startFluid() {

   clearTable();

//背景层清空 backLayer.die(); backLayer.removeAllChild();

//背景图片显示,再次初始化 var bitmap = new LBitmap(new LBitmapData(imglist["backImage"])); backLayer.addChild(bitmap);

for (var s = TOTALCELL/2, i = 0; i < 3; i++) { if (i < 2) { userCellTot[i] = TOTALCELL/6 - 1000 + Math.floor(2000*Math.random());

           // userCellTot[i] = TOTALCELL/6; // Change

aiCellTot[i] = TOTALCELL/3 - userCellTot[i]; s -= userCellTot[i]; } else { userCellTot[i] = s; aiCellTot[i] = TOTALCELL/3 - s; } }

   inputLayer = new InputLayer(userCellTot);
   backLayer.addChild(inputLayer);

}

// Main Function function startGame() {

   initOneGameData();
   if (!detectNumber()) {
       return;
   }
   setAICellNum();
   
   // Change Scene
   backLayer.removeChild(inputLayer);
   inputLayer = null;
   //addChild(backLayer);
   clickLayer = new ClickLayer();
   backLayer.addChild(clickLayer);
   lifeLayer = new LifeLayer();
   backLayer.addChild(lifeLayer);
   lifeLayer.drawLife(getCellNum());
   cellLayer = new CellLayer();
   backLayer.addChild(cellLayer);
   started = 0;
   logTotal = logNow = 0;
   //backLayer.addEventListener(LEvent.ENTER_FRAME, flashFrame);
   backLayer.addEventListener(LEvent.ENTER_FRAME, onframe);
   // setTimeout(cal, 10);
   // timer1 = setInterval(cal, 29);
   // var myTimer = new LTimer(20, 0);
   // myTimer.addEventListener(LTimerEvent.TIMER, flashFrame);
   // myTimer.start();
   // timer2 = setInterval(draw, 31);

}

/*function cal() {

   var m = max(nowPara.elements.slice(0, 6))
   if (!started && m > 1e5) {
       started = 1;
   }
   if (started &&  m < 1e5) {
       //backLayer.removeEventListener(LEvent.ENTER_FRAME, onframe);
       //backLayer.removeEventListener(LEvent.ENTER_FRAME, flashFrame);
       //alert(times);
       //endGame();
       clearInterval(timer1);
       return;
   }
   for (var i = 0; i < 22; i++) {
       nowPara = nextNum(nowPara);
       var tmp = sum(nowPara.elements.slice(0, 3));
       if (userMax < tmp) {
           userMax = tmp;
       }
       tmp = sum(nowPara.elements.slice(3, 6));
       if (aiMax < tmp) {
           aiMax = tmp;
       }
   }
   cellLog[(logTotal++) % QUEUE_LEN] = nowPara.elements.slice(0, 6);

}

function draw() {

   lifeLayer.drawLife(cellLog[(logNow++) % QUEUE_LEN]);
   flashFrame();
   if (logNow > logTotal) {
       endGame();
       clearInterval(timer2);
       return;
   }

}*/

// Update Dom function clearTable() {

   var table = document.getElementById("re_table");
   table.removeChild(table.children[0]);
   table.innerHTML = 
`
  Init Final Result
Warriors Farmers Beggars
Game 1 User
AI
Game 2 User
AI
Game 3 User
AI
Game 4 User
AI
Game 5 User
AI
`;

}


// Give you a button function getButton(value){

   var btnUp = new LBitmap(new LBitmapData(imglist[value]));
   var btnOver = new LBitmap(new LBitmapData(imglist[value]));
   var btn = new LButton(btnUp,btnOver);
   btn.name = value;
   return btn;

}


//------------------- Frame Loop /*function onframe1() {

   var m = max(nowPara.elements.slice(0, 6))
   if (!started && m > 1e5) {
       started = 1;
   }
   if (started &&  m < 1e5) {
       backLayer.removeEventListener(LEvent.ENTER_FRAME, onframe);
       //backLayer.removeEventListener(LEvent.ENTER_FRAME, flashFrame);
       //alert(times);
       endGame();
       return;
   }
   for (var i = 0; i < 100; i++) {
       nowPara = nextNum(nowPara);
       var tmp = sum(nowPara.elements.slice(0, 3));
       if (userMax < tmp) {
           userMax = tmp;
       }
       tmp = sum(nowPara.elements.slice(3, 6));
       if (aiMax < tmp) {
           aiMax = tmp;
       }
   }
   //alert(nowPara.inspect());
   //alert(userMax);
   lifeLayer.drawLife(getCellNum());

}*/


function onframe() {

   var m = max(nowPara.elements.slice(0, 6))
   if (!started && m > 1e5) {
       started = 1;
   }
   if (started &&  m < 5) {
       backLayer.removeEventListener(LEvent.ENTER_FRAME, onframe);
       //backLayer.removeEventListener(LEvent.ENTER_FRAME, flashFrame);
       //alert(times);
       endGame();
       return;
   }
   for (var i = 0; i < 50; i++) {
       nowPara = nextNum(nowPara);
       var tmp = sum(nowPara.elements.slice(0, 3));
       if (userMax < tmp) {
           userMax = tmp;
       }
       tmp = sum(nowPara.elements.slice(3, 6));
       if (aiMax < tmp) {
           aiMax = tmp;
       }
   }
   //alert(nowPara.inspect());
   //alert(userMax);
   lifeLayer.drawLife(nowPara.elements.slice(0, 6)); // getCellNum()
   cellLayer.onframe();

}


//------------------- Button Listener function exitGame() {

   backLayer.removeAllChild();
   initBackLayer();
   fluidButton.addEventListener(LMouseEvent.MOUSE_UP,startFluid);
   initTopData();

}

function fastForword() {

   backLayer.removeEventListener(LEvent.ENTER_FRAME, onframe);
   //backLayer.removeEventListener(LEvent.ENTER_FRAME, flashFrame);
   while (max(nowPara.elements.slice(0, 6)) > 1) {
       nowPara = nextNum(nowPara);
       var tmp = sum(nowPara.elements.slice(0, 3));
       if (userMax < tmp) {
           userMax = tmp;
       }
       tmp = sum(nowPara.elements.slice(3, 6));
       if (aiMax < tmp) {
           aiMax = tmp;
       }
   }
   endGame();

}


//----------------- Change Scene

function endGame() {

   // Max cell number
   var str = "u" + gameNum + "fin";
   var tb  = document.getElementById(str);
   tb.innerText = userMax.toExponential(2);
   str = "a" + gameNum + "fin";
   tb = document.getElementById(str);
   tb.innerText = aiMax.toExponential(2);
   // Result
   var restr;
   str = "g" + gameNum;
   tb = document.getElementById(str);
   if (Math.abs(aiMax - userMax) < 1e7) {
       restr = "Draw!";
       drawNum += 1;
       gameResult[gameNum-1] = DRAW;
   }
   else if (aiMax > userMax) {
       restr = "AI won!";
       aiWin += 1;
       gameResult[gameNum-1] = AI;
   } else {
       restr = "You won!";
       userWin += 1;
       gameResult[gameNum-1] = USER;
   }
   tb.innerText = restr;
   if (userWin == 3 || aiWin == 3) {
       gameNum = 5;
   }
   // Next Game
   changeGameNum();
   
   resultLayer = new ResultLayer();
   resultLayer.showResult(userMax, aiMax, restr);
   backLayer.addChild(resultLayer);
   backLayer.removeChild(cellLayer);
   cellLayer = null;
   clickLayer.changeBtn();
   if (gameNum == 1) {
       if (userWin > aiWin) {
           alert("You won this game!");
       } else if (aiWin > userWin) {
           alert("AI won this game!");
       } else {
           alert("This game was tied!");
       }
   }

}

function nextGame() {

   backLayer.removeChild(resultLayer);
   backLayer.removeChild(lifeLayer);
   backLayer.removeChild(clickLayer);
   resultLayer = null;
   lifeLayer = null;
   clickLayer = null;
   inputLayer = new InputLayer(userCellTot);
   backLayer.addChild(inputLayer);

}

function changeGameNum() {

   gameNum ++;
   if (gameNum > 5) {
       gameNum = 1;
   }

}


//----------------- Cell Numbers Setting

function detectNumber() {

   // Number Detecting
   // Need to be optimizing
   var nums = new Array(3);
   for (var i = 0; i < 3; i++) {
       nums[i] = inputLayer.inputBoxNum(i);
       // nums[i] = gameNum < 5 ? 2000 : userCellTot[i]; // Change
       if (isNaN(nums[i])) {
           // if (i != 2) {
           //     alert('Please input Numbers!')
           //     return false;
           // } else {
               nums[i] = 0;
           // }
       }
       if (nums[i] < 0) {
           alert('Please input numbers above 0!');
           return false;
       }
       if (i == 2 && sum(nums) != ONEGAMECELL) {
           alert(`The total number must be ${ONEGAMECELL}!\nChange Beggars automatically~`);
           nums[2] = ONEGAMECELL - nums[0] - nums[1];
           inputLayer.setBoxNum(2, nums[2]);
           return false;
       }
       if (nums[i] > userCellTot[i]) {
           alert('The number must not more than total!');
           return false;
       }
   }
   for (var i = 0; i < 3; i++) {
       userCellTot[i] -= nums[i];
       nowPara.elements[i] = nums[i];
       var str = "u" + gameNum;
       switch (i) {
           case 0: str += 'w'; break;
           case 1: str += 'f'; break;
           case 2: str += 'b'; break;
       }
       var tb = document.getElementById(str);
       tb.innerText = nums[i].toString();
   }
   userCellGame[gameNum-1] = nums;
   return true;

}

function setAICellNum() {

   var i, j;
   var nums = new Array(3);
   var inds = [0,0,0];
   for (i = 0; i < 3; i++) {
       if (i == 2) {
           nums[i] = ONEGAMECELL - nums[0] - nums[1];
       } else {
           if (gameNum == 1) {
               nums[i] = Math.round(aiCellTot[i] / (6 - gameNum));
           } else {
               if (gameResult[gameNum-2] == DRAW) {
                   nums[i] = aiCellGame[gameNum-2][i];
               } else if (gameResult[gameNum-2] == AI) {
                   nums[i] = Math.round(aiCellGame[gameNum-2][i] * 1.1 + (-0.1) * userCellGame[gameNum-2][i]);
               } else {
                   nums[i] = Math.round(aiCellGame[gameNum-2][i] * (-0.1) + 1.1 * userCellGame[gameNum-2][i]);
               }
           }
       }
   }
   // sort
   for (i = 0; i < 3; i++) {
       if (nums[i] > nums[inds[0]]) inds[0] = i;
       if (nums[i] <= nums[inds[2]] ) inds[2] = i;
   }
   inds[1] = 3 - inds[0] - inds[2];
   for (i = 0; i < 2; i++) {
       j = inds[i];
       nums[j] = (nums[j] < ONEGAMECELL) ? nums[j] : ONEGAMECELL;
       nums[j] = (nums[j] > 0) ? nums[j] : 0;
       if (nums[j] > aiCellTot[j]) {
           nums[j] = aiCellTot[j];
       }
   }
   j = inds[2];
   nums[j] = ONEGAMECELL - nums[inds[0]] - nums[inds[1]];
   if (nums[j] < 0) {
       nums[inds[0]] += nums[j];
       nums[j] = 0;
   } else if (nums[j] > aiCellTot[j]) {
       var r = nums[j] - aiCellTot[j];
       nums[j] = aiCellTot[j];
       for (i = 0; i < 2; i++) {
           var k = inds[i];
           nums[k] += r;
           if (nums[k] > aiCellTot[k]) {
               r = nums[k] - aiCellTot[k];
               nums[k] = aiCellTot[k];
           }
       }
   }
   for (i = 0; i < 3; i++) {
       aiCellTot[i] -= nums[i];
       nowPara.elements[3+i] = nums[i];
       var str = "a" + gameNum;
       switch (i) {
           case 0: str += 'w'; break;
           case 1: str += 'f'; break;
           case 2: str += 'b'; break;
       }
       var tb = document.getElementById(str);
       tb.innerText = nums[i].toString();
   }
   aiCellGame[gameNum-1] = nums;

}

/*function ddetectNumber() {

   for (var i = 0; i < 3; i++) {
       userCellGame[i] = Math.round(userCellTot[i] / (6 - gameNum));
       userCellTot[i] -= userCellGame[i];
       nowPara.elements[i] = userCellGame[i];
       var str = "u" + gameNum;
       switch (i) {
           case 0: str += 'w'; break;
           case 1: str += 'f'; break;
           case 2: str += 'b'; break;
       }
       var tb = document.getElementById(str);
       tb.innerText = userCellGame[i].toString();
   }
   return true;

}*/