|
| 1 | +let game_arr = []; |
| 2 | +let gamePlayer = true; |
| 3 | +const onPlayerTrun = function(i){ |
| 4 | + if(!gamePlayer){ |
| 5 | + return false; |
| 6 | + } |
| 7 | + if(isPositionisfree(i)){ |
| 8 | + gamePlayer=false; |
| 9 | + setGameData(0,i); |
| 10 | + if(isWinnig(0,game_arr,false)){ |
| 11 | + setMessage("Player X is Won"); |
| 12 | + return false; |
| 13 | + } |
| 14 | + let computermove = computerMove(1); |
| 15 | + if(computermove){ |
| 16 | + setGameData(1,computermove); |
| 17 | + } |
| 18 | + if(isWinnig(1,game_arr,false)){ |
| 19 | + setMessage("Player 0 is Won"); |
| 20 | + return false; |
| 21 | + } |
| 22 | + if(!isGameBordisFree()){ |
| 23 | + setMessage("Draw"); |
| 24 | + return false; |
| 25 | + } |
| 26 | + setMessage("Player X turn"); |
| 27 | + |
| 28 | + gamePlayer=true; |
| 29 | + } |
| 30 | + |
| 31 | +}; |
| 32 | +const isGameBordisFree = function(){ |
| 33 | + return getCornerFreePosition(1,2,3,4,5,6,7,8,9).length > 0; |
| 34 | +}; |
| 35 | +const computerMove=function(turn){ |
| 36 | + setMessage("Player 0 turn"); |
| 37 | + let oppTurn = (turn === 0)?1:0; |
| 38 | + let winingposition = getWiningPosition(turn); |
| 39 | + //console.log("getWiningPosition",turn,winingposition); |
| 40 | + if(winingposition){ |
| 41 | + return winingposition; |
| 42 | + } |
| 43 | + winingposition = getWiningPosition(oppTurn); |
| 44 | + //console.log("getWiningPosition",oppTurn,winingposition); |
| 45 | + if(winingposition){ |
| 46 | + return winingposition; |
| 47 | + } |
| 48 | + let freePosition; |
| 49 | + if(!isCornerisfree(oppTurn)){ |
| 50 | + freePosition = getCornerFreePosition(5); |
| 51 | + if(freePosition.length > 0){ |
| 52 | + return getRandomItem(freePosition); |
| 53 | + } |
| 54 | + } |
| 55 | + if(isCrossCorner(oppTurn)){ |
| 56 | + freePosition = getCornerFreePosition(2,4,6,8); |
| 57 | + if(freePosition.length > 0){ |
| 58 | + return getRandomItem(freePosition); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + freePosition = getCornerFreePosition(1,3,7,9); |
| 63 | + if(freePosition.length > 0){ |
| 64 | + return getRandomItem(freePosition); |
| 65 | + } |
| 66 | + freePosition = getCornerFreePosition(5); |
| 67 | + if(freePosition.length > 0){ |
| 68 | + return getRandomItem(freePosition); |
| 69 | + } |
| 70 | + freePosition = getCornerFreePosition(2,4,6,8); |
| 71 | + if(freePosition.length > 0){ |
| 72 | + return getRandomItem(freePosition); |
| 73 | + } |
| 74 | + |
| 75 | + return null; |
| 76 | +}; |
| 77 | +const getRandomItem=function(arr) { |
| 78 | + |
| 79 | + // get random index value |
| 80 | + const randomIndex = Math.floor(Math.random() * arr.length); |
| 81 | + |
| 82 | + // get random item |
| 83 | + const item = arr[randomIndex]; |
| 84 | + |
| 85 | + return item; |
| 86 | +}; |
| 87 | +const getCornerFreePosition=function(...position){ |
| 88 | + let freePosition = []; |
| 89 | + position.forEach(item=>{ |
| 90 | + if(isPositionisfree(item)){ |
| 91 | + freePosition.push(item); |
| 92 | + } |
| 93 | + }); |
| 94 | + return freePosition; |
| 95 | +}; |
| 96 | +const isCornerisfree=function(turn){ |
| 97 | + let retValue = true; |
| 98 | + let char=getPlayerChar(turn); |
| 99 | + [1,3,7,9].forEach(item=>{ |
| 100 | + if(!isPositionisfree(item) && game_arr[item]===char){ |
| 101 | + retValue = false; |
| 102 | + return retValue; |
| 103 | + } |
| 104 | + }); |
| 105 | + return retValue; |
| 106 | +}; |
| 107 | +const isCrossCorner=function(turn){ |
| 108 | + let char=getPlayerChar(turn); |
| 109 | + if(!isPositionisfree(1) && !isPositionisfree(9) && game_arr[1]===char && game_arr[9]===char){ |
| 110 | + return true; |
| 111 | + }else if(!isPositionisfree(3) && !isPositionisfree(7) && game_arr[3]===char && game_arr[7]===char){ |
| 112 | + return true; |
| 113 | + } |
| 114 | + return false; |
| 115 | +}; |
| 116 | +const getWiningPosition=function(turn){ |
| 117 | + let computerMovePosition; |
| 118 | + let char = getPlayerChar(turn); |
| 119 | + for(let i=1;i<=9;i++){ |
| 120 | + if(isPositionisfree(i)){ |
| 121 | + let copyGameBord = cloneArray(game_arr); |
| 122 | + copyGameBord[i]=char; |
| 123 | + if(isWinnig(turn,copyGameBord,true)){ |
| 124 | + computerMovePosition=i; |
| 125 | + return computerMovePosition; |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + return computerMovePosition; |
| 130 | +}; |
| 131 | +const cloneArray=function(arr){ |
| 132 | + return arr.map(a => a); |
| 133 | +}; |
| 134 | +const getPlayerChar=function(turn){ |
| 135 | + return (turn === 0)?"X":"0"; |
| 136 | +}; |
| 137 | +const setGameData=function(turn,i){ |
| 138 | + let char = getPlayerChar(turn); |
| 139 | + document.getElementById("b"+i).value = char; |
| 140 | + document.getElementById("b"+i).disabled = true; |
| 141 | + game_arr[i]=char; |
| 142 | +}; |
| 143 | +const isWinnig=function(turn,gameBord,isdummy){ |
| 144 | + let char = getPlayerChar(turn); |
| 145 | + if(winingCheck(gameBord,char,1,2,3,isdummy)){ |
| 146 | + return true; |
| 147 | + }else if(winingCheck(gameBord,char,4,5,6,isdummy)){ |
| 148 | + return true; |
| 149 | + }else if(winingCheck(gameBord,char,7,8,9,isdummy)){ |
| 150 | + return true; |
| 151 | + }else if(winingCheck(gameBord,char,1,4,7,isdummy)){ |
| 152 | + return true; |
| 153 | + }else if(winingCheck(gameBord,char,2,5,8,isdummy)){ |
| 154 | + return true; |
| 155 | + }else if(winingCheck(gameBord,char,3,6,9,isdummy)){ |
| 156 | + return true; |
| 157 | + }else if(winingCheck(gameBord,char,1,5,9,isdummy)){ |
| 158 | + return true; |
| 159 | + }else if(winingCheck(gameBord,char,3,5,7,isdummy)){ |
| 160 | + return true; |
| 161 | + } |
| 162 | + return false; |
| 163 | +}; |
| 164 | +const setWiningColor = function(a,b,c){ |
| 165 | + document.getElementById("b"+a).style = "color:red;"; |
| 166 | + document.getElementById("b"+b).style = "color:red;"; |
| 167 | + document.getElementById("b"+c).style = "color:red;"; |
| 168 | +}; |
| 169 | +const winingCheck=function(gameBord,ch,a,b,c,isdummy){ |
| 170 | + let isWining = ch === gameBord[a] && gameBord[a] === gameBord[b] && gameBord[b] === gameBord[c]; |
| 171 | + if(isWining && !isdummy){ |
| 172 | + setWiningColor(a,b,c); |
| 173 | + } |
| 174 | + return isWining; |
| 175 | +}; |
| 176 | +const isPositionisfree=function(i){ |
| 177 | + if(game_arr[i]){ |
| 178 | + return false; |
| 179 | + } |
| 180 | + return true; |
| 181 | +}; |
| 182 | +const setMessage=function(msg){ |
| 183 | + document.getElementById("print").innerText = msg; |
| 184 | +}; |
| 185 | +setMessage("Player X turn"); |
| 186 | + |
| 187 | + |
0 commit comments