// (C)2008 Julio Di Egidio
// http://julio.diegidio.name
// mailto:julio@diegidio.name

var jde_3t_aStrategy={ran:"RAN",heu:"HEU",fuz:"FUZ"};jde_3t_aStrategy.getRandomNonFuzzy=function(){var idx=Math.floor(Math.random()*2);if(idx===0){return this.ran;}return this.heu;};jde_3t_aStrategy.getDefaultNonFuzzy=function(){return this.heu;};jde_3t_aStrategy.parse=function(strategy){if(!(strategy===this.ran||strategy===this.heu||strategy===this.fuz)){return jde_ge_noValue;}return strategy;};jde_3t_aStrategy.getDefault=function(){return this.fuz;};jde_3t_aStrategy.getOptions=function(){var options=[];options[0]={value:this.ran,text:"Random",selected:false};options[1]={value:this.heu,text:"Heuristic",selected:false};options[2]={value:this.fuz,text:"Fuzzy",selected:true};return options;};function jde_3t_aThreshold(){return null;}jde_3t_aThreshold.getRandomIsOver=function(threshold){return Math.random()>=threshold;};jde_3t_aThreshold.parse=function(threshold){if(isNaN(threshold=parseFloat(threshold))||threshold<0||threshold>1){return jde_ge_noValue;}return threshold;};jde_3t_aThreshold.getDefault=function(){return(7/10);};jde_3t_aThreshold.getOptions=function(){var options=[];for(var i=0;i<=10;i++){options[i]={value:""+(i/10),text:(i*10)+"%",selected:(i===7)};}return options;};function jde_3t_APlayer(board){if(typeof(jde_ge_noValue)==="undefined"){return null;}if(typeof(jde_3t_Board)==="undefined"){return null;}function getWinMove(board,newCellState){for(var r=0;r<3;r++){var cell_r_0=board.getCell(new jde_3t_BoardCoord(r,0));var cell_r_1=board.getCell(new jde_3t_BoardCoord(r,1));var cell_r_2=board.getCell(new jde_3t_BoardCoord(r,2));if(cell_r_0===jde_3t_boardCellState.e&&newCellState===cell_r_1&&cell_r_1===cell_r_2){return new jde_3t_BoardCoord(r,0);}if(cell_r_1===jde_3t_boardCellState.e&&newCellState===cell_r_2&&cell_r_2===cell_r_0){return new jde_3t_BoardCoord(r,1);}if(cell_r_2===jde_3t_boardCellState.e&&newCellState===cell_r_0&&cell_r_0===cell_r_1){return new jde_3t_BoardCoord(r,2);}}for(var c=0;c<3;c++){var cell_0_c=board.getCell(new jde_3t_BoardCoord(0,c));var cell_1_c=board.getCell(new jde_3t_BoardCoord(1,c));var cell_2_c=board.getCell(new jde_3t_BoardCoord(2,c));if(cell_0_c===jde_3t_boardCellState.e&&newCellState===cell_1_c&&cell_1_c===cell_2_c){return new jde_3t_BoardCoord(0,c);}if(cell_1_c===jde_3t_boardCellState.e&&newCellState===cell_2_c&&cell_2_c===cell_0_c){return new jde_3t_BoardCoord(1,c);}if(cell_2_c===jde_3t_boardCellState.e&&newCellState===cell_0_c&&cell_0_c===cell_1_c){return new jde_3t_BoardCoord(2,c);}}if(1){var cell_0_0=board.getCell(new jde_3t_BoardCoord(0,0));var cell_1_1=board.getCell(new jde_3t_BoardCoord(1,1));var cell_2_2=board.getCell(new jde_3t_BoardCoord(2,2));if(cell_0_0===jde_3t_boardCellState.e&&newCellState===cell_1_1&&cell_1_1===cell_2_2){return new jde_3t_BoardCoord(0,0);}if(cell_1_1===jde_3t_boardCellState.e&&newCellState===cell_2_2&&cell_2_2===cell_0_0){return new jde_3t_BoardCoord(1,1);}if(cell_2_2===jde_3t_boardCellState.e&&newCellState===cell_0_0&&cell_0_0===cell_1_1){return new jde_3t_BoardCoord(2,2);}var cell_0_2=board.getCell(new jde_3t_BoardCoord(0,2));var cell_2_0=board.getCell(new jde_3t_BoardCoord(2,0));if(cell_0_2===jde_3t_boardCellState.e&&newCellState===cell_1_1&&cell_1_1===cell_2_0){return new jde_3t_BoardCoord(0,2);}if(cell_1_1===jde_3t_boardCellState.e&&newCellState===cell_2_0&&cell_2_0===cell_0_2){return new jde_3t_BoardCoord(1,1);}if(cell_2_0===jde_3t_boardCellState.e&&newCellState===cell_0_2&&cell_0_2===cell_1_1){return new jde_3t_BoardCoord(2,0);}}return jde_ge_noValue;}function getNextMove_RAN(board){var emptyCount=0;for(var r=0;r<3;r++){for(var c=0;c<3;c++){var cell_r_c=board.getCell(new jde_3t_BoardCoord(r,c));if(cell_r_c===jde_3t_boardCellState.e){emptyCount++;}}}if(emptyCount===0){return jde_ge_noValue;}var ref=Math.floor(Math.random()*emptyCount);var idx=0;for(var r=0;r<3;r++){for(var c=0;c<3;c++){var cell_r_c=board.getCell(new jde_3t_BoardCoord(r,c));if(cell_r_c===jde_3t_boardCellState.e){if(idx===ref){return new jde_3t_BoardCoord(r,c);}idx++;}}}return jde_ge_noValue;}function getNextMove_HEU(board,buGetNextMoveRef){var emptyCount=0;for(var r=0;r<3;r++){for(var c=0;c<3;c++){var cell_r_c=board.getCell(new jde_3t_BoardCoord(r,c));if(cell_r_c===jde_3t_boardCellState.e){emptyCount++;}}}if(emptyCount===0){return jde_ge_noValue;}var boardState=board.getState();var coordW=getWinMove(board,boardState===jde_3t_boardState.moveX?jde_3t_boardCellState.x:jde_3t_boardCellState.o);if(coordW!==jde_ge_noValue){return coordW;}var coordL=getWinMove(board,boardState===jde_3t_boardState.moveX?jde_3t_boardCellState.o:jde_3t_boardCellState.x);if(coordL!==jde_ge_noValue){return coordL;}var who_1_1=board.getCell(new jde_3t_BoardCoord(1,1));if(who_1_1===jde_3t_boardCellState.e){return new jde_3t_BoardCoord(1,1);}var who_0_0=board.getCell(new jde_3t_BoardCoord(0,0));var who_0_2=board.getCell(new jde_3t_BoardCoord(0,2));var who_2_0=board.getCell(new jde_3t_BoardCoord(2,0));var who_2_2=board.getCell(new jde_3t_BoardCoord(2,2));var emptyAngles=[];if(who_0_0===jde_3t_boardCellState.e){emptyAngles[emptyAngles.length]=new jde_3t_BoardCoord(0,0);}if(who_0_2===jde_3t_boardCellState.e){emptyAngles[emptyAngles.length]=new jde_3t_BoardCoord(0,2);}if(who_2_0===jde_3t_boardCellState.e){emptyAngles[emptyAngles.length]=new jde_3t_BoardCoord(2,0);}if(who_2_2===jde_3t_boardCellState.e){emptyAngles[emptyAngles.length]=new jde_3t_BoardCoord(2,2);}if(emptyAngles.length!==0){var idxAngle=Math.floor(Math.random()*emptyAngles.length);return emptyAngles[idxAngle];}return buGetNextMoveRef(board);}var _board=board;var _strategy,_fuzzyThreshold;var that=this;var def_strategy=jde_3t_aStrategy.getDefault();var def_fuzzyThreshold=jde_3t_aThreshold.getDefault();this.reset=function(){_strategy=def_strategy;_fuzzyThreshold=def_fuzzyThreshold;};this.getNextMove=function(strategy){if(!jde_3t_boardState.isMove(_board.getState())){return jde_ge_noValue;}strategy=jde_3t_aStrategy.parse(strategy);if(strategy===jde_ge_noValue){strategy=_strategy;}if(strategy===jde_3t_aStrategy.fuz){if(jde_3t_aThreshold.getRandomIsOver(_fuzzyThreshold)){strategy=jde_3t_aStrategy.getRandomNonFuzzy();}else{strategy=jde_3t_aStrategy.getDefaultNonFuzzy();}}if(strategy===jde_3t_aStrategy.ran){return getNextMove_RAN(_board);}else if(strategy===jde_3t_aStrategy.heu){return getNextMove_HEU(_board,getNextMove_RAN);}DEBUG_ASSERT_ERROR();};this.getStrategy=function(){return _strategy;};this.setStrategy=function(strategy){strategy=jde_3t_aStrategy.parse(strategy);if(strategy===jde_ge_noValue){return jde_ge_noValue;}return(_strategy=strategy);};this.getFuzzyThreshold=function(){return _fuzzyThreshold;};this.setFuzzyThreshold=function(threshold){threshold=jde_3t_aThreshold.parse(threshold);if(threshold===jde_ge_noValue){return jde_ge_noValue;}return(_fuzzyThreshold=threshold);};}