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
0 commit comments