[gnome-games/sudoku-vala] Fix up code style
- From: Robert Ancell <rancell src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-games/sudoku-vala] Fix up code style
- Date: Tue, 31 Jan 2012 04:23:08 +0000 (UTC)
commit da3928a772478a6d64b8ed7e1c264a921ebda550
Author: Robert Ancell <robert ancell canonical com>
Date: Tue Jan 31 15:21:29 2012 +1100
Fix up code style
gnome-sudoku/src/sudoku-game.vala | 183 ++++++++++++++++----------------
gnome-sudoku/src/sudoku-generator.vala | 36 ++++---
gnome-sudoku/src/sudoku-view.vala | 66 +++++-------
3 files changed, 138 insertions(+), 147 deletions(-)
---
diff --git a/gnome-sudoku/src/sudoku-game.vala b/gnome-sudoku/src/sudoku-game.vala
index 20f4cd0..0dfd0f7 100644
--- a/gnome-sudoku/src/sudoku-game.vala
+++ b/gnome-sudoku/src/sudoku-game.vala
@@ -1,72 +1,60 @@
public class SudokuBoard
{
/* Implemented in such a way that it can be extended for other sizes ( like 2x3 sudoku or 4x4 sudoku ) instead of normal 3x3 sudoku. */
-
+
public int[,] cells; /* stores the value of cell */
public bool[,] is_fixed; /* if the value at location is fixed or not */
private bool[,] possible_in_row; /* if specific value is possible in specific row */
private bool[,] possible_in_col; /* if specific value is possible in specific col */
private bool[,,] possible_in_block; /* if specific value is possible in specific block */
- private int _row; /* size of sudoku board */
- private int _col; /* size of sudoku board */
private int block_row; /* size of sub-part ( block ) in board */
private int block_col; /* size of sub-part ( block ) in board */
- private int _max_val; /* maximum possible val on board. 9 for 3x3 sudoku*/
- private int _filled;
-
+
public signal void value_changed (int row, int col, int old_value, int new_value);
public signal void value_changing (int row, int col, int old_value, int new_value);
-
+
+ /* Number of rows in board */
+ private int _row;
public int row
{
- get
- {
- return _row;
- }
+ get { return _row; }
}
-
+
+ /* Number of columns in board */
+ private int _col;
public int col
{
- get
- {
- return _col;
- }
+ get { return _col; }
}
-
+
+ /* Maximum possible val on board. 9 for 3x3 sudoku*/
public int max_val
{
- get
- {
- return _max_val;
- }
+ get { return block_row * block_col; }
}
-
+
+ /* Number of numbers entered */
+ private int _filled;
public int filled
{
- get
- {
- return _filled;
- }
+ get { return _filled; }
}
-
+
public SudokuBoard (int row = 9, int col = 9, int block_row = 3, int block_col = 3)
{
_row = row;
_col = col;
this.block_row = block_row;
this.block_col = block_col;
- _max_val = block_row * block_col;
- cells = new int[_row, _col] ;
+ cells = new int[_row, _col];
is_fixed = new bool[_row, _col];
- /* as any row would contain "col" different values */
- possible_in_row = new bool[_row, _col];
- /* as any col would contain "row" different values */
- possible_in_col = new bool[_col, _row];
+ possible_in_row = new bool[_row, _col];
+ possible_in_col = new bool[_col, _row];
possible_in_block = new bool[block_row, block_col, block_row * block_col];
-
- for (int l1 = 0; l1 < _row; l1++)
+
+ for (var l1 = 0; l1 < _row; l1++)
{
- for (int l2 = 0; l2 < _col; l2++)
+ for (var l2 = 0; l2 < _col; l2++)
{
cells[l1, l2] = 0;
is_fixed[l1, l2] = false;
@@ -74,112 +62,119 @@ public class SudokuBoard
possible_in_col[l2, l1] = true;
}
}
- for (int l1 = 0; l1 < block_row; l1++)
+ for (var l1 = 0; l1 < block_row; l1++)
{
- for (int l2 = 0; l2 < block_col; l2++)
- {
- for (int l3 = 0; l3 < _max_val;l3++)
- {
+ for (var l2 = 0; l2 < block_col; l2++)
+ {
+ for (var l3 = 0; l3 < max_val; l3++)
possible_in_block[l1, l2, l3] = true;
- }
}
}
}
-
+
public void set_from_string (string s)
{
- for (int l1 = 0; l1 < s.length; l1++)
+ for (var l1 = 0; l1 < s.length; l1++)
{
/* TODO: extend for all size of sudoku. */
- if (s[l1] >= '0' && s[l1] <= '9')
- this.silent_insert (l1 / _col, l1 % _col, s[l1] - '0', true);
+ if (s[l1] >= '0' && s[l1] <= '9')
+ silent_insert (l1 / _col, l1 % _col, s[l1] - '0', true);
}
}
-
+
public bool is_possible (int row, int col, int val)
{
- --val;
+ val--;
return (possible_in_row[row,val] && possible_in_col[col,val] && possible_in_block [ row / block_col, col / block_row, val]);
}
-
+
public int count_possible (int row, int col)
{
- int count = 0;
+ var count = 0;
+
if (cells [row, col] != 0)
- return (0);
- for (int l = 1; l <= this.max_val; l++)
+ return 0;
+
+ for (var l = 1; l <= max_val; l++)
{
if (is_possible (row, col, l))
count++;
}
return count;
}
-
- public void silent_insert (int row, int col, int val, bool is_fixed = false)
+
/* This function should only be used by SudokuSolver to stop call to SudokuViewer. */
+ public void silent_insert (int row, int col, int val, bool is_fixed = false)
{
+ /* This should not happen when coded properly ;) */
if (cells[row, col] != 0)
- /* This should not happen when coded properly ;) */
- return;
+ return;
+
+ /* This should not happen when coded properly ;) */
if (!is_possible (row, col, val))
- /* This should not happen when coded properly ;) */
- return;
+ return;
+
cells[row, col] = val;
this.is_fixed[row, col] = is_fixed;
- --val;
+ val--;
possible_in_row[row, val] = false;
possible_in_col[col, val] = false;
possible_in_block[row / block_col, col / block_row, val] = false;
_filled++;
}
-
+
public void insert (int row, int col, int val, bool is_fixed = false)
{
- int old_val = cells[row, col];
+ var old_val = cells[row, col];
+
value_changing (row, col, old_val, val);
if (cells [row, col] != 0)
silent_remove (row, col);
silent_insert (row, col, val, is_fixed);
value_changed (row, col, old_val, val);
}
-
- public void silent_remove (int row, int col)
+
/* This function should only be used by SudokuSolver to stop call to SudokuViewer. */
+ public void silent_remove (int row, int col)
{
+ /* This should not happen when coded properly ;) */
if (is_fixed[row, col])
- /* This should not happen when coded properly ;) */
return;
+
+ /* Empty cell, Just pretend everything is okay... */
if (cells[row, col] == 0)
- /* Empty cell, Just pretend everything is okay... */
return;
+
int val = cells[row, col];
- --val;
+ val--;
cells[row, col] = 0;
possible_in_row[row, val] = true;
possible_in_col[col, val] = true;
possible_in_block[row / block_col, col / block_row, val] = true;
_filled--;
}
-
+
public void remove (int row, int col)
{
+ /* This should not happen when coded properly ;) */
if (is_fixed[row, col])
- /* This should not happen when coded properly ;) */
return;
+
+ /* Empty cell, Just pretend everything is okay... */
if (cells[row, col] == 0)
- /* Empty cell, Just pretend everything is okay... */
return;
+
int old_val = cells[row, col];
value_changing (row, col, old_val, 0);
silent_remove (row, col);
value_changed (row, col, old_val, 0);
}
-
+
public void to_initial_state ()
{
- for (int l1 = 0; l1 < _row; l1++)
+ for (var l1 = 0; l1 < _row; l1++)
{
- for (int l2 = 0; l2 < _col; l2++)
+ for (var l2 = 0; l2 < _col; l2++)
{
if (!is_fixed[l1, l2])
remove (l1, l2);
@@ -191,7 +186,7 @@ public class SudokuBoard
public class SudokuGame
{
public SudokuBoard board;
-
+
private struct UndoItem
{
public int row;
@@ -201,8 +196,7 @@ public class SudokuGame
private SList<UndoItem?> undostack;
private SList<UndoItem?> redostack;
-
-
+
public signal void cell_changed (int row, int col, int old_val, int new_val);
public SudokuGame ()
@@ -213,7 +207,7 @@ public class SudokuGame
board.value_changing.connect (update_undo);
board.value_changed.connect (cell_changed_cb);
}
-
+
public SudokuGame.from_string (string game)
{
this ();
@@ -226,6 +220,7 @@ public class SudokuGame
undostack = null;
redostack = null;
}
+
public void undo ()
{
apply_stack (ref undostack, ref redostack);
@@ -238,20 +233,21 @@ public class SudokuGame
public void reset ()
{
- int count = board.filled;
+ var count = board.filled;
board.to_initial_state ();
count -= board.filled;
add_to_stack (ref undostack, -1, -1, count);
}
-
+
public void hint (ref int row, ref int col)
{
- int[,] total_pos = new int [board.row, board.col];
- int min = board.max_val + 1;
- int count = 0;
- for (int l1 = 0; l1 < board.row; l1++)
+ var total_pos = new int [board.row, board.col];
+ var min = board.max_val + 1;
+ var count = 0;
+
+ for (var l1 = 0; l1 < board.row; l1++)
{
- for(int l2 = 0; l2 < board.col; l2++)
+ for (var l2 = 0; l2 < board.col; l2++)
{
total_pos [l1, l2] = board.count_possible (l1, l2);
if (total_pos [l1, l2] < min && total_pos [l1, l2] > 0)
@@ -265,20 +261,22 @@ public class SudokuGame
}
if (count == 0)
return;
+
count = Random.int_range (0, count);
- for (int l1 = 0; l1 < board.row; l1++)
+ for (var l1 = 0; l1 < board.row; l1++)
{
- for (int l2 = 0; l2 < board.col; l2++)
+ for (var l2 = 0; l2 < board.col; l2++)
{
if (total_pos[l1, l2] == min)
{
- if (count-- == 0)
+ count--;
+ if (count == 0)
{
row = l1;
col = l2;
return;
}
- }
+ }
}
}
}
@@ -287,7 +285,7 @@ public class SudokuGame
{
cell_changed (row, col, old_val, new_val);
}
-
+
public void update_undo (SudokuBoard board, int row, int col, int old_val, int new_val)
{
add_to_stack (ref undostack, row, col, old_val);
@@ -302,7 +300,9 @@ public class SudokuGame
private void apply_stack (ref SList<UndoItem?> from, ref SList<UndoItem?> to)
{
- if (from == null) return;
+ if (from == null)
+ return;
+
/* Undoing change of single cell */
if (from.data.row >= 0 && from.data.col >= 0)
{
@@ -316,12 +316,11 @@ public class SudokuGame
/* Undoing reset action */
else
{
- int num = from.data.val;
+ var num = from.data.val;
from.remove (from.data);
- for (int l = 0; l < num; l++)
+ for (var l = 0; l < num; l++)
apply_stack (ref from, ref to);
add_to_stack (ref to, -1, -1, num);
}
-
}
}
diff --git a/gnome-sudoku/src/sudoku-generator.vala b/gnome-sudoku/src/sudoku-generator.vala
index 109e9c6..69843d0 100644
--- a/gnome-sudoku/src/sudoku-generator.vala
+++ b/gnome-sudoku/src/sudoku-generator.vala
@@ -22,20 +22,20 @@ public class SudokuGenerator
public class SudokuSolver
{
private SudokuBoard board;
-
+
public SudokuSolver (ref SudokuBoard board)
{
this.board = board;
}
-
+
+ /* Check if current SudokuBoard has at lesat one solution or not */
public bool has_solution ()
- /*Check if current SudokuBoard has at lesat one solution or not*/
{
int solutions = 0;
solve (-1, -1, -1, -1, ref solutions);
return (solutions > 0);
}
-
+
public bool has_unique_solution ()
/*Check if current SudokuBoard has unique solution or not*/
{
@@ -43,32 +43,32 @@ public class SudokuSolver
solve (-1, -1, -1, -1, ref solutions);
return (solutions == 1);
}
-
+
+ /* Check if current SudokuBoard has more than one solution or not */
public bool has_many_solution ()
- /*Check if current SudokuBoard has more thatn one solution or not*/
{
int solutions = 0;
solve (-1, -1, -1, -1, ref solutions);
return (solutions > 1);
}
-
+
private void solve (int row, int col, int no, int filled, ref int solution)
{
if (filled == -1)
- {
filled = board.filled;
- }
+
if (filled == board.row * board.col)
{
solution++;
print_sol ();
return;
}
+
if (row == -1 || col == -1)
{
- for (int l1 = 0; l1 < board.row; l1++)
+ for (var l1 = 0; l1 < board.row; l1++)
{
- for (int l2 = 0;l2 < board.col; l2++)
+ for (var l2 = 0;l2 < board.col; l2++)
{
if (board.cells[l1, l2] == 0)
{
@@ -78,32 +78,34 @@ public class SudokuSolver
}
}
}
+
if (no == -1)
{
- for (int l1 = 1; l1 <= board.max_val; l1++)
+ for (var l1 = 1; l1 <= board.max_val; l1++)
{
if (board.is_possible (row, col, l1))
{
solve (row, col, l1, filled, ref solution);
+ /* Break at solutions == 2 as we don't need exact count of possible solutions */
if (solution > 1)
- /*Break at solutions == 2 as we don't need exact count of possible solutions*/
return;
}
}
return;
}
+
board.silent_insert (row, col, no);
solve (-1, -1, -1, filled + 1, ref solution);
board.silent_remove (row, col);
}
-
- private void print_sol ()
+
/* For checking purpose only. Should be removed from final release */
+ private void print_sol ()
{
stdout.printf ("Solution found..\n");
- for (int l1=0;l1<9;l1++)
+ for (var l1 = 0; l1 < 9; l1++)
{
- for (int l2=0;l2<9;l2++)
+ for (var l2 = 0; l2 < 9; l2++)
{
stdout.printf ("%d ",board.cells[l1,l2]);
}
diff --git a/gnome-sudoku/src/sudoku-view.vala b/gnome-sudoku/src/sudoku-view.vala
index 0410061..db2c6b7 100644
--- a/gnome-sudoku/src/sudoku-view.vala
+++ b/gnome-sudoku/src/sudoku-view.vala
@@ -7,33 +7,21 @@ private class SudokuCellView : Gtk.DrawingArea
private Gtk.Window? popup = null;
private SudokuBoard board;
+
private int _row;
- private int _col;
-
public int row
{
- get
- {
- return _row;
- }
- set
- {
- _row = value;
- }
+ get { return _row; }
+ set { _row = value; }
}
+ private int _col;
public int col
{
- get
- {
- return _col;
- }
- set
- {
- _col = value;
- }
+ get { return _col; }
+ set { _col = value; }
}
-
+
public int value
{
get
@@ -70,7 +58,7 @@ private class SudokuCellView : Gtk.DrawingArea
}
}
}
-
+
public bool is_fixed
{
get
@@ -94,7 +82,7 @@ private class SudokuCellView : Gtk.DrawingArea
}
get { return _text; }
}
-
+
public SudokuCellView (int row, int col, ref SudokuBoard board)
{
this.board = board;
@@ -154,8 +142,9 @@ private class SudokuCellView : Gtk.DrawingArea
popup = new Gtk.Window (Gtk.WindowType.POPUP);
var table = new Gtk.Table (3, 3, false);
- for (int col = 0; col < 3; col++)
- for (int row = 0; row < 3; row++)
+ for (var col = 0; col < 3; col++)
+ {
+ for (var row = 0; row < 3; row++)
{
int n = col + row * 3 + 1;
@@ -170,7 +159,7 @@ private class SudokuCellView : Gtk.DrawingArea
label.use_markup = true;
button.add (label);
label.show ();
-
+
button.clicked.connect (() => {
value = n;
hide_popup ();
@@ -179,6 +168,7 @@ private class SudokuCellView : Gtk.DrawingArea
if (n == 5)
button.grab_focus ();
}
+ }
table.show ();
popup.add (table);
@@ -285,7 +275,7 @@ private class SudokuCellView : Gtk.DrawingArea
value = 0;
return true;
}
-
+
if (event.keyval == 32 /* space */ || event.keyval == 65293 /* enter */)
{
show_number_picker ();
@@ -339,13 +329,11 @@ private class SudokuCellView : Gtk.DrawingArea
public void value_changed_cb (SudokuBoard b, int row, int col, int old_val, int new_val)
{
- if(row == this.row && col == this.col)
- {
+ if (row == this.row && col == this.col)
this.value = new_val;
- }
}
-
- public void hint()
+
+ public void hint ()
{
/* TODO: Show hint properly. */
stdout.printf ("TODO: Show hint properly.\nHint at %d %d\n", _row + 1, _col + 1);
@@ -363,7 +351,7 @@ public class SudokuView : Gtk.AspectFrame
this.obey_child = false;
shadow_type = Gtk.ShadowType.NONE;
-
+
/* Use an EventBox to be able to set background */
var box = new Gtk.EventBox ();
box.modify_bg (Gtk.StateType.NORMAL, box.style.black);
@@ -380,14 +368,16 @@ public class SudokuView : Gtk.AspectFrame
table.border_width = 2;
cells = new SudokuCellView[9,9];
- for (int row = 0; row < 9; row++)
- for (int col = 0; col < 9; col++)
+ for (var row = 0; row < 9; row++)
+ {
+ for (var col = 0; col < 9; col++)
{
var cell = new SudokuCellView (row, col, ref game.board);
cells[row, col] = cell;
cell.show ();
table.attach_defaults (cell, col, col+1, row, row+1);
}
+ }
box.add (table);
table.show ();
}
@@ -412,7 +402,7 @@ public class SudokuView : Gtk.AspectFrame
get { return _show_warnings; }
set { _show_warnings = value; }
}
-
+
public void hint ()
{
int row=0, col=0;
@@ -422,16 +412,16 @@ public class SudokuView : Gtk.AspectFrame
public void clear_top_notes ()
{
- for (int i = 0; i < 9; i++)
- for (int j = 0; j < 9; j++)
+ for (var i = 0; i < 9; i++)
+ for (var j = 0; j < 9; j++)
cells[i,j].top_notes = "";
queue_draw ();
}
public void clear_bottom_notes ()
{
- for (int i = 0; i < 9; i++)
- for (int j = 0; j < 9; j++)
+ for (var i = 0; i < 9; i++)
+ for (var j = 0; j < 9; j++)
cells[i,j].bottom_notes = "";
queue_draw ();
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]