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 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
| import { Node } from 'cc'; import { HallTurtle } from '../hall/HallTurtle'; export class GridHandler { private static _instance: GridHandler = null; private constructor() { } public static get instance(): GridHandler { if (this._instance == null) { this._instance = new GridHandler(); } return this._instance; }
public grid: Node[][] = [ [null, null, null], [null, null, null], [null, null, null], ];
public dispatchItemToEmptyCells(item: Node) { for (let i = 0; i < 3; i++) { for (let j = 0; j < 3; j++) { if (this.grid[i][j] === null) { this.grid[i][j] = item; return [i, j]; } } } return null }
public clearCells(i: number, j: number) { this.grid[i][j] = null; }
public clearAllCells() { this.grid = Array(3).fill(null).map(() => Array(3).fill(null)); }
public hasTwoSameItems(): Node[] { let grid = this.grid const itemMap = new Map<number, Node>(); for (let i = 0; i < 3; i++) { for (let j = 0; j < 3; j++) { const item = grid[i][j]; if (item !== null) { let turtleColorID = item.getComponent(HallTurtle).turtleColorID if (itemMap.has(turtleColorID)) { return [itemMap.get(turtleColorID), item] } else { itemMap.set(turtleColorID, item); } } } } return []; }
public hasSpecifiedItem(luckyColorID: number): Node { let grid = this.grid for (let i = 0; i < 3; i++) { for (let j = 0; j < 3; j++) { const item = grid[i][j] if (item && item.getComponent(HallTurtle).turtleColorID === luckyColorID) { return item; } } } return null; }
public checkThreeInARow(): Node[] { let grid = this.grid;
const isThreeInARow = (nodes: Node[]): boolean => { return nodes.every(node => node && node.getComponent(HallTurtle).turtleColorID === nodes[0].getComponent(HallTurtle).turtleColorID); };
for (let i = 0; i < 3; i++) { let row = [grid[i][0], grid[i][1], grid[i][2]]; if (isThreeInARow(row)) { return row; } }
for (let i = 0; i < 3; i++) { let col = [grid[0][i], grid[1][i], grid[2][i]]; if (isThreeInARow(col)) { return col; } }
let leftDiagonal = [grid[0][0], grid[1][1], grid[2][2]]; if (isThreeInARow(leftDiagonal)) { return leftDiagonal; }
let rightDiagonal = [grid[0][2], grid[1][1], grid[2][0]]; if (isThreeInARow(rightDiagonal)) { return rightDiagonal; }
return []; }
public areAllCellsEmpty(): boolean { let grid = this.grid for (let i = 0; i < 3; i++) { for (let j = 0; j < 3; j++) { if (grid[i][j] !== null) { return false; } } } return true; }
public hasFamily(): Node[] { let grid = this.grid; const itemMap = new Map<number, Node>();
for (let i = 0; i < 3; i++) { for (let j = 0; j < 3; j++) { const item = grid[i][j]; if (!item) { return []; }
let turtleColorID = item.getComponent(HallTurtle).turtleColorID;
if (itemMap.has(turtleColorID)) { return []; }
itemMap.set(turtleColorID, item); } }
return Array.from(itemMap.values()); }
public countEmptyCells(): number { let emptyCount = 0; for (let i = 0; i < 3; i++) { for (let j = 0; j < 3; j++) { if (this.grid[i][j] === null) { emptyCount++; } } } return emptyCount; }
}
|