1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
| function winlinze_by_row(ques) { let count = function (arr, target) { let c = 0; for (let j = 0; j < arr.length; j++) { if (arr[j] === target) c++; } return c; }
let checkRow = function (arr, besides, target) { for (let i = 0; i < arr.length; i++) { if (i === besides) continue; for (let j = 0; j < arr[i].length; j++) { if (arr[i][j] === target) return [i, j] } } }
let checkEmpty = function (arr, row) { for (let i = 0; i < arr[row].length; i++) { if (arr[row][i] === 0) return [row, i] } }
for (let i = 0; i < ques.length; i ++){ let c = []; let emptySite = 0; for (let m = 0; m < ques[i].length; m++) c[m] = count(ques[i], m); for (let m = 0; m < ques[i].length; m++) { if (c[m] === 4 && c[0] === 1) { let t = checkRow(ques, i, m); if (t.length !== 0) return [t, checkEmpty(ques, i)] } } } }
function winlinze_by_diagonal(ques) { let leftSites = [[0, 0], [1, 1], [2, 2], [3, 3], [4, 4]]; let rightSites = [[0, 4], [1, 3], [2, 2], [3, 1], [4, 0]]; let checkEmpty = function (ques, sites) { for (let i = 0; i < sites.length; i++) { if (ques[sites[i][0]][sites[i][1]] === 0) return [i, sites[i]] } }
let count = function (ques, sites, target) { let c = 0; for (let j = 0; j < sites.length; j++) { if (ques[sites[j][0]][sites[j][1]] === target) c++; } return c; }
let indexOf = function (sites, i, j) { for (let k = 0; k < sites.length; k++) { if (sites[k][0] === i && sites[k][1] === j) return true; } return false }
let search = function (ques, sites, target) { for (let i = 0; i < ques.length; i++) { for (let j = 0; j < ques[i].length; j++) { if (ques[i][j] === target && !indexOf(sites,i, j)) return [i, j] } } }
let sites = [leftSites, rightSites]; for (let i = 0; i < sites.length; i++) { let e = checkEmpty(ques, sites[i]); let s = new Set(); for (let j = 0; j < sites[i].length; j++) s.add(ques[sites[i][j][0]][sites[i][j][1]]); if (s.size === 2 && count(ques, sites[i], ques[e[1][0]][e[1][1]]) === 1) { let rand = (e[0]+1) % 5; let w = search(ques, sites[i], ques[sites[i][rand][0]][sites[i][rand][1]]); if (w && w.length > 0) return [e[1], w] } } }
function get_userresponse(ques) { let arr = winlinze_by_row(ques); if (arr && arr.length > 0) return arr;
if (arr === undefined || arr.length === 0) { let new_ques = []; for (let index = 0; index < ques.length; index++) new_ques[index] = [ques[0][index], ques[1][index], ques[2][index], ques[3][index], ques[4][index]]; arr = winlinze_by_row(new_ques); let new_arr = []; for (let index = 0; arr && index < arr.length; index++) new_arr[index] = [arr[index][1], arr[index][0]] if (new_arr.length > 0) return new_arr;
return winlinze_by_diagonal(ques) } }
|