Skip to content

Commit 870f0e6

Browse files
committed
add hint check unplaced Xs
1 parent 741d6a6 commit 870f0e6

File tree

5 files changed

+128
-32
lines changed

5 files changed

+128
-32
lines changed

src/engine/hints/checkUnplacedX.ts

Lines changed: 0 additions & 5 deletions
This file was deleted.
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
import { HintFunction } from ".";
2+
import { Board, Cell, Hint, Mark, Region, Regions } from "../interfaces";
3+
4+
const getQueens = (board: Board): Cell[] => {
5+
const queens: Cell[] = []
6+
for (let rowIndex = 0; rowIndex < board.length; rowIndex++) {
7+
for (let colIndex = 0; colIndex < board.length; colIndex++) {
8+
if (board[rowIndex][colIndex] !== Mark.Queen) continue;
9+
queens.push({
10+
col: colIndex,
11+
row: rowIndex
12+
})
13+
}
14+
}
15+
return queens
16+
}
17+
18+
const getUnremovedCellsOfRegion = (board: Board, regions: Regions, cell: Cell): Cell[] => {
19+
const unremovedCells: Cell[] = []
20+
const regionTarget: Region = regions[cell.row][cell.col]
21+
for (let rowIndex = 0; rowIndex < board.length; rowIndex++) {
22+
for (let colIndex = 0; colIndex < board.length; colIndex++) {
23+
if (regions[rowIndex][colIndex] !== regionTarget) continue;
24+
if (board[rowIndex][colIndex] === Mark.Empty) {
25+
unremovedCells.push({
26+
col: colIndex,
27+
row: rowIndex
28+
})
29+
}
30+
}
31+
}
32+
return unremovedCells;
33+
}
34+
35+
const getUnremovedCellsOfARow = (board: Board, regions: Regions, cell: Cell): Cell[] => {
36+
const unremovedCells: Cell[] = []
37+
38+
for (let colIndex = 0; colIndex < board.length; colIndex++) {
39+
if (board[cell.row][colIndex] === Mark.Empty) {
40+
unremovedCells.push({
41+
col: colIndex,
42+
row: cell.row
43+
})
44+
}
45+
}
46+
return unremovedCells
47+
}
48+
49+
const getUnremovedCellsOfAColumn = (board: Board, regions: Regions, cell: Cell): Cell[] => {
50+
const unremovedCells: Cell[] = []
51+
52+
for (let rowIndex = 0; rowIndex < board.length; rowIndex++) {
53+
if (board[rowIndex][cell.col] === Mark.Empty) {
54+
unremovedCells.push({
55+
col: cell.col,
56+
row: rowIndex
57+
})
58+
}
59+
}
60+
return unremovedCells
61+
}
62+
63+
const getUnremovedCellsAround = (board: Board, regions: Regions, cell: Cell): Cell[] => {
64+
const unremovedCells: Cell[] = []
65+
66+
const directions = [
67+
[-1, 0],
68+
[1, 0],
69+
[0, -1],
70+
[0, 1],
71+
[-1, -1],
72+
[-1, 1],
73+
[1, -1],
74+
[1, 1]
75+
]
76+
77+
directions.forEach(([dRow, dCol]) => {
78+
const xRow = cell.row + dRow;
79+
const xCol = cell.col + dCol;
80+
81+
if (
82+
xRow >= 0 &&
83+
xRow < board.length &&
84+
xCol >= 0 &&
85+
xCol < board.length &&
86+
board[xRow][xCol] == Mark.Empty
87+
) {
88+
unremovedCells.push({
89+
col: xCol,
90+
row: xRow
91+
})
92+
}
93+
});
94+
return unremovedCells
95+
}
96+
97+
const buildHint = (crossedCells: Cell[]): Hint => {
98+
return {
99+
highlightedCells: [],
100+
crossedCells,
101+
message: 'TODO'
102+
}
103+
}
104+
105+
const checkUnplacedXs: HintFunction = (board, regions) => {
106+
// per ogni regina
107+
for(const queen of getQueens(board)) {
108+
// 1. controlla che ogni altra altra cella della regione sia rimossa
109+
const unremovedCellsOfRegion = getUnremovedCellsOfRegion(board, regions, queen)
110+
if (unremovedCellsOfRegion.length > 0) return buildHint(unremovedCellsOfRegion)
111+
// 2. che tutta la riga sia stata rimossa
112+
const unremovedCellsOfARow = getUnremovedCellsOfARow(board, regions, queen)
113+
if (unremovedCellsOfARow.length > 0) return buildHint(unremovedCellsOfARow)
114+
// 3. che tutta la colonna sia stata rimossa
115+
const unremovedCellsOfAColumn = getUnremovedCellsOfAColumn(board, regions, queen)
116+
if (unremovedCellsOfAColumn.length > 0) return buildHint(unremovedCellsOfAColumn)
117+
// 4. che le celle intorno siano state rimosse
118+
const unremovedCellsAround = getUnremovedCellsAround(board, regions, queen)
119+
if (unremovedCellsAround.length > 0) return buildHint(unremovedCellsAround)
120+
}
121+
return null;
122+
}
123+
124+
export default checkUnplacedXs

src/engine/hints/checkUnplasedX.test.ts

Lines changed: 0 additions & 27 deletions
This file was deleted.

src/engine/hints/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ import allOtherCellOfARegionAreRemoved from "./allOtherCellOfARegionAreRemoved";
44
import allThisCellsCanBeRemoved from "./allThisCellsCanBeRemoved";
55
import highlightedRegionMustContainOneQueen from "./highlightedRegionMustContainOneQueen";
66
import noHintsFound from "./noHintsFound";
7+
import checkUnplacedXs from "./checkUnplacedXs";
78

89
export type HintFunction = (board: Board, regions: Regions) => Hint;
910

1011
export {
12+
checkUnplacedXs,
1113
allOtherCellOfARegionAreRemoved,
1214
allThisCellsCanBeRemoved,
1315
checkLineWithOnlyOneEmptyCell,

src/engine/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
noHintsFound,
44
allThisCellsCanBeRemoved,
55
checkLineWithOnlyOneEmptyCell,
6+
checkUnplacedXs,
67
highlightedRegionMustContainOneQueen,
78
} from './hints';
89
import { Board, Cell, Hint, Mark, Regions } from './interfaces';
@@ -87,6 +88,7 @@ export default class Engine implements IEngine {
8788
hints(): Hint {
8889
const hintStrategies: HintFunction[] = [
8990
allOtherCellOfARegionAreRemoved,
91+
checkUnplacedXs,
9092
highlightedRegionMustContainOneQueen,
9193
checkLineWithOnlyOneEmptyCell,
9294
// allThisCellsCanBeRemoved, // TODO

0 commit comments

Comments
 (0)