summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael R Sweet <michael.r.sweet@gmail.com>2006-01-13 18:53:38 +0000
committerMichael R Sweet <michael.r.sweet@gmail.com>2006-01-13 18:53:38 +0000
commit21a8aed4993b5e62c0e972872e5969c5451e4cd1 (patch)
tree3954891fae7dc09166123071018ebaa1c947e980
parent328d25219d711acbe0f5f95169b6a52131f1328f (diff)
Fix bugs in Sudoku demo:
1. "Check Game" now flags subgrids with duplicate numbers. 2. "Restart Game" now starts a new game if the current game has been solved. git-svn-id: file:///fltk/svn/fltk/branches/branch-1.1@4743 ea41ed52-d2ee-0310-a9c1-e6b18d33e121
-rw-r--r--test/sudoku.cxx39
1 files changed, 38 insertions, 1 deletions
diff --git a/test/sudoku.cxx b/test/sudoku.cxx
index d57bbceff..869bbf1a7 100644
--- a/test/sudoku.cxx
+++ b/test/sudoku.cxx
@@ -3,7 +3,7 @@
//
// Sudoku game using the Fast Light Tool Kit (FLTK).
//
-// Copyright 2005 by Michael Sweet.
+// Copyright 2005-2006 by Michael Sweet.
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Library General Public
@@ -769,6 +769,38 @@ Sudoku::check_game(bool highlight) {
}
}
+ // Check subgrids for duplicate numbers...
+ for (i = 0; i < 9; i += 3)
+ for (j = 0; j < 9; j += 3)
+ for (int ii = 0; ii < 3; ii ++)
+ for (int jj = 0; jj < 3; jj ++) {
+ SudokuCell *cell = grid_cells_[i + ii][j + jj];
+ int val = cell->value();
+
+ if (cell->readonly() || !val) continue;
+
+ int iii;
+
+ for (iii = 0; iii < 3; iii ++) {
+ int jjj;
+
+ for (jjj = 0; jjj < 3; jjj ++)
+ if (ii != iii && jj != jjj &&
+ grid_cells_[i + iii][j + jjj]->value() == val) break;
+
+ if (jjj < 3) break;
+ }
+
+ if (iii < 3) {
+ if (highlight) {
+ cell->color(FL_YELLOW);
+ cell->redraw();
+ }
+
+ correct = false;
+ }
+ }
+
if (!empty && correct) {
// Success!
for (i = 0; i < 9; i ++) {
@@ -1088,17 +1120,22 @@ Sudoku::resize(int X, int Y, int W, int H) {
void
Sudoku::restart_cb(Fl_Widget *widget, void *) {
Sudoku *s = (Sudoku *)(widget->window());
+ bool solved = true;
for (int i = 0; i < 9; i ++)
for (int j = 0; j < 9; j ++) {
SudokuCell *cell = s->grid_cells_[i][j];
if (!cell->readonly()) {
+ solved = false;
int v = cell->value();
cell->value(0);
+ cell->color(FL_LIGHT3);
if (v) s->sound_->play('A' + v - 1);
}
}
+
+ if (solved) s->new_game();
}