(states,depth, isMaximising)
| 71 | |
| 72 | } |
| 73 | const minimax = (states,depth, isMaximising) =>{ |
| 74 | // console.log(""+states); |
| 75 | result = checkWinner(states) |
| 76 | if(result!=null){ |
| 77 | if(result == PLAYER_X) |
| 78 | return 100-depth |
| 79 | else if(result == PLAYER_Y) |
| 80 | return -100+depth |
| 81 | else return 0 |
| 82 | } |
| 83 | let bestScore; |
| 84 | if(isMaximising){ |
| 85 | bestScore = -Infinity |
| 86 | for(let i =0;i<grid_side;i++){ |
| 87 | for(let j =0; j<grid_side;j++){ |
| 88 | if(states[i][j]==0){ |
| 89 | states[i][j] = 1; |
| 90 | score = minimax(states, depth+1,false) |
| 91 | states[i][j] = 0 |
| 92 | bestScore = Math.max(score,bestScore) |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | else{ |
| 98 | bestScore = Infinity |
| 99 | for(let i =0 ; i<grid_side; i++){ |
| 100 | for(let j = 0; j<grid_side; j++){ |
| 101 | if(states[i][j]==0){ |
| 102 | states[i][j] = -1; |
| 103 | score = minimax(states,depth+1,true) |
| 104 | states[i][j] = 0 |
| 105 | bestScore= Math.min(score,bestScore) |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | return bestScore |
| 111 | |
| 112 | } |
| 113 | const checkWinner = (states)=>{ |
| 114 | // check rows |
| 115 | for(let i=0;i<grid_side;i++){ |
no test coverage detected