Skip to content

Commit

Permalink
feat: add methods to find sub-grid, column, row
Browse files Browse the repository at this point in the history
  • Loading branch information
thisissandipp committed Jun 30, 2024
1 parent f6842e0 commit c42e8aa
Show file tree
Hide file tree
Showing 2 changed files with 266 additions and 86 deletions.
37 changes: 37 additions & 0 deletions lib/models/sudoku.dart
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,43 @@ class Sudoku extends Equatable {
return rawData;
}

/// Returns the list of [Block]s that belong to the same sub-grid as [block].
List<Block> getSubGridBlocks(Block block) {
final result = <Block>[];

// Find the dimension, and the length of the subgrid's column or row.
final dimension = getDimesion();
final subGridLength = sqrt(dimension).toInt();

// Relative position of the sub-grid, for example, if the Sudoku is
// 2 x 2 puzzle, the possible sub-grid positions will be (0, 0), (0, 1),
// (1, 0), and (1, 1).
final subGridPositionX = block.position.x ~/ subGridLength;
final subGridPositionY = block.position.y ~/ subGridLength;

for (var i = 0; i < subGridLength; i++) {
for (var j = 0; j < subGridLength; j++) {
final blockPositionX = subGridPositionX * subGridLength + i;
final blockPositionY = subGridPositionY * subGridLength + j;

final position = Position(x: blockPositionX, y: blockPositionY);
result.add(blocks.firstWhere((block) => block.position == position));
}
}

return result;
}

/// Returns the list of [Block]s that belong to same row as [block].
List<Block> getRowBlocks(Block block) {
return blocks.where((e) => e.position.x == block.position.x).toList();
}

/// Returns the list of [Block]s that belong to same column as [block].
List<Block> getColumnBlocks(Block block) {
return blocks.where((e) => e.position.y == block.position.y).toList();
}

@override
List<Object?> get props => [blocks];
}
315 changes: 229 additions & 86 deletions test/models/sudoku_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,94 +3,126 @@ import 'package:sudoku/models/models.dart';

void main() {
group('Sudoku', () {
const sudoku2x2Block0 = Block(
position: Position(x: 0, y: 0),
correctValue: 4,
currentValue: -1,
);

const sudoku2x2Block1 = Block(
position: Position(x: 0, y: 1),
correctValue: 1,
currentValue: 1,
isGenerated: true,
);

const sudoku2x2Block2 = Block(
position: Position(x: 0, y: 2),
correctValue: 2,
currentValue: -1,
);

const sudoku2x2Block3 = Block(
position: Position(x: 0, y: 3),
correctValue: 3,
currentValue: -1,
);

const sudoku2x2Block4 = Block(
position: Position(x: 1, y: 0),
correctValue: 2,
currentValue: 2,
isGenerated: true,
);

const sudoku2x2Block5 = Block(
position: Position(x: 1, y: 1),
correctValue: 3,
currentValue: 3,
isGenerated: true,
);

const sudoku2x2Block6 = Block(
position: Position(x: 1, y: 2),
correctValue: 4,
currentValue: -1,
);

const sudoku2x2Block7 = Block(
position: Position(x: 1, y: 3),
correctValue: 1,
currentValue: -1,
);

const sudoku2x2Block8 = Block(
position: Position(x: 2, y: 0),
correctValue: 1,
currentValue: -1,
);

const sudoku2x2Block9 = Block(
position: Position(x: 2, y: 1),
correctValue: 4,
currentValue: -1,
);

const sudoku2x2Block10 = Block(
position: Position(x: 2, y: 2),
correctValue: 3,
currentValue: 3,
isGenerated: true,
);

const sudoku2x2Block11 = Block(
position: Position(x: 2, y: 3),
correctValue: 2,
currentValue: 2,
isGenerated: true,
);

const sudoku2x2Block12 = Block(
position: Position(x: 3, y: 0),
correctValue: 3,
currentValue: -1,
);

const sudoku2x2Block13 = Block(
position: Position(x: 3, y: 1),
correctValue: 2,
currentValue: -1,
);

const sudoku2x2Block14 = Block(
position: Position(x: 3, y: 2),
correctValue: 1,
currentValue: 1,
isGenerated: true,
);

const sudoku2x2Block15 = Block(
position: Position(x: 3, y: 3),
correctValue: 4,
currentValue: -1,
);

const sudoku = Sudoku(
blocks: [
Block(
position: Position(x: 0, y: 0),
correctValue: 4,
currentValue: -1,
),
Block(
position: Position(x: 0, y: 1),
correctValue: 1,
currentValue: 1,
isGenerated: true,
),
Block(
position: Position(x: 0, y: 2),
correctValue: 2,
currentValue: -1,
),
Block(
position: Position(x: 0, y: 3),
correctValue: 3,
currentValue: -1,
),
Block(
position: Position(x: 1, y: 0),
correctValue: 2,
currentValue: 2,
isGenerated: true,
),
Block(
position: Position(x: 1, y: 1),
correctValue: 3,
currentValue: 3,
isGenerated: true,
),
Block(
position: Position(x: 1, y: 2),
correctValue: 4,
currentValue: -1,
),
Block(
position: Position(x: 1, y: 3),
correctValue: 1,
currentValue: -1,
),
Block(
position: Position(x: 2, y: 0),
correctValue: 1,
currentValue: -1,
),
Block(
position: Position(x: 2, y: 1),
correctValue: 4,
currentValue: -1,
),
Block(
position: Position(x: 2, y: 2),
correctValue: 3,
currentValue: 3,
isGenerated: true,
),
Block(
position: Position(x: 2, y: 3),
correctValue: 2,
currentValue: 2,
isGenerated: true,
),
Block(
position: Position(x: 3, y: 0),
correctValue: 3,
currentValue: -1,
),
Block(
position: Position(x: 3, y: 1),
correctValue: 2,
currentValue: -1,
),
Block(
position: Position(x: 3, y: 2),
correctValue: 1,
currentValue: 1,
isGenerated: true,
),
Block(
position: Position(x: 3, y: 3),
correctValue: 4,
currentValue: -1,
),
sudoku2x2Block0,
sudoku2x2Block1,
sudoku2x2Block2,
sudoku2x2Block3,
sudoku2x2Block4,
sudoku2x2Block5,
sudoku2x2Block6,
sudoku2x2Block7,
sudoku2x2Block8,
sudoku2x2Block9,
sudoku2x2Block10,
sudoku2x2Block11,
sudoku2x2Block12,
sudoku2x2Block13,
sudoku2x2Block14,
sudoku2x2Block15,
],
);

Expand Down Expand Up @@ -186,5 +218,116 @@ void main() {
);
});
});

group('getSubGridBlocks', () {
test('returns correct blocks - with block 3', () {
final subGridBlocks = sudoku.getSubGridBlocks(sudoku2x2Block3);
expect(
subGridBlocks,
equals(
<Block>[
sudoku2x2Block2,
sudoku2x2Block3,
sudoku2x2Block6,
sudoku2x2Block7,
],
),
);
});

test('returns correct blocks - with block 8', () {
final subGridBlocks = sudoku.getSubGridBlocks(sudoku2x2Block8);
expect(
subGridBlocks,
equals(
<Block>[
sudoku2x2Block8,
sudoku2x2Block9,
sudoku2x2Block12,
sudoku2x2Block13,
],
),
);
});

test('returns correct blocks - with block 15', () {
final subGridBlocks = sudoku.getSubGridBlocks(sudoku2x2Block15);
expect(
subGridBlocks,
equals(
<Block>[
sudoku2x2Block10,
sudoku2x2Block11,
sudoku2x2Block14,
sudoku2x2Block15,
],
),
);
});
});

group('getRowBlocks', () {
test('return correct bocks - with block 0', () {
final rowBlocks = sudoku.getRowBlocks(sudoku2x2Block0);
expect(
rowBlocks,
equals(
<Block>[
sudoku2x2Block0,
sudoku2x2Block1,
sudoku2x2Block2,
sudoku2x2Block3,
],
),
);
});

test('return correct bocks - with block 10', () {
final rowBlocks = sudoku.getRowBlocks(sudoku2x2Block10);
expect(
rowBlocks,
equals(
<Block>[
sudoku2x2Block8,
sudoku2x2Block9,
sudoku2x2Block10,
sudoku2x2Block11,
],
),
);
});
});

group('getColumnBlocks', () {
test('return correct bocks - with block 1', () {
final columnBlocks = sudoku.getColumnBlocks(sudoku2x2Block1);
expect(
columnBlocks,
equals(
<Block>[
sudoku2x2Block1,
sudoku2x2Block5,
sudoku2x2Block9,
sudoku2x2Block13,
],
),
);
});

test('return correct bocks - with block 7', () {
final columnBlocks = sudoku.getColumnBlocks(sudoku2x2Block7);
expect(
columnBlocks,
equals(
<Block>[
sudoku2x2Block3,
sudoku2x2Block7,
sudoku2x2Block11,
sudoku2x2Block15,
],
),
);
});
});
});
}

0 comments on commit c42e8aa

Please sign in to comment.